1. Quick Start

Accept payments in minutes using the Apex FlexPay REST API and Checkout SDK:

  1. Create a merchant account and copy your Test Keys (`pk_test_...` and `sk_test_...`).
  2. Call POST /api/v1/transactions/initialize from your backend to get a checkout_url.
  3. Redirect the customer to checkout_url or open the modal with FlexPay.checkout(...).
  4. Verify the payment server-to-server via GET /api/v1/transactions/verify/{reference} and listen for signed webhooks.

2. Authentication & API Keys

Authenticate server-to-server requests using your Secret Key in the Authorization header:

Authorization: Bearer sk_test_xxxxxxxxxxxxxxxxxxxxxxxx

Never expose your Secret Key (`sk_...`) in frontend JavaScript or mobile apps. Use your Public Key (`pk_...`) for the Checkout JS SDK.

3. Initialize Payment (Web Checkout)

POST https://apex-flexpay.top/api/v1/transactions/initialize

curl -X POST https://apex-flexpay.top/api/v1/transactions/initialize \
  -H "Authorization: Bearer sk_test_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: order-10001-attempt-1" \
  -d '{
    "amount": 100.00,
    "currency": "GHS",
    "email": "customer@example.com",
    "phone": "0244000000",
    "reference": "ORDER-10001",
    "callback_url": "https://yoursite.com/payment/callback",
    "description": "Order #10001",
    "metadata": {"order_id": 10001}
  }'

Direct MoMo PIN Push (For USSD & Headless Apps)

Trigger a Mobile Money PIN prompt directly on the customer's handset without opening a web checkout page: POST https://apex-flexpay.top/api/v1/transactions/charge

curl -X POST https://apex-flexpay.top/api/v1/transactions/charge \
  -H "Authorization: Bearer sk_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: ussd-10001" \
  -d '{
    "amount": 50.00,
    "currency": "GHS",
    "email": "customer@example.com",
    "phone": "0244000000",
    "network": "mtn",
    "reference": "USSD-10001",
    "description": "USSD Order #10001"
  }'

Supported network values: mtn, telecel, at. Returns status: "pending" while awaiting PIN approval, then notifies your server via webhook (payment.success) or GET /api/v1/transactions/verify/{reference}.

4. Verify Payment

Always verify transactions server-to-server before delivering value. Never trust browser redirects alone.

curl https://apex-flexpay.top/api/v1/transactions/verify/ORDER-10001 \
  -H "Authorization: Bearer sk_test_YOUR_KEY"

5. Embedded JavaScript Popup SDK (No Redirect)

Embed the payment modal directly inside your website using sdk/flexpay.js in two ways:

Option A — Frontend (pk_...):

<script src="https://apex-flexpay.top/sdk/flexpay.js"></script>
<script>
FlexPay.checkout({
  key: "pk_live_YOUR_PUBLIC_KEY",
  amount: 10.00,
  currency: "GHS",
  email: "customer@example.com",
  reference: "ord20261118586988",
  onSuccess: function(res) { /* verify on backend */ },
  onCancel: function() {}
});
</script>

Option B — After backend /api/v1/transactions/initialize (access_code / checkout_url):

<script src="https://apex-flexpay.top/sdk/flexpay.js"></script>
<script>
FlexPay.checkout({
  access_code: "SESSION_ACCESS_CODE", // or checkout_url: "..."
  onSuccess: function(res) { window.location.href = "/flexpay_callback?reference=" + res.reference; },
  onCancel: function() {}
});
</script>

6. Complete PHP Server-Side Example

<?php
$ch = curl_init('https://apex-flexpay.top/api/v1/transactions/initialize');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'Authorization: Bearer sk_test_YOUR_KEY',
        'Content-Type: application/json',
        'Idempotency-Key: ' . uniqid('idem_', true),
    ],
    CURLOPT_POSTFIELDS => json_encode([
        'amount' => 150.00,
        'currency' => 'GHS',
        'email' => 'customer@example.com',
        'reference' => 'ORD-' . time(),
        'callback_url' => 'https://yoursite.com/callback',
    ]),
]);
$res = json_decode(curl_exec($ch), true);
header('Location: ' . $res['data']['checkout_url']);

7. Webhooks & Signature Verification

Events: payment.success, payment.failed, payment.pending, payment.refunded, payment.partially_refunded, payment.expired.

Verify the X-ApexPay-Signature header using HMAC-SHA256 with your Webhook Secret:

$payload = file_get_contents('php://input');
$sig = $_SERVER['HTTP_X_APEXPAY_SIGNATURE'] ?? '';
$expected = hash_hmac('sha256', $payload, $yourWebhookSecret);
if (!hash_equals($expected, $sig)) {
    http_response_code(401);
    exit;
}

8. Idempotency

