Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.2 kB
1
Indexable
Never
# settings.py
RAZORPAY_ID = "rzp_test_XXXXXXXXXXXXXXXXXX"
RAZORPAY_SECRET = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

# views.py
from django.conf import settings
from django.shortcuts import render
from .constants import PaymentStatus
from .models import Order
import razorpay



def callback(request):
    # pass arguments as get params to this view
    razorpay_payment_id = request.GET.get('razorpay_payment_id')
    razorpay_order_id = request.GET.get('razorpay_order_id')
    razorpay_signature = request.GET.get('razorpay_signature')
    order_id = request.GET.get('order_id')

    order = Order.objects.get(id=order_id)
    client = razorpay.Client(auth=(settings.RAZORPAY_ID, settings.RAZORPAY_SECRET))
    try:
        params_dict = {
            'razorpay_order_id': razorpay_order_id,
            'razorpay_payment_id': razorpay_payment_id,
            'razorpay_signature': razorpay_signature
        }
        client.utility.verify_payment_signature(params_dict)
        order.status = PaymentStatus.SUCCESS
        order.save()
        send_email()
        return render(request, 'payment/success.html')
    except:
        return render(request, 'payment/failure.html')