在春季启动时通过stripe支付



我想"付款"。键入表单,因为"卡片"不适合,因为它在一行中。我试图从一个表格中获取数据,创建一个令牌来付款。但是我得到了错误提示"你不能用PaymentMethod创建收费。使用支付意图API代替。请求id: req_9AYqRwPYg7MPdU"。如何解决这个问题?谢谢你。

<form id="payment-form" method="post">
<input id="api-key" type="hidden" value="${stripePublicKey}">
<div class="form-group mb-3">
<div class="w-100" id="card-element">
<!-- A Stripe Element will be inserted here. -->
</div>
</div>
<button class="btn w-100 btn-success mt-4" type="submit" id="submitButton">Pay $230</button>
</form>
<script>
const stripe = Stripe($('#api-key').val());
const payment = stripe.elements({clientSecret:'${client_secret}'}).create('payment');
payment.mount('#card-element');
payment.addEventListener('change', function (event) {
$('#card-errors').text(event.error ? event.error.message : '');
});
$('#payment-form').on('submit', async function (e) {
e.preventDefault();
const { paymentMethod, error } = await stripe.createPaymentMethod(
'payment',
payment,
{ billing_details: { email: '${customer.email}' } }
);
if (error) {
alert(error);
} else {
const token = paymentMethod.id;
const email = '${customer.email}';
$.post(
"/payment/create-charge",
{email: email, token: token},
function (data) {
alert(data.details);
}, 'json');
}
});
</script>
@PostMapping("/create-charge")
public @ResponseBody StripeResponse createCharge(String email, String token) {
if (token == null) {
return new StripeResponse(false, "Stripe payment token is missing. please try again later.");
}
String chargeId = stripeService.createCharge(email, token, 999);// 9.99 usd
if (chargeId == null) {
return new StripeResponse(false, "An error accurred while trying to charge.");
}
return new StripeResponse(true, "Success your charge id is " + chargeId);
}
@Override
public String createCharge(String email, String token, int amount) {
String chargeId = null;
try {
Stripe.apiKey = API_SECRET_KEY;
Map<String, Object> chargeParams = new HashMap<>();
chargeParams.put("description", "Charge for " + email);
chargeParams.put("currency", "usd");
chargeParams.put("amount", amount);
chargeParams.put("source", token);
Charge charge = Charge.create(chargeParams);
chargeId = charge.getId();
} catch (Exception e) {
e.printStackTrace();
}
return chargeId;
}

我试图用这个代码获得一个令牌,它进入catch块并返回"{}"到控制台

stripe.createToken(payment).then((result) => {
// The code does not go here
}).catch((error) => {
console.error(error); // Displays "{ }" in console
});

首先你需要创建支付意图,一旦你生成支付意图,你会得到一个client_secrete在响应中,这意味着您的付款已准备好尝试收集付款,您可以选择将使用客户端SDK设置为true或false。

Stripe.apiKey = "your_stripe_secrete_key";
List<Object> paymentMethodTypes =
new ArrayList<>();
paymentMethodTypes.add("card");
Map<String, Object> params = new HashMap<>();
params.put("amount", whatever_amount_you_want_to_charge);
params.put("currency", "currency_abv" //ex- usd, inr, aud, etc);
params.put(
"payment_method_types",
paymentMethodTypes
);
PaymentIntent paymentIntent =
PaymentIntent.create(params);

在这里,你必须执行另一个操作,称为确认支付意图之前你已经创建,显然它有所有的信息,所以你只需要指定支付方式,验证和确认支付意图(之前确认条纹提供选项修改支付意图)。

从后端

Stripe.apiKey = "your_stripe_secrete_key";

PaymentIntent paymentIntent =
PaymentIntent.retrieve(
"payment_intent_id"
);
Map<String, Object> params = new HashMap<>();
params.put("payment_method", "pm_card_visa");
PaymentIntent updatedPaymentIntent =
paymentIntent.confirm(params);

从前端

stripe
.confirmCardPayment('{PAYMENT_INTENT_CLIENT_SECRET}', {
payment_method: {
card: cardElement,
billing_details: {
name: 'name',
},
},
})
.then(function(result) {
// Handle result.error or result.paymentIntent
});

Stripe将使用客户端SDK和您之前从支付意图中生成的客户端秘密进行支付,因此这将尝试收集付款,请记住,如果您使用任何3D安全卡进行支付,您必须确认使用OTP或各自银行提供的任何身份验证进行支付,否则您的支付意图将在require_confirmation模式下进行。国家

参考

https://stripe.com/docs/api/payment_intents/create create_payment_intent

https://stripe.com/docs/js/payment_intents/payment_method confirm_payment_intent_payment_method

https://stripe.com/docs/api/payment_intents/confirm confirm_payment_intent

最新更新