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
54 lines
1.7 KiB
Go
54 lines
1.7 KiB
Go
package square
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hanzoai/commerce/log"
|
|
"github.com/hanzoai/commerce/models/order"
|
|
"github.com/hanzoai/commerce/models/organization"
|
|
"github.com/hanzoai/commerce/models/payment"
|
|
"github.com/hanzoai/commerce/models/types/currency"
|
|
"github.com/hanzoai/commerce/models/user"
|
|
"github.com/hanzoai/commerce/payment/processor"
|
|
squarelib "github.com/hanzoai/commerce/thirdparty/square"
|
|
)
|
|
|
|
func Authorize(org *organization.Organization, ord *order.Order, usr *user.User, pay *payment.Payment) error {
|
|
sqCfg := org.SquareConfig(org.TestMode())
|
|
|
|
proc := squarelib.NewProcessor(squarelib.Config{
|
|
AccessToken: sqCfg.AccessToken,
|
|
LocationID: sqCfg.LocationId,
|
|
WebhookSecret: org.Square.WebhookSignatureKey,
|
|
Environment: org.SquareEnvironment(),
|
|
})
|
|
|
|
req := processor.PaymentRequest{
|
|
Amount: currency.Cents(pay.Amount),
|
|
Currency: pay.Currency,
|
|
Token: pay.Account.Number, // Card nonce from client
|
|
CustomerID: usr.Accounts.Square.CustomerId,
|
|
OrderID: ord.Id(),
|
|
Description: pay.Description,
|
|
}
|
|
|
|
result, err := proc.Authorize(context.Background(), req)
|
|
if err != nil {
|
|
log.Warn("Square authorize failed for payment '%s': %v", pay.Id(), err)
|
|
return err
|
|
}
|
|
|
|
// Update payment with Square response
|
|
pay.Account.Square.PaymentId = result.TransactionID
|
|
pay.Account.Square.LocationId = sqCfg.LocationId
|
|
// pay.Live must follow the SAME authority as the charge environment, so a
|
|
// sandbox authorization is never recorded as a live payment (and vice versa).
|
|
pay.Live = !org.TestMode()
|
|
|
|
// Update user's Square account
|
|
usr.Accounts.Square.CustomerId = req.CustomerID
|
|
|
|
log.Info("Square authorized payment '%s' → transaction '%s'", pay.Id(), result.TransactionID)
|
|
return nil
|
|
}
|