Compare commits

..

3 Commits

Author SHA1 Message Date
158a09b73d feat(sca): charge checkout as customer-initiated transaction
Drop off_session from the initial PaymentIntent so the checkout charge is a Customer-Initiated Transaction (the customer is present in the TUI), not a Merchant-Initiated Transaction. MIT exemptions require a stored mandate we never capture; as a CIT, an SCA challenge surfaces as status=requires_action, the path the handler already had. The re-confirm in respondRequiresAction and the authentication_required error branch remain as defensive fallbacks. Source: stripe-samples accept-a-payment custom-payment-flow (no off_session on customer-present confirms).

Also clarify the MAX_ORDER_CENTS TRA-ceiling anchor comment as an approximate USD/EUR figure (config.go), and carry an unrelated 1-line spend-limit input width fix in pkg/tui/account.go.

Tests: add paymentIntentNewFn stub seam, TestConvertCartIsCustomerInitiated (asserts no off_session) and TestConvertCartRequiresAction (202 + redirect + audit row).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 10:37:17 +02:00
0c0b8e8059 fix(tui): accept spend limit input in dollars (#56)
Spend-limit input now takes dollars (e.g. 25.50) instead of raw cents.
Parsed to integer cents as dollars*100 + cents (no float multiply) so an
amount like 50.01 never loses a cent to binary rounding; the persisted
column and API still use cents. Current limit and prefill render via
formatCentsDollars. Tests updated to the dollar grammar.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-17 15:34:03 +02:00
7148ff1d4d feat(sca): add customer-set spend limit (#55) 2026-06-17 13:25:04 +02:00
5 changed files with 263 additions and 38 deletions

View File

@@ -36,6 +36,11 @@ type CartHandler struct {
// audit rows (audit.EventOrderRequiresAction) so the cap stays migration-free.
const maxAuthIssuances = 4
// paymentIntentNewFn lets tests stub the Stripe create call without spinning
// up an httptest backend. Production callers go straight through to the real
// paymentintent.New. Mirrors paymentIntentGetFn in reconcile.go.
var paymentIntentNewFn = paymentintent.New
// NewCartHandler creates a new cart handler. Stripe credentials are wired
// once at startup via stripe.Key in api/main.go.
func NewCartHandler(appURL string, maxOrderCents int) *CartHandler {
@@ -460,14 +465,24 @@ func (h *CartHandler) ConvertCart(w http.ResponseWriter, r *http.Request) {
audit.OrderCreated(userID, order.ID, total)
// Charge the card. The idempotency key is tied to the order ID so retries
// never produce a double charge.
// Charge the card. This is a Customer-Initiated Transaction (CIT): the
// customer is present in the TUI at checkout. We deliberately do NOT set
// OffSession — that flag marks a Merchant-Initiated Transaction (MIT),
// which under PSD2/SCA may only claim an exemption when a stored mandate
// authorises the merchant to charge while the customer is away. We capture
// no mandate and run no recurring billing, so MIT would misrepresent the
// transaction type. As a CIT, an SCA challenge surfaces as
// pi.Status == requires_action (handled below), not as an
// authentication_required error. See docs/sca-psd2-compliance.md "Gap 1".
// Reference: stripe-samples accept-a-payment custom-payment-flow —
// php/public/google-pay.php drives 3DS off status==requires_action and sets
// no off_session on a customer-present confirm.
// The idempotency key is tied to the order ID so retries never double charge.
piParams := &stripe.PaymentIntentParams{
Amount: stripe.Int64(int64(total)),
Currency: stripe.String(string(stripe.CurrencyUSD)),
Customer: stripe.String(user.StripeCustomerID),
PaymentMethod: stripe.String(paymentMethodID),
OffSession: stripe.Bool(true),
PaymentMethodTypes: stripe.StringSlice([]string{"card"}),
ReturnURL: stripe.String(h.appURL + "/post-3ds"),
Confirm: stripe.Bool(true),
@@ -490,11 +505,16 @@ func (h *CartHandler) ConvertCart(w http.ResponseWriter, r *http.Request) {
}
stripeStart := time.Now()
pi, err := paymentintent.New(piParams)
pi, err := paymentIntentNewFn(piParams)
stripeDur := time.Since(stripeStart).Seconds()
if err != nil {
middleware.ObserveStripeRequest("payment_intent_create", "error", stripeDur)
if stripeErr, ok := err.(*stripe.Error); ok {
// Defensive fallback: as CIT (no off_session) Stripe normally
// signals 3DS via status=requires_action below, not via this error.
// authentication_required can still appear if the saved card needs
// auth that could not run inline; respondRequiresAction re-Confirms
// on-session to recover. (Pre-CIT migration this was the main path.)
if stripeErr.Code == stripe.ErrorCodeAuthenticationRequired && stripeErr.PaymentIntent != nil {
h.respondRequiresAction(w, &order, paymentMethodID, stripeErr.PaymentIntent)
return

View File

@@ -862,6 +862,171 @@ func TestConvertCartLimit(t *testing.T) {
}
}
func seedConvertReadyCart(t *testing.T, user models.User) {
t.Helper()
db := database.GetDB()
user.StripeCustomerID = "cus_test_cit"
if err := db.Save(&user).Error; err != nil {
t.Fatalf("save user: %v", err)
}
addr := models.Address{
UserID: user.ID, Name: "test", Street: "1 Main St",
City: "PDX", State: "OR", Zip: "97201", Country: "US",
}
if err := db.Create(&addr).Error; err != nil {
t.Fatalf("seed address: %v", err)
}
card := models.Card{
UserID: user.ID, StripePaymentID: "pm_test_cit",
Last4: "4242", Brand: "Visa", ExpMonth: 12, ExpYear: 2030,
}
if err := db.Create(&card).Error; err != nil {
t.Fatalf("seed card: %v", err)
}
if err := db.Model(&models.Coffee{}).Where("id = ?", 4).Update("price", 500).Error; err != nil {
t.Fatalf("pin coffee price: %v", err)
}
h := NewCartHandler("", 0)
body, _ := json.Marshal(map[string]any{"coffee_id": 4, "quantity": 2})
w := httptest.NewRecorder()
h.SetItem(w, authRequest("PUT", "/api/v1/cart/item", body, user.ID))
if w.Code != http.StatusOK {
t.Fatalf("SetItem: %d %s", w.Code, w.Body.String())
}
body, _ = json.Marshal(map[string]any{"address_id": addr.ID})
w = httptest.NewRecorder()
h.SetAddress(w, authRequest("PUT", "/api/v1/cart/address", body, user.ID))
if w.Code != http.StatusOK {
t.Fatalf("SetAddress: %d %s", w.Code, w.Body.String())
}
body, _ = json.Marshal(map[string]any{"card_id": card.ID})
w = httptest.NewRecorder()
h.SetCard(w, authRequest("PUT", "/api/v1/cart/card", body, user.ID))
if w.Code != http.StatusOK {
t.Fatalf("SetCard: %d %s", w.Code, w.Body.String())
}
}
func TestConvertCartIsCustomerInitiated(t *testing.T) {
testDB, user := setupCartTestDB(t)
defer func() { _ = os.Remove(testDB) }()
defer database.ResetForTesting()
var gotParams *stripe.PaymentIntentParams
orig := paymentIntentNewFn
defer func() { paymentIntentNewFn = orig }()
paymentIntentNewFn = func(p *stripe.PaymentIntentParams) (*stripe.PaymentIntent, error) {
gotParams = p
return &stripe.PaymentIntent{ID: "pi_cit", Status: stripe.PaymentIntentStatusSucceeded}, nil
}
seedConvertReadyCart(t, user)
handler := NewCartHandler("http://localhost", 0)
w := httptest.NewRecorder()
handler.ConvertCart(w, authRequest("POST", "/api/v1/cart/convert", nil, user.ID))
if w.Code != http.StatusOK {
t.Fatalf("convert: want 200, got %d body=%s", w.Code, w.Body.String())
}
if gotParams == nil {
t.Fatalf("paymentIntentNewFn was never called")
}
if gotParams.OffSession != nil {
t.Errorf("CIT charge must not set off_session; got %v", *gotParams.OffSession)
}
if gotParams.Confirm == nil || !*gotParams.Confirm {
t.Error("expected Confirm=true on the charge")
}
if gotParams.ReturnURL == nil || *gotParams.ReturnURL == "" {
t.Error("expected ReturnURL set so redirect-based 3DS can populate next_action")
}
}
// TestConvertCartRequiresAction asserts that when the bank requires SCA, the
// CIT charge returns status=requires_action (the customer-initiated shape per
// the Stripe sample) and the handler responds 202 + redirect URL, persists the
// order in requires_action, and records the audit event the retry cap counts.
// next_action.redirect_to_url is pre-populated so respondRequiresAction skips
// its re-confirm and never touches the live Stripe API (hermetic).
func TestConvertCartRequiresAction(t *testing.T) {
testDB, user := setupCartTestDB(t)
defer func() { _ = os.Remove(testDB) }()
defer database.ResetForTesting()
// Wire audit persistence so the order_requires_action row actually lands
// (audit.db is package-global and nil unless set — see api/main.go:71).
// Reset to nil after so it does not leak into other tests in this package.
audit.SetDB(database.GetDB())
defer audit.SetDB(nil)
orig := paymentIntentNewFn
defer func() { paymentIntentNewFn = orig }()
paymentIntentNewFn = func(_ *stripe.PaymentIntentParams) (*stripe.PaymentIntent, error) {
return &stripe.PaymentIntent{
ID: "pi_sca",
Status: stripe.PaymentIntentStatusRequiresAction,
NextAction: &stripe.PaymentIntentNextAction{
RedirectToURL: &stripe.PaymentIntentNextActionRedirectToURL{
URL: "https://hooks.stripe.com/3ds/test",
},
},
}, nil
}
seedConvertReadyCart(t, user)
handler := NewCartHandler("http://localhost", 0)
w := httptest.NewRecorder()
handler.ConvertCart(w, authRequest("POST", "/api/v1/cart/convert", nil, user.ID))
if w.Code != http.StatusAccepted {
t.Fatalf("convert: want 202, got %d body=%s", w.Code, w.Body.String())
}
var resp struct {
Success bool `json:"success"`
Data struct {
OrderID uint `json:"order_id"`
Status string `json:"status"`
RedirectURL string `json:"redirect_url"`
} `json:"data"`
}
if err := json.NewDecoder(w.Body).Decode(&resp); err != nil {
t.Fatalf("decode: %v", err)
}
if resp.Data.Status != "requires_action" {
t.Errorf("status: want requires_action, got %q", resp.Data.Status)
}
if resp.Data.RedirectURL == "" {
t.Error("expected redirect_url in 202 response")
}
db := database.GetDB()
var order models.Order
if err := db.First(&order, resp.Data.OrderID).Error; err != nil {
t.Fatalf("load order: %v", err)
}
if order.Status != models.OrderStatusRequiresAction {
t.Errorf("order status: want requires_action, got %q", order.Status)
}
if order.StripePaymentID != "pi_sca" {
t.Errorf("order stripe_payment_id: want pi_sca, got %q", order.StripePaymentID)
}
var events int64
db.Model(&models.OrderEvent{}).
Where("order_id = ? AND type = ?", order.ID, audit.EventOrderRequiresAction).
Count(&events)
if events != 1 {
t.Errorf("expected 1 order_requires_action audit event, got %d", events)
}
}
// TestSetCard_ExpiredReturns410 verifies that selecting an already-expired
// card on the cart returns 410 + CARD_STORAGE_EXPIRED and soft-deletes the
// card. Exercises cart.go SetCard expired-card branch

View File

@@ -138,11 +138,14 @@ func getEnvOrDefault(key, defaultValue string) string {
return defaultValue
}
// loadMaxOrderCents reads MAX_ORDER_CENTS. Default $500 — aligns with the
// PSD2 TRA exemption ceiling (above this, Stripe always requests SCA, so the
// cap is only meaningful below it). Explicit 0 disables the cap. Garbage or
// negative values fall back to the default with a warning so an operator
// typo cannot silently disable a fraud control.
// loadMaxOrderCents reads MAX_ORDER_CENTS. Default $500 — roughly tracks the
// PSD2 TRA exemption ceiling of €500 (≈ $540, FX-dependent), above which Stripe
// always requests SCA, so the cap is only meaningful below it. We charge in USD
// and the regulatory ceiling is in EUR, so this is an approximate anchor, not an
// exact equivalence — $500 trips slightly below the true ceiling, which is the
// safe direction. Explicit 0 disables the cap. Garbage or negative values fall
// back to the default with a warning so an operator typo cannot silently disable
// a fraud control.
func loadMaxOrderCents() int {
const defaultCap = 50000
raw := os.Getenv("MAX_ORDER_CENTS")

View File

@@ -22,9 +22,7 @@ func (m Model) accountBodyHeight() int {
footerHeight := lipgloss.Height(m.BuildFooter())
budget := m.heightContainer - headerHeight - footerHeight - 2
cards := budget / orderCardHeight
if cards < 1 {
cards = 1
}
cards = max(cards, 1)
return cards*orderCardHeight - 2
}
@@ -468,12 +466,12 @@ func (m Model) AccountUpdate(msg tea.Msg) (Model, tea.Cmd) {
case "spend limit":
if !m.account.spendLimitFocused {
ti := textinput.New()
ti.Prompt = ""
ti.Placeholder = "cents (blank = no limit)"
ti.CharLimit = 9
ti.Prompt = "$"
ti.Placeholder = "dollars (blank = no limit)"
ti.CharLimit = 12
// Prefill with the current limit so the user edits rather than retypes.
if m.User != nil && m.User.SelfLimitCents != nil {
ti.SetValue(strconv.Itoa(*m.User.SelfLimitCents))
ti.SetValue(formatCentsDollars(*m.User.SelfLimitCents))
}
cmd := ti.Focus()
m.account.spendLimitInput = ti
@@ -559,8 +557,7 @@ func (m Model) SpendLimitView(width int) string {
current := "no limit set"
if m.User != nil && m.User.SelfLimitCents != nil {
c := *m.User.SelfLimitCents
current = fmt.Sprintf("$%.2f (%d cents)", float64(c)/100, c)
current = "$" + formatCentsDollars(*m.User.SelfLimitCents)
}
out := bodyStyle.Render(wordWrap(
@@ -578,7 +575,8 @@ func (m Model) SpendLimitView(width int) string {
Border(lipgloss.RoundedBorder()).
BorderForeground(m.theme.Highlight()).
Width(max(1, width-2))
out += labelStyle.Render("New limit (cents)") + "\n"
m.account.spendLimitInput.SetWidth(max(1, width-3))
out += labelStyle.Render("New limit (dollars)") + "\n"
out += box.Render(m.account.spendLimitInput.View())
switch {
case m.account.spendLimitSaving:
@@ -616,23 +614,58 @@ func (m Model) setSpendLimitCmd(cents *int) tea.Cmd {
}
}
// parseSpendLimitInput maps the raw input to a *int of cents. Blank clears the
// limit (nil -> revert to admin/global). "0" is a real "block everything"
// ceiling, NOT a clear. Negative or non-numeric input is rejected. These rules
// match the server boundary in api/handlers/account.go:SetSpendLimit.
// parseSpendLimitInput maps the raw dollar input to a *int of cents. Blank
// clears the limit (nil -> revert to admin/global). "0" is a real "block
// everything" ceiling, NOT a clear. Negative, non-numeric, or more-than-2-
// decimal input is rejected. Parsed as integer cents (dollars*100 + cents) with
// no float multiply, so an amount like 50.01 never loses a cent to binary
// rounding. The server still stores cents (api/handlers/account.go).
func parseSpendLimitInput(s string) (*int, error) {
s = strings.TrimSpace(s)
s = strings.TrimSpace(strings.TrimPrefix(s, "$"))
if s == "" {
return nil, nil
}
n, err := strconv.Atoi(s)
if err != nil {
return nil, fmt.Errorf("enter a whole number of cents, or blank to clear")
}
if n < 0 {
if strings.HasPrefix(s, "-") {
return nil, fmt.Errorf("limit cannot be negative")
}
return &n, nil
whole, frac, hasDot := strings.Cut(s, ".")
if whole == "" {
whole = "0" // allow ".50"
}
dollars, err := strconv.Atoi(whole)
if err != nil {
return nil, fmt.Errorf("enter a dollar amount, e.g 25 or 25.50, or blank to clear")
}
cents := 0
if hasDot {
switch len(frac) {
case 0:
// trailing dot, e.g "25." -> treat as .00
case 1:
frac += "0" // "5" -> 50 cents
fallthrough
case 2:
cents, err = strconv.Atoi(frac)
if err != nil {
return nil, fmt.Errorf("enter a dollar amount, e.g 25 or 25.00, or blank to clear")
}
default:
return nil, fmt.Errorf("use at most two decimal places, e.g 25.00")
}
}
total := dollars*100 + cents
return &total, nil
}
// formatCentsDollars renders an integer cents value as a plain dollar string
// (no leading "$"), e.g. 2500 -> "25.00", 50 -> "0.50". Used for the input
// prefill and the current-limit display.
func formatCentsDollars(cents int) string {
return fmt.Sprintf("%d.%02d", cents/100, cents%100)
}
// defaultAccountFooter is the account-menu footer shown when no sub-view owns

View File

@@ -15,13 +15,17 @@ func TestParseSpendLimitInput(t *testing.T) {
wantNil bool
wantErr bool
}{
{"", 0, true, false}, // blank clears
{"0", 0, false, false}, // 0 is block-all, NOT a clear
{"1500", 1500, false, false}, // plain value
{" 250 ", 250, false, false}, // trims whitespace
{"-5", 0, false, true}, // negative rejected
{"abc", 0, false, true}, // non-numeric rejected
{"12.5", 0, false, true}, // non-integer rejected
{"", 0, true, false}, // blank clears
{"0", 0, false, false}, // $0 is block-all, NOT a clear
{"25", 2500, false, false}, // whole dollars
{"25.50", 2550, false, false}, // dollars + cents
{"25.5", 2550, false, false}, // one decimal => tenths
{"0.05", 5, false, false}, // sub-dollar
{" $25.00 ", 2500, false, false}, // strips $ and whitespace
{".50", 50, false, false}, // leading dot
{"-5", 0, false, true}, // negative rejected
{"abc", 0, false, true}, // non-numeric rejected
{"25.999", 0, false, true}, // too many decimals rejected
}
for _, tc := range cases {
got, err := parseSpendLimitInput(tc.in)
@@ -80,8 +84,8 @@ func TestSpendLimitFocusFlow(t *testing.T) {
if !m.account.spendLimitFocused {
t.Fatal("enter did not focus the spend-limit input")
}
if got := m.account.spendLimitInput.Value(); got != "2500" {
t.Errorf("prefill: want \"2500\", got %q", got)
if got := m.account.spendLimitInput.Value(); got != "25.00" {
t.Errorf("prefill: want \"25.00\", got %q", got)
}
m, _ = m.AccountUpdate(tea.KeyPressMsg{Code: tea.KeyEsc})