Merchant Demo
Live

Developers

Take your first payment in six steps. Copy the code, add your credentials, run it.

The code uses example values. Create a test payment and it will use yours.

highlighted in the code = replace with your own

Before you start

A payment is three conversations. Your server asks the gateway to create it. The customer pays on the gateway's page. The gateway tells your server the result.

  • Merchant ID and API key. Your account manager gives you both. The key is a password: it lives on your server and never in a web page or app.
  • API base URL. The samples already use https://api.jgopay.com.
  • A public HTTPS address for the callback. The gateway cannot reach localhost. While developing on your own machine, expose it with a tunnel.

What you change

In every sample, the parts you must replace are highlighted like this. Everything else works as it is: copy it unchanged. There are five things to change.

  1. 1Your credentials in gateway.phpPut in your YOUR_MERCHANT_ID and YOUR_API_KEY. In production, read the key from an environment variable or a secret store instead of typing it into the file.
  2. 2The gateway address in gateway.phpSet the base URL your account manager gave you. Test and production have different addresses.
  3. 3The order values in pay.phpThe amount, reference_id, customer name and phone come from your own order, not from fixed text. Send a new reference_id for every payment.
  4. 4Your own addresses in pay.phpcallback_url is where the gateway reports the result. redirect_url is where the customer lands after paying. Both must be on your site.
  5. 5Your order logic in callback.phpThe highlighted comment lines show where your code goes: mark the order paid, mark it failed, and skip a callback you have already handled.

1. Sign every request

Every request carries three headers: your merchant ID, the current time, and a signature that proves the request came from you and was not changed. The signature is an HMAC-SHA256 of four lines: the timestamp, the HTTP method, the path, and the SHA-256 of the body. Authentication ↗

gateway.php
<?php
// gateway.php: shared helpers. This file stays on your server.
// Never send the API key to a browser or a mobile app.

const GATEWAY_BASE_URL = 'https://api.jgopay.com'; // from your account manager
const MERCHANT_ID      = 'YOUR_MERCHANT_ID';
const API_KEY          = 'YOUR_API_KEY';

/** Headers that sign one request. $path is the URL path without the query string. */
function signed_headers(string $method, string $path, string $body = ''): array {
    $timestamp    = (string) time(); // Unix time in seconds; must be within 5 minutes of the gateway's clock
    $stringToSign = implode("\n", [$timestamp, strtoupper($method), $path, hash('sha256', $body)]);
    $signature    = hash_hmac('sha256', $stringToSign, API_KEY);

    return [
        'X-Merchant-Id: ' . MERCHANT_ID,
        'X-Timestamp: ' . $timestamp,
        'X-Signature: ' . $signature,
        'Content-Type: application/json',
        'Accept: application/json',
    ];
}

/** Sends a signed request. Returns the "data" object, or throws with the gateway's error. */
function gateway_request(string $method, string $endpoint, ?array $payload = null): array {
    $url  = GATEWAY_BASE_URL . $endpoint;
    $path = parse_url($url, PHP_URL_PATH);
    $body = $payload === null ? '' : json_encode($payload);

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_HTTPHEADER     => signed_headers($method, $path, $body),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 15,
    ]);
    if ($payload !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // send exactly the bytes that were signed
    }
    $raw    = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error  = curl_error($ch);
    curl_close($ch);

    if ($raw === false) {
        throw new RuntimeException('Could not reach the gateway: ' . $error);
    }
    $json = json_decode($raw, true);
    if ($status < 200 || $status >= 300 || ($json['status'] ?? '') !== 'success') {
        throw new RuntimeException(($json['error'] ?? 'HTTP_' . $status) . ': ' . ($json['message'] ?? $raw));
    }
    return $json['data'];
}
  • Sign the path with its prefix and without the query string, for example /payment/request.
  • Send exactly the body you signed. Do not encode the JSON a second time after signing.
  • The timestamp is in seconds and must be within 5 minutes of the real time.

2. Create a payment

When the customer clicks Pay, your server sends one request. reference_id is your own order number: letters and numbers only, and never reused. Amounts for this currency: 5 to 50,000 MYR, up to 2 decimals. Create Payment ↗

pay.php
<?php
// pay.php: your checkout calls this when the customer clicks "Pay".
require __DIR__ . '/gateway.php';

