PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.7.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.7.0
1.7.0 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 All 50 releases
fluent-cart / app / Services / Payments / PaymentReceipt.php

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

253 lines 8.2 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\Helper;
6 use FluentCart\Api\CurrencySettings;
7 use FluentCart\App\Helpers\ProductAdminHelper;
8 use FluentCart\App\Models\ProductVariation;
9 use FluentCart\App\Services\OrderService;
10 use FluentCart\App\Vite;
11 use FluentCart\Framework\Database\Orm\Collection;
12 use FluentCart\Framework\Support\Arr;
13 use FluentCart\Framework\Support\DateTime;
14
15 class PaymentReceipt
16 {
17 protected $order;
18
19 public function __construct($order)
20 {
21 $this->order = is_array($order) ? $order : $order->toArray();
22 }
23
24 public function getItems()
25 {
26
27 $this->getItemImage();
28 $orderItems = Arr::get($this->order, 'order_items', []);
29
30 $hasAdjustmentItem = false; // Flag to track if an 'adjustment' item exists
31
32 $subscriptionItem = current(array_filter($orderItems, function ($item) {
33 return Arr::get($item, 'payment_type') === 'subscription';
34 }));
35
36 foreach ($orderItems as $key => $item) {
37
38 if (in_array(Arr::get($item, 'payment_type'), ['signup_fee', 'fee'])) {
39 unset($orderItems[$key]);
40 }
41
42 // Check for 'adjustment' item and set the flag
43 if (Arr::get($item, 'payment_type') == 'adjustment') {
44 $hasAdjustmentItem = true;
45 if ($subscriptionItem) {
46 Arr::set($orderItems, $key . '.media_url', Arr::get($subscriptionItem, 'media_url'));
47 }
48 }
49
50 // Check for 'subscription' item and skip it if 'adjustment' item exists
51 if ($hasAdjustmentItem && Arr::get($item, 'payment_type') == 'subscription') {
52 unset($orderItems[$key]);
53 }
54
55 // Check for 'subscription' item and add it if 'adjustment' item not exists
56 if (!$hasAdjustmentItem && Arr::get($item, 'payment_type') == 'subscription') {
57 $signupFee = 0;
58 if (Arr::get($item, 'other_info.manage_setup_fee') == 'yes') {
59 $signupFee = Arr::get($item, 'other_info.signup_fee');
60 $orderItems[$key]['signup_fee_name'] = Arr::get($item, 'other_info.signup_fee_name') . ':';
61 // if (Arr::get($item, 'other_info')->setup_fee_per_item == 'yes') {
62 // $signupFee *= Arr::get($item, 'quantity');
63 // $orderItems[$key]['signup_fee_name'] .= ' x ' . Arr::get($item, 'quantity');
64 // }
65 // $orderItems[$key]['other_info']->signup_fee = $signupFee;
66 }
67 }
68 }
69
70 // Check for 'adjustment' item and update the order items if 'adjustment' item exists
71 if ($hasAdjustmentItem) {
72 Arr::set($this->order, 'order_items', $orderItems);
73 }
74
75 return $orderItems;
76 }
77
78 public function date($format = 'd M Y')
79 {
80 return DateTime::parse(Arr::get($this->order, 'created_at'))->format($format);
81 }
82
83 public function getEmail()
84 {
85 $customerEmail = Arr::get($this->order, 'customer.email');
86 if ($customerEmail) {
87 return $customerEmail;
88 }
89 return Arr::get($this->order, 'customer.billing_address.0.email', '');
90 }
91
92 public function total()
93 {
94 $totalAmount = Arr::get($this->order, 'total_amount', 0);
95
96 // $totalAmount -= Arr::get($this->order,'discount_total', 0);
97 return CurrencySettings::getPriceHtml(
98 $totalAmount,
99 Arr::get($this->order, 'currency')
100 );
101 }
102
103 public function shippingCharge()
104 {
105 $shippingTotal = Arr::get($this->order, 'shipping_total', 0);
106 if (empty($shippingTotal)) {
107 return 0;
108 }
109
110 return CurrencySettings::getPriceHtml(
111 $shippingTotal,
112 Arr::get($this->order, 'currency')
113 );
114 }
115
116 public function subtotal()
117 {
118 return CurrencySettings::getPriceHtml(
119 Arr::get($this->order, 'subtotal'),
120 Arr::get($this->order, 'currency'),
121 );
122 }
123
124 public function formatPriceHtml($amount, $code = null)
125 {
126 return CurrencySettings::getPriceHtml($amount, $code ?? Arr::get($this->order, 'currency'));
127 }
128
129 public function getItemImage()
130 {
131 $orderItems = Arr::get($this->order, 'order_items', []);
132 $variationIds = Collection::make($orderItems)->pluck('object_id')->toArray();
133 $productVariants = ProductVariation::query()
134 ->with(['product_detail', 'media'])
135 ->whereIn('id', $variationIds)
136 ->get();
137
138 $variantMap = Collection::make($productVariants);
139
140 foreach ($orderItems as $key => &$item) {
141
142 $variant = $variantMap->firstWhere('id', $item['object_id']);
143 // Receipts travel into emails and PDFs, where WebP is not safe
144 // (Outlook), so an image-less item gets the PNG placeholder.
145 $mediaUrl = '';
146 if ($variant) {
147 $detail = $variant->product_detail;
148 $mediaUrl = $variant->thumbnail ?: ($detail ? Arr::get((array) $detail->featured_media, 'url', '') : '');
149 //Image Check Test
150 //$mediaUrl = Arr::get($variant->media, 'meta_value.0.url', (new ProductAdminHelper())->getFeaturedMedia($variant->product_detail->featured_media));
151 }
152
153 Arr::set(
154 $this->order, 'order_items.' . $key . '.media_url',
155 $mediaUrl ?: Helper::getProductPlaceholderEmailUrl()
156 );
157
158 Arr::set(
159 $this->order,
160 'order_items.' . $key . '.currency',
161 Arr::get($this->order, 'currency', '')
162 );
163 }
164
165 return $orderItems;
166
167 // $productThumbnails = ProductMetaResource::findByIds($variationIds)->toArray();
168 // $mediaMap = Collection::make($productThumbnails)->pluck('meta_value', 'object_id');
169
170 // foreach ($order_items as $key => &$item) {
171 // if (isset($mediaMap[$item['object_id']])) {
172 // $mediaUrl = Arr::get($mediaMap[$item['object_id']], '0.url', '');
173 // Arr::set(
174 // $this->order,'order_items.'.$key.'.media_url',
175 // $mediaUrl
176 // );
177 // } else {
178 // Arr::set(
179 // $this->order,
180 // 'order_items.' . $key . '.media_url',
181 // Vite::getEnqueuePath('images/placeholder.svg')
182 // );
183 // }
184 // Arr::set(
185 // $this->order,
186 // 'order_items.' . $key . '.currency',
187 // Arr::get($this->order, 'currency', '')
188 // );
189 // }
190 // return $order_items;
191 }
192
193 public function __call($name, $arguments)
194 {
195 return Arr::get($this->order, $name);
196 }
197
198 public function totalDiscount($formatted = true)
199 {
200 $totalDiscount = Arr::get($this->order, 'manual_discount_total') + Arr::get($this->order, 'coupon_discount_total');
201
202 if (!$formatted) {
203 return $totalDiscount;
204 }
205 return CurrencySettings::getPriceHtml(
206 $totalDiscount,
207 Arr::get($this->order, 'currency'),
208 );
209 }
210
211 public function discount($formatted = true)
212 {
213
214
215 if (intval(Arr::get($this->order, 'discount_total') == 0)) {
216 return 0.0;
217 }
218
219 return (CurrencySettings::getPriceHtml(
220 Arr::get($this->order, 'manual_discount_total'),
221 Arr::get($this->order, 'currency'),
222 ));
223 }
224
225 public function proratedCredit()
226 {
227 $proratedCredit = 0.0;
228 $config = Arr::get($this->order, 'config', 0);
229 if (empty($config)) {
230 return 0.0;
231 }
232
233 if (is_string($config)) {
234 $config = json_decode($config, true);
235 }
236
237 if (is_array($config)) {
238 $proratedCredit = Arr::get($config, 'prorated_credit', 0.0);
239 if (intval($proratedCredit) == 0) {
240 return 0.0;
241 }
242 }
243
244 return intval($proratedCredit) > 0 ? CurrencySettings::getPriceHtml(
245 $proratedCredit,
246 Arr::get($this->order, 'currency'),
247 ) : 0.0;
248
249 }
250
251
252 }
253