Skip to main content

Django Integration

Complete guide for integrating PayTechUZ with Django applications.

Installation

Install PayTechUZ with Django support:

pip install paytechuz[django]

Django Settings

Add PayTechUZ configuration to your Django settings:

# settings.py

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

PAYTECHUZ = {
'PAYME': {
'PAYME_ID': 'your_payme_id',
'PAYME_KEY': 'your_payme_key',
'IS_TEST_MODE': True, # Set to False in production
},
'CLICK': {
'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, # Set to False in production
}
}

Create models for handling orders and payments:

# models.py
from django.db import models
from django.utils import timezone

class Order(models.Model):
STATUS_CHOICES = (
('pending', 'Pending'),
('paid', 'Paid'),
('cancelled', 'Cancelled'),
('delivered', 'Delivered'),
)

product_name = models.CharField(max_length=255)
amount = models.DecimalField(max_digits=12, decimal_places=2)
status = models.CharField(max_length=20, choices=STATUS_CHOICES, default='pending')
created_at = models.DateTimeField(default=timezone.now)

def __str__(self):
return f"{self.id} - {self.product_name} ({self.amount})"

Views

Create views for handling payments:

# views.py
from paytechuz.integrations.django.views import BasePaymeWebhookView, BaseClickWebhookView
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()

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

Configure URL patterns:

# urls.py
from django.urls import path
from django.views.decorators.csrf import csrf_exempt
from .views import PaymeWebhookView, ClickWebhookView

urlpatterns = [
# ...
path('payments/webhook/payme/', csrf_exempt(PaymeWebhookView.as_view()), name='payme_webhook'),
path('payments/webhook/click/', csrf_exempt(ClickWebhookView.as_view()), name='click_webhook'),
]
from paytechuz.gateways.payme import PaymeGateway
from paytechuz.gateways.click import ClickGateway

# Get the order
order = Order.objects.get(id=1)

# Create Payme payment link
payme = PaymeGateway(
payme_id='your_payme_id',
payme_key='your_payme_key',
is_test_mode=True # Set to False in production
)
payme_link = payme.create_payment(
id=order.id,
amount=order.amount,
return_url="https://example.com/return"
)

# Create Click payment link
click = ClickGateway(
service_id='your_click_service_id',
merchant_id='your_click_merchant_id',
merchant_user_id='your_click_merchant_user_id',
secret_key='your_click_secret_key',
is_test_mode=True # Set to False in production
)

click_link = click.create_payment(
id=order.id,
amount=order.amount,
return_url="https://example.com/return",
)