Files
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

68 lines
1.8 KiB
Go

package authorizenet
import (
"errors"
"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/thirdparty/authorizenet"
)
var FailedToCaptureCharge = errors.New("Failed to capture charge")
var NothingToCaptureError = errors.New("Nothing to Capture (Items or Subscriptions)")
func Capture(org *organization.Organization, ord *order.Order) (*order.Order, []*payment.Payment, error) {
// Get namespaced context off order
db := ord.Datastore()
ctx := db.Context
// Get payments for this order
payments := make([]*payment.Payment, 0)
if err := payment.Query(db).Ancestor(ord.Key()).GetModels(&payments); err != nil {
return nil, payments, err
}
log.Debug("payments %v", payments)
// Get client we can use for API calls
con := org.AuthorizeNetToken(!org.Live)
loginId := con.LoginId
transactionKey := con.TransactionKey
key := con.Key
client := authorizenet.New(ctx, loginId, transactionKey, key, !org.Live)
if ord.Total > 0 {
// Capture any uncaptured payments
for _, p := range payments {
if !p.Captured {
p2, err := client.Capture(p)
// Charge failed for some reason, bail
if err != nil {
return ord, payments, err
}
if !p2.Captured {
return ord, payments, FailedToCaptureCharge
}
// Update payment
p2.Captured = true
// p.Amount = currency.Cents(ord.Amount)
// p.AmountRefunded = currency.Cents(p2ch.AmountRefunded)
// p.Account.BalanceTransactionId = ch.Tx.ID
// p.AmountTransferred = currency.Cents(ch.Tx.Amount)
// p.CurrencyTransferred = currency.Type(ch.Tx.Currency)
}
}
} else if len(ord.Subscriptions) == 0 {
return ord, payments, NothingToCaptureError
}
return ord, payments, nil
}