【发布时间】:2020-12-01 11:06:12
【问题描述】:
我正在尝试使用条纹付款。我付款后可以在浏览器上看到成功响应,但是当我在仪表板中签入时,它没有添加到那里。
const express = require("express");
const stripe = require("stripe")("enter your API sk_test");
const app = express();
const port = 8000;
app.post("/charge", async (req, res, next) => {
try {
await stripe.paymentIntents.create(
{
amount: 200,
currency: "gbp",
payment_method_types: ["card"],
receipt_email: "hadeklte@gmail.com",
},
function (err, paymentIntent) {
if (err) {
throw new Error("failed to charge");
}
res.status(200).send(paymentIntent);
}
);
} catch (err) {
console.log(err, "error occure");
}
});
app.listen(port, () => {
console.log(`Server is up at ${port}`);
});
上面的一些类似的反应
{
"id": "pi_1HF19PGz03sGbVedIHyeBeLq",
"object": "payment_intent",
"amount": 200,
"amount_capturable": 0,
"amount_received": 0,
"application": null,
"application_fee_amount": null,
"canceled_at": null,
"cancellation_reason": null,
"capture_method": "automatic",
"charges": {
"object": "list",
"data": [],
"has_more": false,
"total_count": 0,
"url": "/v1/charges?payment_intent=pi_1HF19PGz03sGbVedIHyeBeLq"
},
...
}
在检查了我看到的文档后,要继续我应该附上付款方式并确认付款here。然后确认我使用这行代码
app.post("/charge", async (req, res, next) => {
let charged;
try {
await stripe.paymentIntents.create(
{
amount: 200,
currency: "gbp",
payment_method_types: ["card"],
receipt_email: "hadeklte@gmail.com",
},
function (err, paymentIntent) {
if (err) {
console.log(err, "failed payment");
}
charged = paymentIntent.id;
}
);
} catch (err) {
return res.status(500).send(err);
}
console.log(charged);
try {
await stripe.paymentIntents.confirm(
// "pi_1HF19PGz03sGbVedIHyeBeLq",
charged
{ payment_method: "pm_card_visa" },
function (err, paymentIntent) {
if (err) {
console.log(err, "failed confirmation");
}
res.status(200).send(paymentIntent);
}
);
} catch (err) {
return res.status(500).send(err);
}
});
如果我将参数作为字符串传递给 stripe.paymentIntent.confirm,它会响应成功,就像我评论的那样,它确实有效,但是当我将 Id 作为收费传递时,它确实会给我一个错误
undefined
Error: Stripe: Argument "intent" must be a string, but got: undefined (on API request to `POST /payment_intents/{intent}/confirm`)
at urlParams.reduce (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:21:13)
at Array.reduce (<anonymous>)
at getRequestOpts (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:18:29)
at Promise (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:69:14)
at new Promise (<anonymous>)
at makeRequest (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\makeRequest.js:66:10)
at Constructor.confirm (E:\projects\Stripe\Stripe_API_Docs\node_modules\stripe\lib\StripeMethod.js:31:7)
at app.post (E:\projects\Stripe\Stripe_API_Docs\app.js:70:33)
at processTicksAndRejections (internal/process/task_queues.js:86:5) 'failed confirmation'
现在我如何将在创建函数中创建的 paymentIntent.id 传递给未定义的确认函数。
【问题讨论】:
-
您想在回调函数
function (err, paymentIntent) {内运行第二个函数stripe.paymentIntents.confirm(,您遇到了范围问题,这就是它未定义的原因 -
为了将来参考,从现在到永远,永远不要在任何地方发布您的
client_secret。既然你这样做了,你就必须撤销那个秘密并生成一个新的。 -
嗨 Ermias,为了让这更简单,而不是有两个单独的调用;一个是创建付款意图,另一个是在确认付款意图之后。您可以在一个呼叫中创建并确认付款意图,如下面的示例所示:stripe.com/docs/payments/…。基本上,您在创建付款意图时提供
payment_method,并指定设置为true的confirm参数:stripe.com/docs/api/payment_intents/…
标签: javascript node.js stripe-payments payment