ala*_*udi 20 javascript php stripe-payments
我一直在条带仪表板上收到错误代码400.看起来我不止一次使用相同的条带令牌,这会产生错误.以下是我的代码.
JS:
<script src="https://checkout.stripe.com/checkout.js"></script>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_******************',
image: '/img/documentation/checkout/marketplace.png',
token: function(token) {
/*$.post("php/charge.php",{stripeToken:token.id},function(data,status){
console.log("Data: "+ data+"\nStatus: "+status);
});*/
alert(token.used);//alerts false
$.post("php/charge.php",{stripeToken:token.id});
alert(token.used);// still alerts false
}
});
$('#myButton').on('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Demo Site',
description: '2 widgets',
currency: "cad",
amount: 2000
});
e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function() {
handler.close();
});
</script>
Run Code Online (Sandbox Code Playgroud)
PHP的:
<?php
require_once('config.php');
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => 'test@test.com',
'card' => $token
));
//try {
$charge = \Stripe\Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "cad",
"source" => $token,
"description" => "Example charge")
);
//}catch(\Stripe\Error\Card $e) {
// The card has been declined
//}
?>
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉我为什么我不能向客户收费?我怎么多次使用密钥?
Tus*_*har 48
您确实使用了两次令牌.
首先,在创建客户时.第二,试图给卡充电时.
相反,您可以创建客户,然后$customer->id
在创建费用时传递给Stripe:
$charge = \Stripe\Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "cad",
"customer" => $customer->id,
"description" => "Example charge")
);
Run Code Online (Sandbox Code Playgroud)
您必须创建客户以多次向他收费。
1)将信用卡令牌添加到客户并创建客户
2)使用客户ID向用户收费
if (isset($_POST['stripeToken'])){
$token = $_POST['stripeToken'];
// Create a Customer
$customer = \Stripe\Customer::create(array(
"source" => $token,
"description" => "Example customer")
);
Run Code Online (Sandbox Code Playgroud)
// Charge the Customer instead of the card
\Stripe\Charge::create(array(
"amount" => 1000, # amount in cents, again
"currency" => "usd",
"customer" => $customer->id)
);
}
Run Code Online (Sandbox Code Playgroud)
如需更多帮助,请访问:https : //stripe.com/docs/tutorials/charges