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