🚀 M Pay API Integration Guide

ຄູ່ມືການເຊື່ອມຕໍ່ ແລະ ນຳໃຊ້ M Pay API ສຳລັບຮ້ານຄ້າ

🏁 ເລີ່ມຕົ້ນ (Getting Started)

1. ລົງທະບຽນ Merchant Account

ກ່ອນທີ່ຈະສາມາດໃຊ້ M Pay API ໄດ້ ທ່ານຕ້ອງມີ Merchant Account ກ່ອນ:

📝 ຂັ້ນຕອນການລົງທະບຽນ:
  1. ຕິດຕໍ່ທີມງານ M Pay
  2. ສົ່ງເອກະສານທີ່ຈຳເປັນ (ໃບອະນຸຍາດປະກອບທຸລະກິດ, ບັດປະຊາຊົນ, ອື່ນໆ)
  3. ຮັບ Merchant Code ແລະ API Key
  4. ກຳນົດ Webhook URL

2. ຂໍ້ມູນທີ່ທ່ານຈະໄດ້ຮັບ

Parameter ຄຳອະທິບາຍ ຕົວຢ່າງ
merchant_code ລະຫັດເອກະລັກຂອງຮ້ານຄ້າ MPY000001
api_key API Key ສຳລັບການເຂົ້າເຖິງ sk_live_1234567890abcdef
webhook_url URL ສຳລັບຮັບ notifications https://yoursite.com/webhook

3. ສະພາບແວດລ້ອມ (Environments)

Base URL: https://sandbox-api.mpay.la/v1 Webhook Test: https://webhook.site (ສຳລັບທົດສອບ)

ໃຊ້ສຳລັບການພັດທະນາ ແລະ ທົດສອບລະບົບ

Base URL: https://api.mpay.la/v1

ໃຊ້ສຳລັບລະບົບຈິງທີ່ມີການຊຳລະເງິນຈິງ

🔐 Authentication

M Pay API ໃຊ້ API Key ສຳລັບການຢັ້ງຢືນຕົວຕົນ ທ່ານຕ້ອງສົ່ງ API Key ໃນ HTTP Header:

Authorization: Bearer YOUR_API_KEY Content-Type: application/json
⚠️ ຄຳແນະນຳດ້ານຄວາມປອດໄພ:
  • ບໍ່ເປີດເຜີຍ API Key ໃນ frontend code
  • ເກັບ API Key ໃນ environment variables
  • ໃຊ້ HTTPS ເທົ່ານັ້ນ
  • ໝັ້ນໃຈວ່າ webhook URL ຂອງທ່ານປອດໄພ

📚 API Reference

1. ສ້າງການຊຳລະເງິນ (Create Payment)

POST /payments

ສ້າງລິ້ງການຊຳລະເງິນໃຫມ່ສຳລັບລູກຄ້າ

Request Parameters:

Parameter Type Required ຄຳອະທິບາຍ
amount number ຈຳນວນເງິນ (ຫົວໜ່ວຍ: ກິບ)
currency string ສະກຸນເງິນ (LAK, THB, USD)
reference string ລະຫັດອ້າງອີງຂອງທ່ານ (ເອກະລັກ)
description string ຄຳອະທິບາຍການຊຳລະເງິນ
customer_name string ຊື່ລູກຄ້າ
customer_phone string ເບີໂທລູກຄ້າ
return_url string URL ກັບຄືນຫຼັງຊຳລະເງິນ
expires_at datetime ເວລາໝົດອາຍຸ (default: 1 hour)

Response:

{ "success": true, "data": { "payment_id": "PAY_20250929_ABC123", "payment_url": "https://pay.mpay.la/p/ABC123", "qr_code": "data:image/png;base64,iVBORw0K...", "amount": 50000, "currency": "LAK", "status": "pending", "expires_at": "2025-09-29T16:00:00Z" } }

2. ກວດສອບສະຖານະການຊຳລະເງິນ (Check Payment Status)

GET /payments/{payment_id}

Response:

{ "success": true, "data": { "payment_id": "PAY_20250929_ABC123", "reference": "ORDER_001", "amount": 50000, "currency": "LAK", "status": "completed", "paid_at": "2025-09-29T15:30:00Z", "customer": { "name": "ທ້າວ ວັນນະ", "phone": "020 1234 5678" } } }

