#Nrupal Patel
1 messages ยท Page 1 of 1 (latest)
Could you share the error message and the code how you create a Checkout Session from frontend and perform redirection?
I don't do anything from fronted i redirect from backend Django using
return HttpResponseRedirect(checkout_session.url, status=303)?
I see! Do you use fetch(..) at frontend to create the Checkout Session, then perform redirect at backend?
No i dont use it
how can i do it?
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?
could you shred exmple
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
How about the frontend code that makes the request to create the Checkout Session in your server?
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)
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?
Yes my backend to create a checkout session to perform a redirect
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
yes I'm making a backend call from the frontend
How can I solved it?
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,
- Remove server redirect code and return Checkout Session URL instead.
- At frontend, set
window.location = {CHECKOUT_SESSION_URLfor redirection
Thanks,
My fronted and backend in same domain,
I will check and let you know
No problem! Happy to help ๐
Request req_kvQ9saZilUkXNw: Missing required param: line_items[0][currency].
give error..
In https://dashboard.stripe.com/test/logs/req_kvQ9saZilUkXNw, you didn't set price that the customer is going to subscribe. If price is not set, you'd need to create price_data in line: https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-line_items-price_data
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,
)
Can you check if your plan_id is in the form of price_xxx? https://dashboard.stripe.com/test/logs/req_kvQ9saZilUkXNw shows price isn't set, so it's likely plan_id is invalid in your code
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
Hello
My fronted and backend in same domain, and i use fetch(..) .
but still display cross error
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
so what i have to use for server redirect?
I have to redirect from backend if you have any solution please..
If you wish to use server redirect, then you should use <form> like the example in the guide to perform server redirect instead of using fetch(..): https://stripe.com/docs/payments/accept-a-payment?platform=web&ui=checkout#redirect-customers
If I add my domain in stripe it resolve cross error?...
- Remove server redirect code and return Checkout Session URL instead.
- At frontend, set window.location = {CHECKOUT_SESSION_URL for redirection
it is secured?
If I add my domain in stripe it resolve cross error?.
No, this is the limitation by the browser with usingfetch(..), which is outside of Stripe control
- Remove server redirect code and return Checkout Session URL instead.
- 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
Thanks again๐
if i will need any help give me the answer
No problem! Happy to help ๐