Files
zooqueenandHanzo Dev 9705d8bc03 A deployment caps every org at test with organization.Ceiling
EmbedConfig.Ceiling is declared by the host as Born is: Test where the
network holds no real money, "" or Live for no cap, and any other value
caps at Test. Under a Test ceiling TestMode is true for every org, a
record marked Live included, so it charges sandbox credentials, writes
the test books and publishes the sandbox application.

TestMode is the one reading of the mode: the ledger, balance, transaction,
library, account and Authorize.Net paths, StripeToken and PayPal read it
instead of org.Live, so the ceiling holds on each of them.

Co-authored-by: Hanzo Dev <[email protected]>
2026-10-16 07:27:25 -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.TestMode())
loginId := con.LoginId
transactionKey := con.TransactionKey
key := con.Key
client := authorizenet.New(ctx, loginId, transactionKey, key, org.TestMode())
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
}