Stripe

Gateway slug: stripe

These are instructions for Stripe token-based payment using the stripe.js route.

Settings

In your perch/config/shop.php file, add your settings for Stripe.

<?php
  return [
    'gateways' => [
      'stripe' => [
        'enabled'   => true,
        'test_mode' => true,
        'live' => [
          'secret_key'      => 'sk_live_ABC123',
          'publishable_key' => 'pk_live_ABC123',
        ],
        'test' => [
          'secret_key'      => 'sk_test_ABC123',
          'publishable_key' => 'pk_test_ABC123',
        ],
      ],
    ],
  ];
?>

Payment flow

The process for Stripe involves adding the Stripe JavaScript to the page in a step before calling perch_shop_checkout(). This enables you to collect the Stripe card token and pass it as the option for payment.

In the body of your page, you need to add the Stripe form which uses JavaScript to collect a payment token. We have a pre-built form for this which you can use, via the perch_shop_payment_form() function. You can also customise its template, or replace it completely with your own if you need to.

<?php
  perch_shop_payment_form('stripe');
?>

This form collects a stripeToken and puts it in a hidden field. The form then posts back to the same page. At the top of the page (before any HTML is output) you need to look for the token. If it’s been posted, you can continue the checkout process.

<?php
if (perch_member_logged_in() && perch_post('stripeToken')) {

  // your 'success' and 'failure' URLs
  $return_url = '/payment/stripe';
  $cancel_url = '/payment/went/wrong';

  perch_shop_checkout('stripe', [
    'return_url' => $return_url,
    'cancel_url' => $cancel_url,
    'token'      => perch_post('stripeToken')
  ]);
}
?>