stripe支付后如何存卡?



我想在我的应用程序中保存一张卡,但总是得到相同的异常:"Stripe。StripeException: '提供的PaymentMethod以前与PaymentIntent一起使用,没有客户附件,与连接的帐户共享,没有客户附件,或者从客户分离。不能再用了。要多次使用PaymentMethod,您必须首先将其附加到客户。">我一点儿也不知道怎么解决这个问题。

这是我的Controller.cs:

public class PaymentController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Processing()
{
var service = new PaymentMethodService();
var obj=service.Get("pm_1ICLE7GcqJgpxMZpnTbfS7Jw");

var paymentIntents = new PaymentIntentService();
var paymentIntent = paymentIntents.Create(new PaymentIntentCreateOptions
{
Amount = 2000,
Currency = "usd",
Customer = "cus_Ing6wBxNYVdB44",
ReceiptEmail = "eman29@jdecorz.com",
PaymentMethod = obj.Id,
Confirm = true,
OffSession = true
});//here exception is thrown

return Json(new { clientSecret = paymentIntent.ClientSecret });
}
}

我的client.js代码:

var stripe = Stripe("pk_test_51IBEAOGcqJgpxMZpvVKN2j9K7RJpzazfnG4u0relgSXiVBtNDd7nGgxBmX8BNCvuNerv1jnf0UVL5Uz8ODeJ7wvI00ruu2ByVM");
// Disable the button until we have Stripe set up on the page
document.querySelector("button").disabled = true;
fetch("/Payment/Processing", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(purchase)
})
.then(function (result) {
return result.json();
})
.then(function (data) {
var elements = stripe.elements();
var style = {
base: {
color: "#32325d",
fontFamily: 'Arial, sans-serif',
fontSmoothing: "antialiased",
fontSize: "16px",
"::placeholder": {
color: "#32325d"
}
},
invalid: {
fontFamily: 'Arial, sans-serif',
color: "#fa755a",
iconColor: "#fa755a"
}
};
var card = elements.create("card", { style: style });
// Stripe injects an iframe into the DOM
card.mount("#card-element");
card.on("change", function (event) {
// Disable the Pay button if there are no card details in the Element
document.querySelector("button").disabled = event.empty;
document.querySelector("#card-error").textContent = event.error ? event.error.message : "";
});
var form = document.getElementById("payment-form");
form.addEventListener("submit", function (event) {
event.preventDefault();
// Complete payment when the submit button is clicked
payWithCard(stripe, card, data.clientSecret);
});
});
// Calls stripe.confirmCardPayment
// If the card requires authentication Stripe shows a pop-up modal to
// prompt the user to enter authentication details without leaving your page.
var payWithCard = function (stripe, card, clientSecret) {
loading(true);
stripe
.confirmCardPayment(clientSecret, {
payment_method: {
card: card
}
})
.then(function (result) {
if (result.error) {
// Show error to your customer
showError(result.error.message);
} else {
// The payment succeeded!
orderComplete(result.paymentIntent.id);
}
});
};
/* ------- UI helpers ------- */
// Shows a success message when the payment is complete
var orderComplete = function (paymentIntentId) {
loading(false);
document
.querySelector(".result-message a")
.setAttribute(
"href",
"https://dashboard.stripe.com/test/payments/" + paymentIntentId
);
document.querySelector(".result-message").classList.remove("hidden");
document.querySelector("button").disabled = true;
};
// Show the customer the error from Stripe if their card fails to charge
var showError = function (errorMsgText) {
loading(false);
var errorMsg = document.querySelector("#card-error");
errorMsg.textContent = errorMsgText;
setTimeout(function () {
errorMsg.textContent = "";
}, 4000);
};
// Show a spinner on payment submission
var loading = function (isLoading) {
if (isLoading) {
// Disable the button and show a spinner
document.querySelector("button").disabled = true;
document.querySelector("#spinner").classList.remove("hidden");
document.querySelector("#button-text").classList.add("hidden");
} else {
document.querySelector("button").disabled = false;
document.querySelector("#spinner").classList.add("hidden");
document.querySelector("#button-text").classList.remove("hidden");
}
};

我试着使用https://stripe.com/docs/payments/save-during-payment和https://stripe.com/docs/payments/save-and-reuse的样本,但无法理解,我做错了什么

我知道这是一个愚蠢的问题,但这是我第一次使用Stripe,我找不到任何解决这个问题的方法。提前感谢!

在为客户重用PaymentMethod时,必须将其附加到客户。有几种方法可以做到这一点。例如,一种选择是创建一个支付方法,然后在后端调用attach[1]。另一种选择是使用Stripe.js和Elements收集卡片信息,并"设置未来的使用",这将自动将卡片附加到客户身上[2]。

有一点需要注意,如果你的代码使用了confirmCardPayment()[3],那通常是一个"on-session"。在用户主动确认收费时进行支付。[4]

[1] https://stripe.com/docs/api/payment_methods/attach[2] https://stripe.com/docs/js/payment_intents/confirm_card_payment stripe_confirm_card_payment-data-setup_future_usage

[3] https://stripe.com/docs/js/payment_intents/confirm_card_payment[4] https://stripe.com/docs/api/payment_intents/create create_payment_intent-off_session

最新更新