API Cookbook¶
This cookbook gives practical, end-to-end integration patterns with production-oriented checklists.
Flow 1: Create Payment -> Redirect -> Verify -> Fulfill¶
Create request¶
{
"amount": 5000,
"currency": "NGN",
"description": "Order ORD-1042",
"customerEmail": "jane@example.com",
"customerName": "Jane Doe",
"customerPhone": "+2348012345678",
"redirectUrl": "https://merchant.example.com/pay/callback",
"webhookUrl": "https://merchant.example.com/api/webhook/Paystack",
"paymentMethodType": 0,
"gateway": 2
}
In this example, paymentMethodType: 0 selects PaymentMethodType.Card and
gateway: 2 selects PaymentGatewayType.Paystack.
Checkout workflow¶
- Call CreatePaymentAsync with explicit gateway when possible.
- Persist order as Pending with transaction reference.
- Redirect user to checkoutUrl from SDK response.
- On redirect or webhook, call VerifyPaymentAsync.
- Fulfill only when provider verification confirms success and expected amount/currency.
Checkout checklist¶
- Never fulfill based only on redirect query or webhook payload body.
- Compare verified amount/currency/reference against original order.
- Use idempotency keys for create operations exposed to client retries.
Flow 2: Automatic Routing with Fallback¶
Routing goal¶
Route by currency while preserving deterministic behavior and safe fallback.
Gateway selection¶
- Keep gateway config explicit and validated on startup.
- Set DefaultGateway only when it is compatible and configured.
- Reject unsupported combinations before external provider calls.
Routing checklist¶
- Monitor selected gateway and routing reason in structured logs.
- Keep per-provider callback URLs aligned with actual selected provider.
- Validate startup configuration in CI/CD before deployment.
Flow 3: Secure Webhooks¶
Webhook verification¶
- Preserve raw request body bytes.
- Verify provider signature before JSON parsing.
- Reject missing/invalid signatures with no side effects.
- Apply replay protection and idempotent processing.
- Re-verify transaction with provider API before final state update.
Webhook checklist¶
- Do not log full webhook payloads, signatures, or secrets.
- Keep webhook timestamp tolerance tight and clocks synchronized.
- Store replay receipt IDs with bounded retention.
Flow 4: Refund Request and Reconciliation¶
Refund workflow¶
- Accept refund request with explicit amount and reason.
- Call RefundPaymentAsync.
- Persist refund attempt with provider status and reference.
- Reconcile pending refunds asynchronously until terminal state.
Refund checklist¶
- Treat refunds as asynchronous by default. For Stripe, only
succeededmaps toRefunded;pending,requires_action, and unrecognized or missing statuses stayPending. Stripe'sfailedandcanceledare terminal outcomes. - An accepted request is not proof of disbursement. Use the returned status and provider reference to reconcile; do not resubmit a pending refund.
- Guard against duplicate refund submissions with idempotency keys.
- Reconcile uncertain provider outcomes using durable persistence jobs.
Error-Handling Playbook¶
- 4xx request errors: map to actionable client messages.
- Timeout/transient network failures: retry only where idempotent.
- Provider failures: normalize status and preserve provider reference IDs for audit.
- Security failures: fail closed and return minimal error detail.