diff --git a/give-addon-boilerplate.php b/give-addon-boilerplate.php index 233c6b1..ea7d4b5 100644 --- a/give-addon-boilerplate.php +++ b/give-addon-boilerplate.php @@ -1,66 +1,67 @@ -registerServiceProvider(AddonServiceProvider::class); - give()->registerServiceProvider(SettingsServiceProvider::class); - give()->registerServiceProvider(DomainServiceProvider::class); - give()->registerServiceProvider(FormExtensionServiceProvider::class); - give()->registerServiceProvider(OffSiteGatewayServiceProvider::class); - } - } -); - -// Check to make sure GiveWP core is installed and compatible with this add-on. -add_action('admin_init', [Environment::class, 'checkEnvironment']); +registerServiceProvider(AddonServiceProvider::class); + give()->registerServiceProvider(SettingsServiceProvider::class); + give()->registerServiceProvider(DomainServiceProvider::class); + give()->registerServiceProvider(FormExtensionServiceProvider::class); + give()->registerServiceProvider(OffSiteGatewayServiceProvider::class); + } + } +); + +// Check to make sure GiveWP core is installed and compatible with this add-on. +add_action('admin_init', [Environment::class, 'checkEnvironment']); diff --git a/src/OffSiteGateway/Gateway/OffSiteGateway.php b/src/OffSiteGateway/Gateway/OffSiteGateway.php index 94f2dd5..66d96b6 100644 --- a/src/OffSiteGateway/Gateway/OffSiteGateway.php +++ b/src/OffSiteGateway/Gateway/OffSiteGateway.php @@ -8,18 +8,19 @@ use Give\Donations\ValueObjects\DonationStatus; use Give\Framework\Http\Response\Types\RedirectResponse; use Give\Framework\PaymentGateways\Commands\RedirectOffsite; +use Give\Framework\PaymentGateways\Contracts\WebhookNotificationsListener; use Give\Framework\PaymentGateways\Exceptions\PaymentGatewayException; use Give\Framework\PaymentGateways\Log\PaymentGatewayLog; use Give\Framework\PaymentGateways\PaymentGateway; use Give\Framework\Support\Facades\Scripts\ScriptAsset; use GiveAddon\OffSiteGateway\DataTransferObjects\OffSiteGatewayPayment; use GiveAddon\OffSiteGateway\DataTransferObjects\OffSiteGatewayWebhookNotification; -use GiveAddon\OffSiteGateway\Webhooks\OffSiteGatewayWebhookNotificationHandler; /** + * @unreleased Use new WebhookNotificationsListener interface * @since 1.0.0 */ -class OffSiteGateway extends PaymentGateway +class OffSiteGateway extends PaymentGateway implements WebhookNotificationsListener { /** * @since 1.0.0 @@ -29,21 +30,6 @@ class OffSiteGateway extends PaymentGateway 'handleCanceledPaymentReturn', ]; - /** - * @since 1.0.0 - */ - public $routeMethods = [ - 'webhookNotificationsListener', - ]; - - /** - * @since 1.0.0 - */ - public function getWebhookNotificationsListener(): string - { - return $this->routeMethods[0]; - } - /** * @since 1.0.0 */ @@ -322,7 +308,7 @@ protected function handleCanceledPaymentReturn(array $queryParams): RedirectResp /** * @since 1.0.0 */ - protected function webhookNotificationsListener() + public function webhookNotificationsListener() { try { $webhookNotification = OffSiteGatewayWebhookNotification::fromRequest(give_clean($_REQUEST)); @@ -377,18 +363,17 @@ private function getPaymentsCancelURL(Donation $donation, $gatewayData): string /** * @since 1.0.0 + * + * @throws Exception */ private function getPaymentsWebhookUrl(Donation $donation): string { return urlencode( esc_url_raw( - $this->generateGatewayRouteUrl( - $this->getWebhookNotificationsListener(), - [ - 'notification_type' => 'payments', - 'payment_id' => $donation->id, - ] - ) + $this->webhook->getNotificationUrl([ + 'notification_type' => 'payments', + 'payment_id' => $donation->id, + ]) ) ); } diff --git a/src/OffSiteGateway/Webhooks/OffSiteGatewayWebhookNotificationHandler.php b/src/OffSiteGateway/Gateway/OffSiteGatewayWebhookNotificationHandler.php similarity index 75% rename from src/OffSiteGateway/Webhooks/OffSiteGatewayWebhookNotificationHandler.php rename to src/OffSiteGateway/Gateway/OffSiteGatewayWebhookNotificationHandler.php index 65b4671..c88251b 100644 --- a/src/OffSiteGateway/Webhooks/OffSiteGatewayWebhookNotificationHandler.php +++ b/src/OffSiteGateway/Gateway/OffSiteGatewayWebhookNotificationHandler.php @@ -1,10 +1,9 @@ gatewayPaymentStatus)) { case 'complete': - AsBackgroundJobs::enqueueAsyncAction( - 'givewp_' . OffSiteGateway::id() . '_event_donation_completed', - [$webhookNotification->gatewayPaymentId], - 'ADDON_TEXTDOMAIN' - ); + // Handling completed transactions... + OffSiteGateway::webhook()->events->donationCompleted($webhookNotification->gatewayPaymentId); /** * The block below is not necessary for real-world integrations; @@ -71,10 +68,16 @@ public function __invoke(OffSiteGatewayWebhookNotification $webhookNotification) events->donationFailed($webhookNotification->gatewayPaymentId); break; case 'cancelled': - // Handle cancelled transactions here... + // Handling cancelled transactions... + OffSiteGateway::webhook()->events->donationCancelled($webhookNotification->gatewayPaymentId); + break; + case 'refunded': + // Handling refunded transactions... + OffSiteGateway::webhook()->events->donationRefunded($webhookNotification->gatewayPaymentId); break; default: break; @@ -86,6 +89,7 @@ public function __invoke(OffSiteGatewayWebhookNotification $webhookNotification) */ private function isRecurringDonation(OffSiteGatewayWebhookNotification $webhookNotification): bool { + TestGateway::webhook()->getNotificationUrl(); return 'subscription' === $webhookNotification->gatewayNotificationType; } } diff --git a/src/OffSiteGateway/OffSiteGatewayServiceProvider.php b/src/OffSiteGateway/OffSiteGatewayServiceProvider.php index 8e951ce..44e0aaf 100644 --- a/src/OffSiteGateway/OffSiteGatewayServiceProvider.php +++ b/src/OffSiteGateway/OffSiteGatewayServiceProvider.php @@ -4,7 +4,6 @@ use Exception; use Give\Framework\PaymentGateways\PaymentGatewayRegister; -use Give\Framework\PaymentGateways\Webhooks\EventHandlers\DonationCompleted; use Give\Helpers\Hooks; use Give\ServiceProviders\ServiceProvider; use GiveAddon\OffSiteGateway\Gateway\OffSiteCheckoutPageSimulation; @@ -37,15 +36,6 @@ function (PaymentGatewayRegister $registrar) { } ); - /** - * We are using the DonationCompleted event handler class provided by Give Core to process the - * async background event which is created on the OffSiteGatewayWebhookNotificationHandler class. - * - * A full list of event handler classes can be found on the following link: - * @see https://github.com/impress-org/givewp/tree/develop/src/Framework/PaymentGateways/Webhooks/EventHandlers - */ - Hooks::addAction('givewp_' . OffSiteGateway::id() . '_event_donation_completed', DonationCompleted::class); - /** * IMPORTANT: remember to remove this hook when removing the OffSiteCheckoutPageSimulation class from your real-world gateway integration. */