javascript – 如何使用通过AJAX发送到php的结帐令牌创建条带费用

我试图用自定义按钮使用Stripe的新结帐功能,通过 AJAX POST发送令牌到PHP文件,然后执行费用.不幸的是,我从POST变量中检索到令牌有一些麻烦.我希望这里的某个人能够告诉我我过于简单,如果有一个更简单的方法来做到这一点.

在客户端,我有5个按钮,不同的可能“捐款”.这是迄今为止写的js(不包括html):

$(function() {

  var donationAmt = '';
  var handler = StripeCheckout.configure({
    key: 'pk_test_3plF76arhkygGMgwCEerThpa',image: '/square-image.png',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)
      var chargeData = {
        donationAmt: donationAmt,token: token
      }
      $.ajax({
          url: '/link/to/PHP/stripeDonate.PHP',type: 'post',data: {chargeData: chargeData},success: function(data) {
            if (data == 'success') {
                console.log("Card successfully charged!")
            }
            else {
                console.log("Success Error!")
            }

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

  $('.donate-button').bind('click',function(e) {
    donationAmt = $(this).html().substring(1) + '00';
    donationAmt = parseInt(donationAmt); // Grabs the donation amount in the html of the button and store it in a variable
    // Open Checkout with further options
    handler.open({
      name: 'Company Name',description: 'A donation',amount: donationAmt
    });
    e.preventDefault();
  });
});

这是我正在处理AJAX POST调用PHP

<?PHP

require_once('Stripe.PHP');

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account
Stripe::setApiKey("sk_test_APIKEYREDACTED");

// Get the credit card details submitted by the form
$token = json_decode($_POST['chargeData']);
$tokenid = $token['id'];

// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = Stripe_Charge::create(array(
  "amount" => 2000,// amount in cents,again
  "currency" => "usd","card" => $tokenid,"description" => "payinguser@example.com")
);
echo 'success';
} catch(Stripe_CardError $e) {
  // The card has been declined
    echo $tokenid;
}

?>

PHP错误日志中指出的代码的直接结果是令牌的POST变量不能被“读取”.令牌正在被创建(我正在控制台上看到它),但是当我通过AJAX发送时它消失了.

每个人都一直在说条纹是非常容易实现的,所以我真的觉得我在这里缺少一些东西.有人能够散发光吗?

谢谢!

解决方法

所以经过10个小时的休息和一个更清晰的头,我已经决定以稍微不同的方式解决这个问题.这是任何其他人绊倒我相同的问题,并希望能够像条纹/ ajax / PHP教程一样很好地工作.原来我一直在想POST数据都错了.即使使用AJAX,您也需要一个键和值对来发送任何类型的POST数据.我已经重新编码了我的这个部分:
var handler = StripeCheckout.configure({
    key: 'PUBLISHABLEKEY',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: 'link/to/PHP/stripeDonate.PHP',data: {tokenid: token.id,email: token.email,donationAmt: donationAmt},success: function(data) {
            if (data == 'success') {
                console.log("Card successfully charged!");
            }
            else {
                console.log("Success Error!");
            }

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

请注意,一个主要更改是ajax方法的数据属性.控制台记录令牌对象显示整个JSON令牌对象,您可以使用它来提取ID(您的服务器需要发送到条带以收取费用)以及电子邮件(用于记录目的).由于我有一个可变的捐赠金额,我已经把它作为第三个关键.

现在在你的PHP中,为了获取这些POST变量并把它们放入PHP变量中,你可以使用它们各自的键来获取它们:

$tokenid = $_POST['tokenid'];
$donation = $_POST['donationAmt'];
$email = $_POST['email'];

然后一切都应该是自我解释的(以下几乎是与条纹PHP教程完全相同的例子).

无论如何,希望这有助于一个人在那里.祝你好运!

相关文章

IE6是一个非常老旧的网页浏览器,虽然现在很少人再使用它,但...
PHP中的count()函数是用来计算数组或容器中元素的个数。这个...
使用 AJAX(Asynchronous JavaScript and XML)技术可以在不...
Ajax(Asynchronous JavaScript and XML)是一种用于改进网页...
本文将介绍如何通过AJAX下载Excel文件流。通过AJAX,我们可以...
Ajax是一种用于客户端和服务器之间的异步通信技术。通过Ajax...