Skip to Content
SDKscURL

cURL Examples

A collection of ready-to-use cURL examples for the QPay V2 API. Each script demonstrates a specific API endpoint with proper authentication, request formatting, and documented response structures.

GitHub

Prerequisites

  • curl — command-line HTTP client (pre-installed on macOS and most Linux distributions)
  • jq — JSON processor, used by the auth helper to parse tokens
# macOS brew install jq # Ubuntu / Debian apt-get install jq

Installation

git clone https://github.com/qpay-sdk/qpay-curl.git cd qpay-curl

Configuration

  1. Copy the example environment file and fill in your credentials:
cp .env.example .env
  1. Edit .env with your actual values:
QPAY_BASE_URL=https://merchant.qpay.mn QPAY_USERNAME=your_username QPAY_PASSWORD=your_password QPAY_INVOICE_CODE=your_invoice_code QPAY_CALLBACK_URL=https://your-domain.com/callback
VariableDescription
QPAY_BASE_URLQPay API base URL
QPAY_USERNAMEQPay merchant username
QPAY_PASSWORDQPay merchant password
QPAY_INVOICE_CODEDefault invoice code
QPAY_CALLBACK_URLURL that QPay calls after payment
  1. Make all scripts executable:
chmod +x helpers/*.sh examples/**/*.sh

Quick Start

# Authenticate and export tokens source helpers/auth.sh # Verify the token is set echo "$ACCESS_TOKEN" # Run any example ./examples/invoice/create-simple.sh

Get Token

The auth helper authenticates using Basic Auth and exports ACCESS_TOKEN and REFRESH_TOKEN as environment variables.

source helpers/auth.sh

Or manually with cURL:

curl -s -X POST "${QPAY_BASE_URL}/v2/auth/token" \ -u "${QPAY_USERNAME}:${QPAY_PASSWORD}" | jq .

Response:

{ "token_type": "Bearer", "refresh_expires_in": 3600, "refresh_token": "eyJ...", "access_token": "eyJ...", "expires_in": 600 }

Refresh Token

curl -s -X POST "${QPAY_BASE_URL}/v2/auth/refresh" \ -H "Authorization: Bearer ${REFRESH_TOKEN}" | jq .

Create Invoice

Simple Invoice

source helpers/auth.sh curl -s -X POST "${QPAY_BASE_URL}/v2/invoice" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "invoice_code": "'"${QPAY_INVOICE_CODE}"'", "sender_invoice_no": "ORDER-001", "invoice_receiver_code": "terminal", "invoice_description": "Payment for Order #001", "amount": 10000, "callback_url": "'"${QPAY_CALLBACK_URL}"'" }' | jq .

Response:

{ "invoice_id": "abc123...", "qr_text": "...", "qr_image": "base64_encoded_qr_image...", "qPay_shortUrl": "https://qpay.mn/shorturl", "urls": [ { "name": "Khan Bank", "description": "Khan Bank app", "logo": "https://...", "link": "https://..." } ] }

Cancel Invoice

INVOICE_ID="your_invoice_id" curl -s -X DELETE "${QPAY_BASE_URL}/v2/invoice/${INVOICE_ID}" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" | jq .

Check Payment

INVOICE_ID="your_invoice_id" curl -s -X POST "${QPAY_BASE_URL}/v2/payment/check" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "object_type": "INVOICE", "object_id": "'"${INVOICE_ID}"'", "offset": { "page_number": 1, "page_limit": 10 } }' | jq .

Response:

{ "count": 1, "paid_amount": 1000.00, "rows": [ { "payment_id": "...", "payment_status": "PAID", "payment_amount": "1000.00", "payment_currency": "MNT", "payment_wallet": "qpay" } ] }

List Payments

INVOICE_ID="your_invoice_id" curl -s -X POST "${QPAY_BASE_URL}/v2/payment/list" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "object_type": "INVOICE", "object_id": "'"${INVOICE_ID}"'", "start_date": "2024-01-01", "end_date": "2024-12-31", "offset": { "page_number": 1, "page_limit": 20 } }' | jq .

Get Payment Details

PAYMENT_ID="your_payment_id" curl -s -X GET "${QPAY_BASE_URL}/v2/payment/${PAYMENT_ID}" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" | jq .

Cancel Payment (Card Only)

