carlos / platform Public

billing: PaymentInfo.IntentStatus; document payment fields

Co-Authored-By: Astra (gpt-6-astra) via Codex <noreply@openai.com>
Paul Campbell pushed by claude agent, operated by paul@keymail.dev 55dce6d900d0b52b16b9c5e83ca1668c3daf8f42 parent 5426b1d
5 files changed, +69 −30
  • internal/billing/billing.go +31 −13
  • internal/billing/billingtest/fake.go +18 −7
  • internal/billing/billingtest/payment_test.go +7 −6
  • internal/billingstripe/payment.go +10 −1
  • internal/billingstripe/payment_test.go +3 −3
diff --git a/internal/billing/billing.go b/internal/billing/billing.go
index 436fe964a..e1692f7ed 100644
--- a/internal/billing/billing.go
+++ b/internal/billing/billing.go
@@ -143,24 +143,38 @@ type CheckoutRequest struct {
IdempotencyKey string
}
+// PaymentCheckoutRequest creates a one-off Checkout payment for later capture.
type PaymentCheckoutRequest struct {
AccountSqid, Customer, Currency string
- Amount int64
- Description string
- Reference string
+ Amount int64 // Positive minor units, before tax.
+ Description string // Line item name, for example the domain.
+ Reference string // Our purchase ID, stored in metadata.
SuccessURL, CancelURL string
ExpiresAt time.Time
- Key string
+ Key string // Idempotency key for the create request.
}
-func (PaymentCheckoutRequest) String() string { return "billing.PaymentCheckoutRequest{redacted}" }
+// String redacts the payment request, including its idempotency key.
+func (PaymentCheckoutRequest) String() string { return "billing.PaymentCheckoutRequest{redacted}" }
+
+// GoString redacts the payment request in Go-syntax formatting.
func (r PaymentCheckoutRequest) GoString() string { return r.String() }
+// PaymentInfo describes a one-off Checkout session or a capture/cancel result.
+// Capture/cancel results contain only PaymentIntent, IntentStatus, AmountTotal
+// and Currency; session fields and Amount are zero because the response has no
+// Checkout session or pre-tax subtotal. A failed read returns a zero PaymentInfo.
type PaymentInfo struct {
- SessionID, URL, PaymentIntent, Status string
- Authorised bool
- Amount, AmountTotal int64
- Currency string
+ SessionID, URL, PaymentIntent string
+ Status string // Checkout session status; empty on capture/cancel results.
+ IntentStatus string // PaymentIntent status; empty when no expanded intent is available.
+ Authorised bool // Session is complete and IntentStatus is requires_capture.
+ Amount int64 // Pre-tax minor units; zero on capture/cancel results.
+ // AmountTotal is minor units including tax, positive on successful reads and
+ // writes, including cancellation. It is not the amount captured; zero only
+ // in a zero PaymentInfo.
+ AmountTotal int64
+ Currency string
}
// CheckoutState is a Checkout session's state.
@@ -213,10 +227,10 @@ type Result struct {
// and a pending update.
Subscription *Subscription
Checkout *CheckoutInfo // a Checkout create or expiry
- Payment *PaymentInfo
- Customer string // a customer create
- Replayed bool // the provider replayed a stored answer (fact 7)
- ProviderTime time.Time // the response's Date header; zero when it had none
+ Payment *PaymentInfo // a one-off Checkout create, capture or cancellation
+ Customer string // a customer create
+ Replayed bool // the provider replayed a stored answer (fact 7)
+ ProviderTime time.Time // the response's Date header; zero when it had none
}
// InvoiceQuantities is a renewal invoice reduced to the quantity it billed
@@ -287,9 +301,13 @@ type Provider interface {
PrepareExpire(sessionID, key string) (Prepared, error)
PrepareChange(req ChangeRequest) (Prepared, error)
PrepareRenewal(subscription string, renew bool, key string) (Prepared, error)
+ // PreparePaymentCheckout encodes a one-off payment for later capture.
PreparePaymentCheckout(req PaymentCheckoutRequest) (Prepared, error)
+ // PaymentCheckout reads session and PaymentIntent status. ErrNotFound if absent.
PaymentCheckout(ctx context.Context, sessionID string) (PaymentInfo, error)
+ // PrepareCapture encodes capture of an authorised PaymentIntent under key.
PrepareCapture(paymentIntent, key string) (Prepared, error)
+ // PrepareCancelPayment encodes release of a PaymentIntent's authorisation under key.
PrepareCancelPayment(paymentIntent, key string) (Prepared, error)
// Send makes exactly one request: p's bytes under p's key. A nil error
// is definite success. Otherwise the error is one of the outcome
diff --git a/internal/billing/billingtest/fake.go b/internal/billing/billingtest/fake.go
index 5d3a07a84..1729539a4 100644
--- a/internal/billing/billingtest/fake.go
+++ b/internal/billing/billingtest/fake.go
@@ -1102,15 +1102,15 @@ func (f *Fake) execute(body any) (billing.Result, error) {
if s.PaymentIntent != b.Intent {
continue
}
- if s.intentStatus != "requires_capture" {
+ if s.IntentStatus != "requires_capture" {
return billing.Result{}, billing.ErrRefused
}
- s.intentStatus = "succeeded"
+ s.IntentStatus = "succeeded"
if b.Cancel {
- s.intentStatus = "canceled"
+ s.IntentStatus = "canceled"
}
s.Authorised = false
- info := billing.PaymentInfo{PaymentIntent: s.PaymentIntent, Status: s.intentStatus, AmountTotal: s.AmountTotal, Currency: s.Currency}
+ info := billing.PaymentInfo{PaymentIntent: s.PaymentIntent, IntentStatus: s.IntentStatus, AmountTotal: s.AmountTotal, Currency: s.Currency}
return billing.Result{Payment: &info}, nil
}
return billing.Result{}, billing.ErrRefused
@@ -1444,8 +1444,7 @@ var _ billing.Provider = (*Fake)(nil)
type paymentSession struct {
billing.PaymentInfo
- Request billing.PaymentCheckoutRequest
- intentStatus string
+ Request billing.PaymentCheckoutRequest
}
type paymentCheckoutBody struct {
@@ -1456,6 +1455,8 @@ type paymentActionBody struct {
Cancel bool
}
+// PreparePaymentCheckout encodes a one-off payment for later capture using
+// req.Key. It makes no provider call; the fake adds no tax.
func (f *Fake) PreparePaymentCheckout(req billing.PaymentCheckoutRequest) (billing.Prepared, error) {
if req.AccountSqid == "" || req.Customer == "" || req.Currency == "" || req.Amount <= 0 || req.Description == "" || req.Reference == "" || req.SuccessURL == "" || req.CancelURL == "" || req.ExpiresAt.IsZero() {
return billing.Prepared{}, billing.ErrRefused
@@ -1465,6 +1466,8 @@ func (f *Fake) PreparePaymentCheckout(req billing.PaymentCheckoutRequest) (billi
return prepare(billing.PreparedPaymentCheckout, key, &paymentCheckoutBody{Request: req})
}
+// PrepareCapture encodes capture of an authorised PaymentIntent under key.
+// It makes no provider call.
func (f *Fake) PrepareCapture(paymentIntent, key string) (billing.Prepared, error) {
if paymentIntent == "" {
return billing.Prepared{}, billing.ErrRefused
@@ -1472,6 +1475,8 @@ func (f *Fake) PrepareCapture(paymentIntent, key string) (billing.Prepared, erro
return prepare(billing.PreparedCapture, key, &paymentActionBody{Intent: paymentIntent})
}
+// PrepareCancelPayment encodes release of a PaymentIntent's authorisation
+// under key. It makes no provider call.
func (f *Fake) PrepareCancelPayment(paymentIntent, key string) (billing.Prepared, error) {
if paymentIntent == "" {
return billing.Prepared{}, billing.ErrRefused
@@ -1479,6 +1484,8 @@ func (f *Fake) PrepareCancelPayment(paymentIntent, key string) (billing.Prepared
return prepare(billing.PreparedCancelPayment, key, &paymentActionBody{Intent: paymentIntent, Cancel: true})
}
+// PaymentCheckout reads the session and PaymentIntent status. ErrNotFound if
+// absent. Amount and AmountTotal both equal the requested pre-tax amount.
func (f *Fake) PaymentCheckout(ctx context.Context, sessionID string) (billing.PaymentInfo, error) {
f.mu.Lock()
defer f.mu.Unlock()
@@ -1498,6 +1505,10 @@ func (f *Fake) PaymentCheckout(ctx context.Context, sessionID string) (billing.P
return s.PaymentInfo, nil
}
+// CompletePaymentCheckout simulates a buyer completing an open session and
+// authorising its payment. It returns the new PaymentIntent ID, sets Status
+// to complete and IntentStatus to requires_capture, and leaves capture to Send.
+// Missing or expired sessions, and sessions already complete, return ErrRefused.
func (f *Fake) CompletePaymentCheckout(sessionID string) (string, error) {
f.mu.Lock()
defer f.mu.Unlock()
@@ -1509,7 +1520,7 @@ func (f *Fake) CompletePaymentCheckout(sessionID string) (string, error) {
}
s.Status = "complete"
s.PaymentIntent = f.nextID("pi")
- s.intentStatus = "requires_capture"
+ s.IntentStatus = "requires_capture"
s.Authorised = true
return s.PaymentIntent, nil
}
diff --git a/internal/billing/billingtest/payment_test.go b/internal/billing/billingtest/payment_test.go
index da36f8f3e..ae04e7bf9 100644
--- a/internal/billing/billingtest/payment_test.go
+++ b/internal/billing/billingtest/payment_test.go
@@ -30,7 +30,7 @@ func TestFakePaymentLifecycle(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if res.Payment == nil || res.Payment.Authorised || res.Payment.Status != "open" {
+ if res.Payment == nil || res.Payment.Authorised || res.Payment.Status != "open" || res.Payment.IntentStatus != "" {
t.Fatalf("open: %+v", res.Payment)
}
id := res.Payment.SessionID
@@ -41,7 +41,7 @@ func TestFakePaymentLifecycle(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if !info.Authorised || info.PaymentIntent == "" || info.Amount != 1500 || info.AmountTotal != 1500 || info.Currency != "eur" {
+ if !info.Authorised || info.Status != "complete" || info.IntentStatus != "requires_capture" || info.PaymentIntent == "" || info.Amount != 1500 || info.AmountTotal != 1500 || info.Currency != "eur" {
t.Fatalf("authorised: %+v", info)
}
prepare := f.PrepareCapture
@@ -62,16 +62,17 @@ func TestFakePaymentLifecycle(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if !done.Replayed || done.Payment == nil || done.Payment.Status != status {
+ if !done.Replayed || done.Payment == nil || done.Payment.Status != "" || done.Payment.IntentStatus != status || done.Payment.Amount != 0 || done.Payment.AmountTotal != 1500 {
t.Fatalf("replay: %+v", done)
}
done.Payment.Status = "mutated"
+ done.Payment.IntentStatus = "mutated"
replay, err := f.Send(ctx, action)
- if err != nil || replay.Payment.Status != status {
+ if err != nil || replay.Payment.Status != "" || replay.Payment.IntentStatus != status {
t.Fatal("caller mutated stored answer")
}
info, err = f.PaymentCheckout(ctx, id)
- if err != nil || info.Authorised {
+ if err != nil || info.Authorised || info.Status != "complete" || info.IntentStatus != status {
t.Fatalf("settled: %+v, %v", info, err)
}
if cancel {
@@ -113,7 +114,7 @@ func TestFakePaymentExpiryAndKeyMismatch(t *testing.T) {
}
f.AdvanceProvider(time.Hour)
info, err := f.PaymentCheckout(ctx, res.Payment.SessionID)
- if err != nil || info.Status != "expired" || info.Authorised {
+ if err != nil || info.Status != "expired" || info.IntentStatus != "" || info.Authorised {
t.Fatalf("expired: %+v, %v", info, err)
}
if _, err := f.CompletePaymentCheckout(info.SessionID); !errors.Is(err, billing.ErrRefused) {
diff --git a/internal/billingstripe/payment.go b/internal/billingstripe/payment.go
index afa1635a4..990381dac 100644
--- a/internal/billingstripe/payment.go
+++ b/internal/billingstripe/payment.go
@@ -10,6 +10,8 @@ import (
"github.com/carlosframework/platform/internal/billing"
)
+// PreparePaymentCheckout encodes a one-off Checkout payment with manual capture
+// and automatic tax, using req.Key. It makes no request.
func (p *Provider) PreparePaymentCheckout(req billing.PaymentCheckoutRequest) (billing.Prepared, error) {
if !validID(req.Customer) {
return billing.Prepared{}, &Error{Code: CodeInvalidID}
@@ -43,6 +45,8 @@ func (p *Provider) PreparePaymentCheckout(req billing.PaymentCheckoutRequest) (b
return prepared(billing.PreparedPaymentCheckout, "/v1/checkout/sessions", form, req.Key)
}
+// PrepareCapture encodes capture of an authorised PaymentIntent under key.
+// It makes no request.
func (p *Provider) PrepareCapture(paymentIntent, key string) (billing.Prepared, error) {
if !validID(paymentIntent) {
return billing.Prepared{}, &Error{Code: CodeInvalidID}
@@ -50,6 +54,8 @@ func (p *Provider) PrepareCapture(paymentIntent, key string) (billing.Prepared,
return prepared(billing.PreparedCapture, "/v1/payment_intents/"+paymentIntent+"/capture", url.Values{}, key)
}
+// PrepareCancelPayment encodes release of a PaymentIntent's authorisation
+// under key. It makes no request.
func (p *Provider) PrepareCancelPayment(paymentIntent, key string) (billing.Prepared, error) {
if !validID(paymentIntent) {
return billing.Prepared{}, &Error{Code: CodeInvalidID}
@@ -119,6 +125,7 @@ func paymentSessionInfo(raw json.RawMessage) (billing.PaymentInfo, error) {
if err := json.Unmarshal(s.PaymentIntent.Object, &pi); err != nil || !pi.valid() || pi.Currency != s.Currency || *pi.Amount != *s.AmountTotal {
return billing.PaymentInfo{}, &Error{Code: CodeDecodeError}
}
+ info.IntentStatus = pi.Status
info.Authorised = s.Status == "complete" && pi.Status == "requires_capture"
} else if s.Status == "complete" {
return billing.PaymentInfo{}, &Error{Code: CodeDecodeError}
@@ -126,6 +133,8 @@ func paymentSessionInfo(raw json.RawMessage) (billing.PaymentInfo, error) {
return info, nil
}
+// PaymentCheckout reads a session and its expanded PaymentIntent. Status is
+// the session status; IntentStatus is the intent status. ErrNotFound if absent.
func (p *Provider) PaymentCheckout(ctx context.Context, sessionID string) (billing.PaymentInfo, error) {
if !validID(sessionID) {
return billing.PaymentInfo{}, &Error{Code: CodeInvalidID}
@@ -175,5 +184,5 @@ func paymentResult(prep billing.Prepared, raw json.RawMessage) (billing.PaymentI
if prep.Path != "/v1/payment_intents/"+pi.ID+"/"+action || pi.Status != status {
return billing.PaymentInfo{}, &Error{Code: CodeDecodeError}
}
- return billing.PaymentInfo{PaymentIntent: pi.ID, Status: pi.Status, AmountTotal: *pi.Amount, Currency: pi.Currency}, nil
+ return billing.PaymentInfo{PaymentIntent: pi.ID, IntentStatus: pi.Status, AmountTotal: *pi.Amount, Currency: pi.Currency}, nil
}
diff --git a/internal/billingstripe/payment_test.go b/internal/billingstripe/payment_test.go
index b3fc86830..7ad5629d4 100644
--- a/internal/billingstripe/payment_test.go
+++ b/internal/billingstripe/payment_test.go
@@ -88,7 +88,7 @@ func TestPaymentCheckoutAuthorised(t *testing.T) {
want bool
}{
{"complete", "requires_capture", true}, {"complete", "requires_payment_method", false}, {"open", "requires_capture", false},
- {"complete", "succeeded", false}, {"complete", "canceled", false}, {"expired", "", false},
+ {"complete", "processing", false}, {"complete", "succeeded", false}, {"complete", "canceled", false}, {"expired", "", false},
} {
t.Run(tc.session+"_"+tc.intent, func(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -103,7 +103,7 @@ func TestPaymentCheckoutAuthorised(t *testing.T) {
if err != nil {
t.Fatal(err)
}
- if info.Authorised != tc.want || info.Status != tc.session {
+ if info.Authorised != tc.want || info.Status != tc.session || info.IntentStatus != tc.intent {
t.Fatalf("payment: %+v", info)
}
if tc.intent != "" && info.PaymentIntent != "pi_1" {
@@ -146,7 +146,7 @@ func TestCaptureAndCancelUseIdempotencyKey(t *testing.T) {
if sent.method != "POST" || sent.path != "/v1/payment_intents/pi_1/"+op || sent.key != "operation-key" || len(sent.body) != 0 {
t.Fatal("wrong keyed payment operation")
}
- if res.Payment == nil || res.Payment.PaymentIntent != "pi_1" || res.Payment.Status != status || res.Payment.Authorised {
+ if res.Payment == nil || res.Payment.PaymentIntent != "pi_1" || res.Payment.Status != "" || res.Payment.IntentStatus != status || res.Payment.Authorised || res.Payment.Amount != 0 || res.Payment.AmountTotal != 1845 {
t.Fatalf("payment: %+v", res.Payment)
}
})