充电后将条纹重定向到确认页面

时间:2017-09-08 04:04:23

标签: php stripe-payments

条纹结帐后,我无法重定向到确认页面。事情打破了下面的代码。如果我删除标题部分,事情可以正常工作。收费可以发布,无论如何,一切都会留在收银页面。我做了一件明显不对的事吗?

我找到了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
    }
});

1 个答案:

答案 0 :(得分:0)

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

  

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

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