PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.15.3
GiveWP – Donation Plugin and Fundraising Platform v4.15.3
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / Framework / PaymentGateways / ServiceProvider.php

ServiceProvider.php in GiveWP – Donation Plugin and Fundraising Platform 4.15.3, at src/Framework/PaymentGateways/ServiceProvider.php

104 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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