条带结帐发送额外参数
stripe checkout send extra parameters
请检查下面的代码。我正在尝试从条纹中添加结帐付款。下面的代码已经可以使用了。但是我想从结帐 html 传输一个元数据 data-id="123"
然后我想从控制器 metaData.Add("DomainID", "set id here")
获取这个 id 但我没有得到任何文档我如何发送这个值html 到 mvc5 Charge
控制器。任何的想法?我正在关注来自 stripe
的 this doc
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 控制器:
[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
驻留/访问的位置。
如果您希望 metadata
在结帐会话本身结束 (object ref), then you would supply that in the top-level parameters when creating the session (API ref)。
如果您希望将 metadata
添加到底层 Payment Intent (and Charge), then you should supply it in the nested payment_intent_data[metadata]
parameter (API ref)。
请检查下面的代码。我正在尝试从条纹中添加结帐付款。下面的代码已经可以使用了。但是我想从结帐 html 传输一个元数据 data-id="123"
然后我想从控制器 metaData.Add("DomainID", "set id here")
获取这个 id 但我没有得到任何文档我如何发送这个值html 到 mvc5 Charge
控制器。任何的想法?我正在关注来自 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 控制器:
[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
驻留/访问的位置。
如果您希望 metadata
在结帐会话本身结束 (object ref), then you would supply that in the top-level parameters when creating the session (API ref)。
如果您希望将 metadata
添加到底层 Payment Intent (and Charge), then you should supply it in the nested payment_intent_data[metadata]
parameter (API ref)。