Skip to content

rmpay/rmpay-python

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

RMPay Python SDK

Official Python integration examples for RMPay — Vietnam's real-time payment gateway.

Python License

What is RMPay?

RMPay (Real-time Money Pay) is a payment gateway built for Vietnam's banking ecosystem. It enables merchants to accept payments via:

  • QR Code (VietQR compatible) — customers scan and pay in seconds
  • Bank Transfer — supports 57 Vietnamese banks including VCB, TCB, MB, BIDV, ACB, VPBank
  • Real-time settlement — funds credited to merchant accounts in under 5 seconds

👉 Website: https://rmpay.co
👉 Supported Banks: https://rmpay.co/banks/
👉 How It Works: https://rmpay.co/how-it-works/

Requirements

  • Python 3.7+
  • requests library
pip install requests

Quick Start

1. Create a Payment Order

import requests
import hashlib
import hmac
import json
from datetime import datetime

MERCHANT_ID = "your_merchant_id"
API_KEY = "your_api_key"
API_BASE = "https://api.rmpay.co/v1"

def create_order(order_id: str, amount: int, callback_url: str) -> dict:
    """
    Create a payment order on RMPay.
    
    Args:
        order_id: Your unique order identifier
        amount: Amount in VND (e.g. 500000 for 500,000 VND)
        callback_url: Webhook URL for payment notifications
    
    Returns:
        dict: Order details including cashier_url and qr_code
    """
    payload = {
        "merchant_id": MERCHANT_ID,
        "order_id": order_id,
        "amount": amount,
        "currency": "VND",
        "callback_url": callback_url,
        "created_at": datetime.utcnow().isoformat()
    }
    
    headers = {
        "Content-Type": "application/json",
        "X-API-Key": API_KEY
    }
    
    response = requests.post(
        f"{API_BASE}/order/create",
        json=payload,
        headers=headers,
        timeout=10
    )
    response.raise_for_status()
    return response.json()


def get_order_status(order_id: str) -> dict:
    """Query the status of an existing order."""
    headers = {"X-API-Key": API_KEY}
    response = requests.get(
        f"{API_BASE}/order/{order_id}",
        headers=headers,
        timeout=10
    )
    response.raise_for_status()
    return response.json()


# Example usage
if __name__ == "__main__":
    order = create_order(
        order_id="ORDER_20250613_001",
        amount=500000,  # 500,000 VND
        callback_url="https://yoursite.com/webhook/rmpay"
    )
    
    print(f"Order created: {order['order_id']}")
    print(f"Cashier URL: {order['cashier_url']}")
    print(f"QR Code: {order['qr_code']}")

2. Handle Webhook Callbacks

from flask import Flask, request, jsonify
import hmac
import hashlib

app = Flask(__name__)
WEBHOOK_SECRET = "your_webhook_secret"

def verify_signature(payload: bytes, signature: str) -> bool:
    """Verify HMAC signature from RMPay webhook."""
    expected = hmac.new(
        WEBHOOK_SECRET.encode(),
        payload,
        hashlib.sha256
    ).hexdigest()
    return hmac.compare_digest(expected, signature)

@app.route("/webhook/rmpay", methods=["POST"])
def rmpay_webhook():
    signature = request.headers.get("X-RMPay-Signature", "")
    
    if not verify_signature(request.data, signature):
        return jsonify({"error": "Invalid signature"}), 401
    
    event = request.json
    
    if event["event"] == "payment.success":
        order_id = event["order_id"]
        amount = event["amount"]
        paid_at = event["paid_at"]
        
        # Mark order as paid in your database
        print(f"✅ Payment received: {order_id}{amount:,} VND at {paid_at}")
        
        # TODO: Deliver goods/service to customer
    
    return jsonify({"status": "ok"})

if __name__ == "__main__":
    app.run(port=5000)

3. Django Integration Example

# views.py
from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_POST
import json
import hmac
import hashlib

WEBHOOK_SECRET = "your_webhook_secret"

@csrf_exempt
@require_POST
def rmpay_webhook(request):
    signature = request.META.get("HTTP_X_RMPAY_SIGNATURE", "")
    
    expected = hmac.new(
        WEBHOOK_SECRET.encode(),
        request.body,
        hashlib.sha256
    ).hexdigest()
    
    if not hmac.compare_digest(expected, signature):
        return JsonResponse({"error": "Unauthorized"}, status=401)
    
    data = json.loads(request.body)
    
    if data.get("event") == "payment.success":
        # Process successful payment
        order_id = data["order_id"]
        # Update your Order model
        Order.objects.filter(order_id=order_id).update(status="paid")
    
    return JsonResponse({"status": "ok"})

Webhook Payload Reference

{
  "event": "payment.success",
  "order_id": "ORDER_20250613_001",
  "merchant_id": "your_merchant_id",
  "amount": 500000,
  "currency": "VND",
  "bank": "VCB",
  "reference": "DEMO001",
  "status": "paid",
  "paid_at": "2025-06-13T09:53:02+07:00"
}

Supported Banks

RMPay supports 57 Vietnamese banks including:

Bank Code Type
Vietcombank VCB State-owned
Techcombank TCB Commercial
MB Bank MB Commercial
BIDV BIDV State-owned
VPBank VPB Commercial
ACB ACB Commercial
HSBC Vietnam HSBC Foreign
Citibank Vietnam CITIBANK Foreign
CAKE Digital Bank CAKE Digital
Timo TIMO Digital

View all 57 supported banks →

Getting Started

  1. Contact our business team on Telegram to get your API credentials:

  2. Receive your merchant_id and api_key

  3. Test in sandbox environment

  4. Go live and start accepting payments

Links

License

MIT License — see LICENSE for details.

About

Python integration examples for RMPay — Vietnam real-time payment gateway. 57 banks, QR payments, instant settlement. https://rmpay.co

Resources

License

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages