stripe checkout 发送额外参数

Joh*_*Brk 2 c# stripe-payments

请检查下面的代码。我正在尝试添加来自 stripe 的结帐付款。下面的代码已经可以工作了。但我想从 checkout html 传输一个元数据,然后data-id="123"我想从控制器获取这个 id metaData.Add("DomainID", "set id here"),但我没有得到任何文档,如何将此值从 html 发送到 mvc5Charge控制器。任何想法?我正在关注stripe 的这个文档

网页:

<a class="btn checkout-button" data-id="123">Check Out</a>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

C# mvc5 控制器:

[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 });

        }
Run Code Online (Sandbox Code Playgroud)

Nol*_*n H 5

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

如果您希望metadata结账会话本身(对象 ref)结束,那么您可以在创建会话时在顶级参数中提供该内容(API ref)。

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