How to retrieve checkout information when using Stripe SDK

Here is my Cart and Checkout process in my web application

My purpose is after checkout, I retrieve the payment information like transaction Id or payment reference

In my ASP.NET MVC 5 application, I have following code in Cart View

<div>
@using (Html.BeginForm("", "Cart", FormMethod.Post))
{
     ... here I show products in cart ...

     ... NOTE that I don't have any action in my form ...

     <input type="submit" id="stripe-checkout-button" value="Checkout" />
}
</div>

<script type="text/javascript">
    var stripe = Stripe('@ViewBag.StripePublishKey');
    var checkoutButton = document.getElementById('stripe-checkout-button');

    checkoutButton.addEventListener('click', function () {
        fetch('https://localhost:44323/Cart/CreateCheckoutSession', {
            method: 'POST'
            })
            .then(function(response) {
                return response.json();
            })
            .then(function(session) {
                return stripe.redirectToCheckout({ sessionId: session.id });
            })
            .then(function(result) {
                if (result.error) {
                    alert(result.error.message);
                }
            })
            .catch(function(error) {
                console.error('Error:', error);
            });
    });
</script>

In Cart controller I have following actions

[HttpPost]
public ActionResult CreateCheckoutSession()
{
    var cart = (ShoppingCart)Session["cart"];
    var options = new SessionCreateOptions
    {
        PaymentMethodTypes = new List<string> { "card" },
        Mode = "payment",
        SuccessUrl = Url.Action("CheckoutSuccessful", "Cart", null, Request.Url.Scheme),
        CancelUrl = Url.Action("CheckoutCancelled", "Cart", null, Request.Url.Scheme),
        PaymentIntentData = new SessionPaymentIntentDataOptions
        {
            CaptureMethod = "manual"
        },
        LineItems = new List<SessionLineItemOptions>()
    };

    foreach (var item in cart.Items)
    {
        // here I add the purchased product. I remove the code for simplification
    }

    var service = new SessionService();
    Session session = service.Create(options);

    ViewBag.SessionId = session.Id;

    return Json(new { id = session.Id });
}

public ActionResult CheckoutSuccessful(string sessionId)
{
    // sessionId is null here. I'm even not sure if I need this parameter

    ShoppingCart cart = (ShoppingCart)Session["cart"];
    return RedirectToAction("Index", "Checkout");
}

public ActionResult CheckoutCancelled()
{
    return RedirectToAction("Index", "Cart");
}

When I click on Checkout button, the application redirects to Stripe payment page and I can enter email and billing information and click pay. It works well and payment goes through.

Based on my setting, the application redirects to CheckoutSuccessful action after that.

What I am missing here is, in CheckoutSuccessful action, I need to retrieve email address and billing address plus payment or transaction Id. I don’t know how to do that.
In CheckoutSuccessful, the parameter sessionId is null. I don’t know what I’ve done wrong here or what I’m missing.

Thanks for help.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can ensure that the newly created Checkout Session ID is included in your success URL by appending {CHECKOUT_SESSION_ID to the success URL: https://stripe.com/docs/payments/checkout/custom-success-page#modify-success-url

Then on your success URL you can grab the Session ID from the query params and retrieve the Checkout Session.

Method 2

After a few days research and try out, I figured it out.

After creating session, I need to store the session.Id in Http Session and in CheckoutSuccessful method (which does not need any parameter), I need to call couple of APIs to retrieve session and payment information like below:

var service = new SessionService();
var session = service.Get(the stored session.Id);

var custService = new CustomerService();
var customer = custService.Get(session.CustomerId);

var paymentService = new PaymentIntentService();
var payment = paymentService.Get(session.PaymentIntentId);

Then, I will have all required infromation in session, customer and payment objects.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x