#Nrupal Patel

1 messages ยท Page 1 of 1 (latest)

wooden glacierBOT
trim jewel
#

Could you share the error message and the code how you create a Checkout Session from frontend and perform redirection?

sharp cedar
#

I don't do anything from fronted i redirect from backend Django using
return HttpResponseRedirect(checkout_session.url, status=303)?

trim jewel
#

I see! Do you use fetch(..) at frontend to create the Checkout Session, then perform redirect at backend?

sharp cedar
#

No i dont use it
how can i do it?

trim jewel
#

No, you shouldn't do that. I'm just trying to understand how your integration works in order to debug the issue

#

Could you share your code and the full error message?

trim jewel
#

CORS error can be thrown if you use fetch(..) to call your backend to create Checkout Session and perform redirect at server. If your current integration isn't like this, then this is the root cause.

Could you share the full and raw error message and your code, so that I can investigate further?

#

I don't have the visibility of your integration, so can't tell why the error occurs

sharp cedar
trim jewel
#

How about the frontend code that makes the request to create the Checkout Session in your server?

sharp cedar
#

In frontend i don't do anything..
in my backend code i create
def checkout_payment(self, plan_id, stripe_customer_id, adspend_amount):
if adspend_amount:
extra_charges = {
"price_data": {
"currency": "usd",
# additional amount in cents to Convert in doller
"unit_amount": int(adspend_amount * 100),
"product_data": {
"name": "Additional charges Based on Ad-spend",
},
},
"quantity": 1,
}
else:
extra_charges = {}

    checkout_session = stripe.checkout.Session.create(
        payment_method_types=["card"],
        line_items=[
            {
                "price": plan_id,
                "quantity": 1,
            },
            extra_charges,
        ],
        mode="subscription",
        customer=stripe_customer_id,
        success_url=url_hp.CHECKOUT_PAYMENT_SUCCESS_URL,
        cancel_url=url_hp.CHECKOUT_PAYMENT_CANCEL_URL,
    )
    return HttpResponseRedirect(checkout_session.url, status=303)
trim jewel
#

When will checkout_payment will be called? Shouldn't the customer select the plan at frontend, then a request is made to your backend to create a checkout session to perform a redirect?

sharp cedar
#

Yes my backend to create a checkout session to perform a redirect

trim jewel
#

CORS error usually occurs when you try to make a backend request to perform redirect with frontend JS calling with fetch(..) or ``XMLHttpRequest`

#

I understand that your server create a Checkout Session to perform a redirect, but how does your frontend talk to your backend to create a Checkout Session?

#

CORS (Cross-Origin Resource Sharing) is a security mechanism used by browsers. It relies on HTTP headers to discern if a given request is allowed to come from a different origin (e.g. a different domain than the one making the request).

#

The error is thrown in the browser, meaning that you're making a backend call from the frontend

#

If this is a pure error from the backend, the error won't be shown in the browser, but in your server log only

sharp cedar
#

yes I'm making a backend call from the frontend
How can I solved it?

trim jewel
#

Are you using fetch(..) or XMLHttpRequest in frontend to make a request to your server endpoint? If so, CORS errors will occur when the you try to retrieve a URL that redirects to the Checkout page, using either XMLHttpRequest or fetch(..). The problem with using XMLHttpRequest and fetch(..) is that they are designed to make a request, get a response back, and then you do something with the response in your code. They are not meant to be used with server-side redirects.

To resolve this, I'd recommend returning Checkout Session URL to your frontend instead of using server redirect, then use window.location at frontend to perform redirect

#

Simply put,

  1. Remove server redirect code and return Checkout Session URL instead.
  2. At frontend, set window.location = {CHECKOUT_SESSION_URL for redirection
sharp cedar
#

Thanks,
My fronted and backend in same domain,
I will check and let you know

trim jewel
#

No problem! Happy to help ๐Ÿ˜„

sharp cedar
#

Request req_kvQ9saZilUkXNw: Missing required param: line_items[0][currency].
give error..

trim jewel
sharp cedar
#

I pass plan_id
checkout_session = stripe.checkout.Session.create(
payment_method_types=["card"],
line_items=[
{
"price": plan_id,
"quantity": 1,
},
extra_charges,
],
mode="subscription",
customer=stripe_customer_id,
success_url=url_hp.CHECKOUT_PAYMENT_SUCCESS_URL,
cancel_url=url_hp.CHECKOUT_PAYMENT_CANCEL_URL,
)

trim jewel
sharp cedar
#

Hello
My fronted and backend in same domain, and i use fetch(..) .
but still display cross error

trim jewel
#

Yup! You shouldn't use fetch with server redirect as I mentioned earlier

#

Even the domain is the same, the ports can be different

#

I'd recommend returning Checkout Session URL to the frontend and perform frontend redirect with window.location

sharp cedar
#

so what i have to use for server redirect?

#

I have to redirect from backend if you have any solution please..

trim jewel
sharp cedar
#

If I add my domain in stripe it resolve cross error?...

#
  1. Remove server redirect code and return Checkout Session URL instead.
  2. At frontend, set window.location = {CHECKOUT_SESSION_URL for redirection
    it is secured?
trim jewel
#

If I add my domain in stripe it resolve cross error?.
No, this is the limitation by the browser with using fetch(..), which is outside of Stripe control

#
  1. Remove server redirect code and return Checkout Session URL instead.
  2. At frontend, set window.location = {CHECKOUT_SESSION_URL for redirection
    it is secured?
    Yes, since the Checkout Session creation is at your server. Your customer won't be able to modify the amount or plan that is created in Checkout Session
sharp cedar
#

Thanks again๐Ÿ˜€
if i will need any help give me the answer

trim jewel
#

No problem! Happy to help ๐Ÿ˜„