#omar-paymentintent-error
1 messages · Page 1 of 1 (latest)
@sweet flicker do you have more details?
This is therror it returns inn the console: Error creating PaymentIntent: TypeError {}
H@https://649e08fb3d6e99008cee8cba--ummyahyasbakery.netlify.app/Order:30:30293
insertSync@https://649e08fb3d6e99008cee8cba--ummyahyasbakery.netlify.app/Order:10:30355
@https://649e08fb3d6e99008cee8cba--ummyahyasbakery.netlify.app/Order:10:31806
okay so this has nothing to do with Checkout nor Link entirely
and is solely about your own payments code having an error on PaymentIntent creation?
I mean it works fine on desktop
so i was thinking it had something to do with mobile
All good, just trying to grasp the question first. You mentioned 2 words that are real Stripe products: Checkout and Link. But it seems this is unrelated right now
you seem to have a type error somewhere server-side right? Since PaymentIntent creation happens on the server. Can you share maybe your server-side code?
const express = require("express");
const app = express();
const cors = require('cors');
const path = require('path');
require('dotenv').config();
const stripe = require("stripe")(process.env.STRIPE_API_KEY);
app.use(express.static("src"));
app.use(express.json());
app.use(express.static('public'));
app.use((req, res, next) => {
// Allow requests from any origin
res.header('Access-Control-Allow-Origin', ['http://localhost:3000, https://649e00b479a9e300be9ee7fc--ummyahyasbakery.netlify.app']);
// Allow specific headers
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
// Allow specific methods
res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
next();
});
app.use(cors({
origin: '*',
allowedHeaders: 'Origin, X-Requested-With, Content-Type, Accept'
}));
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
const totalAmount = items.reduce((total, item) => total + item.price, 0);
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: totalAmount,
currency: "usd",
automatic_payment_methods: {
enabled: true,
},
description: "Look out for pickup time email",
});
const clientSecret = paymentIntent.client_secret;
res.setHeader('Content-Type', 'application/json');
res.send({
clientSecret: clientSecret,
});
});
app.get('/success.html', (req, res) => {
const successFilePath = path.join(__dirname, 'public', 'success.html');
res.sendFile(successFilePath);
});
app.listen(4242, () => console.log("Node server listening on port 4242!"));
so what's not working?
Can you add logs to that code to understand which exact part is erroring?
My understanding so far is that the request headers on mobile are sending application/x-www-form-urlencoded
even though I'm sending headers: { "Content-Type": "application/json" },
This is the network tab results for the error: Content-Type application/x-www-form-urlencoded
hum
ah so it crashes with the items decoding?
what's your client-side code that sends items?
useEffect(() => {
if (selectedProduct) {
fetch("http://localhost:4242/create-payment-intent", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ items: selectedProduct }),
})
.then((res) => res.json())
.then((data) => setClientSecret(data.clientSecret))
.catch((error) => console.error("Error creating PaymentIntent:", error));
}
}, [selectedProduct]);
selectedProduct is an array of object(s)
hum that seems correct to me
so that exact code is running on your mobile device and still ignoring the headers?
I wonder if it's something like this
Sorry, I don't know much about CORS and preflight options but google seems to indicate it might be that
still doesn't work