完全被Stripe Checkout卡住了


Completely stuck with Stripe Checkout

目标:屏幕上有几个按钮,比如so–

[产品1–20美元]

[产品2–$35]

等等…

用户可以选择任意数量的产品。在JavaScript中,我有一个变量amount,当用户选择产品时,它会增加产品的成本

用户选择产品1&2-->数量=20+35=55

然后,用户可以按下"用卡支付"按钮来启动Stripe Checkout模式。填写,付款,完成。

问题:现在,我使用的是Checkout的自定义按钮版本,所以我可以按照自己的方式设置按钮的样式,并将这个变量放入。所以,实现非常简单:

       var handler = StripeCheckout.configure({
            key: 'pk_test_XXXXXX',
            image: 'assets/logo-stripe.png',
            locale: 'auto',
            token: function(token, args) {
                // Use the token to create the charge with a server-side script.
                // You can access the token ID with `token.id`
                $.ajax({
                    url: 'php/charge.php',
                    type: 'post',
                    data: {tokenid: token.id, email: token.email, amount: amount},
                    success: function(data) {
                        if (data == 'success') {
                            console.log("Card successfully charged!");
                        }
                        else if (data == 'failure') {
                            console.log("Card error");
                        } else {
                            console.log("Other error");
                        }
                    },
                    error: function(data) {
                        console.log("AJAX error!");
                        console.log(data);
                    }
                });
            }
        });
        $('#pay-button').on('click', function(e) {
            if (amount == 0) {
                $('#pay-button').addClass('animated shake'); // Animates the button and refuses to open the modal if user didn't select any products
                $('#pay-button').one('webkitAnimationEnd mozAnimationEnd MSAnimationEnd oanimationend animationend', removeAnimation);
            } else {
                handler.open({
                    name: 'Company, Inc.',
                    description: "Description",
                    amount: amount // Here's the variable
                });
                e.preventDefault();
            }
        });
        // Close Checkout on page navigation
        $(window).on('popstate', function() {
            handler.close();
        });

这就是JavaScript逻辑。我省略了对checkout.js的调用,因为这对这个问题没有任何影响。

从前端的角度来看,这是桃红色的。现在是代币化和实际充值。这就是事情开始出现问题的地方。如果你看看令牌处理,我正在尝试将电子邮件、金额和令牌数据POST到charge.php。基于我没有从控制台收到任何关于这方面的错误,我认为这是可行的。

在charge.php中,我有这个:(4月20日更新:这是工作代码)

<?php
  require_once('stripe-php/init.php');
  // Set secret key
  'Stripe'Stripe::setApiKey("sk_test_XXXXXXX");
  // Get the credit card details submitted by the form
  $token = $_POST['tokenid'];
  // Create the charge on Stripe's servers - this will charge the user's card
  try {
    $customer = 'Stripe'Customer::create(array(
      'email' => $_POST['email'],
      'source'  => $token
    ));
    $charge = 'Stripe'Charge::create(array(
      "amount" => $_POST['amount'],
      "currency" => "usd",
      "customer" => $customer->id
      ));
    echo "success";
  } catch('Stripe'Error'Card $e) {
    // The card has been declined
    echo "failure";
  }
?>

每当我尝试购买时,日志都会给我"成功错误"。我大致遵循这里提供的代码——如何使用通过AJAX发送到php的结账令牌创建条纹收费。所以,我不知道成功错误是什么,但我猜数据被发送到charge.php fine,但卡没有被收费。我的Stripe仪表板没有显示任何付款记录,这一事实支持了这一点。

我尝试过的:

1) 我以为错误可能是我的config.php。我没有使用Composer(因为我不知道那是什么,也不知道如何开始使用它),所以我从stripephp调用init.php。根据Stripe自己的文档,这是Composer的一个可接受的替代方案。

这是config.php:

<?php
  require_once('php/stripe-php/init.php');
  $stripe = array(
    secret_key      => getenv('sk_test_XXXXXX'),
      publishable_key => getenv('pk_test_XXXXXX')
  );
  'Stripe'Stripe::setApiKey($stripe['secret_key']);
?>

不再使用config.php,全部合并为charge.php

2) 在POST调用中,我传递了一个tokenid,但在charge.php中,我使用stripeToken。这可能是因为我尝试使用Stack Overflow答案和Stripe文档来制定一个完整的解决方案。我觉得奇怪的是,stripeToken没有从我的前端发送。因此,我尝试在我的charge.php中使用tokenid(用tokenid替换stripeToken。更改为tokenid不起作用,并且存在安全问题(我宁愿坚持使用兼容的stripeToken,但我不知道如何实现它)。

3) 我担心将金额变量发送到charge.php。我不希望用户在控制台中更改它,但如果我不发送,我不知道如何使用可变定价。我需要可变定价,因为用户正在选择多个不同价格的产品(请参阅:目标)。所以,这对我来说是另一个难题。

可能值得注意的是,我不得不在生产中测试这一点,甚至得到了成功错误。在localhost中,我得到一个500内部服务器错误。不知道为什么,但猜测这与缺乏PHP环境或文件结构有关。

如有任何帮助,我们将不胜感激!非常感谢。

token回调函数检查是否返回字符串"success"

                success: function(data) {
                    if (data == 'success') {

但您的charge.php文件不返回"success",它返回

echo '<h1>Successfully charged [amount]!</h1>';

echo '<h1>Charge failed</h1>';

您应该修改charge.php文件以返回前端代码所期望的数据。由于它是通过AJAX调用的,浏览器不会显示它的输出。

其他几点意见:

  • 您应该在try/catch子句中移动客户创建调用('Stripe'Customer::create(...))。当创建具有卡令牌的客户时,Stripe将检查卡是否有效,如果不是这样,则返回卡错误

  • 在客户应该自己明确选择金额的情况之外(例如接受客户指定的捐款),你不应该依赖客户浏览器发送的金额,因为更改它非常容易。