我正在使用后端 NodeJS 在我的 React.js 应用程序中实现 razorpay 支付网关。
这里frontend.jsx
razorpayHandler = () =>{
const payment_amount = this.props.TotalPrice;
const backend_url = 'https://25234399bb.ngrok.io';
const self = this;
const options = {
key: config.RAZOR_PAY_KEY,
amount: payment_amount * 100,
name: 'StanPlus',
description: 'pay your ambulance fare',
handler(response) {
const paymentId = response.razorpay_payment_id;
const url = backend_url+'/razorpay/'+paymentId+'/'+payment_amount+'/'+self.id;
console.log(paymentId)
// Using my server endpoints to capture the payment
fetch(url, {
method: 'get',
headers: {
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8"
}
})
.then(resp => resp.json())
.then(function (data) {
console.log(data)
})
.catch(function (error) {
console.log('Request failed', error);
});
},
theme: {
color: '#40A9FF',
},
};
const rzp1 = new window.Razorpay(options);
rzp1.open();
}
后端.js(nodejs)
var express = require('express');
var router = express.Router();
var config = require('../config');
const Razorpay = require('razorpay');
const instance = new Razorpay({
key_id: config.razorpay_live_key,
key_secret: config.razorpay_live_secret,
});
router.get('/:payment_id/:amount/:BID',function(req,res,next){
const {payment_id } = req.params;
const {BID} = req.params;
const amount = Number(req.params.amount*100);
instance.payments.capture(payment_id, amount).then((data) => {
data.Bid = BID;
res.json(data);
}).catch((error) => {
res.json(error);
});
})
module.exports = router;
它向我显示错误
"statusCode":400,"error":{"code":"BAD_REQUEST_ERROR","description":"提供的 ID 不存在"
但是如果相同的代码使用 test key 进行处理,它会成功完成,但不能与实时 api 一起使用。
这里我向后端传递了一个我们需要的额外参数,但如果删除该参数,那么它也不起作用。但是使用参数它可以与测试 API 一起使用。
当我们向后端发送请求时,它会生成 id 并发送到后端,但仍然显示提供的 id 不存在。
如果您使用测试模式,则只需从 json 对象中删除 order_id 参数即可。
一周前我也遇到了这个错误。当我们将测试密钥更改为生产密钥以使最终付款正常工作时,出现此错误。
所以我遇到了这个问题提供的id不存在因为前端和后端侧(node.js侧)的Razorpay密钥不匹配。
因此请确保后端和前端具有相同的客户端密钥和生产环境密钥。
如果仍未解决,请在评论中告诉我。
删除
order_id
不是一个好的做法,我们应该遵循文档。
要在 order_id
中获得 React
,您必须首先在后端创建订单,例如(node.js
)。按照以下步骤从 order_id
获取 Razorpay
。
var instance = new Razorpay({ key_id: 'YOUR_KEY_ID', key_secret: 'YOUR_KEY_SECRET'})
这将启动新的
Razorpay
对象。
MyOrder = instance.orders.create({amount, currency, receipt, notes})
这将为您创建订单,然后您就可以访问
order_id
。您可以登录 MyOrder 查看更多可用属性,或者只需登录 console.log(MyOrder.id)
即可获取 order_id
最后你必须通过你的 order_id
在你的情况下你必须通过 order_id
中的 options
。
注意:您可以像这样访问订单ID
MyOrder.id
欲了解更多信息,请查看官方doc。
您可以在这里
找到各种平台的Razorpay SDKs
如果您用于付款,请从选项 JSON 值中删除 order_id。
var options = {
"key": "xxxxxxxxxxxxxxx", // 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": "order_9A33XWu170gUtm", //This is a sample Order ID. Pass the `id` obtained in the response of Step 1
"handler": function (response){
alert(response.razorpay_payment_id);
alert(response.razorpay_order_id);
alert(response.razorpay_signature)
},
"prefill": {
"name": "Gaurav Kumar",
"email": "[email protected]",
"contact": "9999999999"
},
"notes": {
"address": "Razorpay Corporate Office"
},
"theme": {
"color": "#3399cc"
}
};
当您传递错误的 order_id 来触发付款模式时,会发生此错误。基本上是一个不属于您的 razorpay 帐户的 order_id,或者是一个无效的 order_id。
示例:如果您使用一组凭据生成 order_id,然后使用另一组凭据,则可能会发生此问题。
我在集成 Razorpay 进行订阅付款时也遇到了同样的问题 最初,我在选项参数中传递了
order_id
,这产生了错误 -
{
"code": "BAD_REQUEST_ERROR",
"description": "The id provided does not exist",
"source": "business",
"step": "payment_initiation",
"reason": "input_validation_failed",
"metadata": {}
}
因此将 order_id 替换为
subscription_id
。检查
rp_type
,
订阅通行证options["subscription_id"]
,
订单通过options["order_id"]
我对这个错误有一个非常好的解决方案。 “key”和“key_id”不匹配 我通过将前端选项对象中的“key_id”替换为“key”解决了错误。