【发布时间】:2016-03-06 22:42:31
【问题描述】:
我有这段代码很好用。
<input class="form-control"
type="number"
id="custom-donation-amount"
placeholder="50.00"
min="0"
value="50"
step="10.00"/>
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton" class="pay-button">
<h4 class="donate-text button">Donate by
<img src="http://#/testing/credit-card.png" ></h4>
</button>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_mytestingkey',
image: '',
locale: 'auto',
token: function(token) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
$('#customButton').on('click', function(e) {
// Open Checkout with further options
var amount = $("#custom-donation-amount").val() * 100;
handler.open({
name: 'Testing',
description: 'Testing',
amount: amount
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
它在文档中指出: “在您的服务器上,获取表单提交的 POST 参数中的 Stripe 令牌。从那里,这是一个简单的 API 调用来为卡收费:” 我正在尝试 CHARGE 卡信息。 Stripe 提供了以下 API 调用来做到这一点:我假设这是一个 charge.php 文件?
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_mykey");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "usd",
"source" => $token,
"description" => "Example charge"
));
} catch(\Stripe\Error\Card $e) {
// The card has been declined
}
我的问题:我如何在没有<form> 方法帖子或<form> 操作的情况下使用以下代码对卡进行收费...?它使用 javascript 来捕获令牌。但我不知道如何将该令牌发送到 charge.php 文件...基本上我如何获取该条带令牌并启动 API 调用?如何启动 API 调用....?
【问题讨论】:
标签: php jquery stripe-payments