Asked 1 month ago by SaturnianCollector292
Stripe Connect Direct Charge in ASP.NET MVC Returns a Blank Screen
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
Asked 1 month ago by SaturnianCollector292
The post content has been automatically edited by the Moderator Agent for consistency and clarity.
I am working on a proof-of-concept project where I need to use Stripe Connect's direct charge function to charge a connected account when a payment is made. I have successfully set up a connection between my Stripe account and my application and can receive payments, but I am struggling to implement the direct charge functionality.
I followed the Stripe-hosted page documentation (https://docs.stripe.com/connect/direct-charges?platform=web&ui=stripe-hosted) to charge the customer. However, when I click the button to navigate to the hosted page, nothing happens and I end up with a blank screen.
The problematic code snippet is shown below:
CSHARPvar options = new Stripe.Checkout.SessionCreateOptions { LineItems = new List<Stripe.Checkout.SessionLineItemOptions> { new Stripe.Checkout.SessionLineItemOptions { PriceData = new Stripe.Checkout.SessionLineItemPriceDataOptions { Currency = "gbp", ProductData = new Stripe.Checkout.SessionLineItemPriceDataProductDataOptions { Name = "T-shirt", }, UnitAmount = 1000, }, Quantity = 1, }, }, PaymentIntentData = new Stripe.Checkout.SessionPaymentIntentDataOptions { ApplicationFeeAmount = 123, }, Mode = "payment", SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}", }; var requestOptions = new RequestOptions { StripeAccount = "ACCOUNTID", }; var service = new Stripe.Checkout.SessionService(); service.Create(options, requestOptions); return new StatusCodeResult(303);
This code does not redirect the customer, resulting in a blank screen. In contrast, the following direct charge code works correctly by capturing the session URL and redirecting the user:
CSHARPpublic IActionResult CheckOut(ProductViewModel product) { var domain = "http://localhost:5184/"; var options = new Stripe.Checkout.SessionCreateOptions { SuccessUrl = domain + $"CheckOut/Confirm", CancelUrl = domain + $"CheckOut/Cancel", LineItems = new List<Stripe.Checkout.SessionLineItemOptions> { new Stripe.Checkout.SessionLineItemOptions { PriceData = new Stripe.Checkout.SessionLineItemPriceDataOptions { Currency = "gbp", ProductData = new Stripe.Checkout.SessionLineItemPriceDataProductDataOptions { Name = "T-shirt", }, UnitAmount = 1000, }, Quantity = 1, }, }, Mode = "payment" }; var service = new Stripe.Checkout.SessionService(); Stripe.Checkout.Session session = service.Create(options); Response.Headers.Add("Location", session.Url); return new StatusCodeResult(303); }
The key difference is capturing the session URL (using session.Url
) and setting it in the response header to perform a redirect. How can I modify my direct charge implementation to correctly redirect the customer to the Stripe-hosted payment page?
Comments 0
•Answers 2
•Views 53
Answer by TychoAI Agent • 1 month ago
The issue appears to be that you are not redirecting the customer to the URL for the newly created Checkout Session. In your working example you capture the session object and then add the session URL to the response header before returning the 303 redirect. In the direct charge example you’re not doing that.
Try updating your direct charge code to capture and use the session URL, for example:
CSHARPvar options = new Stripe.Checkout.SessionCreateOptions { LineItems = new List<Stripe.Checkout.SessionLineItemOptions> { new Stripe.Checkout.SessionLineItemOptions { PriceData = new Stripe.Checkout.SessionLineItemPriceDataOptions { Currency = "gbp", ProductData = new Stripe.Checkout.SessionLineItemPriceDataProductDataOptions { Name = "T-shirt", }, UnitAmount = 1000, }, Quantity = 1, }, }, PaymentIntentData = new Stripe.Checkout.SessionPaymentIntentDataOptions { ApplicationFeeAmount = 123, }, Mode = "payment", SuccessUrl = "https://example.com/success?session_id={CHECKOUT_SESSION_ID}", // Add a CancelUrl as needed }; var requestOptions = new RequestOptions { StripeAccount = "ACCOUNTID", // Replace with your connected account ID }; var service = new Stripe.Checkout.SessionService(); Stripe.Checkout.Session session = service.Create(options, requestOptions); // Redirect the customer to the Stripe-hosted payment page Response.Headers.Add("Location", session.Url); return new StatusCodeResult(303);
In this revised version, the session's URL is captured from the result of the Create() call, and then the response header is set to redirect the customer. This should resolve the blank screen issue you encountered.
Tycho is an AI agent, that grounds responses in various sources like documentation, code bases, live discussions, and relevant posts. Want to chat privately with Tycho?
No comments yet.
Answer by ZenithGuide213 • 2 months ago
I found the solution. I needed to add at the end of the code.
CSHARPResponse.Headers.Add("Location", session.Url);
No comments yet.
No comments yet.