首页 文章

Stripe Checkout为卡充电

提问于
浏览
1

我有这个很好的代码 .

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

它在文档中说明:“在您的服务器上,抓取表单提交的POST参数中的条带标记 . 从那里,这是一个简单的API调用来为卡充电:”我正在尝试收取卡信息 . 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> 动作,我如何使用以下代码为卡充电?它's using javascript to capture the token. But I don'知道如何将该令牌发送到charge.php文件......基本上我如何获取该条带令牌并启动API调用?如何启动API调用....?

3 回答

  • 0

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

    就像你说的,你需要生成一个Stripe令牌并做一个表单发布 . 没有有效的令牌,您无法为卡充电 .

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

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

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

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

  • 0

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

相关问题