PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.15.5
GiveWP – Donation Plugin and Fundraising Platform v4.15.5
4.18.0 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 All 257 releases
give / src / Framework / PaymentGateways / Webhooks / EventHandlers / SubscriptionFirstDonationCompleted.php

SubscriptionFirstDonationCompleted.php in GiveWP – Donation Plugin and Fundraising Platform 4.15.5, at src/Framework/PaymentGateways/Webhooks/EventHandlers/SubscriptionFirstDonationCompleted.php

81 lines 3.1 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\Webhooks\EventHandlers;
4
5 use Exception;
6 use Give\Donations\Models\DonationNote;
7 use Give\Donations\ValueObjects\DonationStatus;
8 use Give\Framework\PaymentGateways\Log\PaymentGatewayLog;
9 use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateDonationStatus;
10 use Give\Framework\PaymentGateways\Webhooks\EventHandlers\Actions\UpdateSubscriptionStatus;
11 use Give\Subscriptions\ValueObjects\SubscriptionStatus;
12
13 /**
14 * @since 3.6.0
15 */
16 class SubscriptionFirstDonationCompleted
17 {
18 /**
19 * @since 4.5.0 Add $setDonationComplete and $gatewaySubscriptionId parameters
20 * @since 3.6.0
21 */
22 public function __invoke(
23 string $gatewayTransactionId,
24 string $message = '',
25 bool $setSubscriptionActive = true,
26 bool $setDonationComplete = true,
27 string $gatewaySubscriptionId = ''
28 )
29 {
30 $donation = give()->donations->getByGatewayTransactionId($gatewayTransactionId);
31
32 if (! $donation && ! empty($gatewaySubscriptionId) && $subscription = give()->subscriptions->getByGatewaySubscriptionId($gatewaySubscriptionId)) {
33 $donation = $subscription->initialDonation();
34 $donation->gatewayTransactionId = $gatewayTransactionId;
35 $donation->save();
36 }
37
38 if ( ! $donation || ! $donation->type->isSubscription() || $donation->id !== $donation->subscription->initialDonation()->id) {
39 return;
40 }
41
42 try {
43 if ($setDonationComplete && ! $donation->status->isComplete()) {
44 if (empty($message)) {
45 $message = __('Subscription First Donation Completed.', 'give');
46 }
47
48 (new UpdateDonationStatus())($donation, DonationStatus::COMPLETE(), $message);
49 } else {
50 if (empty($message)) {
51 $message = __('Subscription First Donation Updated.', 'give');
52 }
53
54 DonationNote::create([
55 'donationId' => $donation->id,
56 'content' => $message . ' ' . sprintf(__('%s transaction ID: %s', 'give'),
57 $donation->gateway()->getName(),
58 $donation->gatewayTransactionId
59 ),
60 ]);
61 }
62
63 if ($setSubscriptionActive && ! $donation->subscription->status->isActive()) {
64 (new UpdateSubscriptionStatus())($donation->subscription, SubscriptionStatus::ACTIVE(),
65 __('Subscription Active After The First Donation Got Completed.', 'give'));
66 }
67 } catch (Exception $e) {
68 PaymentGatewayLog::error(
69 sprintf('Subscription First Donation Failed! Error: %s',
70 $e->getCode() . ' - ' . $e->getMessage()),
71 [
72 'Donation Id' => $donation->id,
73 'Gateway Transaction Id' => $gatewayTransactionId,
74 'Gateway Subscription Id' => $donation->subscriptionId,
75 'message' => $message,
76 ]
77 );
78 }
79 }
80 }
81