rapidwebltd/simplestripe

SimpleStripe makes it easier than ever to integrate basic Stripe-powered payments into a site. With only a small amount of code you can have a payment form ready to start charging customers.

v1.0.6 2018-01-30 19:36 UTC

This package is auto-updated.

Last update: 2024-04-06 08:44:10 UTC


README

This super simple Stripe integration package allow you to integrate a Stripe powered payment form and charge your customers with only a tiny amount of code.

Installation & Dependencies

This package and its dependencies can be installed using composer.

Just add the package to your composer.json file as follows and run composer update.

{
  "require": {
       "rapidwebltd/simplestripe": "1.*"
   }
}

If your framework does not do so for you, remember to include the autoload files generated by composer, as follows.

require_once 'vendor/autoload.php';

Setup

To use SimpleStripe, you must first instantiate the SimpleStripe object. To do this, all you need is the Stripe API keys and the currency you wish to take payments in.

// Setup SimpleStripe using Stripe API keys and currency 
$simpleStripe = \RapidWeb\SimpleStripe\Factories\SimpleStripeFactory::create('PUBLISHABLE_KEY', 'SECRET_KEY', 'GBP');

You can find your Stripe API keys within your https://dashboard.stripe.com/account/apikeys. You will need both the secret key and the publishable key.

The currency must be presented in ISO 4217 format, such as GBP, USD, EUR.

Displaying payment form

The following code will display a simple payment form, suitable for taking payment with all common debit and credit cards.

// Display a simple payment form
echo $simpleStripe->paymentForm();

This code will also include all the necessary JavaScript code to handle client-side communication with Stripe and display of validation errors. The form will post back to the same URL it is displayed upon.

Charging the customer

Charging customers is simple. The following code is an example of how you can:

  1. Handle the payment form post back
  2. Attempt to charge the customer
  3. Handle success or failure
// If payment form has been submitted
if (isset($_POST['stripeToken'])) {

    // Get the amount to charge (in the currency's lowest denomination)
    $amount = 500; // Five hundred pence = Five pounds (5 GBP)

    // Charge the customer
    $charge = $simpleStripe->charge($amount, $_POST['stripeToken']);

    if ($charge->succeeded) {
        
        // If charge succeeded, display success messsage, or perhaps redirect the user to a success page
        echo "Success!";
        exit;

    } elseif ($charge->problemType=='Card') {

        // If there was a problem with the card, display details of the problem
        echo $charge->problem;

    } else {

        // Else, display a generic failure message
        echo "Sorry, there was a problem processing your payment.";
    }
  
}

You should include code similar to this towards the top of the page containing your payment form.

Example

For a complete implementation of SimpleStripe, see src/Example.php.

License

This library is licensed under the Lesser General Public License version 3.