WebhookEvents.php
271 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\PaymentGateways\Webhooks; |
| 4 | |
| 5 | use Give\Donations\ValueObjects\DonationStatus; |
| 6 | use Give\Framework\Support\Facades\ActionScheduler\AsBackgroundJobs; |
| 7 | use Give\Subscriptions\ValueObjects\SubscriptionStatus; |
| 8 | |
| 9 | /** |
| 10 | * @since 4.5.0 |
| 11 | */ |
| 12 | class WebhookEvents |
| 13 | { |
| 14 | /** |
| 15 | * @var string |
| 16 | */ |
| 17 | protected $gatewayId; |
| 18 | |
| 19 | /** |
| 20 | * @since 4.5.0 |
| 21 | */ |
| 22 | public function __construct(string $gatewayId) |
| 23 | { |
| 24 | $this->gatewayId = $gatewayId; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * @since 4.5.0 |
| 29 | */ |
| 30 | public function donationAbandoned( |
| 31 | string $gatewayTransactionId, |
| 32 | string $message = '', |
| 33 | $skipRecurringDonations = false |
| 34 | ) { |
| 35 | $this->setDonationStatus(DonationStatus::ABANDONED(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * @since 4.5.0 |
| 40 | */ |
| 41 | public function donationCancelled( |
| 42 | string $gatewayTransactionId, |
| 43 | string $message = '', |
| 44 | $skipRecurringDonations = false |
| 45 | ) { |
| 46 | $this->setDonationStatus(DonationStatus::CANCELLED(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * @since 4.5.0 |
| 51 | */ |
| 52 | public function donationCompleted( |
| 53 | string $gatewayTransactionId, |
| 54 | string $message = '', |
| 55 | $skipRecurringDonations = false |
| 56 | ) { |
| 57 | $this->setDonationStatus(DonationStatus::COMPLETE(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @since 4.5.0 |
| 62 | */ |
| 63 | public function donationFailed(string $gatewayTransactionId, string $message = '', $skipRecurringDonations = false) |
| 64 | { |
| 65 | $this->setDonationStatus(DonationStatus::FAILED(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @since 4.5.0 |
| 70 | */ |
| 71 | public function donationPending(string $gatewayTransactionId, string $message = '', $skipRecurringDonations = false) |
| 72 | { |
| 73 | $this->setDonationStatus(DonationStatus::PENDING(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * @since 4.5.0 |
| 78 | */ |
| 79 | public function donationPreapproval( |
| 80 | string $gatewayTransactionId, |
| 81 | string $message = '', |
| 82 | $skipRecurringDonations = false |
| 83 | ) { |
| 84 | $this->setDonationStatus(DonationStatus::PREAPPROVAL(), $gatewayTransactionId, $message, |
| 85 | $skipRecurringDonations); |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @since 4.5.0 |
| 90 | */ |
| 91 | public function donationProcessing( |
| 92 | string $gatewayTransactionId, |
| 93 | string $message = '', |
| 94 | $skipRecurringDonations = false |
| 95 | ) |
| 96 | { |
| 97 | $this->setDonationStatus(DonationStatus::PROCESSING(), $gatewayTransactionId, $message, |
| 98 | $skipRecurringDonations); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @since 4.5.0 |
| 103 | */ |
| 104 | public function donationRefunded(string $gatewayTransactionId, string $message = '', $skipRecurringDonations = false) |
| 105 | { |
| 106 | $this->setDonationStatus(DonationStatus::REFUNDED(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @since 4.5.0 |
| 111 | */ |
| 112 | public function donationRevoked(string $gatewayTransactionId, string $message = '', $skipRecurringDonations = false) |
| 113 | { |
| 114 | $this->setDonationStatus(DonationStatus::REVOKED(), $gatewayTransactionId, $message, $skipRecurringDonations); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @since 4.5.0 |
| 119 | */ |
| 120 | public function subscriptionActive( |
| 121 | string $gatewaySubscriptionId, |
| 122 | string $message = '', |
| 123 | bool $initialDonationShouldBeCompleted = false |
| 124 | ) |
| 125 | { |
| 126 | $this->setSubscriptionStatus(SubscriptionStatus::ACTIVE(), $gatewaySubscriptionId, $message, |
| 127 | $initialDonationShouldBeCompleted); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @since 4.5.0 |
| 132 | */ |
| 133 | public function subscriptionCancelled(string $gatewaySubscriptionId, string $message = '') |
| 134 | { |
| 135 | $this->setSubscriptionStatus(SubscriptionStatus::CANCELLED(), $gatewaySubscriptionId, $message); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @since 4.5.0 |
| 140 | */ |
| 141 | public function subscriptionCompleted(string $gatewaySubscriptionId, string $message = '') |
| 142 | { |
| 143 | $this->setSubscriptionStatus(SubscriptionStatus::COMPLETED(), $gatewaySubscriptionId, $message); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @since 4.5.0 |
| 148 | */ |
| 149 | public function subscriptionExpired(string $gatewaySubscriptionId, string $message = '') |
| 150 | { |
| 151 | $this->setSubscriptionStatus(SubscriptionStatus::EXPIRED(), $gatewaySubscriptionId, $message); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * @since 4.5.0 |
| 156 | */ |
| 157 | public function subscriptionFailing(string $gatewaySubscriptionId, string $message = '') |
| 158 | { |
| 159 | $this->setSubscriptionStatus(SubscriptionStatus::FAILING(), $gatewaySubscriptionId, $message); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @since 4.5.0 |
| 164 | */ |
| 165 | public function subscriptionPaused(string $gatewaySubscriptionId, string $message = '') |
| 166 | { |
| 167 | $this->setSubscriptionStatus(SubscriptionStatus::PAUSED(), $gatewaySubscriptionId, $message); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * @since 4.5.0 |
| 172 | */ |
| 173 | public function subscriptionPending(string $gatewaySubscriptionId, string $message = '') |
| 174 | { |
| 175 | $this->setSubscriptionStatus(SubscriptionStatus::PENDING(), $gatewaySubscriptionId, $message); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * @since 4.5.0 |
| 180 | */ |
| 181 | public function subscriptionSuspended(string $gatewaySubscriptionId, string $message = '') |
| 182 | { |
| 183 | $this->setSubscriptionStatus(SubscriptionStatus::SUSPENDED(), $gatewaySubscriptionId, $message); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * @since 4.5.0 |
| 188 | * |
| 189 | * @return int The webhook event ID. Zero if there was an error setting the event. |
| 190 | */ |
| 191 | public function subscriptionFirstDonation( |
| 192 | string $gatewayTransactionId, |
| 193 | string $message = '', |
| 194 | bool $setSubscriptionActive = true, |
| 195 | bool $setDonationComplete = true, |
| 196 | string $gatewaySubscriptionId = '' |
| 197 | ): int { |
| 198 | $hook = sprintf('givewp_%s_webhook_event_subscription_first_donation', $this->gatewayId); |
| 199 | $args = [$gatewayTransactionId, $message, $setSubscriptionActive, $setDonationComplete, $gatewaySubscriptionId]; |
| 200 | $group = $this->getGroup(); |
| 201 | |
| 202 | return AsBackgroundJobs::enqueueAsyncAction($hook, $args, $group); |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @since 4.5.0 |
| 207 | * |
| 208 | * @return int The webhook event ID. Zero if there was an error setting the event. |
| 209 | */ |
| 210 | public function subscriptionRenewalDonation( |
| 211 | string $gatewaySubscriptionId, |
| 212 | string $gatewayTransactionId, |
| 213 | string $message = '' |
| 214 | ): int { |
| 215 | $hook = sprintf('givewp_%s_webhook_event_subscription_renewal_donation', $this->gatewayId); |
| 216 | $args = [$gatewaySubscriptionId, $gatewayTransactionId, $message]; |
| 217 | $group = $this->getGroup(); |
| 218 | |
| 219 | return AsBackgroundJobs::enqueueAsyncAction($hook, $args, $group); |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * @since 4.5.0 |
| 224 | * |
| 225 | * @return int The webhook event ID. Zero if there was an error setting the event. |
| 226 | */ |
| 227 | protected function setDonationStatus( |
| 228 | DonationStatus $status, |
| 229 | string $gatewayTransactionId, |
| 230 | string $message = '', |
| 231 | $skipRecurringDonations = false |
| 232 | ): int { |
| 233 | $hook = sprintf('givewp_%s_webhook_event_donation_status_%s', $this->gatewayId, $status->getValue()); |
| 234 | $args = [$gatewayTransactionId, $message, $skipRecurringDonations]; |
| 235 | $group = $this->getGroup(); |
| 236 | |
| 237 | return AsBackgroundJobs::enqueueAsyncAction($hook, $args, $group); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @since 4.5.0 |
| 242 | * |
| 243 | * @return int The webhook event ID. Zero if there was an error setting the event. |
| 244 | */ |
| 245 | protected function setSubscriptionStatus( |
| 246 | SubscriptionStatus $status, |
| 247 | string $gatewaySubscriptionId, |
| 248 | string $message = '', |
| 249 | bool $initialDonationShouldBeCompleted = false |
| 250 | ): int { |
| 251 | $hook = sprintf('givewp_%s_webhook_event_subscription_status_%s', $this->gatewayId, $status->getValue()); |
| 252 | $args = [$gatewaySubscriptionId, $message]; |
| 253 | |
| 254 | if ($initialDonationShouldBeCompleted) { |
| 255 | $args[] = $initialDonationShouldBeCompleted; |
| 256 | } |
| 257 | |
| 258 | $group = $this->getGroup(); |
| 259 | |
| 260 | return AsBackgroundJobs::enqueueAsyncAction($hook, $args, $group); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @since 4.5.0 |
| 265 | */ |
| 266 | protected function getGroup(): string |
| 267 | { |
| 268 | return 'givewp-payment-gateway-' . $this->gatewayId; |
| 269 | } |
| 270 | } |
| 271 |