PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.5.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.5.1
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.5.1, at app/Services/Payments/PaymentInstance.php

155 lines 4.4 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 $taxBehavior = (int) $this->order->tax_behavior;
67
68 $extraItems = $this->order->order_items->filter(function ($item) {
69 return $item->payment_type !== 'subscription' && $item->payment_type !== 'signup_fee' && $item->payment_type !== 'fee';
70 });
71
72 $extraAmount = 0;
73 foreach ($extraItems as $item) {
74 $extraAmount += (int) $item->line_total;
75
76 if ($taxBehavior === 1) {
77 $extraAmount += (int) $item->tax_amount;
78 } elseif ($taxBehavior === 3) {
79 $inclusive = (bool) Arr::get($item->line_meta, 'tax_config.inclusive', false);
80 if (!$inclusive) {
81 $extraAmount += (int) $item->tax_amount;
82 }
83 }
84 }
85
86 // shipping charge, in case addons are physical products
87 $shippingCharge = (int) $this->order->shipping_total;
88 if ($shippingCharge) {
89 $extraAmount += $shippingCharge;
90
91 if ($taxBehavior === 1) {
92 $extraAmount += (int) $this->order->shipping_tax;
93 } elseif ($taxBehavior === 3) {
94 $storeTaxBehavior = (int) $this->order->getMeta('store_tax_behavior', 1);
95 if ($storeTaxBehavior === 1) {
96 $extraAmount += (int) $this->order->shipping_tax;
97 }
98 }
99 }
100
101 return $extraAmount;
102 }
103
104
105 public function getSubscriptionCancelAtTimeStamp()
106 {
107 if (!$this->subscription) {
108 return null;
109 }
110
111 $billTimes = $this->subscription->getRequiredBillTimes();
112
113 if ($billTimes <= 0) {
114 return null;
115 }
116
117 $interval = $this->subscription->billing_interval;
118
119
120 if ($interval == 'daily') {
121 $interval = 'day';
122 }
123
124 $interValMaps = [
125 'day' => 'days',
126 'weekly' => 'weeks',
127 'monthly' => 'months',
128 'quarterly' => 'months',
129 'half_yearly' => 'months',
130 'yearly' => 'years'
131 ];
132
133 if (isset($interValMaps[$interval]) && $billTimes > 0) {
134 $interval = $interValMaps[$interval];
135
136 // Adjust billTimes for quarterly and half_yearly
137 if ($this->subscription->billing_interval === 'quarterly') {
138 $billTimes = $billTimes * 3; // Convert to months
139 } elseif ($this->subscription->billing_interval === 'half_yearly') {
140 $billTimes = $billTimes * 6; // Convert to months
141 }
142 }
143
144
145 $timestamp = strtotime('+ ' . $billTimes . ' ' . $interval);
146
147 if (!$this->subscription->bill_count && $this->subscription->trial_days) {
148 $timestamp = $timestamp + $this->subscription->trial_days * 24 * 60 * 60; // Add trial days in seconds
149 }
150
151 return $timestamp;
152 }
153 }
154
155