-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathdemandware.js
More file actions
336 lines (289 loc) · 11.2 KB
/
demandware.js
File metadata and controls
336 lines (289 loc) · 11.2 KB
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
import getStateNameFromAbbreviation from '../helpers/states.js';
import getCardDetailsByFriendlyName from '../helpers/credit-cards.js';
async function enterAddressDetails({ page, address, type }) {
const firstNameSelector = `input#${type}Address-firstName`;
const lastNameSelector = `input#${type}Address-lastName`;
const address1Selector = `input#${type}Address-address1`;
const address2Selector = `input#${type}Address-address2`;
const citySelector = `input#${type}Address-city`;
const stateSelector = `select[data-auto-id="${type}Address-stateCode"]`;
const postalCodeSelector = `input#${type}Address-zipcode`;
const emailSelector = `input#${type}Address-emailAddress`;
const phoneNumberSelector = `input#${type}Address-phoneNumber`;
await page.waitForSelector(firstNameSelector);
await page.type(firstNameSelector, address.first_name, {
delay: 10
});
await page.waitForTimeout(2000);
await page.waitForSelector(lastNameSelector);
await page.type(lastNameSelector, address.last_name, {
delay: 10
});
await page.waitForTimeout(2000);
await page.waitForSelector(address1Selector);
await page.type(address1Selector, address.address_line_1, {
delay: 10
});
await page.waitForTimeout(2000);
await page.waitForSelector(address2Selector);
await page.type(address2Selector, address.address_line_2, {
delay: 10
});
await page.waitForTimeout(2000);
await page.waitForSelector(citySelector);
await page.type(citySelector, address.city, {
delay: 10
});
await page.waitForTimeout(2000);
try {
await page.waitForSelector(stateSelector);
await page.select(stateSelector, getStateNameFromAbbreviation(address.state));
await page.waitForTimeout(2000);
} catch (err) {
// no op if timeout waiting for state selector
}
await page.waitForSelector(postalCodeSelector);
await page.type(postalCodeSelector, address.postal_code, {
delay: 10
});
await page.waitForTimeout(2000);
// no email address required for billing info
try {
await page.waitForSelector(emailSelector);
await page.type(emailSelector, address.email_address, {
delay: 10
});
await page.waitForTimeout(2000);
} catch (err) {
// no-op if timeout occurs
}
await page.waitForSelector(phoneNumberSelector);
await page.type(phoneNumberSelector, address.phone_number, {
delay: 10
});
await page.waitForTimeout(2000);
}
async function checkout({ taskLogger, page, shippingAddress, shippingSpeedIndex, billingAddress, domain, cardFriendlyName }) {
taskLogger.info('Navigating to checkout page');
await page.goto(`${domain}/delivery`, { waitUntil: 'domcontentloaded' });
let checkoutComplete = false;
let cardDetails = {
cardNumber: process.env.CARD_NUMBER,
nameOnCard: process.env.NAME_ON_CARD,
expirationMonth: process.env.EXPIRATION_MONTH,
expirationYear: process.env.EXPIRATION_YEAR,
securityCode: process.env.SECURITY_CODE
};
if (cardFriendlyName) {
cardDetails = getCardDetailsByFriendlyName(cardFriendlyName);
}
const shippingSpeedsSelector = 'div[data-auto-id="delivery-option"]';
const manualAddressEntrySelector = 'a[aria-label="Enter address manually"]';
const differentBillingAddressSelector = 'input[name="shouldUseShippingAddressForBilling"]';
const reviewAndPayButtonsSelector = 'button[data-auto-id="review-and-pay-button"]';
const creditCardNumberIframeSelector = 'iframe[aria-label="Card Number"]';
const creditCardNumberSelector = 'input[aria-label="Card Number"]';
const nameOnCardSelector = 'input[aria-label="Name on card"]';
const creditCardExpirationDateSelector = 'input[aria-label="MM / YY"]';
const creditCardCVVIframeSelector = 'iframe[aria-label="CVV"]';
const creditCardCVVSelector = 'input[aria-label="CVV"]';
const placeOrderButtonsSelector = 'button[data-auto-id="place-order-button"]';
taskLogger.info('Selecting desired shipping speed');
await page.waitForSelector(shippingSpeedsSelector);
const shippingSpeeds = await page.$$(shippingSpeedsSelector);
await shippingSpeeds[shippingSpeedIndex].click();
await page.waitForTimeout(2000);
await page.evaluate((manualAddressEntrySelectorStr) => {
document.querySelector(manualAddressEntrySelectorStr).click();
}, manualAddressEntrySelector);
await page.waitForTimeout(2000);
taskLogger.info('Entering shipping details');
await enterAddressDetails({ page, address: shippingAddress, type: 'shipping' });
await page.waitForSelector(differentBillingAddressSelector);
await page.click(differentBillingAddressSelector);
await page.waitForTimeout(2000);
taskLogger.info('Entering billing details');
await enterAddressDetails({
page,
address: billingAddress,
type: 'billing'
});
await page.waitForSelector(reviewAndPayButtonsSelector);
await page.click(reviewAndPayButtonsSelector);
await page.waitForTimeout(2000);
taskLogger.info('Entering card details');
await page.waitForSelector(creditCardNumberIframeSelector);
const cardNumberFrameHandle = await page.$(creditCardNumberIframeSelector);
const cardNumberFrame = await cardNumberFrameHandle.contentFrame();
await cardNumberFrame.waitForSelector(creditCardNumberSelector);
await cardNumberFrame.type(creditCardNumberSelector, cardDetails.cardNumber, {
delay: 10
});
await page.waitForSelector(nameOnCardSelector);
const nameOnCardHandle = await page.$(nameOnCardSelector);
await nameOnCardHandle.click();
await nameOnCardHandle.focus();
await nameOnCardHandle.click({ clickCount: 3 });
await nameOnCardHandle.press('Backspace');
await page.type(nameOnCardSelector, cardDetails.nameOnCard, {
delay: 10
});
await page.waitForTimeout(2000);
await page.waitForSelector(creditCardExpirationDateSelector);
await page.type(creditCardExpirationDateSelector, cardDetails.expirationMonth + cardDetails.expirationYear, {
delay: 10
});
await page.waitForTimeout(2000);
await page.waitForSelector(creditCardCVVIframeSelector);
const cardCVVFrameHandle = await page.$(creditCardCVVIframeSelector);
const cardCVVFrame = await cardCVVFrameHandle.contentFrame();
await cardCVVFrame.type(creditCardCVVSelector, cardDetails.securityCode, {
delay: 10
});
await page.waitForTimeout(2000);
taskLogger.info('Clicking place order button');
await page.waitForSelector(placeOrderButtonsSelector);
await page.click(placeOrderButtonsSelector);
await page.waitForTimeout(5000);
await page.goto(`${domain}/payment`, { waitUntil: 'domcontentloaded' });
if (page.url() === `${domain}/cart`) {
checkoutComplete = true;
}
return checkoutComplete;
}
async function closeModal({ taskLogger, page }) {
try {
const modalCloseButtonSelector = 'button.gl-modal__close';
await page.waitForSelector(modalCloseButtonSelector, { visible: true });
taskLogger.info('Closing modal');
await page.click(modalCloseButtonSelector);
} catch (err) {
// no-op
}
}
async function searchByProductCode({ taskLogger, page, productCode, domain }) {
taskLogger.info('Searching for product by product code');
let searchResult;
while (!searchResult) {
await page.goto(`${domain}/search?q=${productCode}`, { waitUntil: 'domcontentloaded' });
await Promise.all([
(async () => {
try {
searchResult = await page.waitForSelector('div[class*="grid-item"] a[data-auto-id="glass-hockeycard-link"]', { timeout: 5 * 1000 });
taskLogger.info('Found search result, clicking');
await searchResult.click();
} catch (err) {
// no-op
}
})(),
(async () => {
try {
searchResult = await page.waitForSelector('div[class*="product-description"]', { timeout: 5 * 1000 });
taskLogger.info('Navigated to product details page');
} catch (err) {
// no-op
}
})()
]);
}
}
const guestCheckout = async ({ taskLogger, page, url, productCode, size, shippingAddress, shippingSpeedIndex, billingAddress, cardFriendlyName }) => {
const splitDomain = url.split('/').slice(0, 4);
const sitePath = splitDomain[3];
const domain = splitDomain.join('/');
await page.setRequestInterception(true);
const availablityPromise = new Promise((resolve) => {
page.on('response', async (response) => {
if (new URL(response.url()).pathname.endsWith('/availability')) {
const responseJson = await response.json();
resolve(responseJson);
}
});
});
closeModal({ taskLogger, page });
if (productCode) {
await searchByProductCode({
taskLogger,
page,
productCode,
domain
});
} else {
taskLogger.info('No product code supplied, navigating to URL');
await page.goto(url, { waitUntil: 'domcontentloaded' });
}
const responseJson = await availablityPromise;
taskLogger.info('Found product availability');
const product_id = responseJson.id;
const sizes = responseJson.variation_list;
const product_variation_sku = sizes.find((sizeObj) => sizeObj.size === size).sku;
await page.waitForTimeout(15 * 1000);
let isInCart = false;
while (!isInCart) {
taskLogger.info('Attempting to add product to cart');
isInCart = await page.evaluate(
async ({ productId, productVariationSku, sizeStr, sitePathStr }) => {
const data = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
product_id: productId,
quantity: 1,
product_variation_sku: productVariationSku,
productId: productVariationSku,
size: sizeStr,
displaySize: sizeStr
})
};
const response = await fetch(`/api/chk/baskets/-/items?sitePath=${sitePathStr}`, data);
return response.ok;
},
{
productId: product_id,
productVariationSku: product_variation_sku,
sizeStr: size,
sitePathStr: sitePath
}
);
if (!isInCart) {
taskLogger.info('Got error while adding to cart from API, falling back to DOM');
const selectedSize = await page.evaluate((sizeStr) => {
const sizeButtons = Array.from(document.querySelectorAll('div[data-auto-id="size-selector"] button'));
const sizeButton = sizeButtons.find((button) => button.innerText === sizeStr);
if (sizeButton) {
sizeButton.click();
return true;
}
return false;
}, size);
if (selectedSize) {
await page.waitForTimeout(2 * 1000);
const atcButtonSelector = 'button[title="Add To Bag"]';
await page.waitForSelector(atcButtonSelector);
await page.click(atcButtonSelector);
taskLogger.info('Selected size from DOM');
const modal = await page.waitForSelector('div.gl-modal', { visible: true });
isInCart = await modal.$eval('h5', (heading) => /SUCCESSFULLY ADDED TO BAG!/i.test(heading.innerText));
taskLogger.info('Found success modal, continuing to checkout');
}
}
}
let checkoutComplete = false;
if (isInCart) {
await page.waitForTimeout(2000);
checkoutComplete = await checkout({
taskLogger,
page,
shippingAddress,
shippingSpeedIndex,
billingAddress,
domain,
cardFriendlyName
});
}
return checkoutComplete;
};
export default guestCheckout;