can not make an API in my express server for the strip payment checkout
i get this error:
Uncaught (in promise) IntegrationError: Invalid value for stripe.confirmCardPayment intent secret: value should be a client secret of the form ${id}secret${secret}. You specified: client secret.
##"client secret is default value" I need to update it and console need to display actual client secret
backend code
const functions = require("firebase-functions");
const express = require("express")
const cors = require("cors");
const stripe = require("stripe")(
"secret_key"
);
const app = express();
app.use(cors({
origin: true
}));
app.use(express.json());
app.get("/", (request, response) => response.status(200).send("hello payment"));
app.post("/payments/create", async (request, response) => {
const total = request.query.total;
console.log("Payment Request Recieved >>> ", total);
console.log(stripe)
const paymentIntent = await stripe.paymentIntents.create({
amount: total,
currency: "usd",
});
response.status(201).send({
clientSecret: paymentIntent.client_secret,
});
});
// - Listen command
exports.api = functions.https.onRequest(app);
calling api in payment js
useEffect(() => {
const getClientSecret = async () => {
const response = await axios({
mehtod: 'post',
url: `/payments/create?total=${totoalItemPrice(basket) * 100}`
})
setClientSecret(response.data.clientSecret)
}
getClientSecret();
}, [basket])
console.log('THE SECRET IS >>>', clientSecret)
Solution 1:
Your sever code is returning :
response.status(201).send({
clientSecret: paymentIntent.client_secret,
});
But on client side you're expecting it to be in format :
setClientSecret(response.data.clientSecret)
when I logged it to console, response object looked like :
{"clientSecret: "pi_someHashValueHere"}
Either you can modify server side code to send :
response.status(201).send({
data: { clientSecret: paymentIntent.client_secret }
});
or modify it on client side. currently you're accessing an undefined value hence the IntegrationError that you're getting.
Solution 2:
Ensure you are confirming the card payment in the frontend with:
const paymentConfirmation = await stripe.confirmCardPayment(
client_secret,
);