PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 All 49 releases
fluent-cart / app / Services / Payments / PaymentInstance.php

PaymentInstance.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.27, at app/Services/Payments/PaymentInstance.php

135 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Services\Payments;
4
5 use FluentCart\App\Helpers\Status;
6 use FluentCart\App\Models\Order;
7 use FluentCart\App\Models\OrderTransaction;
8 use FluentCart\App\Models\Subscription;
9 use FluentCart\App\Services\DateTime\DateTime;
10 use FluentCart\Framework\Support\Arr;
11
12 class PaymentInstance
13 {
14
15 /**
16 * @var Order
17 */
18 public $order;
19
20 public $transaction;
21
22 public $subscription;
23
24 public $paymentType;
25
26 public function __construct(Order $order)
27 {
28 $this->order = $order;
29 $this->setupData();
30 }
31
32 public function setupData()
33 {
34 $this->transaction = OrderTransaction::query()
35 ->where('transaction_type', Status::TRANSACTION_TYPE_CHARGE)
36 ->where('order_id', $this->order->id)
37 ->latest()
38 ->first();
39
40 $this->subscription = null;
41
42 if ($this->order->type === 'subscription') {
43 $this->subscription = Subscription::query()
44 ->where('parent_order_id', $this->order->id)
45 ->first();
46 } else if ($this->order->type === Status::ORDER_TYPE_RENEWAL) {
47 $this->subscription = Subscription::query()
48 ->where('parent_order_id', $this->order->parent_id)
49 ->first();
50 }
51
52 }
53
54 public function setTransaction(OrderTransaction $transaction)
55 {
56 $this->transaction = $transaction;
57 return $this;
58 }
59
60 public function getExtraAddonAmount()
61 {
62 if (empty($this->subscription)) {
63 return 0;
64 }
65
66 $extraItems = $this->order->order_items->filter(function ($item) {
67 return $item->payment_type !== 'subscription' && $item->payment_type !== 'signup_fee' && $item->payment_type !== 'fee';
68 });
69
70 $extraAmount = 0;
71 foreach ($extraItems as $item) {
72 $extraAmount += $item->line_total;
73 }
74
75 // shipping charge, in case addons are physical products
76 $shippingCharge = $this->order->shipping_total;
77 if ($shippingCharge) {
78 $extraAmount += $shippingCharge;
79 }
80
81 return $extraAmount;
82 }
83
84
85 public function getSubscriptionCancelAtTimeStamp()
86 {
87 if (!$this->subscription) {
88 return null;
89 }
90
91 $billTimes = $this->subscription->getRequiredBillTimes();
92
93 if ($billTimes <= 0) {
94 return null;
95 }
96
97 $interval = $this->subscription->billing_interval;
98
99
100 if ($interval == 'daily') {
101 $interval = 'day';
102 }
103
104 $interValMaps = [
105 'day' => 'days',
106 'weekly' => 'weeks',
107 'monthly' => 'months',
108 'quarterly' => 'months',
109 'half_yearly' => 'months',
110 'yearly' => 'years'
111 ];
112
113 if (isset($interValMaps[$interval]) && $billTimes > 0) {
114 $interval = $interValMaps[$interval];
115
116 // Adjust billTimes for quarterly and half_yearly
117 if ($this->subscription->billing_interval === 'quarterly') {
118 $billTimes = $billTimes * 3; // Convert to months
119 } elseif ($this->subscription->billing_interval === 'half_yearly') {
120 $billTimes = $billTimes * 6; // Convert to months
121 }
122 }
123
124
125 $timestamp = strtotime('+ ' . $billTimes . ' ' . $interval);
126
127 if (!$this->subscription->bill_count && $this->subscription->trial_days) {
128 $timestamp = $timestamp + $this->subscription->trial_days * 24 * 60 * 60; // Add trial days in seconds
129 }
130
131 return $timestamp;
132 }
133 }
134
135