【问题标题】:Redirecting Stripe to Confirmation Page After Charge收费后将 Stripe 重定向到确认页面
【发布时间】:2017-09-08 12:28:15
【问题描述】:

我无法在条带结帐后重定向到确认页面。事情与下面的代码不同。如果我删除标题部分,一切都会正常工作。无论哪种方式,费用都会发布,但无论如何,所有内容都保留在结帐页面上。我是不是做错了什么?

我找到了a SO answer by one of their developers,但不幸的是它不起作用。奇怪的是,Stripe for PHP 根本没有记录该操作,并且支持令人愤怒地给了我与标题重定向相同的确切答案。

PHP

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

// Variables passed by Ajax request. Shipping address, selection, and message attached via metadata to Stripe Tx.
  $token  = $_POST['token'];
  $price = $_POST['price'];
  $email = $_POST['email'];
  $metadata = $_POST['metadata'];


// Post charge to Stripe with the variables.
try {
 $customer = \Stripe\Customer::create(array(
      'email'    =>  $email,
      'source'   => $token

  ));

  $charge = \Stripe\Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $price,
      'currency' => 'usd',
      'metadata' => array("metadata" => $metadata)    
  ));

//Ajax hears and prints in console.log that Tx successful. Then page redirects.
echo  'success';

header('Location: /success.php');

} catch(Stripe\CardError $e) {
  // When the card declines.
    echo $token;

}


?>

JS

var handler = StripeCheckout.configure({
    key: 'pk_test_00000000',
    image: '/logo.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`
        console.log(token)
        $.ajax({
            url: '/charge.php',
            type: 'POST',
            data: { token: token.id, email: token.email, price: price, metadata: metadata },

            success: function(data) {
                if (data == 'success') {
                    console.log("Things went well for once, success!");
                    console.log(metadata);
                } else {
                    console.log("Charge Error");
                    console.log(metadata);

                }

            },
            error: function(data) {
                console.log("Ajax Error");
                console.log(data);
            }
        }); // end ajax call
    }
});

【问题讨论】:

    标签: php stripe-payments


    【解决方案1】:

    将内容输出到 DOM 后,您无法使用 header() 更改位置:

    请记住,header() 必须在发送任何实际输出之前调用,无论是通过普通 HTML 标记、文件中的空白行还是通过 PHP。

    删除echo 'success' 将解决问题。

    【讨论】:

    • 那我如何将成功的消息传回给 Ajax?我现在收到一个错误,因为它看不到成功。我想它可以从成功页面的 PHP 触发,但这超出了我的深度。我只是希望只有 PHP 的这个页面。 :/ 当我找到答案时,我会更新这个。
    • 我看到了额外的问题,您有两个选择。您无需检查data 是否成功;你只需要检查data。一个简单的if (data) {} 将涵盖这一点。此外,如果您只通过 AJAX 发布到它,则不需要需要charge.php 重定向。完全删除header() 调用,并通过window.location.href = '/success.php' 在JavaScript 端重定向。
    • 谢谢!经过一段时间的思考,PHP 重定向是不必要的,但我仍然不确定这是否是最好的方法。我已经将这个问题传递给了 Stripe,希望他们能够更新他们的文档以使其更加清晰。在服务器端发布令牌的抽象是必要的,但我只处于可以制作基本模板的 PHP 水平。所有这些 JS>JQuery>Ajax>PHP 并返回 PHP 都不是没有代码可以参考的,我只是在这个问题上跑了,因为没有简单的答案。
    猜你喜欢
    • 1970-01-01
    • 2016-04-17
    • 2012-12-30
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2021-01-13
    相关资源
    最近更新 更多