BBackend CourseLeroy Labs
0/0
Search the course

Payments with Stripe Checkout, Paystack, manual references & compensation

Hosted checkout vs custom forms, the order/payment state machine, metadata to link payments to orders, webhooks as the source of truth, compensation on failure, payouts and reconciliation.

● advancedSpring-Boot-StoreIR3D (Kontentt)Hostel-Web-App

The flow (hosted checkout)

1. POST /checkout -> create Order(PENDING) in DB
2. API -> Stripe: create Checkout Session (line items, metadata.order_id, success/cancel URLs)
3. API -> client: { checkoutUrl }
4. User pays on Stripe's page (card data never touches you -> minimal PCI scope)
5. Stripe -> webhook payment_intent.succeeded (metadata.order_id) -> Order PAID
6. Browser redirected to success URL (this is NOT proof of payment!)

The redirect is not proof. Users can close the tab or forge the success URL. Only the verified webhook (or an API lookup) marks an order paid.

In your projects

Spring-Boot-Storepayment/PaymentGateway · StripePaymentGateway · services/CheckoutService · StripeConfig

A port/adapter: PaymentGateway has createCheckoutSession(Order) and parseWebhookRequest(WebhookRequest), implemented by StripePaymentGateway:

SessionCreateParams.builder()
    .setMode(SessionCreateParams.Mode.PAYMENT)
    .setSuccessUrl(websiteUrl + "/checkout-success?orderId=" + order.getId())
    .setPaymentIntentData(PaymentIntentData.builder().putMetadata("order_id", order.getId().toString()).build())
    // line items: setUnitAmountDecimal(unitPrice x 100), currency "usd"

Compensation in CheckoutService.checkout: save the order as PENDING, create the session, clear the cart. If a PaymentException occurs, run orderRepository.delete(saved); throw e;, which undoes the local step when the remote step fails. That’s a mini saga.

IR3D (Kontentt)wallet/infrastructure/paystack · PaystackWebhookController · LedgerService · RevenueSweepJob · WithdrawalReconciliationJob · CreatorRefundService · CarryoverService

A full money platform on Paystack (popular in Ghana and Nigeria for mobile money and cards):

  • Pay-ins (brands fund campaigns) and payouts (creators withdraw to MoMo or bank) with transfer recipients.
  • HMAC-SHA512 webhooks, deduplicated in webhook_events.
  • A double-entry ledger (LedgerService) for every movement, plus earnings holds, refunds and campaign carryover.
  • Reconciliation: WithdrawalReconciliationJob every 5 minutes and a nightly LedgerReconciliationJob.
  • StubPaymentGateway for dev and tests (app.payments.provider).
Hostel-Web-AppSubmitPaymentRequest · Booking.paymentRef · paymentExpiresAt · BookingExpiredSweeper

Manual payment: the student pays by MTN Mobile Money or bank slip and submits the reference, and the manager verifies it offline. A payment deadline (paymentExpiresAt) and the sweeper release unpaid beds. PaymentFailedException maps to HTTP 402. It’s a valid MVP, with no fees or gateway integration.

Payment state machine

PENDING --webhook success--> PAID --refund--> REFUNDED
   |                          \--partial refund--> PARTIALLY_REFUNDED
   |--webhook failed--> FAILED
   \--expired (timeout)--> EXPIRED / CANCELLED

Never move backward (PAID → FAILED) on a late or out-of-order webhook.

Alternatives

Provider Strength
Stripe Global cards, subscriptions (Billing), Connect for marketplaces
Paystack / Flutterwave / Hubtel African cards + mobile money, payouts
PayPal / Adyen / Braintree Enterprise and global coverage
Lemon Squeezy / Paddle Merchant of record (they handle VAT/sales tax)
  • Idempotency keys on payment API calls (Stripe accepts an Idempotency-Key header), so retries don’t double-charge (Module 6).
  • Never trust client amounts. Compute prices on the server from your DB.
  • PCI DSS. Hosted pages or elements keep raw cards off your servers.
  • Refunds and chargebacks are their own webhooks and states.
  • Reconciliation. Daily: compare provider settlements to your ledger and alert on mismatch.
  • Currency. Minor units; never use floats (money and time lesson).

Check yourself

1. What proves an order is paid?
2. Store deletes the PENDING order if Stripe session creation fails. This is…
3. Why run reconciliation jobs?