registerWebhookEventHandlers(); } /** * @since 4.5.0 */ private function registerWebhookEventHandlers() { add_action('init', function () { $registeredPaymentGatewayIds = give()->gateways->getPaymentGateways(); foreach ($registeredPaymentGatewayIds as $gatewayId => $class) { $this->addDonationStatusEventHandlers($gatewayId); $this->addSubscriptionStatusEventHandlers($gatewayId); $this->addSubscriptionFirstDonationEventHandler($gatewayId); $this->addSubscriptionRenewalDonationEventHandler($gatewayId); } }, 999); } /** * @since 4.16.0 Add $donationId to support gateways that only receive the transaction ID via webhook (e.g. PayFast). * @since 4.5.0 */ private function addDonationStatusEventHandlers(string $gatewayId) { foreach (DonationStatus::values() as $status) { if ($eventHandlerClass = (new GetEventHandlerClassByDonationStatus())($status)) { Hooks::addAction( sprintf('givewp_%s_webhook_event_donation_status_%s', $gatewayId, $status->getValue()), $eventHandlerClass, '__invoke', 10, 4 ); } } } /** * @since 4.5.0 */ private function addSubscriptionStatusEventHandlers(string $gatewayId) { foreach (SubscriptionStatus::values() as $status) { if ($eventHandlerClass = (new GetEventHandlerClassBySubscriptionStatus())($status)) { $parameterCount = $status->isActive() ? 3 : 2; Hooks::addAction( sprintf('givewp_%s_webhook_event_subscription_status_%s', $gatewayId, $status->getValue()), $eventHandlerClass, '__invoke', 10, $parameterCount ); } } } /** * @since 4.16.0 Add $donationId to support gateways that only receive the transaction ID via webhook (e.g. PayFast). * @since 4.5.0 */ private function addSubscriptionFirstDonationEventHandler(string $gatewayId) { Hooks::addAction( sprintf('givewp_%s_webhook_event_subscription_first_donation', $gatewayId), SubscriptionFirstDonationCompleted::class, '__invoke', 10, 6 ); } /** * @since 4.5.0 */ private function addSubscriptionRenewalDonationEventHandler(string $gatewayId) { Hooks::addAction( sprintf('givewp_%s_webhook_event_subscription_renewal_donation', $gatewayId), SubscriptionRenewalDonationCreated::class, '__invoke', 10, 2 ); } }