user4532739
user4532739

Reputation:

integrate stripe payment gateway using php

According to the documentation given by stripe i have used the following code

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

However i also wish to implement billingAddress and shippingAddress so that the users can enter these 2 addresses also, so i added these two lines

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

but it is having no effect, can anyone please tell a proper way to use these 2 parameters

Upvotes: 1

Views: 1973

Answers (2)

Ajith jojo
Ajith jojo

Reputation: 427

i can help you with integrating version 3 stripe api with php

install stripe dependency first 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' => '[email protected]',
  '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>

Upvotes: 2

Ywain
Ywain

Reputation: 17505

To add billing and shipping address collection to the Checkout form, you need to pass those parameters in the handler.open() call:

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

Note that the billing address will automatically be "embedded" into the token returned by Checkout, as the billing address is a property of the card object, which the token represents. The different fields will also be passed to the form handler as the stripeBilling* parameters.

The shipping address, on the other hand, is only returned as the stripeShipping* parameters. If you wish to add the shipping address to the charge or the customer you create, you will have to forward the parameters yourself.

(Also, this is more of a JavaScript issue than a PHP one. You might want to consider changing the tags.)

Upvotes: 0

Related Questions