繁体   English   中英

条带结帐发送额外参数

[英]stripe checkout send extra parameters

请检查下面的代码。 我正在尝试从条纹添加结帐付款。 下面的代码已经有效。 但是我想从结帐 html 中传输一个元数据,它是data-id="123"然后我想从 controller metaData.Add("DomainID", "set id here")获取这个 id 但我没有得到任何文档我如何将此值从 html 发送到 mvc5 Charge controller。 任何想法? 我正在关注 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 });

        }

这取决于您希望metadata驻留/访问的位置。

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 ).

如果您希望将metadata添加到基础支付意图(和费用),那么您应该在嵌套的payment_intent_data[metadata]参数( API 参考)中提供它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM