Search code examples
razorpay

Razor Pay : verify_payment


According to the Razor pay documentation, you after the payment with the Card/Wallet has been successful, you need to verify the payment. [https://razorpay.com/docs/payment-gateway/quick-integration/][1]
"This is a mandatory step that allows you to confirm the authenticity of the details returned to the Checkout form for successful payments."
The following python code has been given as sample:

import razorpay

client = razorpay.Client(auth = ('<key_id>', '<key_secret>'))
params_dict = {
    'razorpay_order_id': '12122',
    'razorpay_payment_id': '332',
    'razorpay_signature': '23233'
}
client.utility.verify_payment_signature(params_dict)

But I don't see the client object doesn't have a member with name : utility. I am not able to find any good working sample. Please provide some help with this step. Thanks in advance!


Solution

  • This is how I found on how to instantiate the utility class. If you go to the definition of razorpay.Client in VSCode (F12), you can see that it imports the utility class. from . import resources, utility

    Now if you go to the definition of utility class, you can see how it can be initiated. enter image description here

    Based on that, I came up with the following code. NOTE that verify_payment_signature() doesn't return any value. But if there is any problem, it will come as an exception. (Eg: Remove one parameter from input and execute again).

    # Install from VSCode command line : >python -m pip install razorpay
    import razorpay
    
    # this is your account credentials got from the dash board
    client = razorpay.Client(auth = ('rzp_test_xxx', 'yyy'))
    
    # These values are got from Razor pay paymentUI response
    params_dict = {
        'razorpay_order_id': 'order_xxx',
        'razorpay_payment_id': 'pay_yyy',
        'razorpay_signature': 'zzzzz'
    }
    
    try:
        util = razorpay.Utility(client)
    
        util.verify_payment_signature(params_dict)
        
    except Exception as e:
        print("Unexpected error:", e)