我试图让Stripe的Checkout自定义按钮为信用卡收费,但在输入信用卡详细信息后,它所做的就是最小化 . 我正在使用文档中的代码但我无法使其工作 . 简单的按钮很容易使用,我认为它就像定制那个简单,但它是非常不同的 .
这是代码:
Payment Page
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/checkout.js"></script>
<input type="submit" value="Pay with card" id="customButton"/>
<?php require_once('./config.php'); ?>
<script>
var handler = StripeCheckout.configure({
key: '<?php echo $stripe['publishable_key']; ?>',
image: 'favicon.png',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Imprintnation',
description: 'Decals',
amount: <?php echo $stripeTotal; ?>
});
e.preventDefault();
});
</script>
</form>
Charge Page(charge.php)
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$customer = Stripe_Customer::create(array(
'email' => 'customer@example.com',
'card' => $token
));
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $5!</h1>';
?>
Configuration Page(config.php)
<?php
require_once('./lib/Stripe.php');
$stripe = array(
secret_key => 'sk_test_************************',
publishable_key => 'pk_test_************************'
);
Stripe::setApiKey($stripe['secret_key']);
?>
我在这里想念的是什么?我甚至无法获取它来检索令牌 .
How can I retrieve a token and charge a card?
2 回答
终于搞定了 . 不得不改变一堆 . 这是代码:
Payment page
charge.php
我希望Stripe的文档更直接,但是这段代码处理费用并将其记录在仪表板上 .
@BlueSix是对的,为什么你有:
<input type="hidden" name="totalPrice" value="<?php echo $price; ?>"/>
仅仅因为隐藏了这个值,并不意味着最终用户无法编辑它 .
最好直接设置
$amount = $price
,通过应用程序后端传递变量 .