fix: add idempotency keys to all Stripe mutation calls #49
Reference in New Issue
Block a user
Delete Branch "userAdityaa/caravel:stripe-keys"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem
All non-read Stripe API calls —> create customer, create/update subscription item, create subscription, pay invoice, and pay invoice out-of-band, were sent without an
Idempotency-Keyheader. On network uncertainty or an upstream retry, Stripe would treat each attempt as a new request, creating duplicate subscriptions, subscription items, or applying multiple payment side effects.Solution
Added a private
Billing::idempotency_key(&[&str]) -> Stringhelper that produces a deterministic, 64-char hex HMAC-SHA256 fingerprint. The key is derived from a colon-separated tuple of an operation label and the business identifiers that uniquely describe "this specific state transition" (e.g.,create_subscription:{customer_id}:{price_id}). The HMAC is keyed withSTRIPE_SECRET_KEYso the output is unguessable externally, and it stays well within Stripe's 255-character limit.The
Idempotency-Keyheader was added to 6 mutation methods:stripe_create_customercreate_customer:{tenant_pubkey}stripe_create_subscriptioncreate_subscription:{customer_id}:{price_id}stripe_create_subscription_itemcreate_subscription_item:{subscription_id}:{price_id}stripe_update_subscription_itemupdate_subscription_item:{item_id}:{price_id}stripe_pay_invoicepay_invoice:{invoice_id}stripe_pay_invoice_out_of_bandpay_invoice_oob:{invoice_id}closes #47