/
OrderEmailNotifier.php
341 lines (298 loc) · 9.71 KB
/
OrderEmailNotifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
<?php
namespace SilverShop\Checkout;
use SilverShop\Extension\ShopConfigExtension;
use SilverShop\Model\Order;
use SilverShop\Model\OrderStatusLog;
use SilverShop\Page\CheckoutPage;
use SilverStripe\Control\Director;
use SilverStripe\Control\Email\Email;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\Core\Extensible;
use SilverStripe\Core\Injector\Injectable;
use SilverStripe\View\SSViewer;
/**
* Handles email notifications to customers and / or admins.
*
* @package shop
*/
class OrderEmailNotifier
{
use Injectable;
use Configurable;
use Extensible;
/**
* @var Order $order
*/
protected $order;
/**
* @var bool
*/
protected $debugMode = false;
/**
* list of current themes for switching when sending emails
* @var array|string[]
*/
private array $current_themes;
/**
* Assign the order to a local variable
*
* @param Order $order
*/
public function __construct(Order $order)
{
$this->order = $order;
$this->current_themes = SSViewer::get_themes();
}
/**
* @param bool $bool
* @return $this
*/
public function setDebugMode($bool)
{
$this->debugMode = $bool;
return $this;
}
public function getOrder(): Order
{
return $this->order;
}
/**
* @param string $template
* @param string $subject
*
* @return Email
*/
protected function buildEmail($template, $subject)
{
$from = ShopConfigExtension::config()->email_from ? ShopConfigExtension::config()->email_from : Email::config()->admin_email;
$to = $this->order->getLatestEmail();
$checkoutpage = CheckoutPage::get()->first();
$completemessage = $checkoutpage ? $checkoutpage->dbObject('PurchaseComplete') : '';
/**
* @var Email $email
*/
$email = Email::create()
->setHTMLTemplate($template)
->setFrom($from)
->setTo($to)
->setSubject($subject);
$email->setData(
[
'PurchaseCompleteMessage' => $completemessage,
'Order' => $this->order,
'BaseURL' => Director::absoluteBaseURL(),
]
);
return $email;
}
/**
* Send a mail of the order to the client (and another to the admin).
*
* @param string $template - the template of the email you wish to send
* @param string $subject - subject of the email
* @param bool $copyToAdmin - true by default, whether it should send a copy to the admin
*
* @return bool|string
*/
public function sendEmail(string $template, string $subject, ?bool $copyToAdmin = true)
{
SSViewer::set_themes(Config::inst()->get(SSViewer::class, 'themes'));
$email = $this->buildEmail($template, $subject);
if ($copyToAdmin) {
$email->setBcc(Email::config()->admin_email);
}
$this->extend('updateClientEmail', $email);
if ($this->debugMode) {
$ret = $this->debug($email);
} else {
$ret = $email->send();
}
SSViewer::set_themes($this->current_themes);
return $ret;
}
/**
* Send a mail to the admin).
*
* @param string $template - the template of the email you wish to send
* @param string $subject - subject of the email
* @param bool $copyToAdmin - true by default, whether it should send a copy to the admin
*
* @return bool|string
*/
public function sendAdminEmail(string $template, string $subject)
{
SSViewer::set_themes(Config::inst()->get(SSViewer::class, 'themes'));
$email = $this->buildEmail($template, $subject)
->setTo(Email::config()->admin_email);
$this->extend('updateAdminEmail', $email);
if ($this->debugMode) {
$ret = $this->debug($email);
} else {
$ret = $email->send();
}
SSViewer::set_themes($this->current_themes);
return $ret;
}
/**
* Send customer a confirmation that the order has been received
*
* @return bool
*/
public function sendConfirmation()
{
$subject = _t(
'SilverShop\ShopEmail.ConfirmationSubject',
'Order #{OrderNo} confirmation',
'',
['OrderNo' => $this->order->Reference]
);
return $this->sendEmail(
'SilverShop/Model/Order_ConfirmationEmail',
$subject,
self::config()->bcc_confirmation_to_admin
);
}
/**
* Notify store owner about new order.
*
* @return bool|string
*/
public function sendAdminNotification()
{
$subject = _t(
'SilverShop\ShopEmail.AdminNotificationSubject',
'Order #{OrderNo} notification',
'',
['OrderNo' => $this->order->Reference]
);
return $this->sendAdminEmail('SilverShop/Model/Order_AdminNotificationEmail', $subject);
}
/**
* Send customer an order receipt email.
* Precondition: The order payment has been successful
*/
public function sendReceipt()
{
$subject = _t(
'SilverShop\ShopEmail.ReceiptSubject',
'Order #{OrderNo} receipt',
'',
['OrderNo' => $this->order->Reference]
);
return $this->sendEmail(
'SilverShop/Model/Order_ReceiptEmail',
$subject,
self::config()->bcc_receipt_to_admin
);
}
/**
* Sends an email to the admin that an order has been cancelled
*/
public function sendCancelNotification()
{
SSViewer::set_themes(Config::inst()->get(SSViewer::class, 'themes'));
$email = Email::create()
->setSubject(_t(
'SilverShop\ShopEmail.CancelSubject',
'Order #{OrderNo} cancelled by member',
'',
['OrderNo' => $this->order->Reference]
))
->setFrom(Email::config()->admin_email)
->setTo(Email::config()->admin_email)
->setHTMLTemplate('SilverShop/Model/Order_CancelEmail')
->setData(
[
'Order' => $this->order,
]
);
$this->extend('updateCancelNotificationEmail', $email);
if ($this->debugMode) {
$ret = $this->debug($email);
} else {
$ret = $email->send();
}
SSViewer::set_themes($this->current_themes);
return $ret;
}
/**
* Send an email to the customer containing the latest note of {@link OrderStatusLog} and the current status.
*
* @param string $title Subject for email
* @param string $note Optional note-content (instead of using the OrderStatusLog)
*
* @return bool|string
*/
public function sendStatusChange($title = null, $note = null)
{
SSViewer::set_themes(Config::inst()->get(SSViewer::class, 'themes'));
$latestLog = null;
if (!$note) {
// Find the latest log message that hasn't been sent to the client yet, but can be (e.g. is visible)
$latestLog = OrderStatusLog::get()
->filter("OrderID", $this->order->ID)
->filter("SentToCustomer", 0)
->filter("VisibleToCustomer", 1)
->first();
if ($latestLog) {
$note = $latestLog->Note;
$title = $latestLog->Title;
}
}
if (Config::inst()->get(OrderProcessor::class, 'receipt_email')) {
$adminEmail = Config::inst()->get(OrderProcessor::class, 'receipt_email');
} else {
$adminEmail = Email::config()->admin_email;
}
/**
* @var Email $e
*/
$email = Email::create()
->setFrom($adminEmail)
->setSubject(_t('SilverShop\ShopEmail.StatusChangeSubject', 'SilverShop – {Title}', ['Title' => $title]))
->setTo($this->order->getLatestEmail())
->setHTMLTemplate('SilverShop/Model/Order_StatusEmail')
->setData(
[
'Order' => $this->order,
'Note' => $note,
'FromEmail' => $adminEmail
]
);
$this->extend('updateStatusChangeEmail', $email);
if ($this->debugMode) {
$result = $this->debug($email);
} else {
$result = $email->send();
}
if ($latestLog) {
// If we got the note from an OrderStatusLog object, mark it as having been sent to the customer
$latestLog->SentToCustomer = true;
$latestLog->write();
}
SSViewer::set_themes($this->current_themes);
return $result;
}
/**
* The new Email::debug method in SilverStripe dumps the entire message with all message parts,
* which makes it unusable to preview an Email.
* This method simulates the old way of the message output and renders only the HTML body.
*
* @param Email $email
* @return string
*/
protected function debug(Email $email)
{
SSViewer::set_themes(Config::inst()->get(SSViewer::class, 'themes'));
$htmlTemplate = $email->getHTMLTemplate();
$htmlRender = $email->getData()->renderWith($htmlTemplate)->RAW();
$template = $email->getHTMLTemplate();
$headers = $email->getHeaders()->toString();SSViewer::set_themes($this->current_themes);
return "<h2>Email HTML template: $template</h2>\n" .
"<h3>Headers</h3>" .
"<pre>$headers</pre>" .
"<h3>Body</h3>" .
$htmlRender;
}
}