![](/img/trans.png)
[英]Error creating a payment intent for Stripe with Firebase in NodeJS
[英]stripe create payment intent with nodeJS
我正在嘗試使用 stripe 進行付款。我進行了付款,並且可以在瀏覽器上看到成功響應,但是當我檢查儀表板時,它並沒有添加到那里。
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"
},
...
}
檢查完我看到的文檔后,要繼續我應該附上付款方式並在此處確認付款。然后確認我使用這行代碼
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 傳遞給未定義的確認函數。
對於打字稿
import Stripe from 'stripe';
...
const paymentIntent: Stripe.PaymentIntent = await
stripeClient.paymentIntents.create({
amount: amount * 100,
currency: 'usd'
});
最后我發現問題是@JimithyPicker 所說的scope
app.post("/charge", async (req, res, next) => {
var charged;
try {
charged = await stripe.paymentIntents.create({
amount: 200,
currency: "gbp",
payment_method_types: ["card"],
receipt_email: "hadeklte@gmail.com",
});
} catch (err) {
return res.status(500).send(err);
}
console.log(charged);
try {
const paymentConfirm = await stripe.paymentIntents.confirm(
charged.id,
{ payment_method: "pm_card_visa" }
);
res.status(200).send(paymentConfirm);
} catch (err) {
return res.status(500).send(err);
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.