首页 > 解决方案 > 用条纹充电

问题描述

我在使用 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>

收费2.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)


 ]);; ?>

标签: javascriptphpstripe-payments

解决方案


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

使用错误的密钥应该会给您一个带有 403 响应代码的 API 错误。您可以在 Stripe仪表板日志中看到这一点。您可能需要在测试和实时模式之间切换(使用左侧导航栏中的开关)才能看到相关请求。

此外,在代码中,print_r语句在\Stripe\Charge::create调用的参数内,这也会给你一个错误——相反,它应该在外面,很可能就setApiKey在行之后。


推荐阅读