-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rias
committed
Jul 8, 2018
0 parents
commit 8b54d4f
Showing
19 changed files
with
951 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# CRAFT ENVIRONMENT | ||
.env.php | ||
.env.sh | ||
.env | ||
|
||
# COMPOSER | ||
/vendor | ||
|
||
# BUILD FILES | ||
/bower_components/* | ||
/node_modules/* | ||
/build/* | ||
/yarn-error.log | ||
|
||
# MISC FILES | ||
.cache | ||
.DS_Store | ||
.idea | ||
.project | ||
.settings | ||
*.esproj | ||
*.sublime-workspace | ||
*.sublime-project | ||
*.tmproj | ||
*.tmproject | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
config.codekit3 | ||
prepros-6.config |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Stripe Webhooks Changelog | ||
|
||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). | ||
|
||
## 1.0.0 - 2018-07-08 | ||
### Added | ||
- Initial release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
The MIT License (MIT) | ||
|
||
Copyright (c) 2018 Rias | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
![icon](./src/icon.svg) | ||
|
||
[![Latest Version](https://img.shields.io/github/release/rias500/craft-stripe-webhooks.svg?style=flat-square)](https://github.com/rias500/craft-stripe-webhooks/releases) | ||
[![Quality Score](https://img.shields.io/scrutinizer/g/rias500/craft-stripe-webhooks.svg?style=flat-square)](https://scrutinizer-ci.com/g/rias500/craft-stripe-webhooks) | ||
[![StyleCI](https://styleci.io/repos/117422620/shield)](https://styleci.io/repos/117422620) | ||
[![Total Downloads](https://img.shields.io/packagist/dt/rias/craft-stripe-webhooks.svg?style=flat-square)](https://packagist.org/packages/rias/craft-stripe-webhooks) | ||
|
||
# Handle Stripe webhooks in a CraftCMS application | ||
|
||
[Stripe](https://stripe.com) can notify your application of events using webhooks. This plugin can help you handle those webhooks. Out of the box it will verify the Stripe signature of all incoming requests. All valid calls will be logged to the database. You can easily define jobs or events that should be dispatched when specific events hit your app. | ||
|
||
This plugin will not handle what should be done after the webhook request has been validated and the right job or event is called. You should still code up any work (eg. regarding payments) yourself. | ||
|
||
Before using this plugin we highly recommend reading [the entire documentation on webhooks over at Stripe](https://stripe.com/docs/webhooks). | ||
|
||
## Requirements | ||
|
||
This plugin requires Craft CMS 3.0.0. | ||
|
||
## Configuration | ||
|
||
Create a `stripe-webhooks.php` config file with the following contents, or copy the one from the root of this plugin. | ||
|
||
|
||
```php | ||
return [ | ||
/* | ||
* Stripe will sign each webhook using a secret. You can find the used secret at the | ||
* webhook configuration settings: https://dashboard.stripe.com/account/webhooks. | ||
*/ | ||
'signingSecret' => '', | ||
|
||
/* | ||
* You can define the job that should be run when a certain webhook hits your application | ||
* here. The key is the name of the Stripe event type with the `.` replaced by a `_`. | ||
* | ||
* You can find a list of Stripe webhook types here: | ||
* https://stripe.com/docs/api#event_types. | ||
*/ | ||
'jobs' => [ | ||
// 'source_chargeable' => \modules\sitemodule\jobs\StripeWebhooks\HandleChargeableSource::class, | ||
// 'charge_failed' => \modules\sitemodule\jobs\StripeWebhooks\HandleFailedCharge::class, | ||
], | ||
|
||
/* | ||
* The classname of the model to be used. The class should equal or extend | ||
* rias\stripewebhooks\records\StripeWebhookCall. | ||
*/ | ||
'model' => \rias\stripewebhooks\records\StripeWebhookCall::class, | ||
|
||
/* | ||
* The url of the Stripe endpoint you want to use in your application | ||
*/ | ||
'endpoint' => 'stripe-webhooks', | ||
]; | ||
|
||
|
||
In the `signingSecret` key of the config file you should add a valid webhook secret. You can find the secret used at [the webhook configuration settings on the Stripe dashboard](https://dashboard.stripe.com/account/webhooks). | ||
|
||
## Usage | ||
|
||
Stripe will send out webhooks for several event types. You can find the [full list of events types](https://stripe.com/docs/api#event_types) in the Stripe documentation. | ||
|
||
Stripe will sign all requests hitting the webhook url of your app. This package will automatically verify if the signature is valid. If it is not, the request was probably not sent by Stripe. | ||
|
||
Unless something goes terribly wrong, this plugin will always respond with a `200` to webhook requests. Sending a `200` will prevent Stripe from resending the same event over and over again. All webhook requests with a valid signature will be logged in the `stripewebhooks_stripewebhookcall` table. The table has a `payload` column where the entire payload of the incoming webhook is saved. | ||
|
||
If the signature is not valid, the request will not be logged in the `stripewebhooks_stripewebhookcall` table but a `rias\stripewebhooks\exceptions\WebhookFailed` exception will be thrown. | ||
If something goes wrong during the webhook request the thrown exception will be saved in the `exception` column. In that case the controller will send a `500` instead of `200`. | ||
|
||
There are two ways this plugin enables you to handle webhook requests: you can opt to queue a job or listen to the events the package will fire. | ||
|
||
### Handling webhook requests using jobs | ||
If you want to do something when a specific event type comes in you can define a job that does the work. Here's an example of such a job: | ||
|
||
```php | ||
<?php | ||
|
||
namespace modules\sitemodule\jobs\StripeWebhooks; | ||
|
||
use Craft; | ||
use craft\queue\BaseJob; | ||
|
||
class HandleChargeableSource extends BaseJob | ||
{ | ||
/** @var \rias\stripewebhooks\records\StripeWebhookCall */ | ||
public $model; | ||
|
||
public function execute($queue) | ||
{ | ||
// do your work here | ||
|
||
// you can access the payload of the webhook call with `$this->model->payload` | ||
} | ||
} | ||
``` | ||
|
||
After having created your job you must register it at the `jobs` array in the `stripe-webhooks.php` config file. The key should be the name of [the stripe event type](https://stripe.com/docs/api#event_types) where but with the `.` replaced by `_`. The value should be the fully qualified classname. | ||
|
||
```php | ||
// config/stripe-webhooks.php | ||
|
||
'jobs' => [ | ||
'source_chargeable' => \modules\sitemodule\jobs\StripeWebhooks\HandleChargeableSource::class, | ||
], | ||
``` | ||
|
||
|
||
### Handling webhook requests using events | ||
|
||
Instead of queueing jobs to perform some work when a webhook request comes in, you can opt to listen to the events this package will fire. Whenever a valid request hits your app, the package will fire a `stripe-webhooks::<name-of-the-event>` event. | ||
|
||
The payload of the events will be the instance of `StripeWebhookCall` that was created for the incoming request. | ||
|
||
You can add a listener in your plugin or module's init function | ||
```php | ||
public function init() | ||
{ | ||
Event::on( | ||
\rias\stripewebhooks\records\StripeWebhookCall::class, | ||
'stripe-webhooks::source.chargeable', | ||
function (\rias\stripewebhooks\events\WebhookEvent $event) { | ||
$webhookCall = $event->model; | ||
} | ||
); | ||
} | ||
``` | ||
|
||
## Credits | ||
- [Spatie's Laravel Stripe Webhooks Package](https://github.com/spatie/laravel-stripe-webhooks) | ||
- [All Contributors](../../contributors) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "rias/craft-stripe-webhooks", | ||
"description": "Handle Stripe webhooks in a CraftCMS application", | ||
"type": "craft-plugin", | ||
"version": "1.0.0", | ||
"keywords": [ | ||
"craft", | ||
"cms", | ||
"craftcms", | ||
"craft-plugin", | ||
"stripe webhooks" | ||
], | ||
"support": { | ||
"docs": "https://github.com/Rias500/craft-stripe-webhooks/blob/master/README.md", | ||
"issues": "https://github.com/Rias500/craft-stripe-webhooks/issues" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Rias", | ||
"homepage": "https://rias.be" | ||
} | ||
], | ||
"require": { | ||
"craftcms/cms": "^3.0.0", | ||
"stripe/stripe-php": "^6.10" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"rias\\stripewebhooks\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"name": "Stripe Webhooks", | ||
"handle": "stripe-webhooks", | ||
"hasCpSettings": false, | ||
"hasCpSection": false, | ||
"changelogUrl": "https://raw.githubusercontent.com/Rias500/craft-stripe-webhooks/master/CHANGELOG.md", | ||
"class": "rias\\stripewebhooks\\StripeWebhooks" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
/** | ||
* Stripe Webhooks plugin for Craft CMS 3.x | ||
* | ||
* Handle Stripe webhooks in a CraftCMS application | ||
* | ||
* @link https://rias.be | ||
* @copyright Copyright (c) 2018 Rias | ||
*/ | ||
|
||
namespace rias\stripewebhooks; | ||
|
||
use rias\stripewebhooks\models\Settings; | ||
|
||
use Craft; | ||
use craft\base\Plugin; | ||
use craft\services\Plugins; | ||
use craft\events\PluginEvent; | ||
use craft\web\UrlManager; | ||
use craft\events\RegisterUrlRulesEvent; | ||
|
||
use yii\base\Event; | ||
|
||
/** | ||
* Class StripeWebhooks | ||
* | ||
* @author Rias | ||
* @package StripeWebhooks | ||
* @since 1.0.0 | ||
* | ||
*/ | ||
class StripeWebhooks extends Plugin | ||
{ | ||
// Static Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @var StripeWebhooks | ||
*/ | ||
public static $plugin; | ||
|
||
// Public Properties | ||
// ========================================================================= | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public $schemaVersion = '1.0.0'; | ||
|
||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
self::$plugin = $this; | ||
|
||
Event::on( | ||
UrlManager::class, | ||
UrlManager::EVENT_REGISTER_SITE_URL_RULES, | ||
function (RegisterUrlRulesEvent $event) { | ||
$event->rules[$this->settings->endpoint] = 'stripe-webhooks/default'; | ||
} | ||
); | ||
} | ||
|
||
// Protected Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function createSettingsModel() | ||
{ | ||
return new Settings(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
/** | ||
* Stripe Webhooks plugin for Craft CMS 3.x | ||
* | ||
* Handle Stripe webhooks in a CraftCMS application | ||
* | ||
* @link https://rias.be | ||
* @copyright Copyright (c) 2018 Rias | ||
*/ | ||
|
||
namespace rias\stripewebhooks\assetbundles\StripeWebhooks; | ||
|
||
use Craft; | ||
use craft\web\AssetBundle; | ||
use craft\web\assets\cp\CpAsset; | ||
|
||
/** | ||
* @author Rias | ||
* @package StripeWebhooks | ||
* @since 1.0.0 | ||
*/ | ||
class StripeWebhooksAsset extends AssetBundle | ||
{ | ||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
$this->sourcePath = "@rias/stripewebhooks/assetbundles/stripewebhooks/dist"; | ||
|
||
$this->depends = [ | ||
CpAsset::class, | ||
]; | ||
|
||
$this->js = [ | ||
'js/StripeWebhooks.js', | ||
]; | ||
|
||
$this->css = [ | ||
'css/StripeWebhooks.css', | ||
]; | ||
|
||
parent::init(); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/assetbundles/stripewebhooks/dist/css/StripeWebhooks.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Stripe Webhooks plugin for Craft CMS | ||
* | ||
* Stripe Webhooks CSS | ||
* | ||
* @author Rias | ||
* @copyright Copyright (c) 2018 Rias | ||
* @link https://rias.be | ||
* @package StripeWebhooks | ||
* @since 1.0.0 | ||
*/ |
Oops, something went wrong.