Lesson 2 of 9 · 27m
Server-Side Order and PaymentIntent Design
Create orders server-side, bind amounts to trusted catalog data, attach idempotency keys, and persist provider object IDs before redirecting customers.
Payment APIs are unforgiving about ambiguity. If the browser says a hoodie costs $29 and your database later says it cost $19, which one is true? The correct answer is neither: the server must calculate amount, currency, discounts, and metadata before asking the provider to create a payment object. The client is a user interface, not a pricing authority.
Create your order first
A durable sequence looks like this:
1. Browser sends cart items and quantities.
2. Server validates inventory, prices, tax, and currency.
3. Server creates a local order in `pending_payment`.
4. Server creates provider checkout/payment object for that order.
5. Server stores provider object ID and checkout/capture URL.
6. Browser redirects or confirms using a client secret.The important part is step 4 happening after a local order exists. If the API request times out after the provider created the object, you can search or retry instead of wondering whether money moved.
Stripe example: create a PaymentIntent safely
Use an idempotency key derived from the local order and retry attempt:
intent = stripe.PaymentIntent.create(
amount=order.total_cents,
currency=order.currency.lower(),
automatic_payment_methods={"enabled": True},
metadata={"order_id": str(order.id)},
idempotency_key=f"order-{order.id}-pi-{order.payment_attempt}",
)
order.payment_provider = "stripe"
order.provider_object_id = intent.id
order.client_secret = intent.client_secret
order.save()Never reuse the same idempotency key for a materially different request. Stripe will return the original result, which is exactly what you want for a network retry and exactly what you do not want after changing the amount.
PayPal example: create then capture
PayPal often has a two-phase shape:
order = paypal_client.orders_create({
"intent": "CAPTURE",
"purchase_units": [{
"reference_id": str(local_order.id),
"amount": {"currency_code": "USD", "value": "29.00"},
}],
})The customer approves the PayPal order, then your server captures it. Approval is not payment. Treating approval as completion is a classic storefront bug because customers can close the browser between approval and capture.
Metadata is operational glue
Attach your stable internal identifiers, not secrets or mutable payloads:
{
"order_id": "ord_01JYXM8KQW",
"account_id": "acct_4382",
"attempt": 2
}That metadata appears in dashboards, webhook payloads, exports, and support workflows. It is how a human can connect provider truth back to application truth at 2 a.m.
Amounts belong in integers or decimal-safe types
For Stripe, use minor units: cents for USD, not floating-point dollars. PayPal accepts decimal strings in major units. Normalize internally to integer minor units plus ISO currency. Floating point has no place in money.
Field notes
- If checkout creation succeeds but your database commit fails, you now have an orphan provider object; design cleanup or lookup jobs for that case.
- Do not put customer PII or free-form cart JSON into provider metadata.
- Store raw provider status plus your normalized status. Provider terms change less often than business logic assumptions.
- A client secret is secret to the payment attempt, not a universal admin credential, but still should not be logged.
- Expire abandoned local payment attempts so inventory and reporting do not lie forever.
Was this lesson helpful?