-
Notifications
You must be signed in to change notification settings - Fork 195
/
Copy pathServiceProvider.php
99 lines (90 loc) · 3.11 KB
/
ServiceProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
namespace Give\Framework\PaymentGateways;
use Give\Donations\ValueObjects\DonationStatus;
use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\GetEventHandlerClassByDonationStatus;
use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\GetEventHandlerClassBySubscriptionStatus;
use Give\Framework\PaymentGateways\Webhooks\EventHandlers\SubscriptionFirstDonationCompleted;
use Give\Framework\PaymentGateways\Webhooks\EventHandlers\SubscriptionRenewalDonationCreated;
use Give\Helpers\Hooks;
use Give\ServiceProviders\ServiceProvider as ServiceProviderInterface;
use Give\Subscriptions\ValueObjects\SubscriptionStatus;
/**
* @unreleased
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* @unreleased
*/
public function register()
{
// TODO: Implement register() method.
}
/**
* @unreleased
*/
public function boot()
{
$this->registerWebhookEventHandlers();
}
private function registerWebhookEventHandlers()
{
add_action('give_init', function () {
$registeredPaymentGatewayIds = give()->gateways->getPaymentGateways();
foreach ($registeredPaymentGatewayIds as $gatewayId) {
$this->addDonationStatusEventHandlers($gatewayId);
$this->addSubscriptionStatusEventHandlers($gatewayId);
$this->addSubscriptionFirstDonationEventHandler($gatewayId);
$this->addSubscriptionRenewalDonationEventHandler($gatewayId);
}
}, 999);
}
/**
* @unreleased
*/
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
);
}
}
}
/**
* @unreleased
*/
private function addSubscriptionStatusEventHandlers(string $gatewayId)
{
foreach (SubscriptionStatus::values() as $status) {
if ($eventHandlerClass = (new GetEventHandlerClassBySubscriptionStatus())($status)) {
Hooks::addAction(
sprintf('givewp_%s_webhook_event_subscription_status_%s', $gatewayId, $status->getValue()),
$eventHandlerClass
);
}
}
}
/**
* @unreleased
*/
private function addSubscriptionFirstDonationEventHandler(string $gatewayId)
{
Hooks::addAction(
sprintf('givewp_%s_webhook_event_subscription_first_donation', $gatewayId),
SubscriptionFirstDonationCompleted::class
);
}
/**
* @unreleased
*/
private function addSubscriptionRenewalDonationEventHandler(string $gatewayId)
{
Hooks::addAction(
sprintf('givewp_%s_webhook_event_subscription_renewal_donation', $gatewayId),
SubscriptionRenewalDonationCreated::class
);
}
}