Click Integratsiyasi
Click O'zbekistonda keng qo'llaniladigan to'lov tizimi. Bu qo'llanma Click ni PayTechUZ bilan qanday integratsiya qilishni ko'rsatadi.
O'rnatish
pip install paytechuz
Asosiy foydalanish
Shlyuz yaratish
import os
from paytechuz.gateways.click import ClickGateway
gateway = ClickGateway(
service_id="your_service_id",
merchant_id="your_merchant_id",
merchant_user_id="your_merchant_user_id",
secret_key="your_secret_key",
is_test_mode=True, # True test uchun, False production uchun
)
To'lov yaratish
payment = gateway.create_payment(
id="12345", # Hisob-faktura ID
amount=500.00, # UZS da summa
return_url="https://example.com/return"
)
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
BaseClickWebhookView, FastAPI uchun ClickWebhookHandler. Imzo tekshiruvi,
tranzaksiyani saqlash va javob formati siz uchun bajariladi; siz faqat
hodisa metodlarini override qilasiz.
Quyidagi bo'limlarga qarang.
Django Integratsiyasi
Settings.py
INSTALLED_APPS = [
# ...
'paytechuz.integrations.django',
]
PAYTECHUZ = {
'CLICK': {
'SERVICE_ID': 'your_service_id',
'MERCHANT_ID': 'your_merchant_id',
'MERCHANT_USER_ID': 'your_merchant_user_id',
'SECRET_KEY': 'your_secret_key',
'ACCOUNT_MODEL': 'shop.models.Order',
'ACCOUNT_FIELD': 'id',
'AMOUNT_FIELD': 'amount',
'IS_TEST_MODE': True,
}
}
Views.py
from paytechuz.integrations.django.views import BaseClickWebhookView
from .models import Order
class ClickWebhookView(BaseClickWebhookView):
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 ClickWebhookView
urlpatterns = [
path('webhooks/click/', ClickWebhookView.as_view(), name='click_webhook'),
]
FastAPI Integratsiyasi
from fastapi import FastAPI, Request
from paytechuz.gateways.click import ClickGateway
from paytechuz.integrations.fastapi import ClickWebhookHandler
import os
app = FastAPI()
# Shlyuz yaratish
click = ClickGateway(
service_id=os.getenv('CLICK_SERVICE_ID'),
merchant_id=os.getenv('CLICK_MERCHANT_ID'),
merchant_user_id=os.getenv('CLICK_MERCHANT_USER_ID'),
secret_key=os.getenv('CLICK_SECRET_KEY'),
is_test_mode=True
)
@app.post("/payment/create")
async def create_payment():
payment_url = click.create_payment(
id="12345",
amount=500.00,
return_url="https://example.com/return"
)
return {"payment_url": payment_url}
@app.post("/webhooks/click")
async def webhook(request: Request):
handler = ClickWebhookHandler(
service_id=os.getenv('CLICK_SERVICE_ID'),
secret_key=os.getenv('CLICK_SECRET_KEY'),
account_model=Order
)
return await handler.handle_webhook(request)
Konfiguratsiya
Test rejimi
Test uchun quyidagi ma'lumotlardan foydalaning:
- Service ID:
test_service_id - Merchant ID:
test_merchant_id - Test karta:
8600 0691 9540 6311
Production rejimi
- Click Business da ro'yxatdan o'ting
- Production ma'lumotlaringizni oling
is_test_mode=Falseqilib o'rnating
Xatolarni qayta ishlash
from paytechuz.core.exceptions import PaymentException
try:
payment = gateway.create_payment(
id="12345",
amount=500.00,
return_url="https://example.com/return"
)
except PaymentException as e:
print(f"Click xatosi: {e.code} - {e.message}")
Webhook javob kodlari
Click webhook maxsus javob kodlarini kutadi:
0- Muvaffaqiyat-1- Imzo tekshiruvi muvaffaqiyatsiz-2- Noto'g'ri summa parametri-3- Amal topilmadi-4- Allaqachon to'langan-5- Foydalanuvchi mavjud emas-6- Tranzaksiya mavjud emas-7- Foydalanuvchini yangilash muvaffaqiyatsiz-8- Click dan so'rovda xato-9- Tranzaksiya bekor qilindi