Actions
2 years 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
11 months ago
DonationSummary.php
3 years ago
PaymentGateway.php
9 months ago
PaymentGatewayRegister.php
2 years ago
ServiceProvider.php
11 months ago
SubscriptionModule.php
1 year ago
ServiceProvider.php
104 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.5.0 |
| 53 | */ |
| 54 | private function addDonationStatusEventHandlers(string $gatewayId) |
| 55 | { |
| 56 | foreach (DonationStatus::values() as $status) { |
| 57 | if ($eventHandlerClass = (new GetEventHandlerClassByDonationStatus())($status)) { |
| 58 | Hooks::addAction( |
| 59 | sprintf('givewp_%s_webhook_event_donation_status_%s', $gatewayId, $status->getValue()), |
| 60 | $eventHandlerClass, '__invoke', 10, 3 |
| 61 | ); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @since 4.5.0 |
| 68 | */ |
| 69 | private function addSubscriptionStatusEventHandlers(string $gatewayId) |
| 70 | { |
| 71 | foreach (SubscriptionStatus::values() as $status) { |
| 72 | if ($eventHandlerClass = (new GetEventHandlerClassBySubscriptionStatus())($status)) { |
| 73 | $parameterCount = $status->isActive() ? 3 : 2; |
| 74 | Hooks::addAction( |
| 75 | sprintf('givewp_%s_webhook_event_subscription_status_%s', $gatewayId, $status->getValue()), |
| 76 | $eventHandlerClass, '__invoke', 10, $parameterCount |
| 77 | ); |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @since 4.5.0 |
| 84 | */ |
| 85 | private function addSubscriptionFirstDonationEventHandler(string $gatewayId) |
| 86 | { |
| 87 | Hooks::addAction( |
| 88 | sprintf('givewp_%s_webhook_event_subscription_first_donation', $gatewayId), |
| 89 | SubscriptionFirstDonationCompleted::class, '__invoke', 10, 5 |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * @since 4.5.0 |
| 95 | */ |
| 96 | private function addSubscriptionRenewalDonationEventHandler(string $gatewayId) |
| 97 | { |
| 98 | Hooks::addAction( |
| 99 | sprintf('givewp_%s_webhook_event_subscription_renewal_donation', $gatewayId), |
| 100 | SubscriptionRenewalDonationCreated::class, '__invoke', 10, 2 |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 |