By Jordan Hall
7th March 2017
Posted in Ecommerce, Payment Gateways, PHP, Web Dev
To help developers easily implement the basics of Stripe into their website and quickly accept payments, we created the open source PHP library SimpleStripe.
SimpleStripe is a package that allows you to really quickly setup a payment form on a website and charge customers. As you probably guessed by its creative name, it makes use of the Stripe payments system.
Although Stripe itself is already a very developer friendly system, SimpleStripe makes it even easier to do the essentials of simply taking a payment. It’s perfect for adding basic e-commerce functionality to a site. It can be integrated into legacy e-commerce systems, but can also be easily used in modern frameworks, such as Laravel.
To install SimpleStripe, just run composer require rapidwebltd/simplestripe
. All dependencies will be pulled in to your project automatically.
SimpleStripe is designed to allow developers to really quickly integrate the basics of charging a customer.
You’ll need your API keys, which you can find in the Stripe dashboard. When you’ve got these, make sure to replace the PUBLISHABLE_KEY
and SECRET_KEY
placeholders in the example code below.
You can also specify the currency to use, and the amount you wish to charge your customer.
require_once 'vendor/autoload.php'; // If you're using a framework, this may be done for you
use RapidWeb\SimpleStripe\Factories\SimpleStripeFactory;
// Setup SimpleStripe using Stripe API keys and currency
$simpleStripe = SimpleStripeFactory::create('PUBLISHABLE_KEY', 'SECRET_KEY', 'GBP');
// 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 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.";
}
}
// Display a simple payment form
echo $simpleStripe->paymentForm();
SimpleStripe is an open source library produced by Rapid Web Services Ltd. It is licensed under the Lesser General Public License 3.0 (LGPL3).
Feel free to take a look at its GitHub repository, report any issues, and know that contributions are very welcome!