Search code examples
node.jspython-3.xdjango-viewsdjango-templatesrazorpay

Getting customer information in Django RazorPay payment gateway


I just integrated Razorpay payment gateway in my django project and i am new to this payment gateway. When i am initiate the payment razorpay show me an payment interface(like this image) and there is options for writing customer contact and email. Here is my question how can i get this two fields back contact and email when customer click on proceed button. enter image description here

Here is my code: Template file

            <h2>Product Checkout</h2>
            <p>Product: {{ order_obj.product.name }}</p>
            <p>Total: {{ order_obj.total }}</p>

 <form method="POST"> {% csrf_token %}
    <script src="https://checkout.razorpay.com/v1/checkout.js"
    data-key="rxxxxxxxxxxxxitP7h"
    data-amount= {{order_amount }}
    data-currency={{order_currency}}
    data-buttontext="Pay with Razorpay"
    data-name="xxxxxInc"
    data-description="We are listning"
    data-image="https://www.bihhs.in/wp-content/uploads/2020/05/jj-logo.png"
    data-prefill.name="company name"
    data-prefill.email="abc@gmail.com"
    data-theme.color="#F37254">
</script><input type="hidden" custom="Hidden Element" name="hidden">
</form>

Views.py file

client = razorpay.Client(auth=("rzxxxxxxxaitP7h", "dZH5xxxxxxxxFmzG"))
payment = client.order.create(dict(amount=order_amount, currency=order_currency))
if payment:
   order_obj.mark_paid()  
   print(payment)
   print(payment.get('email'))

Solution

  • You should be getting an id in the response json. As per the docs here RazorpayAPI Reference and you can then get the details of the order with that ID. Then you create a payment with that order ID and then once payment is made you get a payment ID. There are methods to Fetch Payments with a Payment ID which should have these details if the user has filled it.

    For your Reference, these are python method guidelines for Razorpay: Payment Reference Python Razorpay

    Here's a pseudo-code for the changes:

    client = razorpay.Client(auth=("rzxxxxxxxaitP7h", "dZH5xxxxxxxxFmzG"))
    order_details = client.order.create(dict(amount=order_amount, currency=order_currency))
    if payment:
       order_obj.mark_paid()
       payment_id= client.order.payments(str(order_details["id"]))
       payment_details= client.payment.fetch(str(payment_id)
       payment_email= payment_details["email"]
       print (payment)
       print(payment_details)