我正在从 Changes API 迁移到 PaymentIntent API。我成功设置代码。
但我很想知道,每次我加载页面条带时,都会在条带仪表板上创建一个显示为“未完成”付款状态的付款意图,并且在单击所有详细信息的付款按钮后,此状态变为“成功”状态。
PHP 代码
$customer = \Stripe\Customer::create(array(
'email' => $_SESSION['userEmail']
));
$intent = \Stripe\PaymentIntent::create([ 'amount' => $varTotalPrice, 'currency' => 'eur', 'customer' => $customer->id, 'payment_method_types' => ['card'], 'description' => $arrCreditResult['creditTitle'] ]);
如您所知,这为我提供了在 js 脚本中使用的client_secret密钥。
JS代码
<script type="text/javascript">
var stripe = Stripe('<?php echo $pubkey; ?>');
var elements = stripe.elements();
var payBtnHtml = document.getElementById("submit").innerHTML;
var card = elements.create('card', {
style: {
base: {
iconColor: '#666EE8',
color: '#31325F',
lineHeight: '40px',
fontWeight: 600,
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '15px',
'::placeholder': {
color: '#31325F',
fontWeight:300,
fontFamily: '"Helvetica Neue", Helvetica, sans-serif',
fontSize: '15px'
}
}
}
});
card.mount('#card-element');
var cardholderName = document.querySelector('input[name=cardholder-name]');
var form = document.getElementById('payment-form');
var clientSecret = document.getElementById('payment-form').getAttribute("data-secret");
card.on('change', function(event) {
var displayError = document.getElementById('card-errors');
if (event.error) {
displayError.textContent = event.error.message;
} else {
displayError.textContent = '';
}
});
var form = document.getElementById('payment-form');
form.addEventListener('submit', function(ev) {
ev.preventDefault();
document.getElementById("submit").disabled = true;
document.getElementById("submit").innerHTML = WAIT;
stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: card,
billing_details: {
name: cardholderName.value
}
}
}).then(function(result) {
if (result.error) {
// Show error to your customer (e.g., insufficient funds)
document.getElementById("submit").disabled = false;
console.log(result.error.message);
} else {
// The payment has been processed!
if (result.paymentIntent.status === 'succeeded') {
document.getElementById("submit").disabled = false;
document.getElementById("payBtn").innerHTML=payBtnHtml
alert("paymemt done");
debug(result);
return false;
// Show a success message to your customer
// There's a risk of the customer closing the window before callback
// execution. Set up a webhook or plugin to listen for the
// payment_intent.succeeded event that handles any business critical
// post-payment actions.
}
}
});
});
</script>
我只想在用户点击支付按钮时创建条带支付。与收费 API 相同。