-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
320 lines (292 loc) · 8.77 KB
/
app.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
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
const { format } = require("path");
const request = require("superagent");
const util = require("util");
const { performance } = require("perf_hooks");
const userAgents = require("Data/userAgents.js");
const referers = require("Data/referers.js");
const ips = require("Data/ips.js");
function generateUserAgent() {
let randomIndex = Math.floor(
Math.random() * userAgents.list_of_userAgents.length
);
return userAgents.list_of_userAgents[randomIndex];
}
function generateReferer() {
let randomIndex = Math.floor(
Math.random() * referers.list_of_referers.length
);
return referers.list_of_referers[randomIndex];
}
function generateIP() {
let randomIndex = Math.floor(Math.random() * ips.list_of_ips.length);
return ips.list_of_ips[randomIndex];
}
/**
* Returns cookie object from API
* @returns {object} res.body
*/
async function get_cookies(agent) {
try {
const res = await agent.get("");
return res.body;
} catch (err) {
console.log(err);
throw new Error("Could not pass cookies from API!");
}
}
/**
* Information needed to complete an order
*/
class orderForm {
/**
* @param {string} productLink
* @param {string} email
* @param {'miss' | 'mrs' | 'ms' | 'mr' | 'mx'} title
* @param {string} firstName
* @param {string} lastName
* @param {string} phone
* @param {string} postcode
* @param {string} address_1
* @param {string} address_2
* @param {string} city
* @param {'collect' | 'standarduk'} delivery methods
* @param {'paypal' | 'creditCard'}
*/
constructor(
productLink,
email,
title,
firstName,
lastName,
phone,
postcode,
address1,
address2,
city,
deliveryMethod,
paymentMethod
) {
this.product_link = productLink;
this.email = email;
this.title = title;
this.first_name = firstName;
this.last_name = lastName;
this.phone = phone;
this.postcode = postcode;
this.address_1 = address1;
this.address_2 = address2;
this.city = city;
this.delivery_method = deliveryMethod;
this.payment_method = paymentMethod;
}
}
/**
* @param {OrderForm} information form
*/
async function order(form) {
const t0 = performance.now();
const prodURL = form.product_link;
const prodID = prodURL.substring(prodURL.lastIndexOf("/") + 1);
// Allows for cookies to persist across requests.
const agent = request.agent();
console.log("Adding product to bag");
await addProductToBasket(agent, prodID);
console.log("###SUCCESSFUL###");
console.log("Setting delivery method");
await choosingDeliveryMethod(agent, form.delivery_method);
console.log("###SUCCESSFUL###");
console.log("Setting the billing address from form info");
await setBillingAddress(agent, form);
console.log("###SUCCESSFUL###");
const paymentUrl = await choosePaymentMethod(agent, form.payment_method);
console.log(paymentUrl);
console.log("###SUCCESSFUL###");
const newPaymentUrl = interceptRedirect(agent, paymentUrl);
const t1 = performance.now();
console.log(util.format("Finished in %d seconds", (t1 - t0) / 1000));
// ^ about 3 seconds rn, before any optimizations
return newPaymentUrl;
}
/**
* @param {request.SuperAgentStatic} agent
* @param {string} paymentUrl
* @returns {Promise<string>}
*/
async function interceptRedirect(agent, paymentUrl) {
try {
await agent.get(paymentUrl).redirects(0);
} catch (err) {
return err.response.header.location;
}
throw new Error("Could not intercept redirect!");
}
/**
* @param {request.SuperAgentStatic} agent
* @param {string} prodID
*/
async function addProductToBasket(agent, prodID) {
try {
const bagURL = "https://www.offspring.co.uk/view/basket/add";
const cookies = await get_cookies(agent); // obj containing various cookies
const _abck = cookies["_abck"]; // gets the _abck cookie from cookies obj
const bm_sz = cookies["bm_sz"]; // gets bm_sz cookie from cookies obj
await agent
.post(bagURL)
.set("cookie", `_abck=${_abck}; bm_sz=${bm_sz}`)
.set("user-agent", generateUserAgent()) // gets random mobile user agent
.set("referer", "https://www.offspring.co.uk")
.send({ productCode: prodID, wishlist: false }); // data that is being sent
} catch (err) {
console.log(err);
throw new Error("Could not add product to the bag!");
}
}
/**
* @param {request.SuperAgentStatic} agent
* @param {string} method
*/
async function choosingDeliveryMethod(agent, method) {
try {
switch (method) {
case "standarduk":
setDeliveryMethod(agent, "standarduk");
break;
case "collect":
setDeliveryMethod(agent, "collect");
break;
default:
throw new Error("Could not set the delivery method!");
}
} catch (err) {
console.log(err);
throw new Error("Could not choose the delivery method!");
}
}
/**
* @param {request.SuperAgentStatic} agent
* @param {string} deliveryMethod
*/
async function setDeliveryMethod(agent, deliveryMethod) {
try {
const deliveryURL =
"https://www.offspring.co.uk/view/component/singlepagecheckout/setDeliveryMode";
const cookies = await get_cookies(agent); // obj containing various cookies
const _abck = cookies["_abck"]; // gets the _abck cookie from cookies obj
const bm_sz = cookies["bm_sz"];
agent
.post(deliveryURL)
.set("cookie", `_abck=${_abck}; bm_sz=${bm_sz}`)
.set("user-agent", generateUserAgent()) // gets random mobile user agent
.set("referer", "https://www.offspring.co.uk")
.send({ deliveryModeCode: deliveryMethod }); // data that is being sent
} catch (err) {
console.log(err);
throw new Error("Could not set delivery method!");
}
}
/**
* @param {request.SuperAgentStatic} agent
* @param {OrderForm}
*/
async function setBillingAddress(agent, form) {
try {
const req = {
email: form.email,
title: form.title,
titleCode: form.title,
phone: form.phone,
firstName: form.first_name,
lastName: form.last_name,
companyName: "",
line1: form.address_1,
line2: form.address_2,
town: form.city,
postalCode: form.postcode,
country: "GB",
defaultAddress: "true",
};
const billingAddressURL =
"https://www.offspring.co.uk/view/component/singlepagecheckout/addEditDeliveryAddress";
const cookies = await get_cookies(agent); // obj containing various cookies
const _abck = cookies["_abck"]; // gets the _abck cookie from cookies obj
const bm_sz = cookies["bm_sz"]; // gets the bm_sz cookie from cookies obj
agent
.post(billingAddressURL)
.set("cookie", `_abck=${_abck}; bm_sz=${bm_sz}`)
.set("user-agent", generateUserAgent()) // gets random mobile user agent
.set("referer", "https://www.offspring.co.uk")
.send(req);
} catch (err) {
console.log(err);
throw new Error("Could not set the billing address information!");
}
}
/**
* @param {request.SuperAgentStatic} agent
* @param {string} method
*/
async function choosePaymentMethod(agent, method) {
try {
switch (method) {
case "paypal":
return getPaymentUrl(agent, "worldpay_paypal");
break;
case "creditCard":
return getPaymentUrl(agent, "worldpay");
break;
default:
throw new Error("Could not set the payment method!");
break;
}
} catch (err) {
throw new Error("Could not choose the payment method!");
}
}
/**
* @param {request.SuperAgentStatic} agent
* @param {string} paymentMethod
* @returns {Promise<string>}
*/
async function getPaymentUrl(agent, paymentMethod) {
try {
const paypalURL =
"https://www.offspring.co.uk/view/component/singlepagecheckout/continueToPaymentDetails";
const cookies = await get_cookies(agent); // obj containing various cookies
const _abck = cookies["_abck"]; // gets the _abck cookie from cookies obj
const bm_sz = cookies["bm_sz"]; // gets the bm_sz cookie from cookies obj
const res = await agent
.post(paypalURL)
.set("cookie", `_abck=${_abck}; bm_sz=${bm_sz}`)
.set("user-agent", generateUserAgent()) // gets random mobile user agent
.set(
"referer",
"https://www.offspring.co.uk/view/checkout/content/singlePageCheckout"
)
.send({
paymentMode: paymentMethod,
emailOptIn: "false",
newsAlerts: "false",
});
return res;
} catch (err) {
console.log(err);
throw new Error("Could not find the payment url!");
}
}
const form = new orderForm(
"https://www.offspring.co.uk/view/product/offspring_catalog/5,22/4171396722994",
"mr",
"Bob",
"theBuilder",
"07477372731",
"tw75lj",
"7 Harewood Road ISLEWORTH",
"8 Harewood Road ISLEWORTH",
"London",
"standarduk",
"creditCard"
);
order(form)
.then((x) => console.log(x))
.catch((x) => console.log(x));