使用Stripe充电

时间:2018-08-23 15:14:05

标签: javascript php stripe-payments

在使用Stripe进行充电时,我遇到了一个问题。 Composer无法安装,因此我已手动加载了它。没有收到PHP错误,我的令牌创建工作正常。我看不到任何明显的错误或语法错误,有人可以指出我正确的方向吗?

谢谢

index.html代码:

<script src="https://checkout.stripe.com/checkout.js"></script>


<script>
var handler = StripeCheckout.configure({
    key: 'xxxxxxxxxx',
    image: 'https://stripe.com/img/documentation/checkout/marketplace.png',
    token: function(token) {
        /* Use the token to create the charge with a server-side script.
         You can access the token ID with `token.id`
         Pass along various parameters you get from the token response
         and your form.*/
        var myData = {
                token: token.id,
                email: token.email,
                amount: 500,
                message: $("#message").val()
        };
        /* Make an AJAX post request using JQuery,
           change the first parameter to your charge script*/
        $.post("charge2.php", myData,function (data) {
            // if you get some results back update results
            $(".results").html("Your charge was successful");
        }).fail(function () {
            // if things fail, tell us
            $(".results").html("I'm sorry something went wrong");
        })
    }
});
document.getElementById('customButton').addEventListener('click', function(e) {
    // Open Checkout with further options
    handler.open({
        name: 'GUTIC',
        description: 'Join today!',
        amount: 500
    });
    e.preventDefault();
});
// Close Checkout on page navigation
$(window).on('popstate', function () {
    handler.close();
});


</script>

charge2.php

<?php echo // 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
 require_once('stripe-php/init.php');

 \Stripe\Stripe::setApiKey("pk_test_DapJwVUCol6JDjJ4jsqEr6S7");

 // Token is created using Checkout or Elements!
 // Get the payment token ID submitted by the form:
 $token = $_POST['token'];
 $charge = \Stripe\Charge::create([
     'amount' => 999,
     'currency' => 'usd',
     'description' => 'Example charge',
     'source' => $token,

     print_r($_POST)


 ]);; ?>

1 个答案:

答案 0 :(得分:0)

看起来问题在于您在对pk_test_xxx的调用中使用了可发布的密钥(\Stripe\Stripe::setApiKey)。但是,您应该在此处使用您的秘密密钥(sk_test_xxx),例如创建费用需要使用密钥。您可以在https://dashboard.stripe.com/account/apikeys

处获取密钥。

使用错误的密钥应该会给您一个API错误,并带有403响应代码。您可以在条纹dashboard logs中看到它。您可能需要在测试和实时模式之间切换(使用左侧导航栏中的开关)才能查看相关请求。

此外,在代码中,print_r语句位于\Stripe\Charge::create调用的参数内,这也会给您带来错误-相反,它应位于外部,很可能位于{{ 1}}行。