PHP Example:
<?php
class MPayClient {
private $apiKey;
private $baseUrl;
public function __construct($apiKey, $sandbox = false) {
$this->apiKey = $apiKey;
$this->baseUrl = $sandbox
? 'https://sandbox-api.mpay.la/v1'
: 'https://api.mpay.la/v1';
}
public function createPayment($data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->baseUrl . '/payments');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->apiKey,
'Content-Type: application/json'
]);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return json_decode($response, true);
}
public function getPayment($paymentId) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $this->baseUrl . '/payments/' . $paymentId);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Authorization: Bearer ' . $this->apiKey
]);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response, true);
}
}
// ການນຳໃຊ້
$mpay = new MPayClient('your_api_key', true); // sandbox = true
$payment = $mpay->createPayment([
'amount' => 50000,
'currency' => 'LAK',
'reference' => 'ORDER_' . time(),
'description' => 'ຊື້ສິນຄ້າຈາກຮ້ານ ABC',
'customer_name' => 'ທ້າວ ວັນນະ',
'return_url' => 'https://yoursite.com/success'
]);
if ($payment['success']) {
echo "Payment URL: " . $payment['data']['payment_url'];
echo "QR Code: " . $payment['data']['qr_code'];
}
?>
JavaScript (Node.js) Example:
const axios = require('axios');
class MPayClient {
constructor(apiKey, sandbox = false) {
this.apiKey = apiKey;
this.baseUrl = sandbox
? 'https://sandbox-api.mpay.la/v1'
: 'https://api.mpay.la/v1';
}
async createPayment(data) {
try {
const response = await axios.post(`${this.baseUrl}/payments`, data, {
headers: {
'Authorization': `Bearer ${this.apiKey}`,
'Content-Type': 'application/json'
}
});
return response.data;
} catch (error) {
throw new Error(error.response?.data?.message || error.message);
}
}
async getPayment(paymentId) {
try {
const response = await axios.get(`${this.baseUrl}/payments/${paymentId}`, {
headers: {
'Authorization': `Bearer ${this.apiKey}`
}
});
return response.data;
} catch (error) {
throw new Error(error.response?.data?.message || error.message);
}
}
}
// ການນຳໃຊ້
const mpay = new MPayClient('your_api_key', true);
async function createPayment() {
try {
const payment = await mpay.createPayment({
amount: 50000,
currency: 'LAK',
reference: `ORDER_${Date.now()}`,
description: 'ຊື້ສິນຄ້າຈາກຮ້ານ ABC',
customer_name: 'ທ້າວ ວັນນະ',
return_url: 'https://yoursite.com/success'
});
console.log('Payment URL:', payment.data.payment_url);
console.log('QR Code:', payment.data.qr_code);
} catch (error) {
console.error('Error:', error.message);
}
}
createPayment();
Python Example:
import requests
import json
from datetime import datetime
class MPayClient:
def __init__(self, api_key, sandbox=False):
self.api_key = api_key
self.base_url = 'https://sandbox-api.mpay.la/v1' if sandbox else 'https://api.mpay.la/v1'
def _get_headers(self):
return {
'Authorization': f'Bearer {self.api_key}',
'Content-Type': 'application/json'
}
def create_payment(self, data):
response = requests.post(
f'{self.base_url}/payments',
json=data,
headers=self._get_headers()
)
return response.json()
def get_payment(self, payment_id):
response = requests.get(
f'{self.base_url}/payments/{payment_id}',
headers=self._get_headers()
)
return response.json()
# ການນຳໃຊ້
mpay = MPayClient('your_api_key', sandbox=True)
payment_data = {
'amount': 50000,
'currency': 'LAK',
'reference': f'ORDER_{int(datetime.now().timestamp())}',
'description': 'ຊື້ສິນຄ້າຈາກຮ້ານ ABC',
'customer_name': 'ທ້າວ ວັນນະ',
'return_url': 'https://yoursite.com/success'
}
payment = mpay.create_payment(payment_data)
if payment['success']:
print(f"Payment URL: {payment['data']['payment_url']}")
print(f"QR Code: {payment['data']['qr_code']}")
cURL Example:
# ສ້າງການຊຳລະເງິນ
curl -X POST https://sandbox-api.mpay.la/v1/payments \
-H "Authorization: Bearer your_api_key" \
-H "Content-Type: application/json" \
-d '{
"amount": 50000,
"currency": "LAK",
"reference": "ORDER_001",
"description": "ຊື້ສິນຄ້າຈາກຮ້ານ ABC",
"customer_name": "ທ້າວ ວັນນະ",
"return_url": "https://yoursite.com/success"
}'
# ກວດສອບສະຖານະການຊຳລະເງິນ
curl -X GET https://sandbox-api.mpay.la/v1/payments/PAY_20250929_ABC123 \
-H "Authorization: Bearer your_api_key"