| 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 getPaymentMethodText() |
| 69 |
{ |
| 70 |
return 'card'; |
| 71 |
} |
| 72 |
}; |
| 73 |
} |
| 74 |
|
| 75 |
private function getDummyOrder(): object |
| 76 |
{ |
| 77 |
$customer = $this->getDummyCustomer(); |
| 78 |
$transaction = $this->getDummyTransaction(); |
| 79 |
|
| 80 |
return new class($customer, $transaction, new Collection()) { |
| 81 |
public $id = 1001; |
| 82 |
public $uuid = 'preview-order-uuid'; |
| 83 |
public $invoice_no = 'FC-1001'; |
| 84 |
public $customer; |
| 85 |
public $order_items; |
| 86 |
public $subscriptions; |
| 87 |
public $orderTaxRates; |
| 88 |
public $billing_address; |
| 89 |
public $shipping_address; |
| 90 |
public $fulfillment_type = 'digital'; |
| 91 |
public $subtotal = 4900; |
| 92 |
public $manual_discount_total = 0; |
| 93 |
public $coupon_discount_total = 0; |
| 94 |
public $shipping_tax = 0; |
| 95 |
public $shipping_total = 0; |
| 96 |
public $tax_total = 0; |
| 97 |
public $tax_behavior = 2; |
| 98 |
public $total_amount = 4900; |
| 99 |
public $total_paid = 0; |
| 100 |
public $total_refund = 0; |
| 101 |
public $payment_status = 'pending'; |
| 102 |
private $latestTransaction; |
| 103 |
|
| 104 |
public function __construct($customer, $transaction, $emptyCollection) |
| 105 |
{ |
| 106 |
$this->customer = $customer; |
| 107 |
$this->latestTransaction = $transaction; |
| 108 |
$this->order_items = new Collection([ |
| 109 |
[ |
| 110 |
'post_title' => 'Sample Product', |
| 111 |
'quantity' => 1, |
| 112 |
'title' => 'Standard Plan', |
| 113 |
'payment_type' => 'onetime', |
| 114 |
'payment_info' => '', |
| 115 |
'formatted_total' => '$49.00', |
| 116 |
'package_info' => __('Package: Medium Box · 30 × 20 × 15 cm · Wt: 2 kg · Shipping wt: 2.5 kg', 'fluent-cart'), |
| 117 |
'other_info' => [ |
| 118 |
'package_name' => 'Medium Box', |
| 119 |
'package_type' => 'box', |
| 120 |
'package_length' => 30, |
| 121 |
'package_width' => 20, |
| 122 |
'package_height' => 15, |
| 123 |
'package_dimension_unit' => 'cm', |
| 124 |
'weight' => 2, |
| 125 |
'weight_unit' => 'kg', |
| 126 |
'package_weight' => 0.5, |
| 127 |
'package_weight_unit' => 'kg', |
| 128 |
'package_slug' => 'medium-box', |
| 129 |
], |
| 130 |
], |
| 131 |
]); |
| 132 |
$this->subscriptions = $emptyCollection; |
| 133 |
$this->orderTaxRates = $emptyCollection; |
| 134 |
$this->billing_address = null; |
| 135 |
$this->shipping_address = null; |
| 136 |
} |
| 137 |
|
| 138 |
public function getViewUrl($type = 'customer') |
| 139 |
{ |
| 140 |
return '#'; |
| 141 |
} |
| 142 |
|
| 143 |
public function getLatestTransaction() |
| 144 |
{ |
| 145 |
return $this->latestTransaction; |
| 146 |
} |
| 147 |
|
| 148 |
public function getDownloads($scope = 'email') |
| 149 |
{ |
| 150 |
return []; |
| 151 |
} |
| 152 |
|
| 153 |
public function getLicenses($with = ['product', 'productVariant']) |
| 154 |
{ |
| 155 |
return new Collection([]); |
| 156 |
} |
| 157 |
}; |
| 158 |
} |
| 159 |
|
| 160 |
private function getDummySubscription($order): object |
| 161 |
{ |
| 162 |
$customer = $order->customer ?? $this->getDummyCustomer(); |
| 163 |
$transaction = $this->getDummyTransaction(); |
| 164 |
|
| 165 |
return new class($customer, $transaction, $order) { |
| 166 |
public $id = 2001; |
| 167 |
public $uuid = 'preview-subscription-uuid'; |
| 168 |
public $item_name = 'Sample Subscription'; |
| 169 |
public $billing_interval = 'yearly'; |
| 170 |
public $next_billing_date; |
| 171 |
public $recurring_total = 4900; |
| 172 |
public $status = 'active'; |
| 173 |
public $trial_ends_at; |
| 174 |
public $customer; |
| 175 |
public $order; |
| 176 |
private $latestTransaction; |
| 177 |
|
| 178 |
public function __construct($customer, $transaction, $order) |
| 179 |
{ |
| 180 |
$this->customer = $customer; |
| 181 |
$this->latestTransaction = $transaction; |
| 182 |
$this->order = $order; |
| 183 |
$this->next_billing_date = \FluentCart\App\Services\DateTime\DateTime::gmtNow()->addDays(30)->format('Y-m-d H:i:s'); |
| 184 |
$this->trial_ends_at = \FluentCart\App\Services\DateTime\DateTime::gmtNow()->addDays(3)->format('Y-m-d H:i:s'); |
| 185 |
} |
| 186 |
|
| 187 |
public function getLatestTransaction() |
| 188 |
{ |
| 189 |
return $this->latestTransaction; |
| 190 |
} |
| 191 |
|
| 192 |
public function getViewUrl($type = 'customer') |
| 193 |
{ |
| 194 |
return '#'; |
| 195 |
} |
| 196 |
|
| 197 |
public function getReactivateUrl() |
| 198 |
{ |
| 199 |
return '#'; |
| 200 |
} |
| 201 |
}; |
| 202 |
} |
| 203 |
} |
| 204 |
|