PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.1.0
GiveWP – Donation Plugin and Fundraising Platform v4.1.0
4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 All 253 releases
give / src / Controller / PayPalWebhooks.php

PayPalWebhooks.php in GiveWP – Donation Plugin and Fundraising Platform 4.1.0, at src/Controller/PayPalWebhooks.php

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