-
Notifications
You must be signed in to change notification settings - Fork 70
/
adyenService.js
70 lines (60 loc) · 1.86 KB
/
adyenService.js
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
const Site = require('dw/system/Site');
const Resource = require('dw/web/Resource');
const HashMap = require('dw/util/HashMap');
const Mail = require('dw/net/Mail');
const Template = require('dw/util/Template');
const Transaction = require('dw/system/Transaction');
const Order = require('dw/order/Order');
const COHelpers = require('*/cartridge/scripts/checkout/checkoutHelpers');
function sendEmail(order) {
const confirmationEmail = new Mail();
const context = new HashMap();
const savedOrderModel = order.custom.Adyen_CustomerEmail;
const orderObject = { order: JSON.parse(savedOrderModel) };
confirmationEmail.addTo(order.customerEmail);
confirmationEmail.setSubject(
Resource.msg('subject.order.confirmation.email', 'order', null),
);
confirmationEmail.setFrom(
Site.current.getCustomPreferenceValue('customerServiceEmail') ||
'no-reply@salesforce.com',
);
Object.keys(orderObject).forEach((key) => {
context.put(key, orderObject[key]);
});
const template = new Template('checkout/confirmation/confirmationEmail');
const content = template.render(context).text;
confirmationEmail.setContent(content, 'text/html', 'UTF-8');
confirmationEmail.send();
order.custom.Adyen_CustomerEmail = null;
}
function submit(order) {
try {
Transaction.begin();
// Places the order if not placed yet
if (order.status === Order.ORDER_STATUS_CREATED) {
// custom fraudDetection
const fraudDetectionStatus = { status: 'success' };
const placeOrderResult = COHelpers.placeOrder(
order,
fraudDetectionStatus,
);
if (placeOrderResult.error) {
return placeOrderResult;
}
}
sendEmail(order);
Transaction.commit();
return {
order_created: true,
};
} catch (e) {
Transaction.rollback();
return {
error: true,
};
}
}
module.exports = {
submit,
};