-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
276 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |