Files
commerce/api/checkout/square/authorize.go
zeekay 1943f1c093 Hanzo Commerce
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
2026-09-23 21:52:16 -07:00

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
}