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