| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\PayPalCommerce\Webhooks; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 7 |
use Give\PaymentGateways\PayPalCommerce\Repositories\Webhooks; |
| 8 |
use Give\Route\PayPalWebhooks as WebhooksRoute; |
| 9 |
use Give_Admin_Settings; |
| 10 |
|
| 11 |
class WebhookChecker |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @since 2.9.0 |
| 15 |
* |
| 16 |
* @var Webhooks |
| 17 |
*/ |
| 18 |
private $webhooksRepository; |
| 19 |
|
| 20 |
/** |
| 21 |
* @since 2.9.0 |
| 22 |
* |
| 23 |
* @var WebhooksRoute |
| 24 |
*/ |
| 25 |
private $webhooksRoute; |
| 26 |
|
| 27 |
/** |
| 28 |
* @since 2.9.0 |
| 29 |
* |
| 30 |
* @var WebhookRegister |
| 31 |
*/ |
| 32 |
private $webhookRegister; |
| 33 |
|
| 34 |
/** |
| 35 |
* @since 2.9.0 |
| 36 |
* |
| 37 |
* @var MerchantDetail |
| 38 |
*/ |
| 39 |
private $merchantDetails; |
| 40 |
|
| 41 |
/** |
| 42 |
* WebhookChecker constructor. |
| 43 |
* |
| 44 |
* @since 2.9.0 |
| 45 |
* |
| 46 |
* @param Webhooks $webhooksRepository |
| 47 |
* @param MerchantDetail $merchantDetails |
| 48 |
* @param WebhooksRoute $webhooksRoute |
| 49 |
* @param WebhookRegister $webhookRegister |
| 50 |
*/ |
| 51 |
public function __construct( |
| 52 |
Webhooks $webhooksRepository, |
| 53 |
MerchantDetail $merchantDetails, |
| 54 |
WebhooksRoute $webhooksRoute, |
| 55 |
WebhookRegister $webhookRegister |
| 56 |
) { |
| 57 |
$this->webhooksRepository = $webhooksRepository; |
| 58 |
$this->merchantDetails = $merchantDetails; |
| 59 |
$this->webhooksRoute = $webhooksRoute; |
| 60 |
$this->webhookRegister = $webhookRegister; |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Checks whether the webhook configuration has changed. If it has, then update the webhook with PayPal. |
| 65 |
* |
| 66 |
* @since 2.32.0 Remove second argument from updateWebhook function. |
| 67 |
* @since 2.9.0 |
| 68 |
*/ |
| 69 |
public function checkWebhookCriteria() |
| 70 |
{ |
| 71 |
if (wp_doing_ajax() || wp_doing_cron()) { |
| 72 |
return; |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! $this->merchantDetails->accessToken) { |
| 76 |
return; |
| 77 |
} |
| 78 |
|
| 79 |
$webhookConfig = $this->webhooksRepository->getWebhookConfig(); |
| 80 |
|
| 81 |
if ($webhookConfig === null) { |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$webhookUrl = $this->webhooksRoute->getRouteUrl(); |
| 86 |
$registeredEvents = $this->webhookRegister->getRegisteredEvents(); |
| 87 |
|
| 88 |
$hasMissingEvents = ! empty( |
| 89 |
array_merge( |
| 90 |
array_diff($registeredEvents, $webhookConfig->events), |
| 91 |
array_diff($webhookConfig->events, $registeredEvents) |
| 92 |
) |
| 93 |
); |
| 94 |
|
| 95 |
// Update the webhook if the return url or events have changed |
| 96 |
if ($webhookUrl !== $webhookConfig->returnUrl || $hasMissingEvents) { |
| 97 |
try { |
| 98 |
$this->webhooksRepository->updateWebhook($webhookConfig->id); |
| 99 |
|
| 100 |
$webhookConfig->returnUrl = $webhookUrl; |
| 101 |
$webhookConfig->events = $registeredEvents; |
| 102 |
|
| 103 |
$this->webhooksRepository->saveWebhookConfig($webhookConfig); |
| 104 |
} catch (Exception $exception) { |
| 105 |
Give_Admin_Settings::add_error( |
| 106 |
'paypal-webhook-update-error', |
| 107 |
'There was a problem updating your PayPal Donations webhook. Please disconnect your account and reconnect it.' |
| 108 |
); |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|