Stripe Checkout 为卡充电

Stripe Checkout charging a card

我有这段代码,效果很好。

<input class="form-control"
   type="number"
   id="custom-donation-amount"
   placeholder="50.00"
   min="0"
   value="50"
   step="10.00"/>
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton" class="pay-button"> 
    <h4 class="donate-text button">Donate by
    <img src="http://#/testing/credit-card.png" ></h4>
</button>

<script>
  var handler = StripeCheckout.configure({
key: 'pk_test_mytestingkey',
image: '',
locale: 'auto',
token: function(token) {
  // Use the token to create the charge with a server-side script.
  // You can access the token ID with `token.id`
}
  });

 $('#customButton').on('click', function(e) {
// Open Checkout with further options
 var amount = $("#custom-donation-amount").val() * 100;
handler.open({
  name: 'Testing',
  description: 'Testing',
  amount: amount
});
e.preventDefault();
});

 // Close Checkout on page navigation
 $(window).on('popstate', function() {
    handler.close();
  });
 </script> 

它在文档中指出: "On your server, grab the Stripe token in the POST parameters submitted by your form. From there, it's one simple API call to charge the card:" 我正在尝试收取卡信息。 Stripe 提供以下 API 调用来执行此操作:我假设这是一个 charge.php 文件?

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

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create the charge on Stripe's servers - this will charge the user's card
try {
  $charge = \Stripe\Charge::create(array(
    "amount" => 1000, // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge"
    ));
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}

我的问题:如何在没有 <form> 方法 post 或 <form> 操作的情况下使用以下代码为卡充电...?它使用 javascript 来捕获令牌。但我不知道如何将该令牌发送到 charge.php 文件...基本上我如何获取该条带令牌并启动 API 调用?我如何发起 API 呼叫....?

就像你说的,你需要生成一个 Stripe 令牌并做一个表单 post。没有有效的令牌,您将无法为卡充值。

这里是关于如何使用 Stripe 创建自定义表单的指南,包括如何生成令牌:

https://stripe.com/docs/custom-form

一种更简单的方法是简单地嵌入他们的即用型结帐:

https://stripe.com/docs/checkout/tutorial

您的服务器上应该有一个 charge.php 文件并使用 $.post 将令牌发送到 php。

// make sure you have the right path to charge.php
// pass the token to the php file with POST, your php is expecting $_POST['stripeToken']
    token: function(token) {
        // Use the token to create the charge with a server-side script.
        // You can access the token ID with `token.id
        $.post( "charge.php", { stripeToken: token.id})
           // check if it worked
          .done(function( data ) {
            console.log( "Card charged: " + data );
          });
    }

你的 charge.php 文件,确保你安装了 Stripe PHP library

<?
// composer require stripe/stripe-php
require 'vendor/autoload.php';
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_mykey");

// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];

// Create the charge on Stripe's servers - this will charge the user's card
try {
  $charge = \Stripe\Charge::create(array(
    "amount" => 1000, // amount in cents, again
    "currency" => "usd",
    "source" => $token,
    "description" => "Example charge"
    ));
} catch(\Stripe\Error\Card $e) {
  // The card has been declined
}
?>

如果您正在使用 mamp。 UPDATE TO MAMP 4 stripes API 不再支持旧版本的 MAMP。