使用php

时间:2015-09-29 07:01:43

标签: php api stripe-payments

根据stripe提供的文件,我使用了以下代码

<script src="https://checkout.stripe.com/checkout.js"></script>

<button id="customButton">Purchase</button>

<script>
  var handler = StripeCheckout.configure({
    key: 'key',
    image: '/img/documentation/checkout/marketplace.png',
    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
    handler.open({
      name: 'Demo.',
      description: '2 widgets',
      currency: "gbp",
      amount: 2000
    });
    e.preventDefault();
  });

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

但是我也希望实现billingAddress和shippingAddress,这样用户也可以输入这两个地址,所以我添加了这两行

billingAddress = "true",
shippingAddress = "true",

但它没有任何效果,任何人都可以告诉一个正确的方法来使用这两个参数

2 个答案:

答案 0 :(得分:1)

我可以帮助您将第3版Stripe api与php集成

首先安装数据条依赖项composer require stripe/stripe-php:6.31

<script src="https://js.stripe.com/v3/"></script>

<?
    require_once('vendor/autoload.php');

\Stripe\Stripe::setApiKey('sk_test_XXXXXXXXXX');

$session = \Stripe\Checkout\Session::create([
  'customer_email' => 'customer@example.com',
  'payment_method_types' => ['card'],
  'line_items' => [[
    'name' => 'T-shirt',
    'description' => 'Comfortable cotton t-shirt',
    'images' => ['https://example.com/t-shirt.png'],
    'amount' => 500,
    'currency' => 'USD',
    'quantity' => 1,
  ]],
  'success_url' => 'https://example.com/success',
  'cancel_url' => 'https://example.com/cancel',
]);


$session_id = $session->id;

if ($session_id) {

    echo "<script>
          var stripe = Stripe('pk_test_XXXXXXXXXXXXX');
          stripe.redirectToCheckout({
          sessionId: '" . $session_id . "'
          }).then(function (result) {
          });
          </script>";

} else {

    echo 'No Session ID!';

}

?>


<!DOCTYPE html>
<html>
<head>
    <title>My Purchase</title>
    <script src="https://js.stripe.com/v3/"></script>

    <script>
        <!--
        function checkout(session_id) {

            var stripe = Stripe('pk_test_XXXXXXXXXXXXXXXXXX');

            stripe.redirectToCheckout({
                sessionId: '<? echo $session_id; ?>'
            }).then(function (result) {
                // If `redirectToCheckout` fails due to a browser or network
                // error, display the localized error message to your customer
                // using `result.error.message`.
            });

            }

        //-->
</script>

</head>
<body>

<form>
    <input type="button" onclick="checkout()" value="Buy Now!" />
</form>

</body>
</html>

答案 1 :(得分:0)

要将结算和送货地址集合添加到Checkout表单,您需要在handler.open()调用中传递这些参数:

handler.open({
  name: 'Demo.',
  description: '2 widgets',
  currency: "gbp",
  amount: 2000,
  billingAddress: true,
  shippingAddress: true
});

请注意,帐单邮寄地址将自动嵌入&#34;到Checkout返回的令牌中,因为帐单地址是令牌所代表的card object的属性。不同的字段也将作为stripeBilling* parameters传递给表单处理程序。

另一方面,送货地址仅作为stripeShipping*参数返回。如果您希望将发货地址添加到您创建的chargecustomer,则必须自行转发参数。

(此外,这更像是一个JavaScript问题,而不是PHP问题。您可能需要考虑更改标记。)