-
Notifications
You must be signed in to change notification settings - Fork 69
/
adyenRedirectVerificationSHA256.ds
372 lines (310 loc) · 13 KB
/
adyenRedirectVerificationSHA256.ds
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Generate the parameters needed for the redirect to the Adyen Hosted Payment Page.
* A signature is calculated based on the configured HMAC code
*
* @input Order : dw.order.Order
* @input OrderNo : String The order no
* @input CurrentSession : dw.system.Session
* @input CurrentUser : dw.customer.Customer
* @input PaymentInstrument : dw.order.PaymentInstrument
* @input brandCode : String
* @input issuerId : String
* @input dob : String
* @input gender : String
* @input houseNumber : String
* @input houseExtension : String
* @input personalNumber : String
*
* @output merchantSig : String;
* @output Amount100 : String;
* @output shopperEmail : String;
* @output shopperReference : String;
* @output paramsMap : dw.util.SortedMap;
* @output sessionValidity : String;
*
*/
importPackage( dw.crypto );
importPackage( dw.system );
importPackage( dw.order );
importPackage( dw.util );
importPackage( dw.value);
importPackage( dw.net );
importPackage( dw.web );
//script include
var AdyenHelper = require ("int_adyen/cartridge/scripts/util/AdyenHelper");
function execute( args : PipelineDictionary ) : Number
{
if (verify(args) === PIPELET_ERROR) {
return PIPELET_ERROR;
}
return PIPELET_NEXT;
}
function verify (args) {
//configuration
var VERSION : String= "8";
var Mode : String = Site.getCurrent().getCustomPreferenceValue("Adyen_Mode");
var skinCode : String = Site.getCurrent().getCustomPreferenceValue("Adyen_skinCode");
var merchantAccount : String = Site.getCurrent().getCustomPreferenceValue("Adyen_merchantCode");
var HMACkey : String = Site.getCurrent().getCustomPreferenceValue("Adyen_HMACkey");
var recurringType : EnumValue = Site.getCurrent().getCustomPreferenceValue("Adyen_recurringType");
if(merchantAccount == null){
Logger.getLogger("Adyen").fatal("MERCHANTACCOUNT not set.");
return PIPELET_ERROR;
}
if(skinCode == null){
Logger.getLogger("Adyen").fatal("skinCode not set.");
return PIPELET_ERROR;
}
if(HMACkey == null){
Logger.getLogger("Adyen").fatal("HMACkey not set.");
return PIPELET_ERROR;
}
var paymentInstrument = args.PaymentInstrument;
if(paymentInstrument == null){
Logger.getLogger("Adyen").fatal("paymentInstrument is null.");
}
Logger.getLogger("Adyen").debug("PM : " +paymentInstrument.paymentMethod);
Logger.getLogger("Adyen").debug("merchantAccount : " + merchantAccount);
Logger.getLogger("Adyen").debug("skinCode : " + skinCode);
Logger.getLogger("Adyen").debug("HMACkey : " + HMACkey);
var orderNo = args.OrderNo;
if(orderNo == null){
Logger.getLogger("Adyen").fatal("No orderNo received.");
}
var order = args.Order;
if(order == null){
Logger.getLogger("Adyen").fatal("No existing order {0} received.",orderNo);
}
var currentUser = args.CurrentUser;
if(currentUser == null){
Logger.getLogger("Adyen").fatal("currentUser is null.",orderNo);
}
if (paymentInstrument.paymentMethod != "Adyen"){
return PIPELET_ERROR;
}
var myAmount:number = AdyenHelper.getCurrencyValueForApi(paymentInstrument.paymentTransaction.amount);
Logger.getLogger("Adyen").debug("Payment amount : " + myAmount + " - PIAmount : " + paymentInstrument.paymentTransaction.amount + "\n");
args.Amount100 = myAmount.toFixed(0);
var currencyCode:String = order.currencyCode;
var merchantReference:String = orderNo;
args.shopperEmail = order.customerEmail;
args.shopperReference = currentUser.ID;
var sessionValidity : String = new Date();
sessionValidity.setHours(sessionValidity.getHours() + 1);
args.sessionValidity = sessionValidity.toISOString();
var shipBeforeDate : String = new Date();
shipBeforeDate.setDate(shipBeforeDate.getDate() + 5);
args.shipBeforeDate = shipBeforeDate.toISOString();
if (recurringType.value == "" || recurringType.value == "null" || recurringType.value == null) {
recurringType = "";
}
else recurringType = recurringType.value; //recurringType.value has type string
var adyenRequest : SortedMap = new SortedMap();
adyenRequest.put("paymentAmount", args.Amount100 );
adyenRequest.put("currencyCode", currencyCode );
adyenRequest.put("sessionValidity", args.sessionValidity );
adyenRequest.put("shipBeforeDate", args.shipBeforeDate );
adyenRequest.put("merchantReference", merchantReference );
adyenRequest.put("skinCode", skinCode );
adyenRequest.put("merchantAccount", merchantAccount );
adyenRequest.put("shopperEmail", order.customerEmail );
adyenRequest.put("shopperReference", currentUser.ID );
adyenRequest.put("recurringContract", recurringType );
adyenRequest.put("resURL", URLUtils.https('Adyen-ShowConfirmation'));
adyenRequest.put("countryCode", order.getBillingAddress().countryCode.value.toString().toUpperCase() );
// Optional Fields
var locale = request.locale;
if(!empty(locale) && locale != "default") {
adyenRequest.put("shopperLocale", locale);
}
if(Site.getCurrent().getCustomPreferenceValue("Adyen_directoryLookup")){
if( !empty(args.brandCode)){
adyenRequest.put("brandCode", args.brandCode );
if (args.brandCode == "ratepay" && !empty(args.ratePayFingerprint)){
adyenRequest.put("dfValue", args.ratePayFingerprint);
}else if (!empty(args.adyenFingerprint)){
adyenRequest.put("dfValue", args.adyenFingerprint);
}
if (args.brandCode == "klarna"){
adyenRequest.put("klarna.acceptPrivacyPolicy", "true");
}
}
if( !empty(args.issuerId)){
adyenRequest.put("issuerId", args.issuerId );
}
}
//Open Invoice specific section
//requires Gender and Date Of Birth (DOB) from Open Invoice form on the Billing Page and Phone Number from Billing Form
if(!empty(args.brandCode) && Site.getCurrent().getCustomPreferenceValue("Adyen_Open_Invoice_Whitelist").indexOf(args.brandCode) > -1) {
var billingAddr = order.getBillingAddress();
adyenRequest.put("shopper.firstName", order.getBillingAddress().getFirstName() );
adyenRequest.put("shopper.lastName", order.getBillingAddress().getLastName() );
adyenRequest.put("shopper.telephoneNumber", order.getDefaultShipment().getShippingAddress().getPhone() );
adyenRequest.put("shopperType", '1' );
adyenRequest.put("shopper.gender", (!empty(args.gender)) ? args.gender : 'UNKNOWN');
if( !empty(args.dob)) {
var dob = args.dob.split('-');
adyenRequest.put("shopper.dateOfBirthDayOfMonth", dob[0] );
adyenRequest.put("shopper.dateOfBirthMonth", dob[1] );
adyenRequest.put("shopper.dateOfBirthYear", dob[2] );
}
if( !empty(args.personalNumber)) {
adyenRequest.put("shopper.socialSecurityNumber", args.personalNumber );
}
//open invoice line items
var line = 0;
// Add all product and shipping line items to request
for each ( var lineItem in order.getAllLineItems() ) {
if( (lineItem instanceof dw.order.ProductLineItem && !lineItem.bonusProductLineItem)
|| lineItem instanceof dw.order.ShippingLineItem
|| (lineItem instanceof dw.order.PriceAdjustment && lineItem.promotion.promotionClass==dw.campaign.Promotion.PROMOTION_CLASS_ORDER)
) {
line = line + 1;
var description = getDescription(lineItem),
id = getId(lineItem),
quantity = getQuantity(lineItem),
currencyCode = getCurrency(lineItem),
itemAmount:number = AdyenHelper.getCurrencyValueForApi(getItemAmount(lineItem))/quantity,
vatAmount:number = AdyenHelper.getCurrencyValueForApi(getVatAmount(lineItem)) / quantity,
vatPercentage = getVatPercentage(lineItem);
adyenRequest.put("openinvoicedata.line" + line + '.currencyCode', currencyCode );
adyenRequest.put("openinvoicedata.line" + line + '.description', description );
adyenRequest.put("openinvoicedata.line" + line + '.itemAmount', itemAmount.toFixed());
adyenRequest.put("openinvoicedata.line" + line + '.itemVatAmount', vatAmount.toFixed());
adyenRequest.put("openinvoicedata.line" + line + '.itemVatPercentage', (new Number(vatPercentage) * 100).toFixed() );
adyenRequest.put("openinvoicedata.line" + line + '.numberOfItems', quantity );
adyenRequest.put("openinvoicedata.line" + line + '.vatCategory', 'None' );
adyenRequest.put("openinvoicedata.line" + line + '.itemId', id);
}
else {
// Do nothing for other line items
}
}
adyenRequest.put("openinvoicedata.numberOfLines", line.toFixed() );
}
//Billing Address
var billingAddr = order.getBillingAddress();
adyenRequest.put("billingAddress.city", billingAddr.city );
adyenRequest.put("billingAddress.country", billingAddr.countryCode.value.toString().toUpperCase() );
adyenRequest.put("billingAddress.houseNumberOrName", (!empty(args.houseNumber)) ? args.houseNumber : 'NA' );
adyenRequest.put("billingAddress.postalCode", billingAddr.postalCode );
adyenRequest.put("billingAddress.stateOrProvince", (!empty(billingAddr.stateCode) ? billingAddr.stateCode : 'NA') );
adyenRequest.put("billingAddress.street", billingAddr.address1 );
adyenRequest.put("billingAddressType", '1' );
//Shipping / Delivery Address
var shipments = order.getShipments();
for each ( var s : Shipment in shipments) {
var shippingAddr : OrderAddress = s.getShippingAddress();
adyenRequest.put("deliveryAddress.city", shippingAddr.city );
adyenRequest.put("deliveryAddress.country", shippingAddr.countryCode.value.toString().toUpperCase() );
adyenRequest.put("deliveryAddress.houseNumberOrName", (!empty(args.houseNumber)) ? args.houseNumber : 'NA' );
adyenRequest.put("deliveryAddress.postalCode", shippingAddr.postalCode );
adyenRequest.put("deliveryAddress.stateOrProvince",(!empty( shippingAddr.stateCode) ? shippingAddr.stateCode : 'NA') );
adyenRequest.put("deliveryAddress.street", shippingAddr.address1 );
adyenRequest.put("deliveryAddressType", '1' );
break;
}
var keys : String = "";
var values : String = "";
for(var key in adyenRequest) {
keys = keys.concat(key + ":");
var value = adyenRequest[key];
// escape chars
value = value.toString().replace(/\\/g, "\\\\").replace(/\:/g, "\\:");
values = values.concat(value + ":");
}
values = values.substring(0, values.length - 1);
var requestString : String = keys.concat(values);
//error in doc https://documentation.demandware.com/display/DOCAPI2111/dw.crypto.Mac
var keyBytes : Bytes = Encoding.fromHex(HMACkey);
var c : Mac = Mac(Mac.HMAC_SHA_256);
var merchantSig : String = Encoding.toBase64(c.digest(requestString, keyBytes));
Logger.getLogger("Adyen").debug("merchantSig : " + merchantSig + "\n");
adyenRequest.put("merchantSig", merchantSig );
args.paramsMap = adyenRequest;
//MAKE SURE TO PROTECT THIS INFO (or don't log it)
var msg:String;
msg = "DW2AdyenRedir v " + VERSION + " - Payment info\n================================================================";
msg += "\nMode : " + Mode;
msg += "\nRecurring : " + recurringType;
msg += "\nSessionID : " + args.CurrentSession.sessionID;
msg += "\nmRef : " + orderNo ;
msg += "\nShopper email : " + order.customerEmail + " , ID : " + currentUser.ID;
msg += "\nAmount : " + args.Amount100 ;//+ " , " + args.Amount.currencyCode;
msg += "\nPaydata : " + requestString ;
msg += "\nSignature : " + merchantSig ;
//Log those information just when custom logs debug mode is enabled
Logger.getLogger("Adyen").debug(orderNo+ " - " +msg);
return args;
}
function getDescription(lineItem) {
if (lineItem instanceof dw.order.ShippingLineItem) {
return lineItem.getID();
}
else if (lineItem instanceof dw.order.ProductLineItem){
return lineItem.product.name;
}
else if (lineItem instanceof dw.order.PriceAdjustment){
return "Discount";
}
return null;
}
function getId(lineItem){
if (lineItem instanceof dw.order.ShippingLineItem || lineItem instanceof dw.order.PriceAdjustment) {
return lineItem.UUID;
}
else if (lineItem instanceof dw.order.ProductLineItem) {
return lineItem.product.ID;
}
return null;
}
function getQuantity(lineItem) {
if (lineItem instanceof dw.order.ShippingLineItem) {
return "1";
}
else if (lineItem instanceof dw.order.ProductLineItem) {
return lineItem.quantityValue.toFixed();
}
else if (lineItem instanceof dw.order.PriceAdjustment) {
return lineItem.quantity.toFixed();
}
return null;
}
function getVatPercentage(lineItem) {
var vatPercentage = 0;
if ( getVatAmount(lineItem) != 0 ) {
vatPercentage = lineItem.getTaxRate();
}
return vatPercentage;
}
function getVatAmount(lineItem) {
if(lineItem instanceof dw.order.ProductLineItem || lineItem instanceof dw.order.ShippingLineItem) {
return lineItem.getAdjustedTax();
}
else if (lineItem instanceof dw.order.PriceAdjustment) {
return lineItem.tax;
}
return null;
}
function getCurrency(lineItem) {
if(lineItem instanceof dw.order.ProductLineItem || lineItem instanceof dw.order.ShippingLineItem) {
return lineItem.adjustedPrice.currencyCode;
}
else if (lineItem instanceof dw.order.PriceAdjustment) {
return lineItem.basePrice.currencyCode;
}
return null;
}
function getItemAmount(lineItem) {
if(lineItem instanceof dw.order.ProductLineItem || lineItem instanceof dw.order.ShippingLineItem) {
return lineItem.adjustedPrice;
}
else if (lineItem instanceof dw.order.PriceAdjustment) {
return lineItem.basePrice;
}
return null;
}
module.exports = {
'execute': execute,
'verify': verify
}