PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.16.9
GiveWP – Donation Plugin and Fundraising Platform v4.16.9
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 2.30.0 All 255 releases
give / src / Framework / PaymentGateways / CommandHandlers / SubscriptionSyncedHandler.php

SubscriptionSyncedHandler.php in GiveWP – Donation Plugin and Fundraising Platform 4.16.9, at src/Framework/PaymentGateways/CommandHandlers/SubscriptionSyncedHandler.php

72 lines 2.5 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\CommandHandlers;
4
5 use Exception;
6 use Give\Donations\Models\Donation;
7 use Give\Framework\Http\Response\Types\JsonResponse;
8 use Give\Framework\PaymentGateways\Commands\SubscriptionSynced;
9
10 use function Give\Framework\Http\Response\response;
11
12 /**
13 * @since 2.33.0
14 */
15 class SubscriptionSyncedHandler
16 {
17 /**
18 * @since 2.33.0
19 *
20 * @throws Exception
21 */
22 public function __invoke(SubscriptionSynced $subscriptionSynced): JsonResponse
23 {
24 $dateTimeFormat = get_option('date_format') . ' ' . get_option('time_format');
25
26 $details = [
27 'currentStatus' => $subscriptionSynced->subscription->getOriginal('status'),
28 'gatewayStatus' => $subscriptionSynced->subscription->status,
29 'currentPeriod' => $subscriptionSynced->subscription->getOriginal('period'),
30 'gatewayPeriod' => $subscriptionSynced->subscription->period,
31 'currentCreatedAt' => date($dateTimeFormat,
32 $subscriptionSynced->subscription->getOriginal('createdAt')->getTimestamp()),
33 'gatewayCreatedAt' => date($dateTimeFormat, $subscriptionSynced->subscription->createdAt->getTimestamp()),
34 ];
35 $subscriptionSynced->subscription->save();
36
37 $missingTransactions = [];
38 foreach ($subscriptionSynced->missingDonations as $missingDonation) {
39 $missingTransactions[] = $this->getTransactionData($missingDonation);
40 }
41
42 $presentTransactions = [];
43 foreach ($subscriptionSynced->presentDonations as $presentDonation) {
44 $presentTransactions[] = $this->getTransactionData($presentDonation);
45 }
46
47 return response()->json([
48 'details' => $details,
49 'missingTransactions' => $missingTransactions,
50 'presentTransactions' => $presentTransactions,
51 'notice' => $subscriptionSynced->notice,
52 ]);
53 }
54
55 /**
56 * @since 2.33.0
57 */
58 private function getTransactionData(Donation $donation): array
59 {
60 $dateTimeFormat = get_option('date_format') . ' ' . get_option('time_format');
61
62 return [
63 'id' => $donation->id,
64 'gatewayTransactionId' => $donation->gatewayTransactionId,
65 'amount' => $donation->amount->getCurrency()->getCode() . ' ' . $donation->amount->formatToDecimal(),
66 'createdAt' => date($dateTimeFormat, $donation->createdAt->getTimestamp()),
67 'status' => $donation->status->getValue(),
68 'type' => $donation->type->getValue(),
69 ];
70 }
71 }
72