#Off_set
1 messages ยท Page 1 of 1 (latest)
๐ Thanks for reaching out
Could you please provide further details about your issue ?
ofc, i use axios to call my backend for checkout session on my front-end, on my backup server, the request is sent but it then blocked by CORS
i can provide code details if you want
`const stripe = require('stripe')('sk_test_51M40mwLBUrY91DgzjKbtq3hO7kXItLmDlPWRAhEw271w709vjNmPMtV91yd46ic9PfrM4GqHaYisF5bkNcfgwFiu00aOYMISKd');
const cors = require('cors')
const debug = require("debug")("server");
const express = require('express');
const app = express();
app.use(express.static('public'));
app.use(cors())
app.post('/create-checkout-session', async (req, res) => {
const session = await stripe.checkout.sessions.create({
line_items: [
{
// Provide the exact Price ID (for example, pr_1234) of the product you want to sell
price: 'price_1M4QfnLBUrY91DgzT8WmJ278',
quantity: 1,
},
],
mode: 'payment',
success_url: 'http://localhost:3000',
cancel_url: 'http://localhost:3000',
});
res.redirect(303, session.url);
});
app.listen(8080, () => debug(console.log('Running on port 8080')));`
this is the back-end running on 8080 port
`const createCheckoutSession = async () => {
const stripe = await stripePromise
try {
const checkoutSession = await axios.post('http://localhost:8080/create-checkout-session', {
items: basket.map(item =>(<CheckoutProduct id= {item.id} title= {item.title} image= {item.image} price= {item.price} rating= {item.rating}/>)),
})
} catch(err){
console.log(err.response)
}
};`
this is the front-end where i call
and there is a button <button onClick={createCheckoutSession}>Proceed To Payment</button>
Just to clarify, the CORS error you are facing, is thrown by Stripe side or you backend ?
its on my console
yes, but which part is throwing this your backend or Stripe? could you please share a screen shot about it ?
its my back-end
(redirected from 'http://localhost:8080/create-checkout-session') from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
its the error
You need to see how to enable CORS on your backend. I see that you are using Nodejs, you can search in the community
bro i just fixed it, tysm, it had to do something with the APi
Great to hear that ๐
@young pilot can you share the req_xxx ID from that request that failed?
Yes, I'm also helping other people โ its pretty busy. Please be patient
The error is pretty explicit, your request didn't include the line_items parameter which is required: https://dashboard.stripe.com/test/logs/req_7FvAqwpXZ2T4ZP
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
but i have line_items
description: item.description,
quantity: 1,
price_data: {
currency: "gbp",
unit_amount: item.price * 100,
product_data: {
name: item.title,
images: [item.image],
}
}
}))
const session = await stripe.checkout.sessions.create({
payment_method_types: ["card"],
line_items: transformedItems,
shipping_rates: ['shr_1M7IaFLBUrY91Dgzxa2KPt9z'],
shipping_address_collection: {allowed_countries: ["GB", "US", "CA"]},
mode: 'payment',
success_url: 'http://localhost:3000',
cancel_url: 'http://localhost:3000',
metadata: {
images: JSON.stringify(items.map(item => item.image))
}
});```
My guess is your transformedItems is not the value you think it is (stripe-node omits null properties before sending to API), Can you log that transformedItems?
its [] empty
There's the issue then. It needs to be an array with at least one object that follows this shape: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
You need to figure out why your transformedItems function is returning an empty array
I'd guess items which you're iterating over is empty too
aight thank you
i fixed that [] empty returning problem
StripeInvalidRequestError: Invalid object, and but now i face this problem
๐ stepping in and catching up, give me a moment
What line is throwing that invalid object error?
how do i see that? this question may seem stupid
return new StripeInvalidRequestError(rawStripeError);
^
StripeInvalidRequestError: Invalid object
at Function.generate (F:\User\Downloads\React.Js\back-end\node_modules\stripe\lib\Error.js:38:16)
at res.toJSON.then.StripeAPIError.message (F:\User\Downloads\React.Js\back-end\node_modules\stripe\lib\StripeResource.js:190:35)
at processTicksAndRejections (node:internal/process/task_queues:96:5)```
Ah yeah that won't show you because it is a validation error so it is showing when in the Stripe module it is erroring.
So have you logged out your line_items?
yes
What does that log show?
{
description: '3254354345',
quantity: 1,
price_data: { currency: 'gbp', unit_amount: 59899, product_data: [Object] }
},
{
description: '23445930',
quantity: 1,
price_data: { currency: 'gbp', unit_amount: 9899, product_data: [Object] }
}
]```
Can you log out the product_data?