PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.21
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.21
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 trunk All 48 releases
fluent-cart / app / Hooks / Handlers / ShortCodes / ReceiptHandler.php

ReceiptHandler.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.21, at app/Hooks/Handlers/ShortCodes/ReceiptHandler.php

218 lines 7.8 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\Hooks\Handlers\ShortCodes;
4
5 use FluentCart\Api\Orders;
6 use FluentCart\App\App;
7 use FluentCart\App\Helpers\Status;
8 use FluentCart\App\Models\OrderMeta;
9 use FluentCart\App\Models\OrderOperation;
10 use FluentCart\App\Models\OrderTransaction;
11 use FluentCart\App\Services\DateTime\DateTime;
12 use FluentCart\App\Services\Email\EmailNotifications;
13 use FluentCart\App\Services\FrontendView;
14 use FluentCart\App\Services\Renderer\Receipt\ReceiptRenderer;
15 use FluentCart\App\Services\Renderer\Receipt\ThankYouRender;
16 use FluentCart\App\Services\ShortCodeParser\ShortcodeTemplateBuilder;
17 use FluentCart\App\Vite;
18 use FluentCart\Framework\Support\Arr;
19
20 class ReceiptHandler
21 {
22 protected string $slug = '';
23 protected string $assetsPath = '';
24
25 protected $productId;
26
27 public function register()
28 {
29 add_shortcode('fluent_cart_receipt', [$this, 'renderRedirectPage']);
30 }
31
32 public function renderRedirectPage($attributes): string
33 {
34 $isReceipt = Arr::get($attributes, 'type', '') === 'receipt';
35 $request = App::request();
36 $orderHash = sanitize_text_field($request->get('order_hash'));
37 $transactionHash = sanitize_text_field($request->get('trx_hash'));
38
39 do_action('fluent_cart/before_render_redirect_page', [
40 'order_hash' => $orderHash,
41 'trx_hash' => $transactionHash,
42 'method' => sanitize_text_field($request->get('method')),
43 'is_receipt' => $isReceipt
44 ]);
45
46
47 $isEmailFooter = EmailNotifications::getSettings('show_email_footer');
48
49 if (empty($transactionHash) && empty($orderHash)) {
50 if ($request->get('action') === 'edit') {
51 return '';
52 }
53 return $this->renderNotFoundPage();
54 }
55
56 if (!empty($orderHash)) {
57 $order = (new Orders())->getBy('uuid', $orderHash);
58 } else {
59 $transaction = OrderTransaction::query()->where('uuid', $transactionHash)->first();
60 if (!$transaction) {
61 ob_start();
62 FrontendView::renderNotFoundPage(
63 __('Not found.', 'fluent-cart'),
64 __('Sorry, no transaction found.', 'fluent-cart'),
65 __('The provided transaction appears invalid, expired or does’t match our records', 'fluent-cart')
66 );
67 return ob_get_clean();
68 //return '<div class="fluent_cart_order_confirmation">No Transaction found!</div>';
69 }
70 $order = (new Orders())->getById($transaction->order_id);
71 }
72
73 if (empty($order)) {
74 return $this->renderNotFoundPage();
75 }
76
77 $userId = get_current_user_id();
78 $isOwn = $userId && $order->customer->user_id == $userId;
79
80 $isAdmin = is_user_logged_in() && current_user_can('manage_options');
81 $payment_status = Arr::get($order, 'payment_status');
82 $orderPlacedAt = DateTime::anytimeToGmt(Arr::get($order, 'created_at'));
83 $showOrder =
84 $isOwn ||
85 $isAdmin ||
86 $payment_status === Status::PAYMENT_PAID ||
87 DateTime::now() < $orderPlacedAt->addHours(2);
88
89
90 if (!$showOrder) {
91 return $this->renderNotFoundPage();
92 }
93
94 // Handle PDF download when FluentPDF is active and download=1 is requested
95 $isDownload = (bool) $request->get('download', false);
96 if ($isDownload && App::isProActive() && defined('FLUENT_PDF')) {
97 $order->loadMissing(['customer', 'order_items', 'billing_address', 'shipping_address', 'transactions', 'orderTaxRates.tax_rate']);
98 $pdfPath = apply_filters('fluent_cart/pdf/generate_receipt', null, [
99 'order' => $order,
100 'template_id' => 'order_receipt',
101 ]);
102
103 if ($pdfPath && file_exists($pdfPath)) {
104 $fileName = sanitize_file_name('receipt-' . $order->invoice_no . '.pdf');
105 header('Content-Type: application/pdf');
106 header("Content-Disposition: attachment; filename=\"" . $fileName . "\"; filename*=UTF-8''" . rawurlencode($fileName));
107 header('Content-Length: ' . filesize($pdfPath));
108 header('Cache-Control: no-cache');
109 header('X-Content-Type-Options: nosniff');
110 readfile($pdfPath);
111 @unlink($pdfPath);
112 die();
113 }
114 // If PDF generation failed, fall through to render HTML receipt
115 }
116
117 $lastTransaction = OrderTransaction::query()
118 ->where('order_id', $order->id)
119 ->where('transaction_type', '=', Status::TRANSACTION_TYPE_CHARGE)
120 ->orderBy('id', 'DESC')
121 ->first();
122
123 $order->last_transaction = $lastTransaction;
124
125 $operation = OrderOperation::query()->where('order_id', $order->id)->first();
126
127 $isFirstTime = false;
128 if ($operation && !$operation->sales_recorded && $order->payment_status === Status::PAYMENT_PAID) {
129 $operation->sales_recorded = 1;
130 $operation->save();
131 $isFirstTime = true;
132 }
133
134 $vatTaxId = $order->getMeta('vat_tax_id', '');
135
136
137 ob_start();
138 $defaultBannerImage = Vite::getAssetUrl('images/email-template/email-banner.png');
139 //
140 // $templatePath = !$isReceipt ? 'invoice.thank_you' : 'invoice.receipt_slip';
141 // App::make('view')->render($templatePath, [
142 // 'default_banner_image' => $defaultBannerImage,
143 // 'order' => $order,
144 // 'is_first_time' => $isFirstTime,
145 // 'vat_tax_id' => $vatTaxId,
146 // 'order_operation' => $operation
147 // ]);
148 $config = [
149 'order' => $order,
150 'vat_tax_id' => $vatTaxId,
151 'order_operation' => $operation,
152 'is_first_time' => $isFirstTime,
153 'default_banner_image' => $defaultBannerImage,
154 'user_tz' => Arr::get($order->config, 'user_tz', wp_timezone_string()),
155 ];
156 if ($isReceipt) {
157 (new ReceiptRenderer($config))->render();
158 } else {
159 (new ThankYouRender($config))->render();
160 }
161
162 $slipView = ob_get_clean();
163
164
165 $parsedContent = ShortcodeTemplateBuilder::make($slipView, [
166 'order' => $order,
167 ]);
168
169 $app = fluentCart();
170 $slug = $app->config->get('app.slug');
171
172 Vite::enqueueStyle(
173 $slug . '_checkout_confirmation',
174 'public/checkout/style/confirmation.scss',
175 );
176
177 $content = '';
178
179 ob_start();
180
181 $footerContent = "<div style='padding: 15px; text-align: center; font-size: 16px; color: #2F3448;'>Powered by <a href='https://fluentcart.com' style='color: #017EF3; text-decoration: none;'>". __('FluentCart', 'fluent-cart') ."</a></div>";
182
183 if ((!App::isProActive() || $isEmailFooter == 'yes') && $isReceipt) {
184 $parsedContent .= $footerContent;
185 }
186
187 $viewData = [
188 'order' => $order,
189 'content' => $parsedContent,
190 'is_receipt' => $isReceipt
191 ];
192
193 if(Arr::get($viewData,'is_receipt')){
194 FrontendView::renderForPrint(Arr::get($viewData,'content'),[
195 'wp_head' => false,
196 'wp_footer' => false,
197 ]);
198 }else{
199 FrontendView::make(__('Thank you', 'fluent-cart'), Arr::get($viewData,'content'));
200 }
201
202
203 $content .= ob_get_clean();
204 return $content;
205 }
206
207 public function renderNotFoundPage()
208 {
209 ob_start();
210 FrontendView::renderNotFoundPage(
211 __('Not found.', 'fluent-cart'),
212 __('Sorry, no receipt found.', 'fluent-cart'),
213 __("The requested receipt is invalid", 'fluent-cart')
214 );
215 return ob_get_clean();
216 }
217 }
218