In this short series, we are going to look at how to create a charge to Stripe in a number of their officially supported languages!
In this article, we are going to look at how to do so with PHP and Slim.
The expectations are that you have PHP + Composer installed and have your Stripe API keys setup and ready to go.
The following comes in part from my documentation website.
Get Started
Ensure composer
is installed correctly and run the following. Note that you need to ensure that the downloaded composer.phar
file from the installation instructions must be in your $PATH
as composer
.
mkdir slim-stripe && cd slim-stripe
composer require slim/slim:"4.*"
# required to enable App::Run() etc without manual ServerRequest
composer require slim/psr7
# installing for Stripe
composer require stripe/stripe-php
# required to read dotenv vars
composer require vlucas/phpdotenv
mkdir -p src/public
touch src/public/index.php
touch .env
To check Slim is up and working, add this to src/public/index.php
:
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../../vendor/autoload.php';
$app = AppFactory::create();
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello world!");
return $response;
});
$app->run();
Once completed, change into src/public
and run php -S localhost:8080
.
If we now ping curl localhost:8080
we will see our Hello world!
response.
Setting up .env
Our .env
file should contain our keys for development. Get these from your Stripe Developer dashboard.
Add the following to the .env
file in the root of your project directory:
SK_TEST_KEY= sk_test...
PK_TEST_KEY=pk_test...
Making a simple charge with Stripe API
Let us update src/public/index.php
to take a simple request to make a charge to our Stripe account.
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../../vendor/autoload.php';
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../..');
$dotenv->load();
$stripeKey = getenv('SK_TEST_KEY');
\Stripe\Stripe::setApiKey($stripeKey);
$app = AppFactory::create();
// Parse json, form data and xml
$app->addBodyParsingMiddleware();
$app->addRoutingMiddleware();
$app->addErrorMiddleware(true, true, true);
$app->get('/', function (Request $request, Response $response, $args) {
$response->getBody()->write("Hello world!");
return $response;
});
$app->post('/api/charge', function (Request $request, Response $response, $args) {
try {
$data = $request->getParsedBody();
// parse attributes from JSON
$receiptEmail = $data['receiptEmail'];
$amount = $data['amount'];
// create the charge
$charge = \Stripe\Charge::create([
'amount' => $amount,
'currency' => 'aud',
'source' => 'tok_visa',
'receipt_email' => $receiptEmail
]);
$response->getBody()->write('Successful charge');
$response->withStatus(201);
return $response;
} catch (Exception $e) {
$response->getBody()->write('Failed charge');
$response->withStatus(500);
return $response;
}
});
$app->run();
Note that in the above example we are loading keys from .env
, setting the Stripe API key, then using Slim 4's body parsing middleware to help us with parsing the request body from JSON.
If we run http POST http://localhost:8080/api/charge amount:=1700 receiptEmail=hello_slim@example.com
(using HTTPie) from the console, we will get our Successful charge
message back.
I chose to use HTTPie because I feel it is a fun tool that more should know about! Alternative, you could do the above using curl
as well (or anything that can make a POST request for a matter of fact).
curl --header "Content-Type: application/json" \
--request POST \
--data '{"amount":1700,"receiptEmail":"hello_slim@example.com"}' \
http://localhost:8080/api/charge
If you now go and check your Stripe dashboard, you will be able to see a charge.
Resources and Further Reading
- Composer installation
- Getting started with Slim
- Request Object - Slim
- PHP Exceptions
- Receiving input into a Slim 4 application
- PHP Dotenv
- Stripe PHP Github
- Stripe API
- getParsedBody Slim
Image credit: Sai Kiran Anagani
Originally posted on my blog. Follow me on Twitter for more hidden gems @dennisokeeffe92.
Top comments (0)