PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.0
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.0
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 / Services / Email / EmailPreviewService.php

EmailPreviewService.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.0, at app/Services/Email/EmailPreviewService.php

241 lines 8.3 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\Email;
4
5 use FluentCart\App\Services\DateTime\DateTime;
6 use FluentCart\Framework\Support\Collection;
7 use FluentCart\Framework\Support\Str;
8
9 class EmailPreviewService
10 {
11 public function getPreviewData(string $template): array
12 {
13 $order = $this->getDummyOrder();
14 $subscription = $this->getDummySubscription($order);
15 $customer = $order->customer;
16 $transaction = $order->getLatestTransaction();
17
18 $previewData = [
19 'order' => $order,
20 'subscription' => $subscription,
21 'customer' => $customer,
22 'transaction' => $transaction,
23 'reminder' => [
24 'stage' => 'before_0',
25 'due_at' => DateTime::gmtNow()->addDays(2)->format('Y-m-d H:i:s'),
26 'due_amount' => 4900,
27 'payment_link' => '#',
28 'order_ref' => $order->invoice_no,
29 'billing_date' => DateTime::gmtNow()->addDays(30)->format('Y-m-d H:i:s'),
30 'trial_end_date' => DateTime::gmtNow()->addDays(3)->format('Y-m-d H:i:s'),
31 ],
32 ];
33
34 if (Str::startsWith($template, 'subscription.')) {
35 $previewData['transaction'] = $subscription->getLatestTransaction();
36
37 if (Str::contains($template, 'trial_end')) {
38 $previewData['reminder']['stage'] = 'trial_end_3';
39 }
40
41 if (Str::contains($template, 'canceled')) {
42 $previewData['reason'] = __('canceled on customer request', 'fluent-cart');
43 $previewData['subscription'] = $this->getDummySubscription($previewData['order']);
44 }
45 }
46
47 return $previewData;
48 }
49
50 private function getDummyCustomer(): object
51 {
52 return (object) [
53 'full_name' => 'John Doe',
54 'first_name' => 'John',
55 'last_name' => 'Doe',
56 'email' => '[email protected]',
57 ];
58 }
59
60 private function getDummyTransaction(): object
61 {
62 return new class {
63 public $created_at;
64 public $total = 4900;
65 public $vendor_charge_id = 'ch_preview_123';
66 public $order = null;
67
68 public function __construct()
69 {
70 $this->created_at = DateTime::gmtNow();
71 }
72
73 public function getPaymentMethodText()
74 {
75 return 'card';
76 }
77 };
78 }
79
80 private function getDummyOrder(): object
81 {
82 $customer = $this->getDummyCustomer();
83 $transaction = $this->getDummyTransaction();
84
85 $order = new class($customer, $transaction, new Collection()) {
86 public $id = 1001;
87 public $uuid = 'preview-order-uuid';
88 public $invoice_no = 'FC-1001';
89 public $customer;
90 public $order_items;
91 public $subscriptions;
92 public $orderTaxRates;
93 public $billing_address;
94 public $shipping_address;
95 public $fulfillment_type = 'digital';
96 public $subtotal = 4900;
97 public $manual_discount_total = 0;
98 public $coupon_discount_total = 0;
99 public $shipping_tax = 0;
100 public $shipping_total = 0;
101 public $tax_total = 0;
102 public $tax_behavior = 2;
103 public $total_amount = 4900;
104 public $total_paid = 0;
105 public $total_refund = 0;
106 public $payment_status = 'pending';
107 public $config = [];
108 private $latestTransaction;
109
110 public function __construct($customer, $transaction, $emptyCollection)
111 {
112 $this->customer = $customer;
113 $this->latestTransaction = $transaction;
114 $this->order_items = new Collection([
115 [
116 'post_title' => 'Sample Product',
117 'quantity' => 1,
118 'title' => 'Standard Plan',
119 'payment_type' => 'onetime',
120 'payment_info' => '',
121 'formatted_total' => '$49.00',
122 'package_info' => __('Package: Medium Box · 30 × 20 × 15 cm · Wt: 2 kg · Shipping wt: 2.5 kg', 'fluent-cart'),
123 'other_info' => [
124 'package_name' => 'Medium Box',
125 'package_type' => 'box',
126 'package_length' => 30,
127 'package_width' => 20,
128 'package_height' => 15,
129 'package_dimension_unit' => 'cm',
130 'weight' => 2,
131 'weight_unit' => 'kg',
132 'package_weight' => 0.5,
133 'package_weight_unit' => 'kg',
134 'package_slug' => 'medium-box',
135 ],
136 ],
137 ]);
138 $this->subscriptions = $emptyCollection;
139 $this->orderTaxRates = $emptyCollection;
140 $this->billing_address = null;
141 $this->shipping_address = null;
142 }
143
144 public function getViewUrl($type = 'customer')
145 {
146 return '#';
147 }
148
149 public function getMeta($metaKey, $defaultValue = false)
150 {
151 if ($metaKey === 'due_date') {
152 return gmdate('Y-m-d H:i:s', strtotime('+7 days'));
153 }
154 return $defaultValue;
155 }
156
157 public function isReverseChargeTaxOrder()
158 {
159 return false;
160 }
161
162 public function getOrderRcMode()
163 {
164 return 'fixed';
165 }
166
167 public function getLatestTransaction()
168 {
169 return $this->latestTransaction;
170 }
171
172 public function getDownloads($scope = 'email')
173 {
174 return [];
175 }
176
177 public function getLicenses($with = ['product', 'productVariant'])
178 {
179 return new Collection([]);
180 }
181 };
182
183 // Templates reach the order through the transaction too (e.g. the
184 // renewal admin email's $transaction->order->getViewUrl('admin')).
185 $transaction->order = $order;
186
187 return $order;
188 }
189
190 private function getDummySubscription($order): object
191 {
192 $customer = $order->customer ?? $this->getDummyCustomer();
193 $transaction = $this->getDummyTransaction();
194
195 return new class($customer, $transaction, $order) {
196 public $id = 2001;
197 public $uuid = 'preview-subscription-uuid';
198 public $item_name = 'Sample Subscription';
199 public $display_item_name = 'Sample Subscription';
200 public $billing_interval = 'yearly';
201 public $next_billing_date;
202 public $recurring_total = 4900;
203 public $status = 'active';
204 public $trial_ends_at;
205 public $customer;
206 public $order;
207 private $latestTransaction;
208
209 public function __construct($customer, $transaction, $order)
210 {
211 $this->customer = $customer;
212 $this->latestTransaction = $transaction;
213 $this->order = $order;
214 $transaction->order = $order;
215 $this->next_billing_date = \FluentCart\App\Services\DateTime\DateTime::gmtNow()->addDays(30)->format('Y-m-d H:i:s');
216 $this->trial_ends_at = \FluentCart\App\Services\DateTime\DateTime::gmtNow()->addDays(3)->format('Y-m-d H:i:s');
217 }
218
219 public function getLatestTransaction()
220 {
221 return $this->latestTransaction;
222 }
223
224 public function getViewUrl($type = 'customer')
225 {
226 return '#';
227 }
228
229 public function getReactivateUrl()
230 {
231 return '#';
232 }
233
234 public function getPaymentMethodText()
235 {
236 return 'Visa ***4242';
237 }
238 };
239 }
240 }
241