try {
    // reference_id is your own order number. It must be unique per payment: letters and numbers only.
    $payment = gateway_request('POST', '/payment/request', [
        'amount'         => 111,
        'currency'       => 'MYR',
        'reference_id'   => 'ORDER20260910001',
        'customer_name'  => 'John Doe',
        'customer_phone' => '60123456789',
        'callback_url'   => 'https://your-site.example/callback.php',
        'redirect_url'   => 'https://your-site.example/thank-you.php',
    ]);
} catch (RuntimeException $e) {
    http_response_code(502);
    exit('Payment could not be started: ' . $e->getMessage());
}

// Save $payment['payment_id'] with your order, then send the customer to the payment page.
header('Location: ' . $payment['payment_url']);
exit;

The gateway answers with the payment and the address of its payment page:

response
{
  "status": "success",
  "data": {
    "payment_id": "PAY_ABC1234567",
    "payment_url": "https://payment.example/p/ABC1234567",
    "status": "PENDING",
    "amount": 111,
    "currency": "MYR",
    "reference_id": "ORDER20260910001",
    "expires_in": 1800,
    "expires_at": "2026-09-10T12:30:00Z"
  }
}

Leaving out payment_method lets the customer choose how to pay on the payment page, which is the simplest integration. To choose for them, add payment_method; the response then also has a gateway object with the bank or wallet address. Payment Methods ↗

3. Send the customer to the payment page

Redirect the browser to payment_url, as the sample above does once the payment is created. If you chose the method yourself, use gateway.redirect_url when the response has one. Use a full-page redirect or a link the customer clicks. Do not put the payment page in an iframe: banks and wallets refuse to run inside one.

After paying, the customer returns to your redirect_url. That return is not proof of payment, because anyone can open that address. Only the callback in the next step confirms it.

4. Receive the callback

When the payment ends, the gateway sends a POST to your callback_url. Check its signature, update the order, and answer with HTTP 200. Callbacks ↗

callback.php
<?php
// callback.php: the gateway POSTs here when a payment ends. This is your callback_url.
require __DIR__ . '/gateway.php';

/** True only when the callback was signed with your API key. */
function verify_callback(array $payload): bool {
    foreach (['webhook_id', 'payment_id', 'reference_id', 'amount', 'currency', 'status', 'timestamp', 'signature'] as $field) {
        if (!isset($payload[$field])) return false;
    }
    $dataString = implode('|', [
        $payload['webhook_id'],
        $payload['payment_id'],
        $payload['reference_id'],
        (string) $payload['amount'],
        $payload['currency'],
        $payload['status'],
        $payload['timestamp'],
        $payload['completed_at'] ?? '',
    ]);
    $apiKeyHash = hash('sha256', API_KEY, true);
    $signingKey = hash_hkdf('sha256', $apiKeyHash, 32, 'webhook-signature', '');
    $expected   = hash_hmac('sha256', $dataString, $signingKey);

    return hash_equals($expected, (string) $payload['signature']);
}

$payload = json_decode(file_get_contents('php://input'), true);

if (!is_array($payload) || !verify_callback($payload)) {
    http_response_code(401);
    exit('Invalid signature');
}

// The same callback can arrive more than once. Act only the first time you see this payment_id:
// if (order_already_processed($payload['payment_id'])) { http_response_code(200); exit('OK'); }

if ($payload['status'] === 'SUCCESS') {
    // mark_order_paid($payload['reference_id'], $payload['amount']);
} else {
    // FAILED, EXPIRED, CANCELLED or REFUNDED
    // mark_order_failed($payload['reference_id'], $payload['status']);
}

http_response_code(200); // any 2xx tells the gateway to stop retrying
echo 'OK';

The body you receive:

callback body
{
  "webhook_id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
  "payment_id": "PAY_ABC1234567",
  "reference_id": "ORDER20260910001",
  "amount": 111,
  "currency": "MYR",
  "status": "SUCCESS",
  "transaction_type": "DEPOSIT",
  "gateway_reference": "TXN_20260910_ABC123",
  "completed_at": "2026-09-10T12:15:30Z",
  "created_at": "2026-09-10T12:03:00Z",
  "timestamp": "2026-09-10T12:15:31.000Z",
  "signature": "a1b2c3d4…"
}
  • Reject any callback whose signature does not verify. It is the only thing that tells a real callback from a forged one.
  • The same callback can arrive more than once. Record the payment_id and deliver the order only the first time.
  • Answer quickly with any 2xx status. Without it the gateway retries, up to three times.
  • Deliver the goods on SUCCESS only. Every other status means the customer has not paid.

