I am trying to set payment method on my ecommerce site but the stripe is giving this error: A token may not be passed in as a Source. You can convert your token into a Source using /v1/sources with card[token]=tok_1Lg9eWSEOA145r9TEaDbCDmr.
API Side:
const router = require("express").Router();
const stripe = require("stripe")(process.env.STRIPE_KEY);
router.post("/payment",(req,res)=> {
stripe.paymentIntents.create({
source:req.body.tokenId,
amount:req.body.amount,
currency: "usd",
},(stripeErr,stripeRes)=>{
if(stripeErr) {
res.status(500).json(stripeErr);
} else {
res.status(200).json(stripeRes);
}
})
});
module.exports = router;
Client Side:
useEffect(()=>{
const makeRequest = async ()=> {
try {
const res = await userRequest.post("/checkout/payment",{
tokenId: stripeToken.id,
amount: cart.total*100,
});
history.push("/success",{data: res.data});
} catch{}
};
stripeToken && makeRequest();
},[stripeToken, cart.total, history]);
With Stripe, both Tokens and Sources are legacy and deprecated . You should be using Payment Methods with Payment Intents instead.
You can create a Payment Method instead of a Token, then pass that Payment Method to the Payment Intent using the payment_method
property .
Or, better yet, follow the accept a payment guide instead , which doesn't require you to handle the Payment Method directly (Stripe.js will take care of it for you when confirming the Payment Intent client-side).
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.