【问题标题】:Stripe but won´t charge using Ajax to post tokenStripe,但不会收取使用 Ajax 发布令牌的费用
【发布时间】:2015-05-08 01:50:35
【问题描述】:

我有以下相当基本的 Stripe 实现。它成功创建了一个新客户,但它不会收费并在控制台中返回“SUCCESS ERROR”消息。令牌似乎是正确创建的,所以我的猜测是我在charge.php 中遗漏了一些东西。

注意:我没有使用 composer 来安装 stripe,而是在 config.php 中加载了 init.php(这可能是问题吗?)。即使我使用 ajax 也是必要的吗?(尽管删除它不会解决问题)

索引.php

<?php require_once('stripe/config.php'); ?>
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/checkout.js"></script>
<button id="customButton">Purchase</button>
<script>
var handler = StripeCheckout.configure({
key: 'pk_test_*******************',
token: function(token) {
  // 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: 'stripe/charge.php',
      type: 'post',
      data: {tokenid: token.id, email: token.email},
      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
    }
  });

  $('#customButton').on('click', function(e) {
    // Open Checkout with further options
    handler.open({
      name: 'shipping free',
      description: '2 widgets',
      amount: 2000
    });
e.preventDefault();
  });

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

CONFIG.PHP

<?php require_once('init.php');

$stripe = array(
secret_key      => 'sk_test_************************',
publishable_key => 'pk_test_************************'
);

Stripe::setApiKey($stripe['secret_key']);
?>

CHARGE.PHP

<?php require_once('/config.php');

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

  $customer = \Stripe\Customer::create(array(
      'email' => $email,
      'card'  => $tokenid
  ));

  $charge = \Stripe\Charge::create(array(
      'email' => $email,
      'card'  => $tokenid
      'amount'   => 5000,
      'currency' => 'usd',

  ));

  echo '<h1>Successfully charged $50.00!</h1>';
?>

【问题讨论】:

    标签: php ajax


    【解决方案1】:

    在这一行中,'card' => $tokenid。您缺少逗号(,)。

    在该行中添加逗号。

    $charge = \Stripe\Charge::create(array(
          'email' => $email,
          'card'  => $tokenid,
          'amount'   => 5000,
          'currency' => 'usd',
    
      ));
    

    【讨论】:

      猜你喜欢
      • 2016-10-15
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 2021-10-31
      • 2021-03-27
      • 2017-04-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多