Stripe PHP bindings
The Stripe PHP library provides convenient access to the Stripe API from applications written in the PHP language. It includes a pre-defined set of classes for API resources that initialize themselves dynamically from API responses which makes it compatible with a wide range of versions of the Stripe API.
Requirements
PHP 5.6.0 and later.
Composer
You can install the bindings via Composer. Run the following command:
1 |
composer require stripe/stripe-php |
To use the bindings, use Composer’s autoload:
1 |
<span class="pl-k">require_once</span>(<span class="pl-s">'vendor/autoload.php'</span>); |
Manual Installation
If you do not wish to use Composer, you can download the latest release. Then, to use the bindings, include the init.php
file.
1 |
<span class="pl-k">require_once</span>(<span class="pl-s">'/path/to/stripe-php/init.php'</span>); |
Dependencies
The bindings require the following extensions in order to work properly:
If you use Composer, these dependencies should be handled automatically. If you install manually, you’ll want to make sure that these extensions are available.
Getting Started
Simple usage looks like:
1 2 3 4 5 6 7 |
<span class="pl-s1"><span class="pl-c1">$</span>stripe</span> = <span class="pl-k">new</span> \<span class="pl-v">Stripe</span>\<span class="pl-v">StripeClient</span>(<span class="pl-s">'sk_test_BQokikJOvBiI2HlWgH4olfQ2'</span>); <span class="pl-s1"><span class="pl-c1">$</span>customer</span> = <span class="pl-s1"><span class="pl-c1">$</span>stripe</span>-><span class="pl-c1">customers</span>-><span class="pl-en">create</span>([ <span class="pl-s">'description'</span> => <span class="pl-s">'example customer'</span>, <span class="pl-s">'email'</span> => <span class="pl-s">'email@example.com'</span>, <span class="pl-s">'payment_method'</span> => <span class="pl-s">'pm_card_visa'</span>, ]); <span class="pl-k">echo</span> <span class="pl-s1"><span class="pl-c1">$</span>customer</span>; |
Full Example
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
<?php // Edited boucher/gist:1750375 to work with stripe-php v2 require 'stripe-php/init.php'; if ($_POST) { \Stripe\Stripe::setApiKey("YOUR-API-KEY"); $error = ''; $success = ''; try { if (!isset($_POST['stripeToken'])) throw new Exception("The Stripe Token was not generated correctly"); $charge = \Stripe\Charge::create(array( "amount" => 1000, // amount in cents, again "currency" => "usd", "source" => $_POST['stripeToken'], "description" => "Example charge") ); if ($charge->status == 'succeeded') { $success = 'Your payment was sucessful.'; } else { $success = 'Your payment failed.'; } } catch(\Stripe\Error\Card $e) { // The card has been declined $error = $e->getMessage(); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>Stripe Getting Started Form</title> <script type="text/javascript" src="https://js.stripe.com/v2/"></script> <!-- jQuery is used only for this example; it isn't required to use Stripe --> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript"> // this identifies your website in the createToken call below Stripe.setPublishableKey('YOUR-PUBLISHABLE-API-KEY'); function stripeResponseHandler(status, response) { if (response.error) { // re-enable the submit button $('.submit-button').removeAttr("disabled"); // show the errors on the form $(".payment-errors").html(response.error.message); } else { var form$ = $("#payment-form"); // token contains id, last4, and card type var token = response['id']; // insert the token into the form so it gets submitted to the server form$.append("<input type='hidden' name='stripeToken' value='" + token + "' />"); // and submit form$.get(0).submit(); } } $(document).ready(function() { $("#payment-form").submit(function(event) { // disable the submit button to prevent repeated clicks $('.submit-button').attr("disabled", "disabled"); // createToken returns immediately - the supplied callback submits the form if there are no errors Stripe.createToken({ number: $('.card-number').val(), cvc: $('.card-cvc').val(), exp_month: $('.card-expiry-month').val(), exp_year: $('.card-expiry-year').val() }, stripeResponseHandler); return false; // submit from callback }); }); </script> </head> <body> <h1>Charge $10 with Stripe</h1> <!-- to display errors returned by createToken --> <span class="payment-errors"><?= $error ?></span> <span class="payment-success"><?= $success ?></span> <form action="" method="POST" id="payment-form"> <div class="form-row"> <label>Card Number</label> <input type="text" size="16" autocomplete="off" class="card-number" maxlength="16" /> </div> <div class="form-row"> <label>CVC</label> <input type="text" size="4" autocomplete="off" class="card-cvc" maxlength="4" /> </div> <div class="form-row"> <label>Expiration (MM/YYYY)</label> <input type="text" size="2" class="card-expiry-month" maxlength="2"/> <span> / </span> <input type="text" size="4" class="card-expiry-year" maxlength="4"/> </div> <button type="submit" class="submit-button">Submit Payment</button> </form> </body> </html> |