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