Skip to main content

Payme Integration

Payme is one of the most popular payment systems in Uzbekistan. This guide shows how to integrate Payme with PayTechUZ.

Installation

pip install paytechuz

Basic Usage

Create Gateway

from paytechuz.gateways.payme import PaymeGateway

gateway = PaymeGateway(
payme_id="your_payme_id",
payme_key="your_payme_key",
is_test_mode=True # True for test, False for production
)

Create Payment

payment = gateway.create_payment(
id="12345", # order ID
amount=50000, # 500.00 UZS (in tiyin)
return_url="https://example.com/return"
)

print(f"Payment URL: {payment}")
# Redirect user to payment URL

Check Payment Status

status = gateway.check_payment("transaction_id")
print(f"Status: {status['status']}")

Webhook

from paytechuz.gateways.payme.webhook import PaymeWebhookHandler

webhook_handler = PaymeWebhookHandler(
payme_id="your_payme_id",
payme_key="your_payme_key"
)

def process_webhook(request_data):
response = webhook_handler.handle_webhook(request_data)

if response['result']:
# Payment successful
print("Payment successful!")

return response

Django Integration

Settings.py

INSTALLED_APPS = [
# ...
'paytechuz.integrations.django',
]

PAYTECHUZ = {
'PAYME': {
'PAYME_ID': 'your_payme_id',
'PAYME_KEY': 'your_payme_key',
'ACCOUNT_MODEL': 'shop.models.Order',
'ACCOUNT_FIELD': 'id',
'AMOUNT_FIELD': 'amount',
'IS_TEST_MODE': True,
}
}

Views.py

from paytechuz.integrations.django.views import BasePaymeWebhookView
from .models import Order

class PaymeWebhookView(BasePaymeWebhookView):
def successfully_payment(self, params, transaction):
order = Order.objects.get(id=transaction.account_id)
order.status = 'paid'
order.save()

def cancelled_payment(self, params, transaction):
order = Order.objects.get(id=transaction.account_id)
order.status = 'cancelled'
order.save()

URLs.py

from django.urls import path
from .views import PaymeWebhookView

urlpatterns = [
path('webhooks/payme/', PaymeWebhookView.as_view(), name='payme_webhook'),
]

FastAPI Integration

from fastapi import FastAPI, Request
from paytechuz.gateways.payme import PaymeGateway
from paytechuz.integrations.fastapi import PaymeWebhookHandler
import os

app = FastAPI()

# Create gateway
payme = PaymeGateway(
payme_id=os.getenv('PAYME_ID'),
payme_key=os.getenv('PAYME_KEY'),
is_test_mode=True
)

@app.post("/payment/create")
async def create_payment():
payment_url = payme.create_payment(
id="12345",
amount=50000,
return_url="https://example.com/return"
)
return {"payment_url": payment_url}

@app.post("/webhooks/payme")
async def webhook(request: Request):
handler = PaymeWebhookHandler(
payme_id=os.getenv('PAYME_ID'),
payme_key=os.getenv('PAYME_KEY'),
account_model=Order,
account_field='id',
amount_field='amount'
)
return await handler.handle_webhook(request)

Configuration

Test Mode

For testing, use these credentials:

  • Payme ID: test_payme_id
  • Payme Key: test_payme_key
  • Test Card: 8600 0691 9540 6311

Production Mode

  1. Register at Payme Business
  2. Get your production credentials
  3. Set is_test_mode=False

Error Handling

from paytechuz.exceptions import PaymeException

try:
payment = gateway.create_payment(
id="12345",
amount=50000,
return_url="https://example.com/return"
)
except PaymeException as e:
print(f"Payme error: {e}")

Additional Information