I am trying to make a payment with stripe.I make the payment and could see the success response on the browser but when I check in the dashboard it isn't adding there.
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}`);
});
response of the above some thing like this
{
"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"
},
...
}
after checking the docs I saw, to continue I should attach payment method and confirm the payment here .Then to confirm I use this line of code
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);
}
});
and it respond an success if I pass the paramater to stripe.paymentIntent.confirm as string, like the one I commented it does work but when I pass the Id as a charged It does throw me an error
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'
now how could I pass the paymentIntent.id created in the create function to the confirm function with out undefined.
For typescript
import Stripe from 'stripe';
...
const paymentIntent: Stripe.PaymentIntent = await
stripeClient.paymentIntents.create({
amount: amount * 100,
currency: 'usd'
});
finally I figured out The problem was scope as @JimithyPicker stated
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);
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.