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

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