Thank you! Your submission has been received!

Oops! Something went wrong while submitting the form

The Grue & Blog

Get Stripe Payments Up and Running Fast with PHP

June 15, 2016
Get Stripe Payments Up and Running Fast with PHP

So, you’ve heard that Stripe is an awesome way to accept payments online (it is!), but the instructions for getting things up and running seem a little confusing (they are!). Well have no fear, muchacho! We’re going to sort you out. We’ll cover how to get up and running as quickly as possible using some PHP and as little of your in-demand brain powers as possible. If we get requests for other languages (Java? Python? Ruby? Node, anyone?) we can go through those in subsequent posts. For this tutorial though, we’ll stick with PHP and assume you have XAMPP or a similar local server running.

When we’re done, you’ll have a page that you can send to clients which will accept a query-string variable representing the amount to be charged. For example, http://payments.grueandbleen.com/?100 will charge $100.

Install Composer

Composer is an easy way to install PHP packages and any dependencies that package may have. In this case, we want to use it to install the Stripe-PHP, which is a wrapper for the Stripe API that makes all of the things we want to do with it easier. Hey, why reinvent the wheel, right? We just want to get at that sweet sweet digital cheddar, ASAP.

Click here to get the Composer installer. Follow the install instructions and come back here when you’re done.

Download Composer
This is what the download page looks like now. If you're a visitor from the future, it may look different. Do not be alarmed, time traveler.

Install Stripe-PHP

Open up a command window: Windows-R, type cmd then hit enter

Install Stripe - PHP

Navigate to your project's folder: cd c:\xampp\htdocs\tutorial

Execute the command to get the Stripe-PHP package: composer require stripe/stripe-php

Execute Command to get Stripe-PHP package

Composer will reach out and download our Stripe-PHP package, and store it in a folder at the root of our project

Composer.Json

Now that we have Stripe-PHP installed, we can move on to building the page and custom code to use it.

The Files (and Our Custom Code)

Our project is going to consist of just 3 php files:

Config.php

This file will load up Stripe-PHP, and connect it to your Stripe account using your Secret Key and Publishable Key.

But first, get your Stripe keys

Get your secret and publishable keys from your Stripe account:

  1. ‍Log in to Stripe, go to Your Account > Account Settings
  2. Click on API Keys
  3. Copy each of the keys

For now you can use the test keys. This means that any transactions you place using the widget will be test transactions. You’ll be able to see them come in as if they were real transactions, but they won’t actually be charged.

Stripe API and Keys

Replace YOUR-SECRET-KEY-HERE and YOUR-PUBLISHABLE-KEY-HERE with the actual values pulled from your Stripe account.

<?php
require_once('vendor/autoload.php');
$stripe = array(
 "secret_key"      => "YOUR-SECRET-KEY-HERE",
 "publishable_key" => "YOUR-PUBLISHABLE-KEY-HERE"
);
\Stripe\Stripe::setApiKey($stripe['secret_key']);
?>

Index.php

This is the page where the Stripe widget will sit. It will look for a dollar amount in the query string, and pass that value to the Stripe widget on submit.

What we're doing here:

  1. Require the config file
  2. Do some acrobatics to get the dollar amount from the query string at the end of the URL, multiply by 100 to get the amount in pennies
  3. Include a form that posts to charge.php, and inject the checkout.js script.
  4. Insert the dollar amount from the query string into the data-amount property of the script tag, as well as into the hidden field contained within the form which will pass the dollar amount to the server.
<?php
require_once('./config.php');
// get the current URL
$link = $_SERVER['REQUEST_URI'];
// grab the last bit after the ? as the dollar amount
$link_array = explode('?', $link);
if(count($link_array) > 1) {
// multiply by 100 (Stripe asks for amount in pennies)
 $amount = end($link_array) * 100;
}
else {
// if there is no amount set, default to $5
 $amount = 5 * 100;
}
?>
<form action="charge.php" method="POST">
           <script src="https://checkout.stripe.com/checkout.js" class="stripe-button" data-key="<?php echo $stripe['publishable_key']; ?>" data-amount="<?php echo $amount ?>" data-name="Grue & Bleen" data-description="Payments" data-image="http://res.cloudinary.com/grue-bleen/image/upload/v1462284676/GBLogo-Webclip_tsxfxh.png" data-locale="auto">
           </script>
           <input type="hidden" name="amount" value="<?php echo $amount ?>" />
         </form>

Charge.php

This file takes the input from the form on the previous page, and uses Stripe-PHP to make the charge.

What we're doing here:

  1. Require the config file
  2. Get the Stripe token, amount, and email from the form post
  3. Create a Stripe customer
  4. Charge the customer
<?php
 require_once('./config.php');
 $token  = $_POST['stripeToken'];
 $amount = $_POST['amount'];
 $customer = \Stripe\Customer::create(array(
     'email' => $_POST["stripeEmail"],
     'source'  => $token
 ));
 $charge = \Stripe\Charge::create(array(
     'customer' => $customer->id,
     'amount'   => $amount,
     'currency' => 'usd'
 ));
?>

And that’s it! To see a working version of this, check out http://payments.grueandbleen.com/?1000000 to give us a million dollars!

The Developer

The developer creates programs, apps, websites, and code because that's what he loves. He is a master of languages and loves Marvin the Martian.

The Developer
Subscribe

Comments