Creating a Charge
Create your first charge with KingsPay by following the flow below—prepare the request, hand the payment instructions to your customer, and react to the status updates. More details about the API can be found in the Create Payment Charge reference.
Before you start
This walkthrough assumes you already have:
- An activated KingsPay account (see Getting Started if you still need to complete activation).
- An API key with the
Payments.ManageandPayments.Readpermissions enabled. Your key must be sent as a Bearer token — see Authentication. - A webhook destination configured to receive charge events (
payment_charge.created,payment_charge.succeeded,payment_charge.failed).
1. Create the charge via the API
From your backend, call POST /v1/payments/charges authenticated with your secret API key. To prevent duplicate charges on retries, pass an Idempotency-Key header — see Idempotent Requests. The request body accepts the following key fields:
| Field | Description |
|---|---|
payment_method | The code of the payment method to use. List the ones your account can use with GET /v1/payments/methods, and see Payment Methods for all supported values. |
payment_method_options | (Optional) Method-specific options. Currently used by virtual account methods to set a custom account_number and account_name. See Payment Methods for details. |
amount | Amount to charge, e.g. 20000 for 20,000 IDR, 20.50 for 20.50 MYR. |
currency | Currency code, e.g. idr, myr. |
customer_details | Name, email, and phone number of the customer. |
client_reference_id | Your own internal identifier for this payment — your order ID, cart ID, or any reference meaningful in your system. We store it as-is and echo it back on every charge response and event so you can always map our records to your own. Set this before making the call. |
expires_at | When the charge should expire if unpaid. |
description | A human-readable description of the charge. |
items | Line items included in this charge. |
Here's a simplified example:
curl -X POST "https://api.kingsplatform.net/api/api/v1/payments/charges" \
-H "Authorization: Bearer YOUR_API_KEY_HERE" \
-H "Content-Type: application/json" \
-d '{
"payment_method": "qris",
"customer_details": {
"email": "[email protected]",
"name": "John Doe",
"phone_number": "621234567890"
},
"currency": "idr",
"amount": 10000,
"expires_at": "2026-03-02T06:58:40.000Z",
"client_reference_id": "your-own-order-or-ref-id",
"description": "A Descriptive Description",
"items": [
{
"name": "Premium wallet",
"total_price": 10000,
"quantity": 1
}
]
}'If the request succeeds, you'll receive a charge object with status = "pending", the hosted payment URL, and other instructions you can surface to your customer.
2. Persist the charge
After the API responds, store the data returned by the API against your existing order or record (identified by the client_reference_id you already have). The key fields to persist are:
id— the charge ID. Store this alongside your own record so you can look up charges directly.statusandexpires_at, so you can drive dashboards or SLAs without an extra API call.payment_actiondata — especially anyhosted_url, QR code payload, or virtual-account number you plan to show to the customer.- Amount-related fields (
amount,currency,fee,net_amount) so you can verify nothing changed in transit.
Because you set client_reference_id to your own internal ID before the call, reconciling incoming events is straightforward: the payload always contains your original reference alongside the KingsPay charge id.
3. Show payment instructions to the customer
The charge response includes a payment_action object describing exactly what to present to the customer. Depending on the payment method, this will be a redirect URL, a QR code, or virtual account transfer details. See Payment Actions for the full breakdown of each action type and its fields.
The simplest approach is to redirect your customer to the hosted_url — KingsPay renders the correct payment UI automatically. Alternatively, render the instructions yourself using the structured fields in payment_action.
4. Listen for charge events
Your server should subscribe to payment_charge.created, payment_charge.succeeded, and payment_charge.failed events. Use the payload's charge ID or client_reference_id to locate the charge you stored in step 2 and update its status. This keeps your dashboard, fulfillment system, and customer notifications in sync.
5. Update downstream workflows
Do not mark the order as paid until you receive payment_charge.succeeded. When the status changes to failed, trigger whatever remediation you need (retry, notify the customer, or release reserved inventory). Keeping this logic in one place—usually a background job consuming events—prevents race conditions between your UI and backend.
Testing checklist
Before going live:
- Create a test charge and confirm it appears in your dashboard and database.
- Complete the payment using sandbox credentials and verify that your event handler updates the charge to
succeeded. - Simulate a failed payment (cancel or let it expire) and ensure your system reflects the failure state.
- Confirm that retrying the request with the same
Idempotency-Keydoes not create duplicates in your system.
Once these pieces work together, you can move on to the broader flow described in the charges overview and start handling real customer payments.