-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.js
302 lines (256 loc) · 10.5 KB
/
bot.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
'use strict';
const axios = require('axios');
const fs = require('fs');
require('dotenv').config();
const USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36';
const X_USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36 FKUA/website/42/website/Desktop';
const COOKIE_PATH = 'cookie.txt';
const UPI_CODE = process.env.UPI_CODE;
const LID = new URLSearchParams(process.env.URL).get('lid');
const creds = { loginId: process.env.EMAIL, password: process.env.PASSWORD };
const mainHeaders = {
'User-Agent': USER_AGENT,
'X-User-Agent': X_USER_AGENT,
'Content-Type': 'application/json'
}
const CHALK = {
YELLOW: '\x1b[33m%s\x1b[0m',
CYAN: '\x1b[36m%s\x1b[0m'
}
// ----------------------------- Helpers --------------------------
const temp = console.log
console.log = (arg1, arg2 = '', arg3 = '') => {
const time = new Date().toLocaleString();
temp(CHALK.CYAN, time, arg1, arg2, arg3);
}
async function request(url, config, method = 'post', retries = 10) {
if (retries <= 0) throw new Error('MAX RETRIES EXCEEDED');
const requestConfig = {
url,
method,
...config
};
let response;
try {
response = await axios(requestConfig);
} catch (error) {
if (error.response && error.response.status === 406) {
console.log('DC Change occured. Retrying...');
if (fs.existsSync(COOKIE_PATH)) fs.unlinkSync(COOKIE_PATH);
await sleep(2000);
return await request(url, config, method, retries - 1);
}
if (error.response && error.response.status === 429) {
console.log('Too many requests, trying again in 60 seconds');
await sleep(60000);
return await request(url, config, method, retries - 1);
}
console.log('AXIOS ERROR:', error.message);
console.log(error.response.status, error.response.data);
process.exit();
}
if (!response || response.status !== 200) {
if (response) console.log('REQUEST ERROR', response.status, response.data);
else console.log('ERROR EMPTY RESPONSE');
process.exit();
}
return response;
}
async function saveCookie(cookieString) {
return fs.writeFileSync(COOKIE_PATH, cookieString);
}
function getCookie() {
return fs.readFileSync(COOKIE_PATH, 'utf8');
}
async function sleep(ms) {
await new Promise(resolve => setTimeout(() => resolve(), ms));
}
async function get(url) {
const payload = {
headers: {
'User-Agent': USER_AGENT,
'X-User-Agent': X_USER_AGENT,
}
};
return await request(url, payload, 'get');
}
async function authGet(url) {
const payload = {
headers: {
'User-Agent': USER_AGENT,
'X-User-Agent': X_USER_AGENT,
'Cookie': getCookie()
}
};
return await request(url, payload, 'get');
}
async function post(url, config) {
return await request(url, config, 'post');
}
async function authPost(url, config) {
const payload = {
headers: {...mainHeaders, 'Cookie': getCookie() },
data: JSON.stringify(config),
};
return await request(url, payload, 'post');
}
// -------------------- Main Functions ---------------------
async function authenticate(retries = 3) {
if (retries <= 0) console.log('Max login retries exceeded');
// Checking for existing cookies
if (!fs.existsSync(COOKIE_PATH)) {
console.log('No login cookies found, authenticating...');
const response = await get('https://flipkart.com/');
// save initial cookie
await saveCookie(response.headers['set-cookie'].join('; '));
const authResponse = await authPost('https://1.rome.api.flipkart.com/api/4/user/authenticate', creds);
console.log('AUTH RESPONSE:', authResponse.status, authResponse.data);
// save auth cookies
await saveCookie(authResponse.headers['set-cookie'].join('; '));
if (!authResponse.data.SESSION.email) {
console.log('Incorrect login, retrying...');
await sleep(2000);
return authenticate(retries - 1);
}
console.log('Successfully authenticated. Logged in as ', authResponse.data.SESSION.email);
}
}
async function emptyCart() {
console.log('Trying to empty cart');
const viewCartPayload = {
pageUri: "/viewcart?otracker=PP_GoToCart",
pageContext: { fetchSeoData: "true" }
};
const fetchResponse = await authPost("https://1.rome.api.flipkart.com/api/4/page/fetch", viewCartPayload);
try {
const slots = fetchResponse.data['RESPONSE']['slots'];
if (slots.length < 7) {
console.log('Cart is already empty');
return;
}
const data = slots[6]["widget"]["data"]["actions"][1]["value"]["popupDetails"]["data"]["actions"][1]["action"]["params"];
const listId = data["listingId"];
const productId = data["productId"];
console.log('List id: ', listId);
console.log('ProductId: ', productId);
const emptyCartPayload = {
"actionRequestContext": {
"pageUri": "/viewcart",
"type": "CART_REMOVE",
"pageNumber": 1,
"items": [{ "listingId": listId, "productId": productId }]
}
}
await authPost("https://1.rome.api.flipkart.com/api/1/action/view", emptyCartPayload);
console.log('Cart emptied.');
} catch (error) {
console.log('Error emptying cart: ', error.message);
}
}
async function addToCart(retries = 10000, sleepTime = 5000) {
if (!retries) {
console.log('Max retries exceeded');
process.exit();
}
// Adding product to cart
console.log('Trying to add requested product to cart...');
const cartPayload = {
"cartContext": {
[LID]: { "quantity": 1 }
}
};
const cartResponse = await authPost("https://1.rome.api.flipkart.com/api/5/cart", cartPayload);
if (cartResponse.data.RESPONSE.cartResponse[LID].errorMessage) {
console.log('ERROR:', cartResponse.data.RESPONSE.cartResponse[LID].errorMessage);
console.log('Retrying...');
await sleep(sleepTime);
await addToCart(retries - 1);
}
}
async function checkout() {
const payload = { checkoutType: "PHYSICAL" };
const response = await authPost("https://1.rome.api.flipkart.com/api/5/checkout?loginFlow=false", payload);
const buyableStateItems = response.data["RESPONSE"]["orderSummary"]["requestedStores"][0]["buyableStateItems"];
if (!buyableStateItems.length) {
console.log(response.data["RESPONSE"]["orderSummary"]);
throw new Error('Unable to checkout, product went out of stock');
}
const itemId = buyableStateItems[0]["cartItemRefId"];
// selecting user's default address
const address = response.data['RESPONSE']['addressData']['billingAddressInfos'].filter(item => item.isDefault)[0];
const addressId = address.id;
console.log('ITEM ID:', itemId);
console.log('ADDRESS ID:', addressId);
console.log(`Default address found: \n${address.addressLine1} ${address.addressLine2} \n${address.city} ${address.pincode} \n${address.name} ${address.phone}`)
}
async function getPaymentToken() {
const response = await authGet('https://1.rome.api.flipkart.com/api/3/checkout/paymentToken');
const token = response.data["RESPONSE"]["getPaymentToken"]["token"]
console.log('Received payment token:', CHALK.YELLOW, token);
saveCookie(response.headers['set-cookie'].join('; '));
return token;
}
async function pollForPayment(token, transactionId, sleepTime = 1000, retries = 100) {
// Polling to check payment confirmation
if (retries <= 0) throw new Error('Max Retries exceeded');
const payload = { token: token, transactionId: transactionId };
const response = await authPost('https://1.pay.payzippy.com/fkpay/api/v3/payments/upi/poll', payload)
if (response.data['response_status'] !== 'SUCCESS') {
console.log('Polling for payment completion...');
await sleep(sleepTime);
return await pollForPayment(token, transactionId, sleepTime, retries - 1);
}
saveCookie(response.headers['set-cookie'].join('; '))
console.log('Payment completed!');
const primaryAction = response.data['primary_action'];
const primaryActionURL = primaryAction['target'];
const primaryActionData = primaryAction['parameters'];
const payResponse = await authPost(primaryActionURL, primaryActionData);
console.log(payResponse.status, payResponse.headers);
}
async function startPaymentProcess() {
const token = await getPaymentToken();
// Payment Step 1
const payload1 = { "payment_instrument": "UPI", "token": token };
// Uses UPI for payment
const queryString1 = `https://1.payments.flipkart.com/fkpay/api/v3/payments/pay?instrument=UPI&token=${token}`;
const r1 = await authPost(queryString1, payload1);
// Payment Step 2
const payload2 = { token: token };
const queryString2 = `https://1.pay.payzippy.com/fkpay/api/v3/payments/upi/options?token=${token}`;
const r2 = await authPost(queryString2, payload2);
// Payment Step 3
const payload3 = {
upi_details: {
upi_code: UPI_CODE,
payment_instrument: 'UPI_COLLECT',
token: token
}
};
const queryString3 = `https://1.pay.payzippy.com/fkpay/api/v3/payments/paywithdetails?token=${token}`;
const r3 = await authPost(queryString3, payload3);
console.log('UPI payment request sent. Accept the payment to confirm order of item');
// Payment Step 4
const payload4 = {
upi_details: { app_code: 'collect_flow', upi_code: UPI_CODE },
payment_instrument: 'UPI_COLLECT',
token: token,
section_info: { section_name: 'OTHERS' }
}
const queryString4 = `https://1.pay.payzippy.com/fkpay/api/v3/payments/paywithdetails?token=${token}`;
const r4 = await authPost(queryString4, payload4);
const transactionId = r4.data['txn_id']
console.log('Transaction ID generated: ', transactionId);
await pollForPayment(token, transactionId);
}
async function init() {
await authenticate();
await emptyCart();
await addToCart();
console.log('\x07');
console.log('Product added to cart.');
await checkout();
await startPaymentProcess();
console.log('Product bought. Check your email for order details');
}
init();