Payments
This section explains how to create and manage payment transactions.
Confirm paid orders with a signed webhook first (see Webhooks). Use
GET /payment/statusbelow only for reconciliation when a webhook was delayed or missed.
Payment Flow
- Authentication: Obtain an Access Token (see Authentication).
- Create Transaction: Send payment details to the API (
POST /payment/create). - Complete payment using one of the following paths:
- Hosted redirect (web): Redirect the customer to the
payment_urlfrom the create response, then handlereturn_url/cancel_url(for example WooCommerce or Odoo). - Native Checkout (mobile apps): Open a session with
POST /checkout/sessions(mode: "native"), passsession_idandsession_tokento your app (for example the Flutter Package), and confirm the order with a signed webhook.
- Hosted redirect (web): Redirect the customer to the
- Handle Result: Prefer a signed webhook as the source of truth. Browser or deep-link returns alone are not enough to fulfill safely.
Create a New Transaction
Endpoint: POST /payment/create
Headers:
Authorization: Bearer {access_token}
Content-Type: application/json
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
amount |
string | Yes | Amount to be paid (e.g. "100.00") |
currency |
string | Yes | Currency code (e.g. "LYD") |
return_url |
string | Yes | Redirect URL upon success |
cancel_url |
string | Yes | Redirect URL upon cancellation or failure |
custom |
string | No | Custom identifier for the order (e.g. your system's order ID) |
full_name |
string | No | Customer's full name (to auto-fill the form) |
email |
string | No | Customer's email |
phone |
string | No | Phone number (Libyan format will be auto-formatted) |
payment_gateway |
string | No | Pre-select the payment gateway (alias) |
Simple Request Example
{
"amount": "100.00",
"currency": "LYD",
"return_url": "https://yourstore.com/payment/success",
"cancel_url": "https://yourstore.com/payment/cancel",
"custom": "ORDER_12345"
}
Success Response (200 OK)
{
"message": {
"code": 200,
"success": ["CREATED"]
},
"data": {
"token": "ABC123XYZ789",
"payment_url": "https://mypay.ly/pay/v1/user/payment/preview/ABC123XYZ789"
},
"type": "success"
}
Redirect the customer to payment_url to complete the payment process.
[!NOTE] The
payment_urlpath depends on the API environment you used:
| Environment | payment_url pattern |
|---|---|
| Production | https://mypay.ly/pay/v1/user/payment/preview/{token} |
| Sandbox | https://mypay.ly/pay/sandbox/v1/user/payment/preview/{token} |
When calling the Sandbox API base URL (/pay/sandbox/api/v1/), the returned payment_url includes /sandbox/ in the path.
Access Token and Retry Behavior
| Case | Result |
|---|---|
First payment/create |
Initializes the payment (order status moves to pending) |
Identical retry (same access_token and same request body) |
200 OK with the same token and payment_url (idempotent) |
| Changed payload on the same token | 409 Conflict |
409 Conflict example:
{
"message": {
"code": 409,
"error": ["Payment already initialized for this access token. Request a new authentication token for each order."]
},
"data": {},
"type": "error"
}
Native Checkout Session
Use this after payment/create when the customer pays inside a mobile app (for example Flutter Package) instead of opening payment_url in a browser.
Endpoint: POST /checkout/sessions
Headers:
Authorization: Bearer {access_token}
Content-Type: application/json
Parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
payment_token |
string | Yes | The token returned by POST /payment/create for this same access token |
mode |
string | No | Use "native". If omitted, the API defaults to native mode for this endpoint |
Request Example
{
"payment_token": "ABC123XYZ789",
"mode": "native"
}
Success Response (200 OK)
{
"message": {
"code": 200,
"success": ["SESSION_CREATED"]
},
"data": {
"session_id": "ncs_01EXAMPLESESSIONID",
"session_token": "plain_session_token_once",
"expires_in": 600,
"payment_token": "ABC123XYZ789",
"amount": "100.00",
"currency": "LYD",
"available_gateways": [
{ "alias": "sadad", "name": "SADAD" },
{ "alias": "moamalat", "name": "Banking card" }
],
"payment_url": "https://mypay.ly/pay/v1/user/payment/preview/ABC123XYZ789"
},
"type": "success"
}
Return only session_id and session_token to the mobile app over your own API.
[!IMPORTANT] After the session is open, device calls use the header
X-MyPay-Session-Token(handled by the Flutter package). Do not place the payment access token orclient_id/secret_idon the device.session_tokenis shown once at creation — treat it like a secret for the lifetime of the session (expires_in, typically up to 600 seconds).
[!NOTE] Hosted
payment_urlremains available for web redirect flows. Native Checkout does not replace webhooks — still verifyX-MyPay-Signaturebefore fulfilling. See Flutter Package for app-side setup and Webhooks for confirmation.
The payment_token must belong to the same access_token used on this request; a mismatch returns 400 Bad Request.
Advanced Features
1. Auto-fill Customer Data
You can improve the user experience by sending their data in advance, which will auto-fill the payment form fields.
{
"amount": "100.00",
...
"full_name": "Ahmed Mohamed",
"email": "ahmed@example.com",
"phone": "0912345678"
}
2. Pre-select Payment Gateway
You can redirect the customer directly to a specific payment gateway, bypassing the gateway selection page.
Gateway aliases:
| Alias | Method |
|---|---|
moamalat |
Banking card |
masrafi-pay |
Masrafi Pay |
yasr-pay |
Yasr Pay |
sahary-pay |
Sahara / Sahary Pay |
edfali |
Edfali |
sadad |
SADAD |
mobicash |
Mobicash |
prepaid-card |
Prepaid card redemption (merchant must have prepaid card checkout enabled) |
[!NOTE] If prepaid card checkout is not enabled for your merchant account,
payment/createwithpayment_gateway: "prepaid-card"returns400 Bad Request.
Example:
{
"amount": "100.00",
...
"payment_gateway": "moamalat"
}
[!TIP] When sending all data (Name, Email, Phone, and Gateway), the Auto-Submit feature will be activated, where the customer is redirected to the bank page immediately after a brief summary.
Payment Status (Enquiry)
Use this to reconcile an order when a webhook was delayed or missed. It does not replace signed webhooks as fulfillment authority.
Endpoint: GET /payment/status
Environments:
- Production:
https://mypay.ly/pay/api/v1/payment/status - Sandbox:
https://mypay.ly/pay/sandbox/api/v1/payment/status
Authentication: HTTP Basic Auth with Developer API credentials:
Authorization: Basic base64(client_id:secret_id)
Sandbox URL accepts sandbox credentials only; production URL accepts production credentials only. Do not use the short-lived payment access_token or Native Checkout session token on this endpoint.
Query parameters (at least one required):
| Parameter | Description |
|---|---|
token |
Public payment token from payment/create |
custom |
Your order id; scoped to the authenticated credential |
If both are sent, they must refer to the same payment (400 otherwise). If several payments share the same custom for your credentials, the API returns 409 — narrow the lookup with token.
Example:
GET /pay/api/v1/payment/status?custom=ORDER_12345
Authorization: Basic base64(client_id:secret_id)
Success response (200)
{
"message": {
"code": 200,
"success": ["OK"]
},
"data": {
"status": "success",
"token": "ABC123XYZ789",
"custom": "ORDER_12345",
"amount": "100.00",
"currency": "LYD",
"gateway": "moamalat",
"transaction_id": "QR-ABC123",
"trx_ref": "ABC123XYZ789",
"environment": "PRODUCTION",
"updated_at": "2024-01-12 14:30:00"
},
"type": "success"
}
data.status values: created, pending, success, expired, cancelled, failed.
Error responses
| HTTP | When |
|---|---|
400 |
Missing both token and custom, or they do not match the same payment |
401 |
Invalid credentials, inactive credential, or sandbox/production mismatch |
404 |
No payment found for these credentials |
409 |
Multiple payments match custom — pass token as well |
Abandoned
created/expiredshells may be purged after a short window; a later enquiry can return404. Settled (success) and in-flight (pending) records are kept for reconciliation.
Result Handling (Redirects)
| Outcome | Redirect target | Example |
|---|---|---|
| Success | return_url |
GET https://yourstore.com/payment/success?type=success&token=ABC123XYZ789 |
| Cancellation / failure | cancel_url |
GET https://yourstore.com/payment/cancel?type=cancel&token=ABC123XYZ789 |