Files
commerce/api/billing/payer.go
b9abb94aa2 Tell the processors who is paying (#50)
* billing: processors are told who is paying, so their card checks stop declining a stranger

A live Stripe account was declining first-period charges (card_velocity_exceeded,
do_not_honor, insufficient_funds on a normal-risk buyer). The customer Stripe held
was named for the org slug ("github"), with no email, phone or address; the card
carried only a postal code; the PaymentIntent had no receipt email, no statement
suffix and metadata of only hanzo_subject.

vaultCustomer no longer passes the billing subject as the customer name. A Payer
(name, email, phone, billing address, org, user) is carried from the host to the
processors: Stripe and Square create ONE customer per payer with everything known
and bring an existing customer up to date at its next checkout (processor.PayerProfiles:
CreateCustomerFor, ProfileCustomer; writes nothing when it already agrees). The name
and email are the host's verified identity and no request body can set them
(CreateMethodIn.BillingName is json:"-"); phone and address are the buyer's.
After a SetupIntent saves a card, its billing_details are completed from the customer.
Every Stripe card PaymentIntent now carries receipt_email, statement_descriptor_suffix
"HANZO AI", a description naming plan, seats and date, and metadata org/user/subject;
saved cards remember their payer's email/name/org/user so renewals and reloads carry
them too. The Payment Element intent is made for the payer's customer with the same.

BackfillPayers reads (dry run by default) the customers commerce made whose name is a
billing key and proposes the person's name and email from the host's identity lookup;
writing is a separate explicit argument, and a repeat writes nothing. It was not run.

There is no automatic re-attempt with a new amount: a first-period charge is made only
by the buyer's own request, and a renewal charges the invoice's amount; nothing here
adds a retry.

Testing: Stripe test mode, TestLiveStripe_TheCustomerAndTheCardNameThePayer (customer name/
email/phone/address/metadata, card billing_details, PaymentIntent receipt_email/suffix/
description/metadata) and TestLiveStripe_ACustomerMadeNamelessIsNamedAtItsNextCheckout fail
with the old behaviour (customer "hanzo", no phone, no address) and pass; TestTheBackfill...;
Square TestAPayerIsSplit...; full api/billing, payment/..., thirdparty/square, api/checkout pass.
Risk: medium (billing). Customers already at Stripe are updated in place at their next
checkout; a profile that cannot be written is logged and does not stop the checkout.
How to revert: git revert this commit; customers are named for the billing subject again.

Co-authored-by: Hanzo Dev <[email protected]>

* square: a payer the host could not name is still a customer, under its org's company name

Square refuses a customer with no name, email, phone or company. Before, the billing
subject stood in as the given name; with the subject gone from the name, a sale whose
host passed no name and no email failed at the customer ("Card declined"). The org the
payer buys for is now the customer's company_name, which is what it is.

Testing: cloud apps/commerce TestASquareSaleOfEveryRungIsTheLimitersPlan (Square sandbox)
failed without this ("Card declined — invalid card number") and passes with it.
Risk: low; one more field on a new Square customer.
How to revert: git revert this commit.

Co-authored-by: Hanzo Dev <[email protected]>

---------

Co-authored-by: Hanzo Dev <[email protected]>
2026-11-13 16:43:36 -08:00

71 lines
2.1 KiB
Go

package billing
import (
"strings"
"github.com/hanzoai/commerce/models/organization"
"github.com/hanzoai/commerce/models/paymentmethod"
"github.com/hanzoai/commerce/payment/processor"
"github.com/hanzoai/commerce/types"
)
// PayerIn is who is paying, as the host states them. Name and Email are the
// person's, from the verified identity the host resolved — never a value the request
// body chose. Phone and Address are what the buyer typed in the payment form. The
// billing key (the org slug, a user id) is never a name: a processor shows the name
// to the issuer, and an organization's slug declines.
type PayerIn struct {
Name string
Email string
Phone string
Address *types.Address
}
// payerFor is p as the processors take it, for subject paying in org.
func payerFor(org *organization.Organization, subject string, p PayerIn) processor.Payer {
out := processor.Payer{
Name: strings.TrimSpace(p.Name),
Email: strings.TrimSpace(p.Email),
Phone: strings.TrimSpace(p.Phone),
User: strings.ToLower(strings.TrimSpace(subject)),
}
if org != nil {
out.Org = org.Name
}
if a := p.Address; a != nil && !a.Empty() {
out.Address = &processor.PayerAddress{
Line1: a.Line1, Line2: a.Line2, City: a.City, State: a.State,
Postal: a.PostalCode, Country: a.Country,
}
}
return out
}
// Facts a saved card keeps about its payer, so a charge made later with nobody
// present (a renewal, a reload) still names who paid.
const (
factEmail = "payerEmail"
factName = "payerName"
factOrg = "payerOrg"
factUser = "payerUser"
)
func stampPayer(pm *paymentmethod.PaymentMethod, p processor.Payer) {
if pm.Metadata == nil {
pm.Metadata = map[string]interface{}{}
}
for k, v := range map[string]string{factEmail: p.Email, factName: p.Name, factOrg: p.Org, factUser: p.User} {
if v != "" {
pm.Metadata[k] = v
}
}
}
// payerOfCard is the payer a saved card names.
func payerOfCard(pm *paymentmethod.PaymentMethod) processor.Payer {
return processor.Payer{
Email: metaStr(pm, factEmail), Name: metaStr(pm, factName),
Org: metaStr(pm, factOrg), User: metaStr(pm, factUser),
}
}