Actions
1 week ago
CommandHandlers
2 years ago
Commands
2 years ago
Contracts
11 months ago
Controllers
2 years ago
DataTransferObjects
3 years ago
Exceptions
4 years ago
Log
4 years ago
Routes
3 years ago
Traits
2 years ago
Webhooks
1 week ago
DonationSummary.php
3 years ago
PaymentGateway.php
9 months ago
PaymentGatewayRegister.php
2 years ago
ServiceProvider.php
1 week ago
SubscriptionModule.php
1 year ago
ServiceProvider.php
112 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways; |
| 4 | |
| 5 | use Give\Donations\ValueObjects\DonationStatus; |
| 6 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\GetEventHandlerClassByDonationStatus; |
| 7 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\GetEventHandlerClassBySubscriptionStatus; |
| 8 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\SubscriptionFirstDonationCompleted; |
| 9 | use Give\Framework\PaymentGateways\Webhooks\EventHandlers\SubscriptionRenewalDonationCreated; |
| 10 | use Give\Helpers\Hooks; |
| 11 | use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface; |
| 12 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 13 | |
| 14 | /** |
| 15 | * @since 4.5.0 |
| 16 | */ |
| 17 | class ServiceProvider implements ServiceProviderInterface |
| 18 | { |
| 19 | /** |
| 20 | * @since 4.5.0 |
| 21 | */ |
| 22 | public function register() |
| 23 | { |
| 24 | // TODO: Implement register() method. |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @since 4.5.0 |
| 29 | */ |
| 30 | public function boot() |
| 31 | { |
| 32 | $this->registerWebhookEventHandlers(); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @since 4.5.0 |
| 37 | */ |
| 38 | private function registerWebhookEventHandlers() |
| 39 | { |
| 40 | add_action('init', function () { |
| 41 | $registeredPaymentGatewayIds = give()->gateways->getPaymentGateways(); |
| 42 | foreach ($registeredPaymentGatewayIds as $gatewayId => $class) { |
| 43 | $this->addDonationStatusEventHandlers($gatewayId); |
| 44 | $this->addSubscriptionStatusEventHandlers($gatewayId); |
| 45 | $this->addSubscriptionFirstDonationEventHandler($gatewayId); |
| 46 | $this->addSubscriptionRenewalDonationEventHandler($gatewayId); |
| 47 | } |
| 48 | }, 999); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @since 4.16.0 Add $donationId to support gateways that only receive the transaction ID via webhook (e.g. PayFast). |
| 53 | * @since 4.5.0 |
| 54 | */ |
| 55 | private function addDonationStatusEventHandlers(string $gatewayId) |
| 56 | { |
| 57 | foreach (DonationStatus::values() as $status) { |
| 58 | if ($eventHandlerClass = (new GetEventHandlerClassByDonationStatus())($status)) { |
| 59 | Hooks::addAction( |
| 60 | sprintf('givewp_%s_webhook_event_donation_status_%s', $gatewayId, $status->getValue()), |
| 61 | $eventHandlerClass, |
| 62 | '__invoke', |
| 63 | 10, |
| 64 | 4 |
| 65 | ); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @since 4.5.0 |
| 72 | */ |
| 73 | private function addSubscriptionStatusEventHandlers(string $gatewayId) |
| 74 | { |
| 75 | foreach (SubscriptionStatus::values() as $status) { |
| 76 | if ($eventHandlerClass = (new GetEventHandlerClassBySubscriptionStatus())($status)) { |
| 77 | $parameterCount = $status->isActive() ? 3 : 2; |
| 78 | Hooks::addAction( |
| 79 | sprintf('givewp_%s_webhook_event_subscription_status_%s', $gatewayId, $status->getValue()), |
| 80 | $eventHandlerClass, '__invoke', 10, $parameterCount |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @since 4.16.0 Add $donationId to support gateways that only receive the transaction ID via webhook (e.g. PayFast). |
| 88 | * @since 4.5.0 |
| 89 | */ |
| 90 | private function addSubscriptionFirstDonationEventHandler(string $gatewayId) |
| 91 | { |
| 92 | Hooks::addAction( |
| 93 | sprintf('givewp_%s_webhook_event_subscription_first_donation', $gatewayId), |
| 94 | SubscriptionFirstDonationCompleted::class, |
| 95 | '__invoke', |
| 96 | 10, |
| 97 | 6 |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @since 4.5.0 |
| 103 | */ |
| 104 | private function addSubscriptionRenewalDonationEventHandler(string $gatewayId) |
| 105 | { |
| 106 | Hooks::addAction( |
| 107 | sprintf('givewp_%s_webhook_event_subscription_renewal_donation', $gatewayId), |
| 108 | SubscriptionRenewalDonationCreated::class, '__invoke', 10, 2 |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 |