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]=MYTOKEN.
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]);