Stripe Checkout为卡充电

时间:2016-03-06 22:42:32

标签: php jquery stripe-payments

我有这个很好的代码。

<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> 

它在文件中说明: &#34;在您的服务器上,获取表单提交的POST参数中的Stripe标记。从那里,它是一个简单的API调用来充电卡:&#34; 我正在尝试收取卡信息。 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>方法帖子或<form>操作,我如何使用以下代码为卡充电?它使用javascript来捕获令牌。但我不知道如何将该令牌发送到charge.php文件......基本上我如何获取该条带令牌并启动API调用?如何启动API调用....?

3 个答案:

答案 0 :(得分:2)

您的服务器上应该有一个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
}
?>

答案 1 :(得分:0)

就像你说的那样,你需要生成一个Stripe令牌并做一个表单帖子。没有有效的令牌,您无法为卡充电。

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

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

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

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

答案 2 :(得分:0)

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