qpay
QPay V2 API SDK for Rust. Async client with automatic token management, invoice creation, payment operations, and ebarimt (electronic tax receipt) support.
Installation
Add to your Cargo.toml:
[]
= "1.0.0"
= { = "1", = ["full"] }
Or using cargo add:
Quick Start
use ;
async
Configuration
From environment variables
let config = from_env?;
Required environment variables:
| Variable | Description |
|---|---|
QPAY_BASE_URL |
QPay API base URL (e.g., https://merchant.qpay.mn) |
QPAY_USERNAME |
QPay merchant username |
QPAY_PASSWORD |
QPay merchant password |
QPAY_INVOICE_CODE |
Default invoice code |
QPAY_CALLBACK_URL |
Payment callback URL |
Manual configuration
let config = new;
Custom HTTP client
use Duration;
let http = builder
.timeout
.build
.unwrap;
let client = with_http_client;
Usage
Authentication
Token management is fully automatic. The client obtains and refreshes tokens as needed before each request. You can also manage tokens manually:
// Get a new token
let token = client.get_token.await?;
println!;
// Refresh the current token
let new_token = client.refresh_token.await?;
Create an invoice (simple)
use CreateSimpleInvoiceRequest;
let req = CreateSimpleInvoiceRequest ;
let invoice = client.create_simple_invoice.await?;
println!;
println!;
// Show deeplinks for bank apps
for url in &invoice.urls
Create an invoice (full options)
use *;
let req = CreateInvoiceRequest ;
let invoice = client.create_invoice.await?;
Create an invoice with ebarimt (tax)
use *;
let req = CreateEbarimtInvoiceRequest ;
let invoice = client.create_ebarimt_invoice.await?;
Cancel an invoice
client.cancel_invoice.await?;
Check payment status
use ;
let req = PaymentCheckRequest ;
let result = client.check_payment.await?;
println!;
if let Some = result.paid_amount
for row in &result.rows
Get payment details
let payment = client.get_payment.await?;
println!;
println!;
println!;
println!;
List payments
use ;
let req = PaymentListRequest ;
let result = client.list_payments.await?;
println!;
for item in &result.rows
Cancel a payment
use PaymentCancelRequest;
let req = PaymentCancelRequest ;
client.cancel_payment.await?;
Refund a payment
use PaymentRefundRequest;
let req = PaymentRefundRequest ;
client.refund_payment.await?;
Create ebarimt (electronic tax receipt)
use CreateEbarimtRequest;
let req = CreateEbarimtRequest ;
let ebarimt = client.create_ebarimt.await?;
println!;
println!;
println!;
println!;
Cancel ebarimt
let ebarimt = client.cancel_ebarimt.await?;
println!;
Error Handling
All methods return Result<T, QPayError>. Error variants:
| Variant | Description |
|---|---|
QPayError::Api |
QPay API returned an error response (status code, error code, message) |
QPayError::Http |
Network/HTTP error from reqwest |
QPayError::Json |
JSON serialization/deserialization error |
QPayError::Config |
Configuration error (missing environment variable, etc.) |
QPayError::Token |
Token acquisition failed |
Checking for API errors
use ;
match client.create_simple_invoice.await
Error code constants
The qpay::error module exports all QPay error code constants for pattern matching:
use *;
// Invoice errors
ERR_INVOICE_NOT_FOUND // "INVOICE_NOTFOUND"
ERR_INVOICE_PAID // "INVOICE_PAID"
ERR_INVOICE_ALREADY_CANCELED // "INVOICE_ALREADY_CANCELED"
ERR_INVOICE_CODE_INVALID // "INVOICE_CODE_INVALID"
ERR_INVOICE_LINE_REQUIRED // "INVOICE_LINE_REQUIRED"
// Payment errors
ERR_PAYMENT_NOT_FOUND // "PAYMENT_NOTFOUND"
ERR_PAYMENT_ALREADY_CANCELED // "PAYMENT_ALREADY_CANCELED"
ERR_PAYMENT_NOT_PAID // "PAYMENT_NOT_PAID"
// Auth errors
ERR_AUTHENTICATION_FAILED // "AUTHENTICATION_FAILED"
ERR_PERMISSION_DENIED // "PERMISSION_DENIED"
ERR_NO_CREDENTIALS // "NO_CREDENDIALS"
// Merchant errors
ERR_MERCHANT_NOT_FOUND // "MERCHANT_NOTFOUND"
ERR_MERCHANT_INACTIVE // "MERCHANT_INACTIVE"
// Ebarimt errors
ERR_EBARIMT_NOT_REGISTERED // "EBARIMT_NOT_REGISTERED"
ERR_EBARIMT_CANCEL_NOT_SUPPORTED // "EBARIMT_CANCEL_NOTSUPPERDED"
ERR_EBARIMT_QR_CODE_INVALID // "EBARIMT_QR_CODE_INVALID"
// Amount errors
ERR_INVALID_AMOUNT // "INVALID_AMOUNT"
ERR_MIN_AMOUNT_ERR // "MIN_AMOUNT_ERR"
ERR_MAX_AMOUNT_ERR // "MAX_AMOUNT_ERR"
API Reference
QPayConfig
| Method | Description |
|---|---|
QPayConfig::new(base_url, username, password, invoice_code, callback_url) |
Create config with explicit values |
QPayConfig::from_env() |
Load config from environment variables |
QPayClient
| Method | Description |
|---|---|
QPayClient::new(config) |
Create client with default HTTP settings |
QPayClient::with_http_client(config, http) |
Create client with custom reqwest::Client |
Auth
| Method | Description |
|---|---|
client.get_token() |
Authenticate and get token pair |
client.refresh_token() |
Refresh the current access token |
Invoice
| Method | Description |
|---|---|
client.create_invoice(&req) |
Create invoice with full options |
client.create_simple_invoice(&req) |
Create invoice with minimal fields |
client.create_ebarimt_invoice(&req) |
Create invoice with tax information |
client.cancel_invoice(id) |
Cancel an invoice |
Payment
| Method | Description |
|---|---|
client.get_payment(id) |
Get payment details |
client.check_payment(&req) |
Check payment status for an invoice |
client.list_payments(&req) |
List payments with filters |
client.cancel_payment(id, &req) |
Cancel a payment (card only) |
client.refund_payment(id, &req) |
Refund a payment (card only) |
Ebarimt
| Method | Description |
|---|---|
client.create_ebarimt(&req) |
Create electronic tax receipt |
client.cancel_ebarimt(payment_id) |
Cancel electronic tax receipt |
License
MIT