【发布时间】:2021-07-21 02:52:01
【问题描述】:
请检查下面的代码。我正在尝试从条纹添加结帐付款。下面的代码已经有效。但是我想从结帐 html 中传输一个元数据 data-id="123" 然后我想从控制器 metaData.Add("DomainID", "set id here") 中获取这个 id 但我没有得到任何文档如何将这个值从 html 发送到 mvc5 Charge 控制器。任何想法?我正在关注来自条纹的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 });
}
【问题讨论】:
标签: c# stripe-payments