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