Giter Club home page Giter Club logo

stripe-php-webhook's Introduction

Stripe PHP Webhook

Respond to Stripe webhooks like jQuery events — simple and elegant.

Usage

Event handlers can be added in jQuery style using S::on():

S::on('charge.succeeded', function($charge, $event) {
	/* … */
});

Use S::onSecure() if you'd like to have the event data retrieved from Stripe:

S::onSecure('charge.succeeded', function($charge, $event) {
	/* Now $charge and $event are retrieved from Stripe */
});

One handler can respond to multiple event types (both methods are equivalent):

S::on('charge.succeeded charge.failed', function($charge, $event) {
	/* … */
});

S::on(['charge.succeeded', 'charge.failed'], function($charge, $event) {
	/* … */
});

Examples

Here are two examples from handlers.php:

// This handler sends a "Thank you" message when a charge was successful.
S::on('charge.succeeded', function($charge) {
	$customer = \Stripe\Customer::retrieve($charge->customer);
	mail($customer->email, 'Thanks you', 'Thanks for your payment! <3');
});

// This handler sends an angry message when a customer disputes a charge.
// (You should probably not use this in production.)
S::onSecure('charge.dispute.created charge.dispute.updated', function($dispute) {
	$charge = \Stripe\Charge::retrieve($dispute->charge);
	$customer = \Stripe\Customer::retrieve($charge->customer);
	mail($customer->email, '#@%&@', 'WHY U DISPUTE?', $photo_of_dead_horse_head);
});

Installation

  1. Add (the code from) this repository to your project.
  2. Adjust the path to stripe-php and your Stripe API key in webhook.php.
  3. Create some event handlers and make sure they get loaded by webhook.php.
  4. Make webhook.php publicly accessible and add it's URL to your Stripe webhooks.

Handler Closure Format

Handlers are implemented using PHP closures. When your handler is executed, it receives two arguments:

  1. The object as defined by $event->data->object; and
  2. The event itself.

For a handler function ($object, $event), $object === $event->data->object by definition.

Handler closures are not required to expect two arguments; it's fine if your handler expects one argument, like function ($object), or no argument at all, like function ().

The return value of your handler closures is not considered currently. This might change in future versions.

Compatibility

This code has been tested with stripe-php v3.17.0 and PHP 7. In theory, it should work with PHP 5.4 and newer.

License

This code is licensed under MIT license. See LICENSE for details.

stripe-php-webhook's People

Contributors

cronick avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

phillipwilhelm

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.