Skip to content

Commit

Permalink
fix: implement square connector.
Browse files Browse the repository at this point in the history
  • Loading branch information
jaymesC committed Aug 15, 2024
1 parent dfae0b4 commit 6ae4f61
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
2 changes: 2 additions & 0 deletions connectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/wakflo/extensions/internal/connectors/manual"
"github.com/wakflo/extensions/internal/connectors/shopify"
"github.com/wakflo/extensions/internal/connectors/slack"
"github.com/wakflo/extensions/internal/connectors/square"
"github.com/wakflo/extensions/internal/connectors/todoist"
"github.com/wakflo/extensions/internal/connectors/webhook"
"github.com/wakflo/extensions/internal/connectors/woocommerce"
Expand Down Expand Up @@ -69,6 +70,7 @@ func RegisterConnectors() []*sdk.ConnectorPlugin {
mailchimp.NewConnector, // MailChimp
xero.NewConnector, // Xero
clickup.NewConnector, // Clickup
square.NewConnector, // Square
}

// 🛑Do-Not-Edit
Expand Down
36 changes: 36 additions & 0 deletions internal/connectors/square/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2022-present Wakflo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package square

import (
sdk "github.com/wakflo/go-sdk/connector"
)

func NewConnector() (*sdk.ConnectorPlugin, error) {
return sdk.CreateConnector(&sdk.CreateConnectorArgs{
Name: "Square",
Description: "Payment solutions for every business",
Logo: "simple-icons:square",
Version: "0.0.1",
Category: sdk.Apps,
Authors: []string{"Wakflo <integrations@wakflo.com>"},
Triggers: []sdk.ITrigger{
NewTriggerNewPayment(),
},
Operations: []sdk.IOperation{
NewGetPaymentsOperation(),
},
})
}
76 changes: 76 additions & 0 deletions internal/connectors/square/operation_get_payments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2022-present Wakflo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package square

import (
"errors"

"github.com/wakflo/go-sdk/autoform"
sdk "github.com/wakflo/go-sdk/connector"
sdkcore "github.com/wakflo/go-sdk/core"
)

type getPaymentsOperationProps struct {
OrganizationID string `json:"organization_id"`
}

type GetPaymentsOperation struct {
options *sdk.OperationInfo
}

func NewGetPaymentsOperation() *GetPaymentsOperation {
return &GetPaymentsOperation{
options: &sdk.OperationInfo{
Name: "Get Payments",
Description: "Retrieve a list of Payments",
RequireAuth: true,
Auth: sharedAuth,
Input: map[string]*sdkcore.AutoFormSchema{
"organization_id": autoform.NewShortTextField().
SetDisplayName("").
SetDescription("").
Build(),
},
ErrorSettings: sdkcore.StepErrorSettings{
ContinueOnError: false,
RetryOnError: false,
},
},
}
}

func (c *GetPaymentsOperation) Run(ctx *sdk.RunContext) (sdk.JSON, error) {
if ctx.Auth.AccessToken == "" {
return nil, errors.New("missing Square auth token")
}

_ = sdk.InputToType[getPaymentsOperationProps](ctx)

url := "https://connect.squareup.com/v2/payments"

payments, err := getSquareClient(ctx.Auth.AccessToken, url)
if err != nil {
return nil, err
}
return payments, nil
}

func (c *GetPaymentsOperation) Test(ctx *sdk.RunContext) (sdk.JSON, error) {
return c.Run(ctx)
}

func (c *GetPaymentsOperation) GetInfo() *sdk.OperationInfo {
return c.options
}
78 changes: 78 additions & 0 deletions internal/connectors/square/shared.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2022-present Wakflo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package square

import (
"encoding/json"
"fmt"
"io"
"net/http"

"github.com/wakflo/go-sdk/autoform"
)

var (
// #nosec
tokenURL = "https://connect.squareup.com/oauth2/token"
authURL = "https://connect.squareup.com/oauth2/authorize"
sharedAuth = autoform.NewOAuthField(authURL, &tokenURL, []string{
"MERCHANT_PROFILE_READ",
"CUSTOMERS_READ",
"CUSTOMERS_WRITE",
"ITEMS_READ",
"ITEMS_WRITE",
"ORDERS_READ",
"ORDERS_WRITE",
"PAYMENTS_READ",
"INVOICES_READ",
"APPOINTMENTS_READ",
"APPOINTMENTS_WRITE",
}).
Build()
)

func getSquareClient(accessToken, url string) (map[string]interface{}, error) {
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return nil, fmt.Errorf("error creating request: %v", err)
}

req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response body: %v", err)
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("API request failed with status code %d: %s", resp.StatusCode, string(body))
}

var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
return nil, fmt.Errorf("error unmarshaling response: %v", err)
}

return result, nil
}
84 changes: 84 additions & 0 deletions internal/connectors/square/trigger_new_payment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2022-present Wakflo
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package square

import (
"fmt"
"time"

sdk "github.com/wakflo/go-sdk/connector"
sdkcore "github.com/wakflo/go-sdk/core"
)

type TriggerNewPayment struct {
options *sdk.TriggerInfo
}

func NewTriggerNewPayment() *TriggerNewPayment {
return &TriggerNewPayment{
options: &sdk.TriggerInfo{
Name: "New Payment",
Description: "triggers workflow when a new payment is created",
RequireAuth: true,
Auth: sharedAuth,
Type: sdkcore.TriggerTypeCron,
Input: map[string]*sdkcore.AutoFormSchema{},
Settings: &sdkcore.TriggerSettings{},
ErrorSettings: &sdkcore.StepErrorSettings{
ContinueOnError: false,
RetryOnError: false,
},
},
}
}

func (t *TriggerNewPayment) Run(ctx *sdk.RunContext) (sdk.JSON, error) {
_ = sdk.InputToType[getPaymentsOperationProps](ctx)
var fromDate string
lastRunTime := ctx.Metadata.LastRun

if lastRunTime != nil {
fromDate = lastRunTime.Format(time.RFC3339)
} else {
defaultStartDate := time.Date(2006, 1, 1, 0, 0, 0, 0, time.UTC)
fromDate = defaultStartDate.Format(time.RFC3339)

fmt.Println("DEFAULT", fromDate)
}

baseURL := "https://connect.squareup.com/v2/payments?begin_time=" + fromDate

payments, err := getSquareClient(ctx.Auth.AccessToken, baseURL)
if err != nil {
return nil, err
}
return payments, nil
}

func (t *TriggerNewPayment) Test(ctx *sdk.RunContext) (sdk.JSON, error) {
return t.Run(ctx)
}

func (t *TriggerNewPayment) OnEnabled(ctx *sdk.RunContext) error {
return nil
}

func (t *TriggerNewPayment) OnDisabled(ctx *sdk.RunContext) error {
return nil
}

func (t *TriggerNewPayment) GetInfo() *sdk.TriggerInfo {
return t.options
}

0 comments on commit 6ae4f61

Please sign in to comment.