5. Check the status when in doubt

If a callback has not arrived, for example because your server was down, ask the gateway. You can use its payment_id or your own reference_id. Keep the callback as the main route and this as the fallback.

status.php
<?php
// status.php: ask the gateway for the current status. Use it as a fallback, not instead of the callback.
require __DIR__ . '/gateway.php';

// Works with the gateway's payment_id or with your own reference_id.
$payment = gateway_request('GET', '/payment/status/' . 'ORDER20260910001');

echo $payment['status']; // PENDING, PROCESSING, SUCCESS, FAILED, EXPIRED, CANCELLED or REFUNDED

6. Test it

On the test environment the amount decides the outcome, so you can test the whole flow without paying. Testing ↗

Whole part of the amount ends inResult after 5 to 10 seconds
11 e.g. 111.00, 511.00SUCCESS with a real, signed callback
22 e.g. 122.00, 522.00FAILED with a real, signed callback

You are done when all four are true:

  1. A payment with an amount ending in 11 is created and the browser lands on the payment page.
  2. Your callback address receives the SUCCESS callback and the signature verifies.
  3. Sending the same callback twice delivers the order once.
  4. A payment with an amount ending in 22 leaves the order unpaid.

Complete files

The steps above are parts of these files. Download them, put in your merchant ID and API key, and run. Needs PHP 7.4 or newer with the curl extension. Put the four files in one folder on your server.

gateway.php
<?php
// gateway.php: shared helpers. This file stays on your server.
// Never send the API key to a browser or a mobile app.

const GATEWAY_BASE_URL = 'https://api.jgopay.com'; // from your account manager
const MERCHANT_ID      = 'YOUR_MERCHANT_ID';
const API_KEY          = 'YOUR_API_KEY';

/** Headers that sign one request. $path is the URL path without the query string. */
function signed_headers(string $method, string $path, string $body = ''): array {
    $timestamp    = (string) time(); // Unix time in seconds; must be within 5 minutes of the gateway's clock
    $stringToSign = implode("\n", [$timestamp, strtoupper($method), $path, hash('sha256', $body)]);
    $signature    = hash_hmac('sha256', $stringToSign, API_KEY);

    return [
        'X-Merchant-Id: ' . MERCHANT_ID,
        'X-Timestamp: ' . $timestamp,
        'X-Signature: ' . $signature,
        'Content-Type: application/json',
        'Accept: application/json',
    ];
}

/** Sends a signed request. Returns the "data" object, or throws with the gateway's error. */
function gateway_request(string $method, string $endpoint, ?array $payload = null): array {
    $url  = GATEWAY_BASE_URL . $endpoint;
    $path = parse_url($url, PHP_URL_PATH);
    $body = $payload === null ? '' : json_encode($payload);

    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_CUSTOMREQUEST  => $method,
        CURLOPT_HTTPHEADER     => signed_headers($method, $path, $body),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_TIMEOUT        => 15,
    ]);
    if ($payload !== null) {
        curl_setopt($ch, CURLOPT_POSTFIELDS, $body); // send exactly the bytes that were signed
    }
    $raw    = curl_exec($ch);
    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error  = curl_error($ch);
    curl_close($ch);

    if ($raw === false) {
        throw new RuntimeException('Could not reach the gateway: ' . $error);
    }
    $json = json_decode($raw, true);
    if ($status < 200 || $status >= 300 || ($json['status'] ?? '') !== 'success') {
        throw new RuntimeException(($json['error'] ?? 'HTTP_' . $status) . ': ' . ($json['message'] ?? $raw));
    }
    return $json['data'];
}
pay.php
<?php
// pay.php: your checkout calls this when the customer clicks "Pay".
require __DIR__ . '/gateway.php';

try {
    // reference_id is your own order number. It must be unique per payment: letters and numbers only.
    $payment = gateway_request('POST', '/payment/request', [
        'amount'         => 111,
        'currency'       => 'MYR',
        'reference_id'   => 'ORDER20260910001',
        'customer_name'  => 'John Doe',
        'customer_phone' => '60123456789',
        'callback_url'   => 'https://your-site.example/callback.php',
        'redirect_url'   => 'https://your-site.example/thank-you.php',
    ]);
} catch (RuntimeException $e) {
    http_response_code(502);
    exit('Payment could not be started: ' . $e->getMessage());
}