PAYMENT_ID="your_payment_id" curl -s -X DELETE "${QPAY_BASE_URL}/v2/payment/cancel/${PAYMENT_ID}" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "callback_url": "'"${QPAY_CALLBACK_URL}"'", "note": "Customer requested cancellation" }' | jq .

Refund Payment (Card Only)

PAYMENT_ID="your_payment_id" curl -s -X DELETE "${QPAY_BASE_URL}/v2/payment/refund/${PAYMENT_ID}" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "callback_url": "'"${QPAY_CALLBACK_URL}"'", "note": "Refund requested" }' | jq .

Webhook Handling

QPay sends a POST request to your callback_url when a payment completes. You can verify callbacks by checking the payment status:

# In your callback handler, extract payment_id from the POST body, then verify: curl -s -X POST "${QPAY_BASE_URL}/v2/payment/check" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "object_type": "INVOICE", "object_id": "'"${PAYMENT_ID}"'" }' | jq .

If count > 0, the payment is confirmed.

Create Ebarimt

PAYMENT_ID="your_payment_id" curl -s -X POST "${QPAY_BASE_URL}/v2/ebarimt_v3/create" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" \ -H "Content-Type: application/json" \ -d '{ "payment_id": "'"${PAYMENT_ID}"'", "ebarimt_receiver_type": "83", "district_code": "23" }' | jq .

Response:

{ "id": "...", "ebarimt_receiver_type": "83", "amount": "1000.00", "vat_amount": "100.00", "ebarimt_qr_data": "...", "ebarimt_lottery": "...", "barimt_status": "CREATED" }

Cancel Ebarimt

PAYMENT_ID="your_payment_id" curl -s -X DELETE "${QPAY_BASE_URL}/v2/ebarimt_v3/${PAYMENT_ID}" \ -H "Authorization: Bearer ${ACCESS_TOKEN}" | jq .

Error Handling

All QPay API errors return JSON with an error code and message:

{ "error": "INVOICE_NOTFOUND", "message": "Invoice not found" }

Common error codes:

Error CodeHTTP StatusDescription
AUTHENTICATION_FAILED401Invalid credentials
PERMISSION_DENIED403No permission for this operation
INVOICE_NOTFOUND404Invoice does not exist
INVOICE_PAID400Invoice has already been paid
INVOICE_ALREADY_CANCELED400Invoice was already canceled
INVOICE_CODE_INVALID400Invalid invoice code
INVALID_AMOUNT400Amount is out of valid range
PAYMENT_NOTFOUND404Payment does not exist
PAYMENT_ALREADY_CANCELED400Payment was already canceled

Available Scripts

ScriptMethodPathDescription
helpers/auth.shAuth helper (sources .env, exports tokens)
examples/auth/get-token.shPOST/v2/auth/tokenGet access token
examples/auth/refresh-token.shPOST/v2/auth/refreshRefresh access token
examples/invoice/create-invoice.shPOST/v2/invoiceCreate invoice (full)
examples/invoice/create-simple.shPOST/v2/invoiceCreate invoice (simple)
examples/invoice/cancel-invoice.shDELETE/v2/invoice/{id}Cancel invoice
examples/payment/get-payment.shGET/v2/payment/{id}Get payment details
examples/payment/check-payment.shPOST/v2/payment/checkCheck payment status
examples/payment/list-payments.shPOST/v2/payment/listList payments
examples/payment/cancel-payment.shDELETE/v2/payment/cancel/{id}Cancel payment
examples/payment/refund-payment.shDELETE/v2/payment/refund/{id}Refund payment
examples/ebarimt/create-ebarimt.shPOST/v2/ebarimt_v3/createCreate ebarimt
examples/ebarimt/cancel-ebarimt.shDELETE/v2/ebarimt_v3/{id}Cancel ebarimt

API Reference

EndpointMethodDescription
/v2/auth/tokenPOSTAuthenticate (Basic Auth) and get tokens
/v2/auth/refreshPOSTRefresh access token (Bearer refresh_token)
/v2/invoicePOSTCreate an invoice
/v2/invoice/{id}DELETECancel an invoice
/v2/payment/{id}GETGet payment details
/v2/payment/checkPOSTCheck payment status
/v2/payment/listPOSTList payments
/v2/payment/cancel/{id}DELETECancel a card payment
/v2/payment/refund/{id}DELETERefund a card payment
/v2/ebarimt_v3/createPOSTCreate an ebarimt receipt
/v2/ebarimt_v3/{id}DELETECancel an ebarimt receipt
Last updated on