Home > Mobile >  How to Razorpay generate orderId using firebase call function?
How to Razorpay generate orderId using firebase call function?

Time:10-04

I have tried this but getting err is not null

 const instance = new Razorpay({
      key_id: "myKey",
      key_secret: "mySecret"
    });
     
    exports.generateOrder = functions.https.onCall((data, context) => {
        
         const options = {
          amount: data.amount,
          currency: "INR",
          receipt: "recipt by me"  //req.body.receipt
        };
        
        instance.orders.create(options, (err, order) => {
            if(err !==  null){
                console.log("ORDERID null");
                return null;
            }else{
                console.log("ORDERID " order);
                return order;
            }
        });
        
    
    });

CodePudding user response:

In case of any error, you should log err and check that. However if the order is created you can simply return the orderId as shown below:

exports.generateOrder = functions.https.onCall((data, context) => {        
  const options = {
    amount: data.amount,
    currency: "INR",
    receipt: "recipt by me"  //req.body.receipt
  };        
  return createOrder(options).then((orderId) => {
    return { orderId }
  }).catch(e => {
    return { error: e }
  })  
});


function createOrder(options) {
  return new Promise((resolve, reject) => {
    instance.orders.create(options, (err, order) => {
      if(err !==  null){
        console.log("failed to create order", err);
        return reject(err);
      } else {
        console.log("ORDERID " order.id);
        return resolve(order.id);
      }
    });
  })
}
  • Related