首页 文章

如何将条带令牌传递到服务器,创建用户帐户,从信用卡收费并将其全部显示在仪表板上

提问于
浏览
1

所以我试图在我的服务器上设置条带,但我遇到了问题 . 我在我的页面上使用checkout v2并且表单完美无缺,但令牌永远不会传递到我的php文件 . 该卡已收费,但信息未显示在仪表板上,但它在日志中 .

这就是我所拥有的:

<form action="assets/php/slingshot.php" method="POST">
 <button id="customButton">Purchase</button>
 <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:         'pk_live_*************************',
    address:     true,
    amount:      2500,
    currency:    'usd',
    name:        'test',
    description: 'A bag of test',
    panelLabel:  'Checkout',
    token:       token
  });

  return false;
  });
  </script>
</form>

然后为slingshot.php:

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

$token = $POST['stripeToken']; //get the creditcard details from the form
try {       
$charge = Stripe_Charge::create(array(
        'card' => $token,
        'amount'   => 2500,  //amount in cents
        'currency' => 'usd',
        'description' => 'Get Bacon Direct, LLC'
        ));

        } catch(Stripe_CardError $e) {
// The card has been declined
}
echo '<h1>Successfully charged $25.00 </h1>';
}
?>

和我的config.php文件:

<?php
require_once('stripe-php/lib/Stripe.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account

$stripe = array(
"secret_key"        => "sk_live_************************************",
"publishable_key"   => "pk_live_************************************"
);

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

?>

你能帮帮我吗?

1 回答

  • 3

    我不完全确定如何收费,但我相信你没有得到令牌的原因是因为这条线路

    $token = $POST['stripeToken']; //get the creditcard details from the form
    

    在PHP中访问POST数据时,全局变量以下划线为前缀,因此您需要这样做

    $token = $_POST['stripeToken']; //get the creditcard details from the form
    

    那应该解决它 .

    • 更新---

    为了便于阅读,在您的javascript中,我将指定输入字段,如下所示

    var $input = $('<input type="hidden" name="stripeToken" />').val(res.id);
    

    我还重新检查了条带文档,看起来卡属性是可选的,客户也是如此,但它确实声明必须提供一个 . 机会是,它不会抛出错误,因为它们的代码存在问题而无法捕获既未提供的可能性 .

相关问题