diff --git a/connectors.go b/connectors.go index 378f9a3..60d9643 100644 --- a/connectors.go +++ b/connectors.go @@ -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" @@ -69,6 +70,7 @@ func RegisterConnectors() []*sdk.ConnectorPlugin { mailchimp.NewConnector, // MailChimp xero.NewConnector, // Xero clickup.NewConnector, // Clickup + square.NewConnector, // Square } // 🛑Do-Not-Edit diff --git a/internal/connectors/square/lib.go b/internal/connectors/square/lib.go new file mode 100644 index 0000000..e6b125a --- /dev/null +++ b/internal/connectors/square/lib.go @@ -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 "}, + Triggers: []sdk.ITrigger{ + NewTriggerNewPayment(), + }, + Operations: []sdk.IOperation{ + NewGetPaymentsOperation(), + }, + }) +} diff --git a/internal/connectors/square/operation_get_payments.go b/internal/connectors/square/operation_get_payments.go new file mode 100644 index 0000000..5f3dbeb --- /dev/null +++ b/internal/connectors/square/operation_get_payments.go @@ -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 +} diff --git a/internal/connectors/square/shared.go b/internal/connectors/square/shared.go new file mode 100644 index 0000000..80e858e --- /dev/null +++ b/internal/connectors/square/shared.go @@ -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 +} diff --git a/internal/connectors/square/trigger_new_payment.go b/internal/connectors/square/trigger_new_payment.go new file mode 100644 index 0000000..d27a395 --- /dev/null +++ b/internal/connectors/square/trigger_new_payment.go @@ -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 +}