Part 6 - Integrate Stripe Payments with React Native and FastAPI
Design a secure Stripe payment flow with React Native, FastAPI, trusted server-side totals, and webhooks.
Integrate Stripe Payments with React Native and FastAPI
The MugMori series now has a catalog, product details, cart state, mock checkout, and a future FastAPI boundary. This article replaces the mock payment step with a provider integration design using Stripe.
The code is educational. It does not add Stripe to this Django repository, process a real payment, or claim that the MugMori backend is implemented.
Put payment authority on the server
The correct boundary is:
React Native app
|
v
FastAPI payment endpoint
|
v
Stripe API
|
v
Stripe webhook -> FastAPI
React Native owns the checkout experience. FastAPI owns order creation, trusted prices, payment intent creation, and webhook handling. The Stripe secret key must never be placed in the mobile application.
Create a payment intent on FastAPI
The client should submit product identifiers, quantities, and selected options. It should not submit a final amount that the server blindly trusts.
import os
import stripe
from fastapi import APIRouter
from pydantic import BaseModel
stripe.api_key = os.environ['STRIPE_SECRET_KEY']
router = APIRouter(prefix='/payments')
class PaymentRequest(BaseModel):
product_ids: list[str]
quantities: list[int]
@router.post('/intent')
def create_payment_intent(payload: PaymentRequest):
amount = calculate_trusted_total_from_database(payload)
intent = stripe.PaymentIntent.create(
amount=amount,
currency='sgd',
automatic_payment_methods={'enabled': True},
)
return {'client_secret': intent.client_secret}
The real implementation should validate stock, prices, currency, quantities, discounts, and order ownership before calling Stripe. Use an idempotency key when retrying a request so a network retry does not create duplicate payment intents.
Confirm payment in React Native
The mobile client can use Stripe's React Native SDK and PaymentSheet. It receives a client secret from FastAPI, not the Stripe secret key.
const response = await api.post('/payments/intent', cartPayload);
const { error } = await initPaymentSheet({
merchantDisplayName: 'MugMori',
paymentIntentClientSecret: response.client_secret,
});
if (!error) {
const result = await presentPaymentSheet();
if (result.error) {
showPaymentError(result.error.message);
}
}
The app should show a pending state while the payment flow is open and should not display an order as paid merely because the client returned to the app.
Treat webhooks as the source of truth
After payment, Stripe sends an event to a server endpoint. Verify the webhook signature with the webhook signing secret, record the event identifier, and process each event idempotently.
@router.post('/webhook')
async def stripe_webhook(request: Request):
payload = await request.body()
signature = request.headers.get('stripe-signature', '')
event = stripe.Webhook.construct_event(
payload, signature, os.environ['STRIPE_WEBHOOK_SECRET']
)
if event['type'] == 'payment_intent.succeeded':
mark_order_paid_once(event['data']['object']['id'])
return {'received': True}
Do not trust a value in the mobile response as proof of payment. The webhook is the provider-confirmed event that should move an order from pending to paid.
Where Django fits
For MugMori, FastAPI is the natural payment API boundary for the future React Native client. Django can still own admin screens, content, catalog management, and operational review. If the existing Django site later sells courses or another Django-owned product, Django may integrate with Stripe directly for that separate workflow.
Do not create duplicate payment models in both frameworks. Decide which service owns orders, payment status, refunds, and webhook processing before implementation.
Singapore payment methods with Stripe
Stripe’s exact checkout options depend on the Singapore account, currency, customer location, and device. This table makes unsupported or conditional methods visible instead of silently omitting them.
| Payment method | Singapore Stripe status | Notes |
|---|---|---|
| Visa | Available | Card payment. |
| Mastercard | Available | Card payment. |
| American Express | Available | Card payment where enabled. |
| PayNow | Available | QR payment; one-time payments only. |
| DBS PayLah! | Available through PayNow | PayLah! scans the PayNow QR; it is not a separate Stripe method. |
| GrabPay | Available | Redirect-based wallet flow for supported SGD transactions. |
| Apple Pay | Available | Requires eligible device, browser, card, and merchant setup. |
| Google Pay | Available | Requires eligible device, browser, card, and merchant setup. |
| PayPal | Conditional | Stripe enables it only for eligible business locations and accounts; do not assume it is available for every Singapore account. |
| ShopeePay | Not listed as a standard Stripe Singapore method | Use another provider if ShopeePay is required. |
| Atome | Not listed as a standard Stripe Singapore method | Verify current Stripe account availability before promising BNPL. |
| Alipay, WeChat Pay, and China UnionPay | Conditional | Intended for eligible cross-border customers and subject to account and currency rules. |
Do not hard-code this table into the mobile app. Let Stripe Checkout, Payment Element, or the server-side PaymentIntent configuration determine which methods are actually available. Confirm payment from Stripe webhooks before marking the order as paid.
Test safely
Use Stripe test mode and test payment methods. Test successful payments, declines, cancellations, duplicate webhook delivery, expired sessions, invalid totals, and unavailable products. Never place live keys in source control, mobile bundles, blog posts, or client-side configuration.
This repository currently documents the Django plus FastAPI architecture but does not yet implement this payment flow. The next engineering step would be a separate accepted plan for order models, provider configuration, protected endpoints, webhook verification, and deployment secrets.
Next: Part 7 - Integrate HitPay Payments with React Native and FastAPI