How to integrate Stripe in PHP/Javascript

Integrating a new payment provider can be daunting, and the official documentation unneccesarily complex. Here are four easy steps to a complete Stripe integration.

Note that you don’t technically need an SSL certificate to be compliant, as the form itself is secured by Stripe, but we would highly recommend the purchase of at least the basic certificate, if only for customer confidence.

Step 1

Download the latest SDK from here https://github.com/stripe/stripe-php/releases and upload to your server. You don’t need the “test” folder.

Step 2

Create a config file to include Stripe’s init.php file, plus your API credentials (you’ll need to sign up to get these https://dashboard.stripe.com/register).

require_once("init.php");

// Test
/*$stripe = array(
  "secret_key"      => "sk_test_",
  "publishable_key" => "pk_test_"
);*/

// Live
$stripe = array(
  "secret_key"      => "sk_live_",
  "publishable_key" => "pk_live_"
);

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

Step 3

On your payment page include your config file then create a payment button as below. The price, name and email address are only used for display purposes here, and have nothing to do with how much you will actually charge the customer, which happens in the next step.

require_once("config.php");

<form action="callback.php" method="post">
    <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="<?=$stripe['publishable_key'];?>"
        data-amount="100"
        data-currency="gbp"
        data-name="Test Shop"
        data-email="example@example.com"
        data-locale="auto">
    </script>
</form>

Step 4

On your callback page, to which the above form posts, take the payment. Stripe will have automatically created the stripeToken field, and populated it, in the previous step.

You must first create a customer, then charge the card.

if($token = $_POST['stripeToken']){
    // Stripe
    require_once("config.php");

    $customer = \Stripe\Customer::create(array(
        "email" => "example@example.com",
        "card"  => $token
    ));

    try{
        $charge = \Stripe\Charge::create(array(
            "customer" => $customer->id,
            "amount"   => 100,
            "currency" => "gbp"
        ));

        // Order successful
    }catch(Stripe\Error\Base $e){
        // Order failed
    }
}

The above will charge £1 to the card (the “amount” is in pence). In a production environment you’d want to retrieve the customer’s order details here to get the correct amount and email address.

This code is free to use at your own discretion. It comes without warranty. Please feel free to feedback any edits.


We'd love to hear from you!

If you think Bronco has the skills to take your business forward then what are you waiting for?

Get in Touch Today!

Discussion

Write a comment...
  • Bayard

    Thank you!
    At least an easy way that works.
    Stripe tutorials, and all other Youtube videos I found, didn’t work for me..

    • Chris Antcliff

      You’re welcome, glad we could help 🙂

  • Ming

    may i know how to connect to alipay via stripe?

  • Kevin

    Got me started so I am very grateful. Thank you.

    However, the code is 5 years old and some things have been deprecated.

    The Stripe\Error\ namespace should now read Stripe\Exception\

    • Chris Antcliff

      Hi Kevin, I’m glad it helped. Thanks for the feedback.

Add a Comment