Skip to main content

Payme Integratsiyasi

Payme O'zbekistondagi eng mashhur to'lov tizimlaridan biri. Bu qo'llanma Payme ni PayTechUZ bilan qanday integratsiya qilishni ko'rsatadi.

O'rnatish

pip install paytechuz

Asosiy foydalanish

Shlyuz yaratish

import os
from paytechuz.gateways.payme import PaymeGateway

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

To'lov yaratish

payment = gateway.create_payment(
id="12345", # Hisob-faktura ID
amount=50000, # 500.00 UZS (tiyinda)
return_url="https://example.com/return",
account_field_name="id" # Majburiy: hisob qidiruvi uchun maydon nomi
)

print(f"Payment URL: {payment}")
# Foydalanuvchini to'lov URL ga yo'naltiring

To'lov holatini tekshirish

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

Webhook

Webhook'lar framework integratsiyasi orqali qayta ishlanadi — Django uchun BasePaymeWebhookView, FastAPI uchun PaymeWebhookHandler. JSON-RPC metodlari, Basic auth tekshiruvi va tranzaksiya holati siz uchun boshqariladi; siz faqat hodisa metodlarini override qilasiz.

Quyidagi bo'limlarga qarang.

Django Integratsiyasi

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,
}
}

To'lov Yaratish

Settings dan foydalanib to'lov yaratish:

from django.conf import settings
from paytechuz.gateways.payme import PaymeGateway

# Settings dan gateway yaratish
gateway = PaymeGateway(
payme_id=settings.PAYTECHUZ['PAYME']['PAYME_ID'],
payme_key=settings.PAYTECHUZ['PAYME']['PAYME_KEY'],
is_test_mode=settings.PAYTECHUZ['PAYME']['IS_TEST_MODE']
)

# To'lov yaratish
payment_url = gateway.create_payment(
id=order.id,
amount=order.amount,
return_url='https://example.com/payment/callback',
account_field_name=settings.PAYTECHUZ['PAYME']['ACCOUNT_FIELD'] # Settings dan
)

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()

def get_check_data(self, params, account): # optional
"""
Return additional data for CheckPerformTransaction (fiscal receipt)

You can override this method to return additional data for fiscal receipt
"""
return {
"additional": {"first_name": account.user.first_name}, # optional
"detail": { # optional
"receipt_type": 0,
"shipping": {"title": "Yetkazib berish", "price": 10000},
"items": [
{
"discount": 0,
"title": account.product_name,
"price": account.amount,
"count": 1,
"code": "00001",
"units": 1,
"vat_percent": 0,
"package_code": "123456"
}
]
}
}

URLs.py

from django.urls import path
from .views import PaymeWebhookView

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

FastAPI Integratsiyasi

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

app = FastAPI()

# Shlyuz yaratish
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",
account_field_name="id" # Majburiy: hisob qidiruvi uchun maydon nomi
)
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)

Konfiguratsiya

Test rejimi

Test uchun quyidagi ma'lumotlardan foydalaning:

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

Production rejimi

  1. Payme Business da ro'yxatdan o'ting
  2. Production ma'lumotlaringizni oling
  3. is_test_mode=False qilib o'rnating

Xatolarni qayta ishlash

from paytechuz.core.exceptions import PaymentException

try:
payment = gateway.create_payment(
id="12345",
amount=50000,
return_url="https://example.com/return",
account_field_name="id" # Majburiy: hisob qidiruvi uchun maydon nomi
)
except PaymentException as e:
print(f"Payme xatosi: {e.code} - {e.message}")

Qo'shimcha ma'lumot