Stripe: company purchases, payment details kept for the invoice
CI / test (push) Successful in 1m25s
CI / build (push) Successful in 5m20s
CI / deploy (push) Skipped

The checkout offers buying as a company (name, tax number). A confirmed
payment keeps Stripe's payment id, the amount charged and the buyer's
billing details, and queues the invoice for a background job (Billingo,
once the company exists). Payments outlive a deleted account for the
accounting law's 8 years. The package cards, the FAQ and the privacy
policy say that card details go to Stripe only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-20 07:26:03 +00:00
co-authored by Claude Opus 5.5
parent d9e71ed349
commit e530e78c54
10 changed files with 180 additions and 48 deletions
+40 -2
View File
@@ -148,7 +148,10 @@ async fn fake_stripe_get(axum::extract::Path(id): axum::extract::Path<String>) -
StatusCode::OK,
Json(json!({"id": id, "url": null,
"payment_status": if *paid { "paid" } else { "unpaid" },
"status": if *paid { "complete" } else { "open" }})),
"status": if *paid { "complete" } else { "open" },
"amount_total": 200000, "currency": "huf", "payment_intent": "pi_fake",
"customer_details": {"name": "Anna Teszt", "email": "anna@example.com",
"address": {"city": "Szeged", "country": "HU"}, "tax_ids": []}})),
),
None => (StatusCode::NOT_FOUND, Json(json!({"error": {"message": "no such session"}}))),
}
@@ -2301,6 +2304,8 @@ async fn stripe_checkout_grants_the_paid_package_once(pool: PgPool) {
Some("Sorsfonalak – Páros")
);
assert_eq!(stripe_form(&id, "customer_email").as_deref(), Some("anna@example.com"));
assert_eq!(stripe_form(&id, "tax_id_collection[enabled]").as_deref(), Some("true"));
assert_eq!(stripe_form(&id, "billing_address_collection").as_deref(), Some("required"));
assert_eq!(
stripe_form(&id, "success_url").as_deref(),
Some("https://sorsfonalak.test/kepletek/abc?fizetes={CHECKOUT_SESSION_ID}")
@@ -2327,7 +2332,12 @@ async fn stripe_checkout_grants_the_paid_package_once(pool: PgPool) {
// The webhook: a bad signature is refused, a good one grants the package,
// and Stripe's retries do not grant it again.
let event = json!({"id": "evt_1", "type": "checkout.session.completed",
"data": {"object": {"id": id, "payment_status": "paid"}}});
"data": {"object": {"id": id, "payment_status": "paid", "status": "complete",
"amount_total": 600000, "currency": "huf", "payment_intent": "pi_1",
"customer_details": {"name": "Kovács Anna", "email": "anna@example.com",
"business_name": "Csillag Kft.",
"address": {"line1": "Fő utca 1.", "city": "Szeged", "postal_code": "6720", "country": "HU"},
"tax_ids": [{"type": "hu_tin", "value": "12345678-1-23"}]}}}});
let body = event.to_string();
let now = time::OffsetDateTime::now_utc().unix_timestamp();
let bad = crate::stripe::sign("whsec_other", body.as_bytes(), now);
@@ -2344,6 +2354,17 @@ async fn stripe_checkout_grants_the_paid_package_once(pool: PgPool) {
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
}
assert_eq!(stripe_orders(&app.pool, "anna@example.com").await, vec![("couple".to_string(), 6000)]);
// What the invoice needs is kept, and the invoice is queued.
let (intent, amount, customer, invoice): (String, i32, Value, String) = sqlx::query_as(
"SELECT payment_intent, amount_huf, customer, invoice_status FROM stripe_checkouts WHERE id = $1",
)
.bind(&id)
.fetch_one(&app.pool)
.await
.unwrap();
assert_eq!((intent.as_str(), amount, invoice.as_str()), ("pi_1", 6000, "pending"));
assert_eq!(customer["tax_ids"][0]["value"], "12345678-1-23");
assert_eq!(customer["address"]["postal_code"], "6720");
let r = app.get(&format!("/v1/payments/checkout/{id}"), Some(&anna)).await;
assert_eq!(r.body["status"], "paid");
assert_eq!(r.body["name"], "Páros");
@@ -2367,6 +2388,23 @@ async fn stripe_checkout_grants_the_paid_package_once(pool: PgPool) {
stripe_orders(&app.pool, "anna@example.com").await,
vec![("couple".to_string(), 6000), ("single".to_string(), 2000)]
);
let (name, invoice): (String, String) =
sqlx::query_as("SELECT customer->>'name', invoice_status FROM stripe_checkouts WHERE id = $1")
.bind(&other)
.fetch_one(&app.pool)
.await
.unwrap();
assert_eq!((name.as_str(), invoice.as_str()), ("Anna Teszt", "pending"));
// Deleting the account keeps the payments (accounting law).
let r = app.call("DELETE", "/v1/me", Some(&anna), Some(json!({"confirm": "DELETE"}))).await;
assert!(r.status.is_success(), "{}", r.body);
let (kept,): (i64,) =
sqlx::query_as("SELECT count(*) FROM stripe_checkouts WHERE user_id IS NULL AND completed_at IS NOT NULL")
.fetch_one(&app.pool)
.await
.unwrap();
assert_eq!(kept, 2);
}
#[sqlx::test(migrator = "db::MIGRATOR")]