mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
49 lines
1.2 KiB
Go
49 lines
1.2 KiB
Go
package plaid
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/plaid/plaid-go/v15/plaid"
|
|
|
|
. "github.com/hanzoai/commerce/thirdparty/paymentmethods"
|
|
)
|
|
|
|
type Environment plaid.Environment
|
|
|
|
const (
|
|
SandboxEnvironment Environment = Environment(plaid.Sandbox)
|
|
DevelopmentEnvironment Environment = Environment(plaid.Development)
|
|
ProductionEnvironment Environment = Environment(plaid.Production)
|
|
)
|
|
|
|
type Client struct {
|
|
*plaid.APIClient
|
|
ctx context.Context
|
|
}
|
|
|
|
func (c Client) GetPayToken(p PaymentMethodParams) (*PaymentMethodOutput, error) {
|
|
// Exchange the publicToken for an accessToken
|
|
exchangePublicTokenResp, _, err := c.PlaidApi.ItemPublicTokenExchange(c.ctx).ItemPublicTokenExchangeRequest(
|
|
*plaid.NewItemPublicTokenExchangeRequest(p.VerifierToken),
|
|
).Execute()
|
|
accessToken := exchangePublicTokenResp.GetAccessToken()
|
|
|
|
// Get Accounts
|
|
accountsGetResp, _, err := c.PlaidApi.AccountsGet(c.ctx).AccountsGetRequest(
|
|
*plaid.NewAccountsGetRequest(accessToken),
|
|
).Execute()
|
|
accountID := accountsGetResp.GetAccounts()[0].GetAccountId()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &PaymentMethodOutput{
|
|
Inputs: p,
|
|
PayToken: accessToken,
|
|
PayTokenId: accountID,
|
|
ExternalUserId: p.ExternalUserId,
|
|
Type: PlaidType,
|
|
}, nil
|
|
}
|