【问题标题】:Charging with Stripe用条纹充电
【发布时间】:2018-08-23 15:14:04
【问题描述】:

我在让 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)


 ]);; ?>

【问题讨论】:

  • 您能稍微澄清一下确切的问题是什么吗?只是没有创建费用吗?如果您查看dashboard.stripe.com/test/logs,您是否看到任何错误或收费请求?
  • @karllekko 是的 - 尚未创建。那里没有错误。也没有收费要求。只有 v1 令牌
  • 您是否在浏览器的开发人员工具中看到对charge2.php 的请求?如果您从脚本中print_r($_POST),您会看到什么?
  • 当我放置并加载 PHP 文件时,我看到了“1Array ( )”。我在我的开发人员工具中看到正在请求 change2.php 并返回 500 错误。据我所知,没有语法错误......
  • 哦,我刚刚注意到 - 您在 PHP 脚本中将您的公钥传递给 setApiKey。它应该是你的密钥(sk_test_xxx)。

标签: javascript php stripe-payments


【解决方案1】:

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

使用错误的密钥会导致 API 错误并显示 403 响应代码。您可以在您的 Stripe dashboard logs 中看到这一点。您可能需要在测试和实时模式之间切换(使用左侧导航栏中的开关)才能看到相关请求。

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

【讨论】:

    猜你喜欢
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2015-12-18
    • 2021-12-20
    • 2019-04-03
    • 1970-01-01
    • 2021-10-20
    相关资源
    最近更新 更多