#Suzz
1 messages · Page 1 of 1 (latest)
hello! can you share more details on how you're implementing the redirection?
are you redirecting from your server?
i have a client built on js
and a server with asp.net core
`
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
what about the backend?
[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
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
so how can i modify?
try only returning the url from your backend, without 303
remove the redirect: 'follow' in your fetch
ok
if it's successful it should be responding with 200
ok
so i use get the data from the fetch result right?
yup!
thankyou it worked!!
No problem! Glad to hear that it works 😄
how can i setup default success url and cancel url when i'm using SessionCreateOptions in dotnet
Those URLs should be set up and created in your server where customer will be directed to after successful payment or cancelling payment
so there is no way that i should get the default stripe success/ cancel pages like saas nocode integration?
For Checkout Session, there's no default success or cancel pages provided by Stripe
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?
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
ok