【问题标题】:Stripe Checkout charging a cardStripe Checkout 向卡收费
【发布时间】:2016-03-06 22:42:31
【问题描述】:

我有这段代码很好用。

<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 参数中的 Stripe 令牌。从那里,这是一个简单的 API 调用来为卡收费:” 我正在尝试 CHARGE 卡信息。 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
}

我的问题:我如何在没有&lt;form&gt; 方法帖子或&lt;form&gt; 操作的情况下使用以下代码对卡进行收费...?它使用 javascript 来捕获令牌。但我不知道如何将该令牌发送到 charge.php 文件...基本上我如何获取该条带令牌并启动 API 调用?如何启动 API 调用....?

【问题讨论】:

    标签: php jquery stripe-payments


    【解决方案1】:

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

    【讨论】:

    • 但是我在哪里运行这个脚本呢?就在
    • @JonathanSafa charge.php 是一个单独的文件。该脚本将在您的 token:function(token) 中。
    • 哦,这更有意义。但是,现在,我的 charge.php 文件中存在什么?
    • @JonathanSafa 你是什么意思,“存在”?不确定是否理解您的问题。
    • 对不起,我在“charge.php”文件中放了什么脚本?
    【解决方案2】:

    就像你说的,你需要生成一个 Stripe 令牌并做一个表单发布。如果没有有效的令牌,您将无法从卡中收取费用。

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

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

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

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

    【讨论】:

    • 问题是将 Checkout 与自定义集成一起使用,以便我可以拥有自己的自定义按钮。我已经开始使用这些基本的了。
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2015-07-23
      • 1970-01-01
      • 2016-01-23
      • 1970-01-01
      • 2021-05-29
      • 2017-03-12
      • 1970-01-01
      • 1970-01-01
      • 2012-04-19
      相关资源
      最近更新 更多