| 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 |
|