Source of file StripeWebhook.php
Size: 3,199 Bytes - Last Modified: 2021-12-23T10:57:23+00:00
/var/www/docs.ssmods.com/process/src/src/StripeWebhook.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php namespace Vulcan\StripeWebhook; use Psr\SimpleCache\CacheInterface; use SilverStripe\Core\ClassInfo; use SilverStripe\Core\Config\Configurable; use SilverStripe\Core\Flushable; use SilverStripe\Core\Injector\Injectable; use SilverStripe\Core\Injector\Injector; use Vulcan\StripeWebhook\Handlers\StripeEventHandler; class StripeWebhook implements Flushable { use Injectable, Configurable; private static $secret_key = false; private static $endpoint_secret = false; public function __construct() { \Stripe\Stripe::setApiKey($this->getSecret()); } /** * @return string * @throws \RuntimeException */ public function getSecret() { $key = $this->config()->get('secret_key'); if (!$key) { throw new \RuntimeException('$secret_key needs to be configured via YML'); } return $key; } /** * @return string * @throws \RuntimeException */ public function getEndpointSecret() { $key = $this->config()->get('endpoint_secret'); if (!$key) { throw new \RuntimeException('$endpoint_secret needs to be configured via YML'); } return $key; } /** * Returns a multi-dimensional array of classes indexed by the event they handle * * @return array|mixed */ public function getHandlers() { /** * * * @var CacheInterface $cache */ $cache = Injector::inst()->get(CacheInterface::class . '.stripeWebhook'); if ($manifest = $cache->get('eventHandlers')) { return $manifest; } $classes = ClassInfo::subclassesFor(StripeEventHandler::class); $manifest = []; /** * * * @var StripeEventHandler $class */ foreach ($classes as $class) { if ($class == StripeEventHandler::class) { continue; } $handlerFor = $class::config()->get('events'); if (!$handlerFor) { throw new \InvalidArgumentException($class . ' is missing private static $events'); } if (is_array($handlerFor)) { foreach ($handlerFor as $event) { $manifest[$event][] = $class; } continue; } if (!is_string($handlerFor)) { throw new \InvalidArgumentException('Invalid type, expecting string or array but got ' . gettype($handlerFor) . ' instead'); } $manifest[$handlerFor] = $class; } $cache->set('eventHandlers', $manifest); return $manifest; } /** * This function is triggered early in the request if the "flush" query * parameter has been set. Each class that implements Flushable implements * this function which looks after it's own specific flushing functionality. * * @see FlushMiddleware */ public static function flush() { /** * * * @var CacheInterface $cache */ $cache = Injector::inst()->get(CacheInterface::class . '.stripeWebhook'); $cache->delete('eventHandlers'); } } |