This site runs best with JavaScript enabled.

Quick Recurring Payments with Stripe & Checkout

Photo by rupixen.com on Unsplash


How to quickly set up a recurring payment page with PHP and Stripe

My mom called me last night. She owns a hot tub dealership in the Salt Lake City area. She said she had a woman come in and purchase a top of the line hot tub the other day. The woman said, "I'm glad I found you. You were not on Google when I searched. Someone up the street said I should check out your store."

My mom has been paying some company $320 per month for the last nine months that promised to get her on the first page of Google. They have literally done nothing except continue to bill her month after month.

I don't know how effective an SEO campaign would be if you are only paying $320 per month, but I know I could take half that and start getting lots more exposure than she is getting just by using Google Adwords.

Basically my mom was calling to ask if she could send that money to me instead in return for me helping her get more traffic. I agreed to do it. I have enough experience that I put together a simple marketing plan for her so I can start to get her more customers and as I prove I can get a lot better ROI, then we can increase our marketing spending and efforts, and I can hire a virtual assistant to do the majority of the work.

But this post isn't really about marketing, SEO, or hiring VAs. My first order of business was to figure out how to allow my mom to set up an auto-payment plan on her business credit card to put that $320 into my account each month.

Without doing much research I turned to stripe, because I know I could get it set up fairly quickly with their API. And it was relatively quick. It took me about 2 hours to do the research and create the page. It is nothing fancy.

First I created a folder on my server named payments and dropped the lib folder of the PHP Stripe API library into it.

I created a config.php file with the following:

1<?php
2require_once('./lib/Stripe.php');
3
4$stripe = array(
5 "secret_key" => "**my_secret_key**",
6 "publishable_key" => "**my_publishable_key**"
7);
8
9Stripe::setApiKey($stripe['secret_key']);
10?>

I created a subscription plan on the Stripe admin page and gave it an ID of "soakers320". Then I created this very simple index.php page:

1<?php
2if($_SERVER["HTTPS"] != "on")
3{
4 header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
5 exit();
6}
7
8if ($_SERVER['REQUEST_METHOD'] == 'POST') {
9 require_once(dirname(**FILE**) . '/config.php');
10
11 $token = $_POST['stripeToken'];
12 $customer = Stripe_Customer::create(array(
13 'email' =&gt; 'customer@example.com',
14 'card' =&gt; $token,
15 'plan' =&gt; 'soakers320'
16 ));
17
18 echo '<h1>Successfully charged $320.00. Thank you!</h1>';
19 exit;
20}
21
22?><html>
23<head>
24<title>Red Seam Marketing Subscription</title>
25</head>
26<body>
27
28<?php if ($_SERVER['REQUEST_METHOD'] == 'GET'): ?>
29
30<script src="https://checkout.stripe.com/v2/checkout.js"></script>
31 <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
32
33<form method="post">
34 </form>
35
36<script>
37 $(document).ready(function(){
38 var token = function(res){
39 var $input = $('<input type=hidden name=stripeToken />').val(res.id);
40 $('form').append($input).submit();
41 };
42
43 StripeCheckout.open({
44 key: '**my_publishable_key**',
45 address: true,
46 currency: 'usd',
47 name: 'Marketing',
48 description: 'Marketing Plan for Soakers.biz',
49 panelLabel: 'Subscribe',
50 token: token,
51 allowRememberMe: false
52 });
53
54 return false;
55 });
56</script>
57<?php endif; ?>
58
59</body>
60</html>

Some notes on the code:

Lines 3-7: I should also mention I purchased a $9.00 SSL certificate at namecheap.com for added security. This code ensures that the page is only accessed via https.

Lines 9-22: After the form is filled out and submitted, it creates a form past back to this page. This is the call that creates the customer using the subscription plan and actually charges their card.

Lines 41-59: This creates the Checkout form that generates the card token that is passed into this page when it is posted.

That's about it! Let me know if you have questions on any other portions of this code.

Discuss on TwitterEdit post on GitHub

Share article
Dustin Davis

Dustin Davis is a software engineer, people manager, hacker, and entreprenuer. He loves to develop systems and automation. He lives with his wife and five kids in Utah.

Join the Newsletter



Dustin Davis