// Save $payment['payment_id'] with your order, then send the customer to the payment page.
header('Location: ' . $payment['payment_url']);
exit;
callback.php
<?php
// callback.php: the gateway POSTs here when a payment ends. This is your callback_url.
require __DIR__ . '/gateway.php';

/** True only when the callback was signed with your API key. */
function verify_callback(array $payload): bool {
    foreach (['webhook_id', 'payment_id', 'reference_id', 'amount', 'currency', 'status', 'timestamp', 'signature'] as $field) {
        if (!isset($payload[$field])) return false;
    }
    $dataString = implode('|', [
        $payload['webhook_id'],
        $payload['payment_id'],
        $payload['reference_id'],
        (string) $payload['amount'],
        $payload['currency'],
        $payload['status'],
        $payload['timestamp'],
        $payload['completed_at'] ?? '',
    ]);
    $apiKeyHash = hash('sha256', API_KEY, true);
    $signingKey = hash_hkdf('sha256', $apiKeyHash, 32, 'webhook-signature', '');
    $expected   = hash_hmac('sha256', $dataString, $signingKey);

    return hash_equals($expected, (string) $payload['signature']);
}

$payload = json_decode(file_get_contents('php://input'), true);

if (!is_array($payload) || !verify_callback($payload)) {
    http_response_code(401);
    exit('Invalid signature');
}

// The same callback can arrive more than once. Act only the first time you see this payment_id:
// if (order_already_processed($payload['payment_id'])) { http_response_code(200); exit('OK'); }

if ($payload['status'] === 'SUCCESS') {
    // mark_order_paid($payload['reference_id'], $payload['amount']);
} else {
    // FAILED, EXPIRED, CANCELLED or REFUNDED
    // mark_order_failed($payload['reference_id'], $payload['status']);
}

http_response_code(200); // any 2xx tells the gateway to stop retrying
echo 'OK';
status.php
<?php
// status.php: ask the gateway for the current status. Use it as a fallback, not instead of the callback.
require __DIR__ . '/gateway.php';

// Works with the gateway's payment_id or with your own reference_id.
$payment = gateway_request('GET', '/payment/status/' . 'ORDER20260910001');

echo $payment['status']; // PENDING, PROCESSING, SUCCESS, FAILED, EXPIRED, CANCELLED or REFUNDED

Signature checker

If the gateway answers INVALID_SIGNATURE, enter what your code used. The checker shows each value of the calculation so you can find the line where yours differs.

Stays in your browser. It is not sent to this site.

Only used to fill the headers below. It is not part of the signature.

Unix time in seconds. Use the value your code sent to reproduce its signature.

The path includes the base prefix and has no query string.

Every character counts: spaces, key order and number format must match the bytes your code sends.

Enter your API key to see the signature worked out line by line.

When something fails

Errors come back as JSON with an error code and a message. Error Handling ↗

CodeWhat to do
INVALID_SIGNATUREHTTP 401The signature, timestamp or merchant ID is wrong. Reproduce the request in the signature checker and compare line by line. Check your server clock first.
TOO_MANY_ATTEMPTSHTTP 429Five bad signatures in a row lock the merchant for 15 minutes. Fix the signature in the checker before you retry.
VALIDATION_ERRORHTTP 400A field is missing or malformed. The most common cause is a reference_id with a hyphen, space or symbol: use letters and numbers only.
DUPLICATE_REFERENCEHTTP 409This reference_id was already used. Send a new one for every payment, even after a failed attempt.
INVALID_AMOUNTHTTP 400The amount is outside the allowed range for the currency, or has more decimals than the currency allows.
MISSING_PROVIDER_CHANNELHTTP 400You chose online banking yourself (payment_method BANK_TRANSFER) without metadata.provider_channel_id. Add the bank's channel, or omit payment_method and let the payment page ask.
HTTP 422HTTP 422callback_url and redirect_url must start with https:// in production.
GATEWAY_ERROR, GATEWAY_TIMEOUT, NO_GATEWAY_AVAILABLEHTTP 5xxTemporary. Retry after a short delay with the same reference_id only if the first request did not create a payment; otherwise check the status first.