gpt4 book ai didi

javascript - Stripe 结帐自定义按钮不充电

转载 作者:可可西里 更新时间:2023-11-01 13:11:10 24 4
gpt4 key购买 nike

我试图让 Stripe 的结帐自定义按钮对信用卡收费,但它所做的只是在我输入信用卡详细信息后最小化。我正在使用文档中的代码,但我无法让它工作。简单的按钮易于使用,我认为它就像自定义那个按钮一样简单,但它非常不同。

代码如下:

支付页面

<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.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>';
?>

配置页面(config.php)

<?php
require_once('./lib/Stripe.php');

$stripe = array(
secret_key => 'sk_test_************************',
publishable_key => 'pk_test_************************'
);

Stripe::setApiKey($stripe['secret_key']);
?>

我在这里错过了什么?我什至无法取回 token 。

如何取回 token 并为卡充值?

最佳答案

终于搞定了。不得不换一堆。这是代码:

支付页面

<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/checkout.js"></script>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<button id="customButton">Pay with Card</button>
<style>
#customButton{
width:300px;
height:50px;
background-color:orange;
color:white;
border:2px solid green;
}
</style>
<script>
$('#customButton').click(function(){
var token = function(res){
var $input = $('<input type=hidden name=stripeToken />').val(res.id);
$('form').append($input).submit();
};

StripeCheckout.open({
key: '<?php echo $stripe['publishable_key']; ?>',
address: false,
amount: '<?php echo $price; ?>',
currency: 'usd',
name: 'test',
description: '<?php echo $desc; ?>',
panelLabel: 'Checkout',
token: token
});

return false;
});
</script>
<input type="hidden" name="desc" value="<?php echo $desc; ?>"/>
<input type="hidden" name="totalPrice" value="<?php echo $price; ?>"/>
</form>

charge.php

<?php
require_once(dirname(__FILE__) . '/config.php');

$token = $_POST['stripeToken'];
$amount = $_POST['totalPrice'];
$desc = $_POST['desc'];
$percent = "0.01";
$realAmount = $amount * $percent;
try {
$charge = Stripe_Charge::create(array(
'card' => $token,
'amount' => $amount,
'currency' => 'usd',
'description' => $desc
));

} catch(Stripe_CardError $e) {
// The card has been declined
}

echo "<h1>Successfully charged $$realAmount!</h1>";
?>

我希望 Stripe 的文档更简单明了,但这段代码会处理费用并将其记录在您的仪表板上。

关于javascript - Stripe 结帐自定义按钮不充电,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22546050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com