PayPalWebhooks.php
98 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Controller; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 7 | use Give\PaymentGateways\PayPalCommerce\Repositories\Webhooks; |
| 8 | use Give\PaymentGateways\PayPalCommerce\DataTransferObjects\PayPalWebhookHeaders; |
| 9 | use Give\PaymentGateways\PayPalCommerce\Webhooks\WebhookRegister; |
| 10 | |
| 11 | class PayPalWebhooks { |
| 12 | /** |
| 13 | * @since 2.8.0 |
| 14 | * |
| 15 | * @var MerchantDetails |
| 16 | */ |
| 17 | private $merchantRepository; |
| 18 | |
| 19 | /** |
| 20 | * @var Webhooks |
| 21 | */ |
| 22 | private $webhookRepository; |
| 23 | |
| 24 | /** |
| 25 | * @since 2.9.0 |
| 26 | * |
| 27 | * @var WebhookRegister |
| 28 | */ |
| 29 | private $webhookRegister; |
| 30 | |
| 31 | /** |
| 32 | * PayPalWebhooks constructor. |
| 33 | * |
| 34 | * @since 2.8.0 |
| 35 | * |
| 36 | * @param MerchantDetails $merchantRepository |
| 37 | * @param WebhookRegister $register |
| 38 | * @param Webhooks $webhookRepository |
| 39 | */ |
| 40 | public function __construct( MerchantDetails $merchantRepository, WebhookRegister $register, Webhooks $webhookRepository ) { |
| 41 | $this->merchantRepository = $merchantRepository; |
| 42 | $this->webhookRegister = $register; |
| 43 | $this->webhookRepository = $webhookRepository; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Handles all webhook event requests. First it verifies that authenticity of the event with |
| 48 | * PayPal, and then it passes the event along to the appropriate listener to finish. |
| 49 | * |
| 50 | * @since 2.8.0 |
| 51 | * |
| 52 | * @throws Exception |
| 53 | */ |
| 54 | public function handle() { |
| 55 | if ( ! $this->merchantRepository->accountIsConnected() ) { |
| 56 | return; |
| 57 | } |
| 58 | |
| 59 | $merchantDetails = $this->merchantRepository->getDetails(); |
| 60 | |
| 61 | $event = json_decode( file_get_contents( 'php://input' ), false ); |
| 62 | |
| 63 | // If we receive an event that we're not expecting, just ignore it |
| 64 | if ( ! $this->webhookRegister->hasEventRegistered( $event->event_type ) ) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | $payPalHeaders = PayPalWebhookHeaders::fromHeaders( getallheaders() ); |
| 69 | |
| 70 | if ( ! $this->webhookRepository->verifyEventSignature( $merchantDetails->accessToken, $event, $payPalHeaders ) ) { |
| 71 | give_record_gateway_error( |
| 72 | 'Failed webhook event verification', |
| 73 | print_r( |
| 74 | [ |
| 75 | 'merchant' => $merchantDetails, |
| 76 | 'event' => $event, |
| 77 | 'headers' => getallheaders(), |
| 78 | ], |
| 79 | true |
| 80 | ) |
| 81 | ); |
| 82 | throw new Exception( 'Failed event verification' ); |
| 83 | } |
| 84 | |
| 85 | try { |
| 86 | $this->webhookRegister |
| 87 | ->getEventHandler( $event->event_type ) |
| 88 | ->processEvent( $event ); |
| 89 | |
| 90 | } catch ( Exception $exception ) { |
| 91 | $eventType = empty( $event->event_type ) ? 'Unknown' : $event->event_type; |
| 92 | give_record_gateway_error( "Error processing webhook: {$eventType}", print_r( $event, true ) ); |
| 93 | |
| 94 | throw $exception; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 |