duncanjbrown / simple-stripe
WordPress plumbing for Stripe
Installs: 389
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:wordpress-plugin
Requires
- duncanjbrown/plain-route: ^0.1
- stripe/stripe-php: ^3.5
- vlucas/phpdotenv: ^2.1
This package is not auto-updated.
Last update: 2018-06-19 06:12:37 UTC
README
This plugin provides an endpoint for your website to make a charge using Stripe, given a Stripe token.
It does not provide any frontend code and never will.
It only supports US dollars, but it can be extended.
Installation
For a Bedrock project
composer require duncanjbrown/simple-stripe
This will install the plugin and its dependencies.
You should add S_STRIPE_PUBLISHABLE_KEY
and S_STRIPE_SECRET_KEY
to your project’s .env
.
In vanilla WP
Install the plugin but do not activate it.
Open a terminal and visit its folder, then run
composer install
You should add S_STRIPE_PUBLISHABLE_KEY
and S_STRIPE_SECRET_KEY
to the .env
file in the plugin folder.
Activate it.
Usage
Obtain a Stripe token. You can do this using Stripe checkout.
When you have obtained your token from Stripe checkout, POST that token and the charge amount in cents to /_stripe/charge
in the following format:
{
's-stripe': {
'token': token,
'amount': 100
}
}
If the charge is successful, the hook simple_stripe_charge_succeeded
will be called.
If the charge failed, the hook simple_stripe_charge_failed
will be called.
Both of these hooks receive a SimpleStripeOneOffPayment
object.
The SimpleStripeOneOffPayment object
This object has the following properties
$currency // the currency, eg "usd"
$amount_in_cents // the amount charged, in cents
$charge // Stripe response, if the charge succeeded
$errors // a WP_Error containing error messages, if the charge failed
Example
This code shows a success or failure message when the charge is processed.
add_action( 'simple_stripe_charge_failed', function( $payment ) {
$errors = $payment->errors->get_error_messages();
$message = sprintf( '<ul><li>%s</li></ul>', implode( '</li><li>', $errors ) );
wp_die( '<h2>Something went wrong</h2>' . $message . '<p>Your card has not been charged.</p>',
'Something went wrong', [ 'response' => 400 ] );
});
add_action( 'simple_stripe_charge_succeeded', function( $payment ) {
$amount = sprintf( "$%01.2f", $payment->amount_in_cents / 100 );
wp_die( "<h2>✓ Your payment has been processed</h2>
<p>Thank you for your donation of ${amount}.</p>", 'Thank you', [ 'response' => 201 ] );
});