编程语言
首页 > 编程语言> > PHP-如何从Stripe Checkout返回电子邮件?

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

作者:互联网

我想使用Stripe存储他们在Checkout中提供的电子邮件中的客户电子邮件地址.不幸的是,在我的charge.php文件中发布stripeEmail返回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']);
?>

任何帮助将非常感激.

谢谢!

解决方法:

这里的问题是您使用的是Custom Checkout,这意味着Checkout不会自动将数据发布到服务器,而是在令牌回调中提供数据.就您而言,您仅在此处检索令牌ID,这就是为什么您没有看到电子邮件的原因.

更新您的代码,以便令牌回调也可以检索电子邮件并将其通过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();
};

标签:stripe-payments,php
来源: https://codeday.me/bug/20191028/1952399.html