WebhookRegister.php
106 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\PayPalStandard\Webhooks; |
| 4 | |
| 5 | use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 6 | use Give\PaymentGateways\Gateways\PayPalStandard\Webhooks\Listeners\EventListener; |
| 7 | use Give\PaymentGateways\Gateways\PayPalStandard\Webhooks\Listeners\PaymentUpdated; |
| 8 | |
| 9 | class WebhookRegister |
| 10 | { |
| 11 | /** |
| 12 | * Array of the PayPal webhook event handlers. Add-ons can use the registerEventHandler method |
| 13 | * to add additional events/handlers. |
| 14 | * |
| 15 | * Structure: PayPalTractionType => EventHandlerClass |
| 16 | * |
| 17 | * @since 2.19.0 |
| 18 | * |
| 19 | * @var string[] |
| 20 | */ |
| 21 | private $eventHandlers = [ |
| 22 | 'web_accept' => PaymentUpdated::class, |
| 23 | 'cart' => PaymentUpdated::class, |
| 24 | ]; |
| 25 | |
| 26 | /** |
| 27 | * Use this to register additional events and handlers |
| 28 | * |
| 29 | * @since 2.19.0 |
| 30 | * |
| 31 | * @param string $payPalEvent PayPal event to listen for, i.e. CHECKOUT.ORDER.APPROVED |
| 32 | * @param string $eventHandler The FQCN of the event handler |
| 33 | * |
| 34 | * @return $this |
| 35 | */ |
| 36 | public function registerEventHandler($payPalEvent, $eventHandler) |
| 37 | { |
| 38 | if (isset($this->eventHandlers[$payPalEvent])) { |
| 39 | throw new InvalidArgumentException('Cannot register an already registered event'); |
| 40 | } |
| 41 | |
| 42 | if ( ! is_subclass_of($eventHandler, EventListener::class)) { |
| 43 | throw new InvalidArgumentException('Listener must be a subclass of ' . EventListener::class); |
| 44 | } |
| 45 | |
| 46 | $this->eventHandlers[$payPalEvent] = $eventHandler; |
| 47 | |
| 48 | return $this; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Registers multiple event handlers using an array where the key is the |
| 53 | * |
| 54 | * @since 2.19.0 |
| 55 | * |
| 56 | * @param array $handlers = [ 'web_accept' => EventHandler::class ] |
| 57 | * https://developer.paypal.com/api/nvp-soap/ipn/IPNandPDTVariables/#link-ipntransactiontypes |
| 58 | */ |
| 59 | public function registerEventHandlers(array $handlers) |
| 60 | { |
| 61 | foreach ($handlers as $event => $handler) { |
| 62 | $this->registerEventHandler($event, $handler); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns Event Listener instance for given event |
| 68 | * |
| 69 | * @since 2.19.0 |
| 70 | * |
| 71 | * @param string $event |
| 72 | * |
| 73 | * @return EventListener |
| 74 | */ |
| 75 | public function getEventHandler($event) |
| 76 | { |
| 77 | return give($this->eventHandlers[$event]); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Checks whether the given event is registered |
| 82 | * |
| 83 | * @since 2.19.0 |
| 84 | * |
| 85 | * @param string $event |
| 86 | * |
| 87 | * @return bool |
| 88 | */ |
| 89 | public function hasEventRegistered($event) |
| 90 | { |
| 91 | return isset($this->eventHandlers[$event]); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Returns an array of the registered events |
| 96 | * |
| 97 | * @since 2.19.0 |
| 98 | * |
| 99 | * @return string[] |
| 100 | */ |
| 101 | public function getRegisteredEvents() |
| 102 | { |
| 103 | return array_keys($this->eventHandlers); |
| 104 | } |
| 105 | } |
| 106 |