stripe checkout send extra parameters

Viewed 440

Please check the code bellow. I am trying to add a checkout payment from stripe. Code bellow works already. But i want to transfer one meta data from checkout html which is data-id="123" then i want to grab this id from controller metaData.Add("DomainID", "set id here") but i am not getting any documentation how can i send this value from html to mvc5 Charge controller. Any idea? i am following this doc from stripe

Html:

<a class="btn checkout-button" data-id="123">Check Out</a>

JS:

<script src="https://js.stripe.com/v3/"></script>
<script type="text/javascript">
    // Create an instance of the Stripe object with your publishable API key
    var stripe = Stripe("pk_test_51H5JwbI0Y3sF_fake_2keibuWoD8nKTm6joRYPXRH7Nk7t6dqo1OetP3rPQRR005SfevmAY");

    var checkoutButton = document.getElementsByClassName("checkout-button")[0];
    checkoutButton.addEventListener("click", function () {
        fetch("/Settings/Charge", {
            method: "POST",
        }).then(function (response) {
                return response.json();
            }).then(function (session) {
                return stripe.redirectToCheckout({ sessionId: session.id });
            }).then(function (result) {
                // If redirectToCheckout fails due to a browser or network
                // error, you should display the localized error message to your
                // customer using error.message.
                if (result.error) {
                    alert(result.error.message);
                }
            }).catch(function (error) {
                console.error("Error:", error);
            });
    });
</script>

C# mvc5 controller:

[HttpPost]
        public JsonResult Charge()
        {
            StripeConfiguration.ApiKey = "sk_test_51H5JwbI0Y3sF_fake_Pb9oadqZNkoZPRJD048gToZsMgDGzCu3D23iEZEnyyCtndB00jrFvKF3W";

            var domain = "http://localhost:55555/settings";

            var metaData = new Dictionary<string, string>();
            metaData.Add("DomainID", "here i want to set domain id which will come from html checkout button");


            var options = new Stripe.Checkout.SessionCreateOptions
            {
                PaymentMethodTypes = new List<string>
                {
                    "card",
                },
                LineItems = new List<SessionLineItemOptions>
                {
                  new SessionLineItemOptions
                  {
                    PriceData = new SessionLineItemPriceDataOptions
                    {
                      UnitAmount = 900,
                      Currency = "usd",
                      ProductData = new SessionLineItemPriceDataProductDataOptions
                      {
                        Name = "Premium charge",
                        Description = "this is some description",
                        Metadata = metaData,
                      },
                    },
                    Quantity = 1,
                  },
                },
                Mode = "payment",
                SuccessUrl = domain + "/Domain?session_id={CHECKOUT_SESSION_ID}",
                CancelUrl = domain + "/Domain",
            };
            var service = new Stripe.Checkout.SessionService();
            Stripe.Checkout.Session session = service.Create(options);
            return Json(new { id = session.Id });

        }
1 Answers

It depends on where you want the metadata to reside / be accessed.

If you want to have the metadata end up on the Checkout Session itself (object ref), then you would supply that in the top-level parameters when creating the session (API ref).

If you want the metadata to be added to the underlying Payment Intent (and Charge), then you should supply it in the nested payment_intent_data[metadata] parameter (API ref).

Related