如何从Stripe Checkout返回电子邮件?

时间:2015-04-21 22:18:53

标签: php stripe-payments

使用Stripe,我想存储他们在Checkout中提供的电子邮件中的客户电子邮件地址。不幸的是,在我的stripeEmail文件中发布charge.php会返回null

如何从结帐时退回电子邮件,以便我可以用它来发送收据?

这是我的表单代码:

<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<form action="charge.php" method="post">
  <input type="hidden" id="amount" name="chargeAmount"/>
  <button data-charge-amount="300000" data-charge-name="Name" data-charge-description="Description">Select Pledge Level</button>
  <button data-charge-amount="123123" data-charge-name="Name2" data-charge-description="Description2">Donate</button>
</form>
<script>
  $('button').click(function(){
    var token = function(res){
      var $theToken = $('<input type=hidden name=stripeToken />').val(res.id);
      $('form').append($theToken).submit();
    };
    var amount = $(this).data("chargeAmount");
    var name = $(this).data("chargeName");
    var description = $(this).data("chargeDescription");
    $('input#amount').val(amount);
    StripeCheckout.open({
      key:         'pk_test_xxxxxxxxxxxxxxxxxxxxxxxx',
      address:     true,
      amount:      amount,
      currency:    'usd',
      name:        name,
      description: description,
      panelLabel:  'Pledge',
      token:       token,
    });
    return false;
  });
</script>

这是我的charge.php代码:

<?php
require_once('./config.php');
$token  = $_POST['stripeToken'];
$amount = $_POST['chargeAmount'];
$customer = \Stripe\Customer::create(array(
    'email' => $email,
    'card'  => $token,
));
$charge = \Stripe\Charge::create(array(
  'customer' => $customer->id,
  'amount'   => $amount,
  'currency' => 'usd',
));
?> 

这是我的config.php代码:

<?php
require_once('./stripe-php-2.1.2/init.php');

$stripe = array(
  "secret_key"      => "sk_test_xxxxxxxxxxxxxxxxxxxxxxxx",
  "publishable_key" => "pk_test_xxxxxxxxxxxxxxxxxxxxxxxx"
);

\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

非常感谢任何帮助。

谢谢!

3 个答案:

答案 0 :(得分:2)

这里的问题是您使用的是Custom Checkout,这意味着Checkout不会自动将数据发布到您的服务器,而是在token回调中提供。在您的情况下,您只是在这里检索令牌ID,这就是您没有看到电子邮件的原因。

更新您的代码,以便token回调还检索电子邮件并将其发送到stripeEmail参数中:

var token = function(res){
  var $theToken = $('<input type="hidden" name="stripeToken" />').val(res.id);
  var $theEmail = $('<input type="hidden" name="stripeEmail" />').val(res.email);
  $('form').append($theToken).append($theEmail).submit();
};

答案 1 :(得分:2)

我只是花了一整夜才解决这个问题! @Koopajah帮助了很多,所以这是我的整个解决方案,以防其他任何人碰巧碰到这个。

以下是表格:

    <form action="/charge.php" method="post">
      <input 
            type="submit"
            id="payMe"          
            class="btn btn-default btn-lg btn-success"
            value="&nbsp;&nbsp; Pay &nbsp;&nbsp;"
            data-key="xxxxxxx"
            data-amount="199"
            data-currency="usd"
            data-name="Stuff"
            data-description="20 Whozits ($19.99)"
    data-image="images/image.jpg"
    data-bitcoin="true"
        /> 
        <script src="https://checkout.stripe.com/v2/checkout.js"></script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
        <script>
        $(document).ready(function() {
            $('#payMe').on('click', function(event) {
                event.preventDefault();

                var $button = $(this),
                    $form = $button.parents('form');

                var opts = $.extend({}, $button.data(), {
                    token: function(result) {

                        var $theToken = $('<input>').attr({ type: 'hidden', name: 'stripeToken', value: result.id })
                        var $theEmail = $('<input>').attr({ type: 'hidden', name: 'stripeEmail', value: result.email })
                        $form.append($theToken).append($theEmail).submit();
                    }
                });

                StripeCheckout.open(opts);
            });
        });
        </script>
</form>

这是charge.php:

<?php
require_once('vendor/autoload.php');

$stripe = array(
  "secret_key"      => "xxxx",
  "publishable_key" => "xxxx"
);

\Stripe\Stripe::setApiKey($stripe['secret_key']);
$token = $_POST['stripeToken'];
$email = $_POST['stripeEmail'];

 \Stripe\Customer::create(array(
      "source"  => $token,
      "email" => $email,
      "description" => "It Worked!"
  ));

  try {
$charge = \Stripe\Charge::create(array(
  "amount" => 199, // amount in cents, again
  "currency" => "usd",
  "source" => $_POST['stripeToken'],
  "description" => "Cat Facts"));
} catch(\Stripe\Error\Card $e) {
  $error = $e->getMessage();
}
?>

答案 2 :(得分:0)

我遇到了一个非常类似的问题,但我使用的是node.js.我修改了koopajah的答案并将其放在charge.js文件中

 const token = req.body.stripeToken;<br>
 const email = req.body.stripeEmail;

然后我就像这样使用这个电子邮件变量......

return stripe.charges.create({
// ensures we send a number, and not a string
amount: parseInt(process.env.STRIPE_COST, 10),
currency: process.env.STRIPE_CCY,
source: token,
description: 'My product', //  remember to change this!
receipt_email: email, // that line sends a receipt email to the customer, you can customise that email inside stripe
metadata: {},
});

我希望这对某人有用。