使用 JavaScript 创建条纹收费



我在下面有一段工作代码,我想知道是否可以在结帐过程的"成功令牌"中进行条纹收费?下面的所有代码都在工作,我只是想知道我会在哪里插入创建累积费用的代码。

<script src="https://checkout.stripe.com/checkout.js"></script>
<script src="https://js.stripe.com/v3/"</script>
<button id="payment-button" type="button" class="btn btn-success">Pay With Card</button>
<script>
var handler = StripeCheckout.configure({
key: 'mykeyhere',
image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
locale: 'auto',
token: function(token) {
// You can access the token ID with `token.id`.
// Get the token ID to your server-side code for use.
document.getElementById("book-appointment-submit").style.display="block";
document.getElementById("payment-button").style.display="none";
}
});
document.getElementById('payment-button').addEventListener('click', function(e) {
// Open Checkout with further options:
handler.open({
name: 'DenchCodeLTD',
description: '2 widgets',
zipCode: true,
currency: 'gbp',
amount: 2000
});
e.preventDefault();
});
// Close Checkout on page navigation:
window.addEventListener('popstate', function() {
handler.close();
});
</script>
<!-- End Of Stripe Payment -->
<button id="book-appointment-submit" type="button" class="btn btn-success" style="display:none">
<span class="glyphicon glyphicon-ok"></span>
<?php
echo (!$manage_mode) ? $this->lang->line('confirm')
: $this->lang->line('update');
?>
</button>

你不能在JS本身中收费。向客户收取 Stripe 费用的过程分为两步。

第 1 步:您使用 Checkout 收集客户的信用卡信息,然后将其发送到 Stripe。你得到一个令牌。这就是你在上面的代码中所做的。

第 2 步:您必须获取此令牌并将其传递给您的后端,然后告诉 Stripe 用它收费,或将其保存给客户以便稍后收费。您需要在服务器上(PHP,Ruby等(上编写脚本来处理此步骤。

第二步的一些简短示例如下: https://stripe.com/docs/charges

在您的token: function(token) { }回调中,您至少需要获取token.id,将其附加到结帐按钮周围并.submit()<form></form>

如果需要更复杂的内容,可以通过 AJAX 请求将令牌 ID 传递到后端,然后将收费请求的成功或失败中继回用户。

https://stripe.com/docs/checkout#integration-custom

最新更新