#Off_set

1 messages ยท Page 1 of 1 (latest)

worn trellisBOT
normal meteor
#

๐Ÿ‘‹ Thanks for reaching out
Could you please provide further details about your issue ?

young pilot
#

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>

normal meteor
#

Just to clarify, the CORS error you are facing, is thrown by Stripe side or you backend ?

young pilot
#

its on my console

normal meteor
#

yes, but which part is throwing this your backend or Stripe? could you please share a screen shot about it ?

young pilot
#

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

normal meteor
#

You need to see how to enable CORS on your backend. I see that you are using Nodejs, you can search in the community

young pilot
#

bro i just fixed it, tysm, it had to do something with the APi

normal meteor
#

Great to hear that ๐Ÿ™‚

left pine
#

@young pilot can you share the req_xxx ID from that request that failed?

young pilot
#

requestId: 'req_7FvAqwpXZ2T4ZP', this?

#

are you there sir?

left pine
#

Yes, I'm also helping other people โ€“ its pretty busy. Please be patient

young pilot
#

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))


    }
  });```
left pine
#

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?

young pilot
#

its [] empty

left pine
#

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

young pilot
#

aight thank you

#

i fixed that [] empty returning problem

#

StripeInvalidRequestError: Invalid object, and but now i face this problem

green whale
#

๐Ÿ‘‹ stepping in and catching up, give me a moment

#

What line is throwing that invalid object error?

young pilot
#

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)```
green whale
#

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?

young pilot
#

yes

green whale
#

What does that log show?

young pilot
#
  {
    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] }
  }
]```
green whale
#

Can you log out the product_data?

young pilot
#

im getting this error

#

ReferenceError: product_data is not defined

green whale
#

okay so that is likely the issue then.

#

You need to debug transfromedItems further