Search code examples
javascriptpythonapipaymentrazorpay

Razorpay API gives inavlid key error even though the key is valid


Have been working on Razorpay payments API using flask,facing this issue since forever

    import razorpay
    import json

    from flask import Flask, render_template, request ,jsonify

    app = Flask(__name__)
    razorpay_client = razorpay.Client(auth=("rzp_test_8ydfJQKGSKoloz", "7LNONq8PYWjg4ImLujHDxpst"))

    order_id = ''

    def app_create():
        order_amount = 50000
        order_currency = 'INR'
        order_receipt = 'order_rcptid_100'
        notes = {'Shipping address': 'Pune, Maharashtra'}
        something = razorpay_client.order.create(dict(amount=order_amount, currency=order_currency,                         receipt=order_receipt, notes=notes))
return something['id']

    @app.route('/')
    def app_start() :
        return render_template('app.html')

    #@app.route('/create')
    #def app_create():
       # order_amount = 50000
       # order_currency = 'INR'
       # order_receipt = 'order_rcptid_100'
      #  notes = {'Shipping address': 'Pune, Maharashtra'}
      #  something = razorpay_client.order.create(dict(amount=order_amount, currency=order_currency,         receipt=order_receipt, notes=notes))
      #  order_id = something['id']
      #  return(something)


    @app.route('/pay')
    def app_pay():

        order_id = app_create()

        razrp_config = {
            "options" : {
                            "key": "7LNONq8PYWjg4ImLujHDxpst",
                            "amount": "50000",
                            "currency": "INR",
                            "name": "Acme Corp",
                            "description": "Test Transaction",
                            "order_id": order_id,
                            "callback_url": "https://eneqd3r9zrjok.x.pipedream.net/",
                            "notes": {
                                        "address": "Razorpay Corporate Office"
                                    },
                            "theme": {
                                    "color": "#3399cc"
                                } 
                    }        
}
        return jsonify(razrp_config)


    @app.route('/charge', methods=['POST'])
    def app_charge():
        amount = 50000
        payment_id = request.form['razorpay_payment_id']
        razorpay_client.payment.capture(payment_id, amount)
        return json.dumps(razorpay_client.payment.fetch(payment_id))

    if __name__ == '__main__':
        app.run()

that was the python code, the javscript :

   // Get data 
   //fetch('/create')
   //.then((result) => { console.log( result.json() )})


   fetch('/pay')
   .then((result) => { return result.json(); })
   .then((data) => {
     // Initialize js
     console.log(data)
     const rzp1 = new Razorpay(data.options);
     // Event handler
     document.querySelector("#submitBtn").addEventListener("click", () => {
       rzp1.open()
       //fetch('/charge')
       //.then((result) => { return result.json(); })
       //.then((data) => {
       //  console.log(data);
         // Redirect 
       //})
     });
   });

there are a lot of comments, try to look past them, when i click the button on the html dom it gives an error : 'serviceworker' should be a dict in the web app manifest

I get the API key is invalid error when infact it is valid and recent


Solution

  • The key you are passing in options is invalid.

    use correct keys. try using the keys you mentioned at the top.

    "options" : { "key": "rzp_test_8ydfJQKGSKoloz"