简体   繁体   中英

Razorpay Django Integration with Callback URL (CSRF token missing or incorrect.)

I am trying to Class Based View with Razor Pay, Everything working Perfectly. But When i am POST data too same view it is giving error Forbidden (CSRF token missing or incorrect.): /buy-coin .

I am having two question here

  1. How we can exempt CSRF Token for post method
  2. In Razorpay Javascript Code, can we add csrf token in callback url.

Razorpay Python Integration Link - https://razorpay.com/docs/payment-gateway/server-integration/python/

View.py

class BuyCoinPageView(LoginRequiredMixin, View):
    def get(self, request, *args, **kwargs):
        client = razorpay.Client(auth=("scretKey", "secretKey"))
        data = { "amount": 10000, "currency": "INR", "receipt": "order_rcptid_11" }
        payment = client.order.create(data=data)
        print('Razor Pay - ', payment['id'])
        return render(request, "pricing-page.html", {'payment': payment})
    
    def post(self, request, *args, **kwargs):
        data = request.POST
        print(data)
        return render(request, "pricing-page.html")

HTML FILE

<a href="#" id="rzp-button1" class="btn-buy">Buy Now</a>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
    "key": "rzp_test_hwAkAHZlKJdgee", // Enter the Key ID generated from the Dashboard
    "amount": "50000", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise
    "currency": "INR",
    "name": "Acme Corp",
    "description": "Test Transaction",
    "image": "https://example.com/your_logo",
    "order_id": "{{payment.id}}", //This is a sample Order ID. Pass the `id` obtained in the response of Step 1
    "callback_url": "{% url 'buy_coin' %}",
    "prefill": {
        "name": "Gaurav Kumar",
        "email": "gaurav.kumar@example.com",
        "contact": "9999999999"
    },
    "notes": {
        "address": "Razorpay Corporate Office"
    },
    "theme": {
        "color": "#3399cc"
    }
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e){
    rzp1.open();
    e.preventDefault();
}
</script>

Terminal Logs -

[08/Nov/2021 15:22:12] "GET /static/assets/css/style.css HTTP/1.1" 200 27172
[08/Nov/2021 15:22:12] "GET /media/exam-logos/Java-logo.jpg HTTP/1.1" 200 38915
Razor Pay -  order_IJ9ngRpV19pdEc
[08/Nov/2021 15:22:16] "GET /buy-coin HTTP/1.1" 200 8478
Forbidden (CSRF token missing or incorrect.): /buy-coin
[08/Nov/2021 15:22:37] "POST /buy-coin HTTP/1.1" 403 2519

浏览器错误图像

write a function for your call_back and do a csrf exempt

from django.views.decorators.csrf import csrf_exempt

@csrf_exempt
def razorpay_call_back(request):
   "write your code here"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM