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',
'ACCOUNT_MODEL': 'your_app.models.Order', # Example: 'shop.models.Order' or 'payments.models.Transactions'
'ACCOUNT_FIELD': 'id',
'AMOUNT_FIELD': 'amount',
'ONE_TIME_PAYMENT': True,
'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',
'ACCOUNT_MODEL': 'your_app.models.Order',
'COMMISSION_PERCENT': 0.0,
'IS_TEST_MODE': True, # Set to False in production
},
'ATMOS': {
'CONSUMER_KEY': 'your_atmos_consumer_key',
'CONSUMER_SECRET': 'your_atmos_consumer_secret',
'STORE_ID': 'your_atmos_store_id',
'TERMINAL_ID': 'your_atmos_terminal_id', # Optional
'API_KEY': 'your_atmos_api_key', # For webhook signature verification
'ACCOUNT_MODEL': 'your_app.models.Order',
'ACCOUNT_FIELD': 'id',
'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,
BaseAtmosWebhookView
)
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()

class AtmosWebhookView(BaseAtmosWebhookView):
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 .views import PaymeWebhookView, ClickWebhookView, AtmosWebhookView

urlpatterns = [
# ...
path('webhooks/payme/', PaymeWebhookView.as_view(), name='payme_webhook'),
path('webhooks/click/', ClickWebhookView.as_view(), name='click_webhook'),
path('webhooks/atmos/', AtmosWebhookView.as_view(), name='atmos_webhook'),
]
from paytechuz.gateways.payme import PaymeGateway
from paytechuz.gateways.click import ClickGateway
from paytechuz.gateways.atmos import AtmosGateway

# 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=int(order.amount * 100), # Payme works with tiyin
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",
)

# Create Atmos payment link
atmos = AtmosGateway(
consumer_key='your_atmos_consumer_key',
consumer_secret='your_atmos_consumer_secret',
store_id='your_atmos_store_id',
is_test_mode=True # Set to False in production
)

atmos_payment = atmos.create_payment(
account_id=order.id,
amount=int(order.amount * 100) # Atmos works with tiyin
)
atmos_link = atmos_payment['payment_url']