Send Idempotency-Key: your-unique-key on POST /api/v1/transactions/initialize. Retrying with the same key and payload returns the original transaction without creating a duplicate charge.

9. Refunds & Transfers (Payouts)

curl -X POST https://apex-flexpay.top/api/v1/refunds \
  -H "Authorization: Bearer sk_test_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"reference": "ORDER-10001", "amount": 50.00, "reason": "Partial refund"}'

Platform Withdrawals & Transfers (MoMo or Bank): POST https://apex-flexpay.top/api/v1/withdrawals (alias: /api/v1/transfers). Ideal for platforms, fintechs, and apps paying out user profits or wallet balances directly using your business balance.

curl -X POST https://apex-flexpay.top/api/v1/withdrawals \
  -H "Authorization: Bearer sk_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"amount": 200.00, "channel": "mobile_money", "network": "mtn", "recipient_msisdn": "0244000000", "recipient_name": "Ama Owusu", "narration": "User profit withdrawal"}'

Bank Transfer Withdrawal:

curl -X POST https://apex-flexpay.top/api/v1/withdrawals \
  -H "Authorization: Bearer sk_live_YOUR_KEY" \
  -H "Content-Type: application/json" \
  -d '{"amount": 500.00, "channel": "bank_transfer", "recipient_bank_code": "300312", "recipient_account_number": "1234567890", "recipient_name": "Vendor Ltd", "narration": "Wallet payout"}'

Check Available Balance & Unified Status Lookup: GET https://apex-flexpay.top/api/v1/balance, GET https://apex-flexpay.top/api/v1/withdrawals/{reference}, or GET https://apex-flexpay.top/api/v1/status/{reference} (works for both collections and withdrawals).

curl https://apex-flexpay.top/api/v1/status/TRF-20261120-XXXX \
  -H "Authorization: Bearer sk_live_YOUR_KEY"

Official SDKs & Plugins: PHP SDK Node.js SDK Python SDK WooCommerce Gateway

Bulk Withdrawals, Sub-Accounts & Invoices: POST /api/v1/withdrawals/bulk, GET/POST /api/v1/subaccounts, GET/POST /api/v1/invoices.

Safe User Withdrawals (with Idempotency-Key Protection)

Pass an optional Idempotency-Key header (or unique reference in JSON) when letting users on your site withdraw funds. If a network retry or double-click sends the same key twice, we safely return the original transfer without paying twice.

// PHP Example — User Withdrawal to Mobile Money (Idempotent)
$ch = curl_init("https://apex-flexpay.top/api/v1/withdrawals");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer sk_live_YOUR_SECRET_KEY",
        "Content-Type: application/json",
        "Idempotency-Key: WD-USER-1042-20261201"
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "amount" => 120.00,
        "channel" => "mobile_money",
        "network" => "mtn",
        "recipient_msisdn" => "0244000000",
        "recipient_name" => "Kwame Mensah",
        "narration" => "User wallet cashout"
    ]),
]);
$res = json_decode(curl_exec($ch), true);
// Node.js (fetch) Example — User Withdrawal
const res = await fetch("https://apex-flexpay.top/api/v1/withdrawals", {
  method: "POST",
  headers: {
    "Authorization": "Bearer sk_live_YOUR_SECRET_KEY",
    "Content-Type": "application/json",
    "Idempotency-Key": "WD-USER-1042-20261201"
  },
  body: JSON.stringify({
    amount: 120.00,
    channel: "mobile_money",
    network: "mtn",
    recipient_msisdn: "0244000000",
    recipient_name: "Kwame Mensah",
    narration: "User wallet cashout"
  })
});
const data = await res.json();
# Python (requests) Example — User Withdrawal
import requests
r = requests.post(
    "https://apex-flexpay.top/api/v1/withdrawals",
    headers={
        "Authorization": "Bearer sk_live_YOUR_SECRET_KEY",
        "Idempotency-Key": "WD-USER-1042-20261201",
    },
    json={
        "amount": 120.00,
        "channel": "mobile_money",
        "network": "mtn",
        "recipient_msisdn": "0244000000",
        "recipient_name": "Kwame Mensah",
        "narration": "User wallet cashout",
    },
)
print(r.json())

10. Standardized Error Codes

INVALID_API_KEY (401), INVALID_AMOUNT (422), INVALID_CURRENCY (422), MERCHANT_NOT_ACTIVE (403), API_PAYOUTS_DISABLED (403), IP_NOT_WHITELISTED (403), DAILY_PAYOUT_LIMIT_EXCEEDED (422), TRANSACTION_NOT_FOUND (404), DUPLICATE_REFERENCE (409), IDEMPOTENCY_CONFLICT (409), RATE_LIMITED (429), PROCESSOR_ERROR (503), REFUND_NOT_ALLOWED (422).