3. Payment Status Values

Status ຄຳອະທິບາຍ
pending ລໍຖ້າການຊຳລະເງິນ
completed ຊຳລະເງິນສຳເລັດແລ້ວ
failed ການຊຳລະເງິນລົ້ມແຫລວ
expired ໝົດອາຍຸແລ້ວ
cancelled ຖືກຍົກເລີກ

💻 Code Examples

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"

🧪 Testing & Sandbox

Sandbox Environment

ໃຊ້ Sandbox ສຳລັບການທົດສອບໂດຍບໍ່ມີການຫັກເງິນຈິງ:

✅ Sandbox Features:
  • ການຊຳລະເງິນຈຳລອງ
  • ທົດສອບ webhook notifications
  • API ຄືກັນກັບ production
  • ບໍ່ມີການຫັກເງິນຈິງ

Test Credentials

Base URL: https://sandbox-api.mpay.la/v1 Test API Key: sk_test_1234567890abcdef Test Merchant Code: MPY999999

Webhook Testing

ສຳລັບການທົດສອບ webhooks ແນະນຳໃຫ້ໃຊ້:

🔗 Webhooks

Webhooks ຈະຖືກສົ່ງໄປຫາ URL ທີ່ທ່ານກຳນົດເມື່ອມີການປ່ຽນແປງສະຖານະການຊຳລະເງິນ:

Webhook Events

Event ຄຳອະທິບາຍ
payment.completed ການຊຳລະເງິນສຳເລັດ
payment.failed ການຊຳລະເງິນລົ້ມແຫລວ
payment.expired ການຊຳລະເງິນໝົດອາຍຸ

Webhook Payload Example

{ "event": "payment.completed", "data": { "payment_id": "PAY_20250929_ABC123", "reference": "ORDER_001", "amount": 50000, "currency": "LAK", "status": "completed", "paid_at": "2025-09-29T15:30:00Z", "customer": { "name": "ທ້າວ ວັນນະ", "phone": "020 1234 5678" } }, "timestamp": "2025-09-29T15:30:05Z" }

Webhook Verification

ເພື່ອຄວາມປອດໄພ ທ່ານຄວນຢັ້ງຢືນວ່າ webhook ມາຈາກ M Pay ຈິງໆ:

// PHP Webhook Verification $payload = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_MPAY_SIGNATURE'] ?? ''; $expectedSignature = hash_hmac('sha256', $payload, $your_webhook_secret); if (hash_equals($expectedSignature, $signature)) { // Webhook is valid $data = json_decode($payload, true); switch ($data['event']) { case 'payment.completed': // Handle successful payment break; case 'payment.failed': // Handle failed payment break; } } else { // Invalid webhook http_response_code(401); }

📦 SDKs & Libraries

Official SDKs

Community Libraries

Quick Setup with Composer (PHP)

composer require mpay/php-sdk use MPay\Client; $mpay = new Client('your_api_key', $sandbox = true); $payment = $mpay->payments->create([ 'amount' => 50000, 'currency' => 'LAK', 'reference' => 'ORDER_001' ]);

🆘 ການຊ່ວຍເຫຼືອ & Support

ຕິດຕໍ່ທີມງານ

  • 📧 Email: [email protected]
  • 📱 WhatsApp: +856 20 1234 5678
  • 💬 Telegram: @mpay_support
  • ⏰ ເວລາເຮັດວຽກ: ຈັນ-ສຸກ 8:00-17:00

ຄຳຖາມທີ່ພົບເລື້ອຍ

Q: ຄ່າທຳນຽມໃນການໃຊ້ງານເທົ່າໃດ?
A: ຄ່າທຳນຽມຂື້ນກັບປະເພດທຸລະກິດ ກະລຸນາຕິດຕໍ່ທີມງານເພື່ອຂໍໃບເສນີລາຄາ
Q: ໃຊ້ເວລາເທົ່າໃດໃນການ setup?
A: ປົກກະຕິ 1-3 ວັນເຮັດວຽກຫຼັງຈາກສົ່ງເອກະສານຄົບຖ້ວນ
Q: ຮອງຮັບສະກຸນເງິນໃດແດ່?
A: ປັດຈຸບັນຮອງຮັບ LAK, THB, ແລະ USD