#Suzz

1 messages · Page 1 of 1 (latest)

umbral ridgeBOT
atomic arch
#

hello! can you share more details on how you're implementing the redirection?

#

are you redirecting from your server?

bright leaf
#

i have a client built on js

#

`
let baseForm = document.querySelector('#baseForm');
baseForm.addEventListener('submit', (e) => {
e.preventDefault();
const user = JSON.parse(sessionStorage.getItem('User'));

const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");

const data = JSON.stringify({
    paymentRequest: {
        "Email": `${user.email}`,
        "PriceId": `${e.target.priceId.value}`
    }
});
console.log(data)
fetch("http://localhost:5146/create-checkout", {
    method: 'POST',
    headers: myHeaders,
    body: data,
    redirect: 'follow'
})
    .then(response => {
        const body = response.json()
        window.location.href = body.url
    })
    .catch(error => console.log('error', error));
return true;

});`

#

this is my code

atomic arch
#

what about the backend?

bright leaf
#

[HttpPost("create-checkout")] [Consumes("application/json")] public async Task<IActionResult> CreateCheckoutSession(PaymentRequest paymentRequest) { var _baseUrl = $"{Request.Scheme}://{Request.Host}"; SessionCreateOptions options = new SessionCreateOptions { SuccessUrl = $"{_baseUrl}/success.html?session_id={{CHECKOUT_SESSION_ID}}", CancelUrl = $"{_baseUrl}/canceled.html", Mode = "subscription", LineItems = new List<SessionLineItemOptions> { new() { Price = paymentRequest.PriceId, Quantity = 1, }, }, }; }; var service = new SessionService(_client); try { var session = await service.CreateAsync(options); Response.Headers.Add("Location", session.Url); return new StatusCodeResult(303); } catch (StripeException e) { Console.WriteLine(e.StripeError.Message); return BadRequest(new ErrorResponse { ErrorMessage = new ErrorMessage { Message = e.StripeError.Message, } }); } }

#

backend

atomic arch
#

i'm a bit confused as to why you're doing a 303 on your backend and then window.location.href on your frontend also

bright leaf
#

so how can i modify?

atomic arch
#

try only returning the url from your backend, without 303

bright leaf
#

ok

#

use 200Ok right?

atomic arch
#

remove the redirect: 'follow' in your fetch

bright leaf
#

ok

atomic arch
#

if it's successful it should be responding with 200

bright leaf
#

ok

umbral ridgeBOT
bright leaf
#

so i use get the data from the fetch result right?

keen prism
#

yup!

bright leaf
#

thankyou it worked!!

keen prism
#

No problem! Glad to hear that it works 😄

bright leaf
#

how can i setup default success url and cancel url when i'm using SessionCreateOptions in dotnet

keen prism
#

Those URLs should be set up and created in your server where customer will be directed to after successful payment or cancelling payment

bright leaf
#

so there is no way that i should get the default stripe success/ cancel pages like saas nocode integration?

keen prism
#

For Checkout Session, there's no default success or cancel pages provided by Stripe

bright leaf
#

ok

#

SessionCreateOptions options=new SessionCreateOptions { Customer = list.First().Id, SuccessUrl = $"{_baseUrl}/success.html?session_id={{CHECKOUT_SESSION_ID}}",

we will get the checkout sessionid after creating the service right? how can we setuo the CHECKOUT_SESSION_ID here?

#

is there any need to set it up or it will defaulty populate after creating a session?

keen prism
#

Yes, when creating a Checkout Session, you will receive the Checkout Session ID.

By setting session_id={{CHECKOUT_SESSION_ID}}, the checkout session ID will be populated automatically when redirecting customer to the success_url. You may find more details here: https://stripe.com/docs/payments/checkout/custom-success-page

bright leaf
#

ok