cloudcommerce 0.0.98 → 0.0.99
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/package.json +1 -1
- package/packages/api/package.json +1 -1
- package/packages/apps/correios/package.json +1 -1
- package/packages/apps/custom-shipping/package.json +1 -1
- package/packages/apps/discounts/package.json +1 -1
- package/packages/apps/frenet/package.json +1 -1
- package/packages/apps/tiny-erp/lib/event-to-tiny.js +94 -94
- package/packages/apps/tiny-erp/lib/index.js +1 -1
- package/packages/apps/tiny-erp/lib/integration/after-tiny-queue.js +71 -74
- package/packages/apps/tiny-erp/lib/integration/export-order-to-tiny.js +70 -73
- package/packages/apps/tiny-erp/lib/integration/export-product-to-tiny.js +49 -53
- package/packages/apps/tiny-erp/lib/integration/helpers/format-tiny-date.js +3 -3
- package/packages/apps/tiny-erp/lib/integration/import-order-from-tiny.js +76 -75
- package/packages/apps/tiny-erp/lib/integration/import-product-from-tiny.js +137 -140
- package/packages/apps/tiny-erp/lib/integration/parsers/order-from-tiny.js +40 -39
- package/packages/apps/tiny-erp/lib/integration/parsers/order-to-tiny.js +173 -178
- package/packages/apps/tiny-erp/lib/integration/parsers/product-from-tiny.js +173 -171
- package/packages/apps/tiny-erp/lib/integration/parsers/product-to-tiny.js +123 -127
- package/packages/apps/tiny-erp/lib/integration/parsers/status-from-tiny.js +32 -32
- package/packages/apps/tiny-erp/lib/integration/parsers/status-to-tiny.js +37 -37
- package/packages/apps/tiny-erp/lib/integration/post-tiny-erp.js +42 -43
- package/packages/apps/tiny-erp/lib/tiny-erp.js +8 -6
- package/packages/apps/tiny-erp/lib/tiny-webhook.js +76 -73
- package/packages/apps/tiny-erp/package.json +1 -1
- package/packages/cli/package.json +1 -1
- package/packages/config/package.json +1 -1
- package/packages/events/package.json +1 -1
- package/packages/firebase/package.json +1 -1
- package/packages/modules/lib/firebase/ajv.js +24 -19
- package/packages/modules/lib/firebase/call-app-module.js +110 -116
- package/packages/modules/lib/firebase/checkout.js +152 -151
- package/packages/modules/lib/firebase/functions-checkout/fix-items.js +187 -194
- package/packages/modules/lib/firebase/functions-checkout/get-custumerId.js +26 -25
- package/packages/modules/lib/firebase/functions-checkout/handle-order-transaction.js +109 -110
- package/packages/modules/lib/firebase/functions-checkout/new-order.js +177 -177
- package/packages/modules/lib/firebase/functions-checkout/request-to-module.js +59 -59
- package/packages/modules/lib/firebase/functions-checkout/utils.js +197 -195
- package/packages/modules/lib/firebase/handle-module.js +146 -144
- package/packages/modules/lib/firebase/proxy-apps.js +1 -2
- package/packages/modules/lib/firebase/serve-modules-api.js +53 -52
- package/packages/modules/lib/firebase.js +6 -4
- package/packages/modules/lib/index.js +15 -12
- package/packages/modules/package.json +1 -1
- package/packages/passport/package.json +1 -1
- package/packages/ssr/package.json +1 -1
- package/packages/storefront/astro.config.mjs +1 -1
- package/packages/storefront/dist/client/{LoginOffcanvas.daf3f717.js → LoginOffcanvas.c2faa1dc.js} +1 -1
- package/packages/storefront/dist/client/chunks/{LoginForm.d9251274.js → LoginForm.3bcb85fb.js} +10 -10
- package/packages/storefront/dist/client/chunks/{LoginOffcanvas.07fe6492.js → LoginOffcanvas.e48f274b.js} +1 -1
- package/packages/storefront/dist/client/sw.js +1 -1
- package/packages/storefront/dist/server/entry.mjs +2 -2
- package/packages/storefront/package.json +2 -2
- package/packages/storefront/tsconfig.json +1 -1
- package/packages/types/package.json +1 -1
|
@@ -1,79 +1,75 @@
|
|
|
1
1
|
import { logger } from 'firebase-functions';
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import config from '@cloudcommerce/firebase/lib/config';
|
|
4
|
+
|
|
4
5
|
// Blacklist urls to prevent consecultive errors
|
|
5
6
|
const blacklist = {};
|
|
7
|
+
|
|
6
8
|
export default async (appId, modName, url, data, isBigTimeout) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
if (blacklist[url] > 2) {
|
|
10
|
+
logger.log(`> Skipping blacklisted ${url}`);
|
|
11
|
+
const err = new Error('Blacklited endpoint URL');
|
|
12
|
+
return Promise.reject(err);
|
|
13
|
+
}
|
|
14
|
+
const { storeId, apps } = config.get();
|
|
15
|
+
const checkErrorResponse = (logHead, resData) => {
|
|
16
|
+
if (typeof resData === 'object' && resData !== null) {
|
|
17
|
+
const { error, message } = resData;
|
|
18
|
+
if (typeof error === 'string' && error.length && typeof message === 'string') {
|
|
19
|
+
logger.warn(logHead, JSON.stringify({ error, message }));
|
|
20
|
+
}
|
|
11
21
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
delete blacklist[url];
|
|
35
|
-
}
|
|
36
|
-
}, !status ? 180000 : 6000);
|
|
37
|
-
const logHead = `${url} : ${status}`;
|
|
38
|
-
if (status >= 400 && status < 500) {
|
|
39
|
-
checkErrorResponse(logHead, response.data);
|
|
40
|
-
}
|
|
41
|
-
else {
|
|
42
|
-
logger.info(logHead);
|
|
43
|
-
}
|
|
44
|
-
};
|
|
22
|
+
};
|
|
23
|
+
const debugAndBlacklist = (response) => {
|
|
24
|
+
const status = response ? response.status : 0;
|
|
25
|
+
if (!blacklist[url]) {
|
|
26
|
+
blacklist[url] = 1;
|
|
27
|
+
} else {
|
|
28
|
+
blacklist[url] += 1;
|
|
29
|
+
}
|
|
30
|
+
setTimeout(() => {
|
|
31
|
+
if (blacklist[url] > 1) {
|
|
32
|
+
blacklist[url] -= 1;
|
|
33
|
+
} else {
|
|
34
|
+
delete blacklist[url];
|
|
35
|
+
}
|
|
36
|
+
}, !status ? 180000 : 6000);
|
|
37
|
+
const logHead = `${url} : ${status}`;
|
|
38
|
+
if (status >= 400 && status < 500) {
|
|
39
|
+
checkErrorResponse(logHead, response.data);
|
|
40
|
+
} else {
|
|
41
|
+
logger.info(logHead);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
45
44
|
// eslint-disable-next-line no-unused-vars
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
}
|
|
45
|
+
let internalModuleFn;
|
|
46
|
+
if (modName === 'apply_discount') {
|
|
47
|
+
if (appId === apps.discounts.appId) {
|
|
48
|
+
internalModuleFn = async (_data = data) => {
|
|
49
|
+
return import('@cloudcommerce/app-discounts')
|
|
50
|
+
.then(({ applyDiscount }) => applyDiscount(_data));
|
|
51
|
+
};
|
|
54
52
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
.then(({ calculateShipping }) => calculateShipping(_data));
|
|
72
|
-
};
|
|
73
|
-
}
|
|
53
|
+
} else if (modName === 'calculate_shipping') {
|
|
54
|
+
if (appId === apps.correios.appId) {
|
|
55
|
+
internalModuleFn = async (_data = data) => {
|
|
56
|
+
return import('@cloudcommerce/app-correios')
|
|
57
|
+
.then(({ calculateShipping }) => calculateShipping(_data));
|
|
58
|
+
};
|
|
59
|
+
} else if (appId === apps.customShipping.appId) {
|
|
60
|
+
internalModuleFn = async (_data = data) => {
|
|
61
|
+
return import('@cloudcommerce/app-custom-shipping')
|
|
62
|
+
.then(({ calculateShipping }) => calculateShipping(_data));
|
|
63
|
+
};
|
|
64
|
+
} else if (appId === apps.frenet.appId) {
|
|
65
|
+
internalModuleFn = async (_data = data) => {
|
|
66
|
+
return import('@cloudcommerce/app-frenet')
|
|
67
|
+
.then(({ calculateShipping }) => calculateShipping(_data));
|
|
68
|
+
};
|
|
74
69
|
}
|
|
75
|
-
|
|
76
|
-
|
|
70
|
+
}
|
|
71
|
+
if (internalModuleFn) {
|
|
72
|
+
/*
|
|
77
73
|
global.app1_apply_discount_middleware = async (
|
|
78
74
|
data: any,
|
|
79
75
|
next: () => Promise<any>,
|
|
@@ -84,58 +80,56 @@ export default async (appId, modName, url, data, isBigTimeout) => {
|
|
|
84
80
|
return next(data);
|
|
85
81
|
};
|
|
86
82
|
*/
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
message,
|
|
108
|
-
};
|
|
109
|
-
}
|
|
83
|
+
const middleware = global[`app${appId}_${modName}_middleware`];
|
|
84
|
+
try {
|
|
85
|
+
let appResponse;
|
|
86
|
+
if (typeof middleware === 'function') {
|
|
87
|
+
appResponse = await middleware(data, internalModuleFn);
|
|
88
|
+
} else {
|
|
89
|
+
appResponse = await internalModuleFn(data);
|
|
90
|
+
}
|
|
91
|
+
checkErrorResponse(`${appId}_${modName}`, appResponse);
|
|
92
|
+
return appResponse;
|
|
93
|
+
} catch (err) {
|
|
94
|
+
logger.error(err);
|
|
95
|
+
let message = 'Failed to execute module function';
|
|
96
|
+
if (typeof middleware === 'function') {
|
|
97
|
+
message += ' (has middleware)';
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
error: 'INTERNAL_MODULE_ERROR',
|
|
101
|
+
message,
|
|
102
|
+
};
|
|
110
103
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
104
|
+
}
|
|
105
|
+
return axios({
|
|
106
|
+
method: 'POST',
|
|
107
|
+
maxRedirects: 2,
|
|
108
|
+
responseType: 'json',
|
|
109
|
+
maxContentLength: 1000000,
|
|
110
|
+
url,
|
|
111
|
+
data,
|
|
112
|
+
headers: {
|
|
113
|
+
'X-Store-ID': storeId.toString(),
|
|
114
|
+
},
|
|
115
|
+
// Wait 10s by default and 30s in specific cases
|
|
116
|
+
timeout: isBigTimeout ? 30000 : 10000,
|
|
117
|
+
})
|
|
118
|
+
.then((response) => {
|
|
119
|
+
debugAndBlacklist(response);
|
|
120
|
+
return response.data;
|
|
127
121
|
})
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
}
|
|
136
|
-
logger.warn(msg);
|
|
122
|
+
.catch((err) => {
|
|
123
|
+
const { response } = err;
|
|
124
|
+
debugAndBlacklist(response);
|
|
125
|
+
if (err.message || err.code) {
|
|
126
|
+
let msg = `Axios error ${err.code}: ${err.message}`;
|
|
127
|
+
if (data) {
|
|
128
|
+
msg += `\n\n${JSON.stringify(data)}`;
|
|
137
129
|
}
|
|
138
|
-
|
|
130
|
+
logger.warn(msg);
|
|
131
|
+
}
|
|
132
|
+
throw err;
|
|
139
133
|
});
|
|
140
134
|
};
|
|
141
|
-
|
|
135
|
+
// # sourceMappingURL=call-app-module.js.map
|
|
@@ -1,162 +1,163 @@
|
|
|
1
|
-
import { ajv, sendRequestError
|
|
1
|
+
import { ajv, sendRequestError } from './ajv.js';
|
|
2
2
|
import fixItems from './functions-checkout/fix-items.js';
|
|
3
3
|
import getCustomerId from './functions-checkout/get-custumerId.js';
|
|
4
4
|
import requestModule from './functions-checkout/request-to-module.js';
|
|
5
|
-
import {
|
|
5
|
+
import {
|
|
6
|
+
sendError, fixAmount, getValidResults, handleShippingServices, handleApplyDiscount, handleListPayments,
|
|
7
|
+
} from './functions-checkout/utils.js';
|
|
6
8
|
import createOrder from './functions-checkout/new-order.js';
|
|
9
|
+
|
|
7
10
|
const runCheckout = async (checkoutBody, accessToken, res, validate, hostname) => {
|
|
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
|
-
if (subtotal <= 0 && items.length < countCheckoutItems) {
|
|
82
|
-
return sendError(res, 400, 'CKT801', 'Cannot handle checkout, any valid cart item');
|
|
83
|
-
}
|
|
84
|
-
amount.subtotal = subtotal;
|
|
85
|
-
body.subtotal = subtotal;
|
|
86
|
-
fixAmount(amount, body, orderBody);
|
|
87
|
-
const transactions = Array.isArray(body.transaction) ? body.transaction : [body.transaction];
|
|
88
|
-
// add customer ID to order and transaction
|
|
89
|
-
customer._id = customerId;
|
|
90
|
-
transactions.forEach(({ buyer }) => {
|
|
91
|
-
if (buyer) {
|
|
92
|
-
buyer.customer_id = customerId;
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
let listShipping = await requestModule(body, hostname, 'shipping');
|
|
96
|
-
if (listShipping) {
|
|
97
|
-
listShipping = getValidResults(listShipping, 'shipping_services');
|
|
98
|
-
handleShippingServices(body, listShipping, amount, orderBody);
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
// problem with shipping response object
|
|
102
|
-
return sendError(res, 400, 'CKT901', 'Any valid shipping service from /calculate_shipping module', {
|
|
103
|
-
en_us: 'Shipping method not available, please choose another',
|
|
104
|
-
pt_br: 'Forma de envio indisponível, por favor escolha outra',
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
let discounts = await requestModule(body, hostname, 'discount');
|
|
108
|
-
if (discounts) {
|
|
109
|
-
discounts = getValidResults(discounts);
|
|
110
|
-
handleApplyDiscount(body, discounts, amount, orderBody);
|
|
111
|
-
}
|
|
112
|
-
const { transaction, ...bodyPayment } = body;
|
|
113
|
-
let paymentsBody;
|
|
114
|
-
if (Array.isArray(transaction)) {
|
|
115
|
-
paymentsBody = {
|
|
116
|
-
...bodyPayment,
|
|
117
|
-
transaction: transaction[0],
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
paymentsBody = {
|
|
122
|
-
...bodyPayment,
|
|
123
|
-
transaction,
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
let listPaymentGateways = await requestModule(paymentsBody, hostname, 'payment');
|
|
127
|
-
if (listPaymentGateways) {
|
|
128
|
-
listPaymentGateways = getValidResults(listPaymentGateways, 'payment_gateways');
|
|
129
|
-
handleListPayments(body, listPaymentGateways, paymentsBody, amount, orderBody);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
return sendError(res, 409, 'CKT902', 'Any valid payment gateway from /list_payments module', {
|
|
133
|
-
en_us: 'Payment method not available, please choose another',
|
|
134
|
-
pt_br: 'Forma de pagamento indisponível, por favor escolha outra',
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
return createOrder(res, accessToken, hostname, amount, checkoutBody, orderBody, transactions, dateTime);
|
|
11
|
+
if (!validate(checkoutBody)) {
|
|
12
|
+
return sendRequestError(res, '@checkout', validate.errors);
|
|
13
|
+
}
|
|
14
|
+
const { items, ...newBody } = checkoutBody;
|
|
15
|
+
const newItems = await fixItems(items);
|
|
16
|
+
const amount = {
|
|
17
|
+
subtotal: 0,
|
|
18
|
+
discount: 0,
|
|
19
|
+
freight: 0,
|
|
20
|
+
total: 0,
|
|
21
|
+
};
|
|
22
|
+
const body = {
|
|
23
|
+
...newBody,
|
|
24
|
+
items: [...newItems],
|
|
25
|
+
subtotal: 0,
|
|
26
|
+
amount,
|
|
27
|
+
};
|
|
28
|
+
const countCheckoutItems = body.items.length;
|
|
29
|
+
const { customer } = body;
|
|
30
|
+
const customerId = await getCustomerId(accessToken, customer);
|
|
31
|
+
if (customerId && customer) {
|
|
32
|
+
if (newItems.length) {
|
|
33
|
+
const { _id, ...newCustomer } = customer;
|
|
34
|
+
// start mounting order body
|
|
35
|
+
// https://developers.e-com.plus/docs/api/#/store/orders/orders
|
|
36
|
+
const dateTime = new Date().toISOString();
|
|
37
|
+
const orderBody = {
|
|
38
|
+
opened_at: dateTime,
|
|
39
|
+
buyers: [
|
|
40
|
+
// received customer info
|
|
41
|
+
{
|
|
42
|
+
_id: customerId,
|
|
43
|
+
...newCustomer,
|
|
44
|
+
},
|
|
45
|
+
],
|
|
46
|
+
items: [],
|
|
47
|
+
amount: {
|
|
48
|
+
total: 0,
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
// bypass some order fields
|
|
52
|
+
const fields = [
|
|
53
|
+
'utm',
|
|
54
|
+
'affiliate_code',
|
|
55
|
+
'browser_ip',
|
|
56
|
+
'channel_id',
|
|
57
|
+
'channel_type',
|
|
58
|
+
'domain',
|
|
59
|
+
'notes',
|
|
60
|
+
];
|
|
61
|
+
fields.forEach((field) => {
|
|
62
|
+
if (body[field]) {
|
|
63
|
+
orderBody[field] = body[field];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
if (orderBody.domain) {
|
|
67
|
+
// consider default Storefront app routes
|
|
68
|
+
if (!orderBody.checkout_link) {
|
|
69
|
+
orderBody.checkout_link = `https://${orderBody.domain}/app/#/checkout/(_id)`;
|
|
70
|
+
}
|
|
71
|
+
if (!orderBody.status_link) {
|
|
72
|
+
orderBody.status_link = `https://${orderBody.domain}/app/#/order/(_id)`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// count subtotal value
|
|
76
|
+
let subtotal = 0;
|
|
77
|
+
newItems.forEach((item) => {
|
|
78
|
+
subtotal += (item.final_price || item.price * item.quantity);
|
|
79
|
+
// pass each item to prevent object overwrite
|
|
80
|
+
if (orderBody.items) {
|
|
81
|
+
orderBody.items.push({ ...item });
|
|
138
82
|
}
|
|
83
|
+
});
|
|
84
|
+
if (subtotal <= 0 && items.length < countCheckoutItems) {
|
|
139
85
|
return sendError(res, 400, 'CKT801', 'Cannot handle checkout, any valid cart item');
|
|
86
|
+
}
|
|
87
|
+
amount.subtotal = subtotal;
|
|
88
|
+
body.subtotal = subtotal;
|
|
89
|
+
fixAmount(amount, body, orderBody);
|
|
90
|
+
const transactions = Array.isArray(body.transaction) ? body.transaction : [body.transaction];
|
|
91
|
+
// add customer ID to order and transaction
|
|
92
|
+
customer._id = customerId;
|
|
93
|
+
transactions.forEach(({ buyer }) => {
|
|
94
|
+
if (buyer) {
|
|
95
|
+
buyer.customer_id = customerId;
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
let listShipping = await requestModule(body, hostname, 'shipping');
|
|
99
|
+
if (listShipping) {
|
|
100
|
+
listShipping = getValidResults(listShipping, 'shipping_services');
|
|
101
|
+
handleShippingServices(body, listShipping, amount, orderBody);
|
|
102
|
+
} else {
|
|
103
|
+
// problem with shipping response object
|
|
104
|
+
return sendError(res, 400, 'CKT901', 'Any valid shipping service from /calculate_shipping module', {
|
|
105
|
+
en_us: 'Shipping method not available, please choose another',
|
|
106
|
+
pt_br: 'Forma de envio indisponível, por favor escolha outra',
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
let discounts = await requestModule(body, hostname, 'discount');
|
|
110
|
+
if (discounts) {
|
|
111
|
+
discounts = getValidResults(discounts);
|
|
112
|
+
handleApplyDiscount(body, discounts, amount, orderBody);
|
|
113
|
+
}
|
|
114
|
+
const { transaction, ...bodyPayment } = body;
|
|
115
|
+
let paymentsBody;
|
|
116
|
+
if (Array.isArray(transaction)) {
|
|
117
|
+
paymentsBody = {
|
|
118
|
+
...bodyPayment,
|
|
119
|
+
transaction: transaction[0],
|
|
120
|
+
};
|
|
121
|
+
} else {
|
|
122
|
+
paymentsBody = {
|
|
123
|
+
...bodyPayment,
|
|
124
|
+
transaction,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
let listPaymentGateways = await requestModule(paymentsBody, hostname, 'payment');
|
|
128
|
+
if (listPaymentGateways) {
|
|
129
|
+
listPaymentGateways = getValidResults(listPaymentGateways, 'payment_gateways');
|
|
130
|
+
handleListPayments(body, listPaymentGateways, paymentsBody, amount, orderBody);
|
|
131
|
+
} else {
|
|
132
|
+
return sendError(res, 409, 'CKT902', 'Any valid payment gateway from /list_payments module', {
|
|
133
|
+
en_us: 'Payment method not available, please choose another',
|
|
134
|
+
pt_br: 'Forma de pagamento indisponível, por favor escolha outra',
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return createOrder(res, accessToken, hostname, amount, checkoutBody, orderBody, transactions, dateTime);
|
|
140
138
|
}
|
|
141
|
-
return sendError(res,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
139
|
+
return sendError(res, 400, 'CKT801', 'Cannot handle checkout, any valid cart item');
|
|
140
|
+
}
|
|
141
|
+
return sendError(res, 404, -404, 'Not found', {
|
|
142
|
+
en_us: 'No customers found with ID or email provided',
|
|
143
|
+
pt_br: 'Nenhum cliente encontrado com ID ou e-mail fornecido',
|
|
144
|
+
});
|
|
145
145
|
};
|
|
146
|
+
|
|
146
147
|
export default (schema, req, res, hostname) => {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
148
|
+
const validate = ajv.compile(schema);
|
|
149
|
+
const ip = req.headers['x-real-ip'];
|
|
150
|
+
if (!req.body.browser_ip && ip) {
|
|
151
|
+
req.body.browser_ip = ip;
|
|
152
|
+
}
|
|
153
|
+
let acessToken = req.headers.authorization;
|
|
154
|
+
if (acessToken) {
|
|
155
|
+
acessToken = acessToken.replace(/Bearer /i, '');
|
|
156
|
+
return runCheckout(req.body, acessToken, res, validate, hostname);
|
|
157
|
+
}
|
|
158
|
+
return sendError(res, 401, 109, "Token is required on 'Authorization'", {
|
|
159
|
+
en_us: 'No authorization for the requested method and resource',
|
|
160
|
+
pt_br: 'Sem autorização para o método e recurso solicitado',
|
|
161
|
+
});
|
|
161
162
|
};
|
|
162
|
-
|
|
163
|
+
// # sourceMappingURL=checkout.js.map
|