-
Notifications
You must be signed in to change notification settings - Fork 1
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
0 parents
commit 8a2d026
Showing
17 changed files
with
1,087 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,4 @@ | ||
.idea | ||
*.patch | ||
*.rej | ||
*.orig |
Large diffs are not rendered by default.
Oops, something went wrong.
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,5 @@ | ||
Stripe API Drupal module | ||
------------------------ | ||
Provider Stripe as forked from: https://www.drupal.org/project/stripe_api | ||
|
||
This module provides a simple abstraction to use the Stripe PHP SDK. |
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,30 @@ | ||
{ | ||
"name": "wasare/provider_stripe", | ||
"type": "drupal-module", | ||
"description": "Provides an integration to use the Stripe API library", | ||
"keywords": [ | ||
"Drupal" | ||
], | ||
"license": "GPL-2.0+", | ||
"homepage": "https://github.com/wasare/provider_stripe", | ||
"minimum-stability": "dev", | ||
"support": { | ||
"issues": "https://github.com/wasare/provider_stripe/issues", | ||
"source": "https://github.com/wasare/provider_stripe" | ||
}, | ||
"repositories": { | ||
"wasare": { | ||
"type": "path", | ||
"url": "https://github.com/wasare/provider_stripe" | ||
} | ||
}, | ||
"require": { | ||
"stripe/stripe-php": "^7.36", | ||
"drupal/key": "^1.1" | ||
}, | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "1.x-dev" | ||
} | ||
} | ||
} |
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,7 @@ | ||
test_secret_key: '' | ||
test_public_key: '' | ||
live_secret_key: '' | ||
live_public_key: '' | ||
log_webhooks: TRUE | ||
enable_webhooks: TRUE | ||
mode: 'test' |
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,8 @@ | ||
name: '[Provider] Stripe' | ||
description: 'Provides an integration to use the Stripe API library' | ||
core_version_requirement: ^9 | ||
type: module | ||
package: Provider | ||
configure: provider_stripe.admin | ||
dependencies: | ||
- key:key |
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,5 @@ | ||
provider_stripe.admin: | ||
title: 'Provider Stripe' | ||
description: 'Edit Provider Stripe API credentials.' | ||
parent: system.admin_config_services | ||
route_name: provider_stripe.admin |
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,20 @@ | ||
<?php | ||
|
||
/** | ||
* Implements hook_preprocess_HOOK(). | ||
*/ | ||
function provider_stripe_preprocess_page(&$variables) { | ||
$admin_context = \Drupal::service('router.admin_context'); | ||
if ($admin_context->isAdminRoute()) { | ||
/** @var $stripe_api \Drupal\provider_stripe\StripeApiService **/ | ||
$stripe_api = \Drupal::service('provider_stripe.stripe_api'); | ||
if ($stripe_api->getMode() === 'test') { | ||
\Drupal::messenger() | ||
->addMessage(t('Provider Stripe is running in test mode.'), | ||
\Drupal\Core\Messenger\MessengerInterface::TYPE_WARNING); | ||
} | ||
} | ||
// $module_data = \Drupal::config('core.extension')->get('module'); | ||
// unset($module_data['subscription_manager']); | ||
// \Drupal::configFactory()->getEditable('core.extension')->set('module', $module_data)->save(); | ||
} |
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,2 @@ | ||
administer provider stripe: | ||
title: 'Administer Provider Stripe' |
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,26 @@ | ||
provider_stripe.admin: | ||
path: '/admin/config/services/provider/stripe' | ||
defaults: | ||
_form: '\Drupal\provider_stripe\Form\StripeApiAdminForm' | ||
_title: 'Provider Stripe Settings' | ||
requirements: | ||
_permission: 'administer provider stripe' | ||
options: | ||
_admin_route: TRUE | ||
|
||
provider_stripe.webhook: | ||
path: '/provider/stripe/webhook' | ||
methods: [POST] | ||
defaults: | ||
_controller: '\Drupal\provider_stripe\Controller\StripeApiWebhook::handleIncomingWebhook' | ||
requirements: | ||
_permission: 'access content' | ||
_content_type_format: json | ||
|
||
provider_stripe.webhook_redirect: | ||
path: '/provider/stripe/webhook' | ||
methods: [GET, HEAD, PUT, DELETE] | ||
defaults: | ||
_controller: '\Drupal\provider_stripe\Controller\StripeApiWebhookRedirect::webhookRedirect' | ||
requirements: | ||
_permission: 'access content' |
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,13 @@ | ||
services: | ||
provider_stripe.webhook_subscriber: | ||
class: Drupal\provider_stripe\Event\StripeApiWebhookSubscriber | ||
tags: | ||
- {name: event_subscriber} | ||
|
||
provider_stripe.stripe_api: | ||
class: Drupal\provider_stripe\StripeApiService | ||
arguments: ["@config.factory", "@entity_type.manager", "@logger.channel.provider_stripe", "@key.repository"] | ||
|
||
logger.channel.provider_stripe: | ||
parent: logger.channel_base | ||
arguments: ['provider_stripe'] |
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,114 @@ | ||
<?php | ||
|
||
namespace Drupal\provider_stripe\Controller; | ||
|
||
use Drupal\Core\Controller\ControllerBase; | ||
use Drupal\provider_stripe\Event\StripeApiWebhookEvent; | ||
use Drupal\provider_stripe\StripeApiService; | ||
use Stripe\Event; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
/** | ||
* Class StripeApiWebhook. | ||
* | ||
* Provides the route functionality for provider_stripe.webhook route. | ||
*/ | ||
class StripeApiWebhook extends ControllerBase { | ||
|
||
/** | ||
* Fake ID from Stripe we can check against. | ||
* | ||
* @var string | ||
*/ | ||
const FAKE_EVENT_ID = 'evt_00000000000000'; | ||
|
||
/** | ||
* Stripe API service. | ||
* | ||
* @var \Drupal\provider_stripe\StripeApiService | ||
*/ | ||
protected $stripeApi; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function __construct(StripeApiService $stripe_api) { | ||
$this->stripeApi = $stripe_api; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static( | ||
$container->get('provider_stripe.stripe_api') | ||
); | ||
} | ||
|
||
/** | ||
* Captures the incoming webhook request. | ||
* | ||
* @param \Symfony\Component\HttpFoundation\Request $request | ||
* The current request. | ||
* | ||
* @return \Symfony\Component\HttpFoundation\Response | ||
* A Response object. | ||
*/ | ||
public function handleIncomingWebhook(Request $request) { | ||
$config = $this->config('provider_stripe.settings'); | ||
if ($config->get('enable_webooks') === FALSE) { | ||
return new Response('Incoming webhooks are disabled by the Stripe API module configuration.', Response::HTTP_FORBIDDEN); | ||
} | ||
|
||
$input = $request->getContent(); | ||
$decoded_input = json_decode($input); | ||
|
||
if (!$event = $this->isValidWebhook($decoded_input)) { | ||
$this->getLogger('provider_stripe') | ||
->error('Invalid webhook event: @data', [ | ||
'@data' => $input, | ||
]); | ||
return new Response(NULL, Response::HTTP_FORBIDDEN); | ||
} | ||
|
||
if ($config->get('log_webhooks')) { | ||
/** @var \Drupal\Core\Logger\LoggerChannelInterface $logger */ | ||
$logger = $this->getLogger('provider_stripe'); | ||
$logger->info("Stripe webhook received event:\n @event", ['@event' => (string)$event]); | ||
} | ||
|
||
// Dispatch the webhook event. | ||
$dispatcher = \Drupal::service('event_dispatcher'); | ||
$webhook_event = new StripeApiWebhookEvent($event->type, $decoded_input->data, $event); | ||
$dispatcher->dispatch('provider_stripe.webhook', $webhook_event); | ||
|
||
return new Response('Okay', Response::HTTP_OK); | ||
} | ||
|
||
/** | ||
* Determines if a webhook is valid. | ||
* | ||
* @param object $event_json | ||
* Stripe event object parsed from JSON. | ||
* | ||
* @return bool|\Stripe\Event | ||
* Returns a Stripe Event object or false if validation fails. | ||
*/ | ||
private function isValidWebhook(object $event_json) { | ||
if (!empty($event_json->id)) { | ||
|
||
if ($this->stripeApi->getMode() === 'test' && $event_json->livemode === FALSE && $event_json->id === self::FAKE_EVENT_ID) { | ||
// Don't try to verify this event, as it doesn't exist at stripe. | ||
return Event::constructFrom($event_json->object); | ||
} | ||
|
||
// Verify the event by fetching it from Stripe. | ||
return Event::retrieve($event_json->id); | ||
} | ||
|
||
return FALSE; | ||
} | ||
|
||
} |
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,28 @@ | ||
<?php | ||
|
||
namespace Drupal\provider_stripe\Controller; | ||
|
||
use Drupal\Core\Controller\ControllerBase; | ||
use Drupal\Core\Url; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
||
/** | ||
* Class StripeApiWebhookRedirect. | ||
*/ | ||
class StripeApiWebhookRedirect extends ControllerBase { | ||
|
||
/** | ||
* Redirects the user to home page and show the message. | ||
* | ||
* @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
* A redirect response object. | ||
*/ | ||
public function webhookRedirect() { | ||
$this->messenger() | ||
->addMessage($this->t('The webhook route works properly.')); | ||
return new RedirectResponse(Url::fromRoute('<front>') | ||
->setAbsolute() | ||
->toString()); | ||
} | ||
|
||
} |
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,52 @@ | ||
<?php | ||
|
||
namespace Drupal\provider_stripe\Event; | ||
|
||
use Stripe\Event; | ||
use Symfony\Component\EventDispatcher\Event as SymfonyEvent; | ||
|
||
/** | ||
* Class StripeApiWebhookEvent. | ||
* | ||
* Provides the Stripe API Webhook Event. | ||
*/ | ||
class StripeApiWebhookEvent extends SymfonyEvent { | ||
|
||
/** | ||
* Webhook event type. | ||
* | ||
* @var string | ||
*/ | ||
public $type; | ||
|
||
/** | ||
* Webhook event data. | ||
* | ||
* @var array | ||
*/ | ||
public $data; | ||
|
||
/** | ||
* Stripe event object. | ||
* | ||
* @var \Stripe\Event | ||
*/ | ||
public $event; | ||
|
||
/** | ||
* Sets the default values for the event. | ||
* | ||
* @param string $type | ||
* Webhook event type. | ||
* @param object $data | ||
* Webhook event data. | ||
* @param \Stripe\Event $event | ||
* Stripe event object. | ||
*/ | ||
public function __construct(string $type, $data, Event $event = NULL) { | ||
$this->type = $type; | ||
$this->data = $data; | ||
$this->event = $event; | ||
} | ||
|
||
} |
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,40 @@ | ||
<?php | ||
|
||
namespace Drupal\provider_stripe\Event; | ||
|
||
use Drupal\Component\Serialization\Json; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
|
||
/** | ||
* Class StripeApiWebhookSubscriber. | ||
* | ||
* Provides the webhook subscriber functionality. | ||
*/ | ||
class StripeApiWebhookSubscriber implements EventSubscriberInterface { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getSubscribedEvents() { | ||
$events['provider_stripe.webhook'][] = ['onIncomingWebhook']; | ||
return $events; | ||
} | ||
|
||
/** | ||
* Process an incoming webhook. | ||
* | ||
* @param \Drupal\provider_stripe\Event\StripeApiWebhookEvent $event | ||
* Logs an incoming webhook of the setting is on. | ||
*/ | ||
public function onIncomingWebhook(StripeApiWebhookEvent $event) { | ||
$config = \Drupal::config('provider_stripe.settings'); | ||
if ($config->get('log_webhooks')) { | ||
\Drupal::logger('provider_stripe') | ||
->info('Processed webhook: @name<br /><br />Data: @data', [ | ||
'@name' => $event->type, | ||
'@data' => Json::encode($event->data), | ||
]); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.