cloudcommerce 0.0.91 → 0.0.93

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.
Files changed (51) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/package.json +1 -1
  3. package/packages/api/package.json +1 -1
  4. package/packages/apps/correios/package.json +1 -1
  5. package/packages/apps/custom-shipping/package.json +1 -1
  6. package/packages/apps/discounts/package.json +1 -1
  7. package/packages/apps/frenet/package.json +1 -1
  8. package/packages/apps/tiny-erp/package.json +1 -1
  9. package/packages/cli/config/firebase.json +4 -2
  10. package/packages/cli/package.json +1 -1
  11. package/packages/config/package.json +1 -1
  12. package/packages/events/package.json +1 -1
  13. package/packages/firebase/package.json +1 -1
  14. package/packages/modules/lib/firebase/checkout.js +162 -0
  15. package/packages/modules/lib/firebase/checkout.js.map +1 -1
  16. package/packages/modules/lib/firebase/functions-checkout/fix-items.js +200 -0
  17. package/packages/modules/lib/firebase/functions-checkout/fix-items.js.map +1 -0
  18. package/packages/modules/lib/firebase/functions-checkout/get-custumerId.js +34 -0
  19. package/packages/modules/lib/firebase/functions-checkout/get-custumerId.js.map +1 -0
  20. package/packages/modules/lib/firebase/functions-checkout/handle-order-transaction.js +123 -0
  21. package/packages/modules/lib/firebase/functions-checkout/handle-order-transaction.js.map +1 -0
  22. package/packages/modules/lib/firebase/functions-checkout/new-order.js +191 -0
  23. package/packages/modules/lib/firebase/functions-checkout/new-order.js.map +1 -0
  24. package/packages/modules/lib/firebase/functions-checkout/request-to-module.js +67 -0
  25. package/packages/modules/lib/firebase/functions-checkout/request-to-module.js.map +1 -0
  26. package/packages/modules/lib/firebase/functions-checkout/utils.js +227 -0
  27. package/packages/modules/lib/firebase/functions-checkout/utils.js.map +1 -0
  28. package/packages/modules/lib/firebase/serve-modules-api.js +9 -4
  29. package/packages/modules/lib/firebase/serve-modules-api.js.map +1 -1
  30. package/packages/modules/package.json +1 -1
  31. package/packages/modules/schemas/@checkout.cjs +1 -1
  32. package/packages/modules/src/firebase/checkout.ts +235 -0
  33. package/packages/modules/src/firebase/functions-checkout/fix-items.ts +219 -0
  34. package/packages/modules/src/firebase/functions-checkout/get-custumerId.ts +33 -0
  35. package/packages/modules/src/firebase/functions-checkout/handle-order-transaction.ts +195 -0
  36. package/packages/modules/src/firebase/functions-checkout/new-order.ts +273 -0
  37. package/packages/modules/src/firebase/functions-checkout/request-to-module.ts +76 -0
  38. package/packages/modules/src/firebase/functions-checkout/utils.ts +301 -0
  39. package/packages/modules/src/firebase/serve-modules-api.ts +9 -4
  40. package/packages/modules/src/types/index.d.ts +67 -0
  41. package/packages/passport/lib/firebase/handle-passport.js +3 -1
  42. package/packages/passport/lib/firebase/handle-passport.js.map +1 -1
  43. package/packages/passport/lib/index.js +1 -0
  44. package/packages/passport/lib/index.js.map +1 -1
  45. package/packages/passport/package.json +1 -1
  46. package/packages/passport/src/firebase/handle-passport.ts +2 -0
  47. package/packages/passport/src/index.ts +1 -0
  48. package/packages/ssr/package.json +1 -1
  49. package/packages/storefront/package.json +1 -1
  50. package/packages/types/package.json +1 -1
  51. package/pnpm-workspace.yaml +1 -0
@@ -0,0 +1,195 @@
1
+ import type { Response } from 'firebase-functions';
2
+ import type { Orders } from '@cloudcommerce/types';
3
+ import type {
4
+ Amount, BodyOrder,
5
+ BodyPaymentHistory,
6
+ OrderPaymentHistory,
7
+ TransactionOrder,
8
+ } from '../../types/index';
9
+ import { logger } from 'firebase-functions';
10
+ import api from '@cloudcommerce/api';
11
+ import { sendError } from './utils';
12
+
13
+ const checkoutRespond = (
14
+ res: Response,
15
+ orderId:string & {length: 24},
16
+ orderNumber: number | undefined,
17
+ transaction?: TransactionOrder,
18
+ ) => {
19
+ return res.send({
20
+ status: 200,
21
+ order: {
22
+ _id: orderId,
23
+ number: orderNumber,
24
+ },
25
+ transaction,
26
+ });
27
+ };
28
+
29
+ const newOrder = async (
30
+ orderBody:BodyOrder,
31
+ accessToken:string,
32
+ ) => {
33
+ try {
34
+ const orderId = (await api.post(
35
+ 'orders',
36
+ orderBody,
37
+ {
38
+ accessToken,
39
+ },
40
+ )).data._id;
41
+
42
+ return new Promise<Orders|null>((resolve) => {
43
+ setTimeout(async () => {
44
+ try {
45
+ const order = (await api.get(
46
+ `orders/${orderId}`,
47
+ {
48
+ accessToken,
49
+ },
50
+ )).data;
51
+ resolve(order);
52
+ } catch (e) {
53
+ logger.error(e);
54
+ resolve(null);
55
+ }
56
+ }, 800);
57
+ });
58
+ } catch (e) {
59
+ logger.error(e);
60
+ return null;
61
+ }
62
+ };
63
+
64
+ const cancelOrder = async (
65
+ staffNotes: string,
66
+ orderId: string & {length: 24},
67
+ accessToken:string,
68
+ isOrderCancelled:boolean,
69
+ res: Response,
70
+ usrMsg: { en_us: string, pt_br: string },
71
+ errorMessage?: string,
72
+ ) => {
73
+ let msgErro: string | undefined;
74
+ if (!isOrderCancelled) {
75
+ const msgCancell = () => {
76
+ return new Promise((resolve) => {
77
+ setTimeout(async () => {
78
+ const body = {
79
+ status: 'cancelled',
80
+ staff_notes: staffNotes,
81
+ };
82
+ if (errorMessage) {
83
+ body.staff_notes += ` - \`${errorMessage.substring(0, 200)}\``;
84
+ }
85
+ try {
86
+ const response = (await api.patch(
87
+ `orders/${orderId}`,
88
+ body,
89
+ {
90
+ accessToken,
91
+ },
92
+ ));
93
+ if (response.status === 204) {
94
+ isOrderCancelled = true;
95
+ }
96
+ } catch (e) {
97
+ logger.error(e);
98
+ }
99
+ resolve(`${body.staff_notes}`);
100
+ }, 400);
101
+ });
102
+ };
103
+ msgErro = await msgCancell() as string;
104
+ }
105
+ return sendError(
106
+ res,
107
+ 409,
108
+ 'CKT704',
109
+ msgErro || staffNotes,
110
+ usrMsg,
111
+ );
112
+ };
113
+
114
+ const saveTransaction = (
115
+ accessToken: string,
116
+ orderId: string,
117
+ transactionBody: any, // TODO: error type 'status' incompatible
118
+ ) => {
119
+ return new Promise((resolve, reject) => {
120
+ api.post(
121
+ `orders/${orderId}/transactions`,
122
+ transactionBody,
123
+ {
124
+ accessToken,
125
+ },
126
+ )
127
+ .then(({ data }) => {
128
+ resolve(data._id);
129
+ })
130
+ .catch((e) => {
131
+ reject(e);
132
+ });
133
+ });
134
+ };
135
+
136
+ const addPaymentHistory = async (
137
+ orderId: string,
138
+ accessToken:string,
139
+ paymentHistory: BodyPaymentHistory[],
140
+ isFirstTransaction: boolean,
141
+ paymentEntry: BodyPaymentHistory,
142
+ dateTime: string,
143
+ loyaltyPointsBalance: number,
144
+ amount: Amount,
145
+ ) => {
146
+ return new Promise((resolve, reject) => {
147
+ setTimeout(async () => {
148
+ const body: OrderPaymentHistory = {
149
+ amount,
150
+ };
151
+ body.payments_history = paymentHistory;
152
+
153
+ if (isFirstTransaction) {
154
+ body.financial_status = {
155
+ current: paymentEntry.status,
156
+ updated_at: dateTime,
157
+ };
158
+ }
159
+ if (loyaltyPointsBalance > 0) {
160
+ const balance = Math.round(loyaltyPointsBalance * 100) / 100;
161
+ body.amount = {
162
+ ...amount,
163
+ balance,
164
+ total: amount.total - balance,
165
+ };
166
+ }
167
+
168
+ try {
169
+ const response = (await api.patch(
170
+ `orders/${orderId}`,
171
+ body,
172
+ {
173
+ accessToken,
174
+ },
175
+ ));
176
+ if (response.status === 204) {
177
+ resolve(true);
178
+ } else {
179
+ reject(new Error('Error adding payment history'));
180
+ }
181
+ } catch (e) {
182
+ logger.error(e);
183
+ reject(e);
184
+ }
185
+ }, isFirstTransaction ? 200 : 400);
186
+ });
187
+ };
188
+
189
+ export {
190
+ newOrder,
191
+ cancelOrder,
192
+ saveTransaction,
193
+ addPaymentHistory,
194
+ checkoutRespond,
195
+ };
@@ -0,0 +1,273 @@
1
+ import type { Response } from 'firebase-functions';
2
+ import type { CheckoutBody } from '@cloudcommerce/types';
3
+ import type {
4
+ BodyOrder,
5
+ Amount,
6
+ CheckoutTransaction,
7
+ TransactionOrder,
8
+ BodyPaymentHistory,
9
+ } from '../../types/index';
10
+ import logger from 'firebase-functions/lib/logger';
11
+ import { sendError, getValidResults } from './utils';
12
+ import {
13
+ newOrder,
14
+ cancelOrder,
15
+ saveTransaction,
16
+ addPaymentHistory,
17
+ checkoutRespond,
18
+ } from './handle-order-transaction';
19
+ import requestModule from './request-to-module';
20
+
21
+ const usrMsg = {
22
+ en_us: 'Your order was saved, but we were unable to make the payment, '
23
+ + 'please contact us',
24
+ pt_br: 'Seu pedido foi salvo, mas não conseguimos efetuar o pagamento, '
25
+ + 'por favor entre em contato',
26
+ };
27
+
28
+ const createOrder = async (
29
+ res: Response,
30
+ accessToken: string,
31
+ hostname: string,
32
+ amount: Amount,
33
+ checkoutBody: CheckoutBody,
34
+ orderBody:BodyOrder,
35
+ transactions: CheckoutTransaction[],
36
+ dateTime: string,
37
+ ) => {
38
+ // start creating new order to API
39
+
40
+ const order = await newOrder(orderBody, accessToken);
41
+ if (order) {
42
+ const orderId = order._id;
43
+ const orderNumber = order.number;
44
+ const isOrderCancelled = false;
45
+
46
+ let countDone = 0;
47
+ let paymentsAmount = 0;
48
+ let loyaltyPointsBalance = 0;
49
+ const paymentHistory: BodyPaymentHistory[] = [];
50
+
51
+ const nextTransaction = async (index = 0) => {
52
+ const newTransaction = transactions[index];
53
+
54
+ // merge objects to create transaction request body
55
+ const transactionBody = {
56
+ ...checkoutBody,
57
+ transaction: newTransaction,
58
+ order_id: orderId,
59
+ order_number: orderNumber,
60
+ // also need shipping address
61
+ // send from shipping object if undefined on transaction object
62
+ to: { ...checkoutBody.shipping.to },
63
+ ...newTransaction,
64
+ amount: { ...amount },
65
+ };
66
+ newTransaction.amount_part = newTransaction.amount_part || 0;
67
+ if (transactionBody.amount && newTransaction.amount_part > 0
68
+ && newTransaction.amount_part < 1) {
69
+ // fix amount for multiple transactions
70
+ const partialAmount = transactionBody.amount.total * newTransaction.amount_part;
71
+ transactionBody.amount.discount = transactionBody.amount.discount || 0;
72
+ transactionBody.amount.discount += transactionBody.amount.total - partialAmount;
73
+ transactionBody.amount.total = partialAmount;
74
+ if (transactionBody.payment_method.code === 'loyalty_points') {
75
+ loyaltyPointsBalance += partialAmount;
76
+ }
77
+ delete transactionBody.amount_part;
78
+ }
79
+ // logger.log(JSON.stringify(transactionBody, null, 2))
80
+ // logger.log(JSON.stringify(checkoutBody, null, 2))
81
+
82
+ // finally pass to create transaction
83
+ let listTransactions = await requestModule(transactionBody, hostname, 'transaction');
84
+ if (listTransactions) {
85
+ listTransactions = getValidResults(listTransactions);
86
+ // simulateRequest(transactionBody, checkoutRespond, 'transaction', storeId, (results) => {
87
+ const isFirstTransaction = index === 0;
88
+ let isDone: boolean = false;
89
+ for (let i = 0; i < listTransactions.length; i++) {
90
+ const result = listTransactions[i];
91
+ // treat transaction response
92
+ const { response } = result;
93
+ const transaction: TransactionOrder = response && response.transaction;
94
+ if (transaction) {
95
+ // complete transaction object with some request body fields
96
+ [
97
+ 'type',
98
+ 'payment_method',
99
+ 'payer',
100
+ 'currency_id',
101
+ 'currency_symbol',
102
+ ].forEach((field) => {
103
+ if (transactionBody[field] !== undefined && transaction[field] === undefined) {
104
+ transaction[field] = transactionBody[field];
105
+ }
106
+ });
107
+
108
+ // setup transaction app object
109
+ if (!transaction.app) {
110
+ transaction.app = { _id: result._id };
111
+ // complete app object with some request body fields
112
+ const transactionOptions = Array.isArray(checkoutBody.transaction)
113
+ ? checkoutBody.transaction.find(
114
+ (transactionFound) => transactionFound.app_id === result._id,
115
+ )
116
+ : checkoutBody.transaction;
117
+ if (transactionOptions) {
118
+ [
119
+ 'label',
120
+ 'icon',
121
+ 'intermediator',
122
+ 'payment_url',
123
+ ].forEach((field) => {
124
+ if (transactionOptions[field] !== undefined && transaction.app) {
125
+ transaction.app[field] = transactionOptions[field];
126
+ }
127
+ });
128
+ }
129
+ // logger.log(transaction.app)
130
+ }
131
+
132
+ // check for transaction status
133
+ if (!transaction.status) {
134
+ transaction.status = {
135
+ current: 'pending',
136
+ };
137
+ }
138
+ transaction.status.updated_at = dateTime;
139
+
140
+ if (isFirstTransaction) {
141
+ // merge transaction body with order info and respond
142
+ return checkoutRespond(res, orderId, orderNumber, transaction);
143
+ }
144
+
145
+ // save transaction info on order data
146
+ // saveTransaction(transaction, orderId, storeId, (err, transactionId) => {
147
+ try {
148
+ // eslint-disable-next-line no-await-in-loop
149
+ const transactionId = await saveTransaction(
150
+ accessToken,
151
+ orderId,
152
+ transaction,
153
+ ) as string;
154
+ // add entry to payments history
155
+ const paymentEntry: BodyPaymentHistory = {
156
+ transaction_id: transactionId,
157
+ status: transaction.status.current,
158
+ date_time: dateTime,
159
+ flags: ['checkout'],
160
+ };
161
+ paymentHistory.push(paymentEntry);
162
+ try {
163
+ // eslint-disable-next-line no-await-in-loop
164
+ await addPaymentHistory(
165
+ orderId,
166
+ accessToken,
167
+ paymentHistory,
168
+ isFirstTransaction,
169
+ paymentEntry,
170
+ dateTime,
171
+ loyaltyPointsBalance,
172
+ amount,
173
+ );
174
+ } catch (e) {
175
+ logger.error(e);
176
+ }
177
+ } catch (e) {
178
+ logger.error(e);
179
+ }
180
+ index += 1;
181
+ if (index < transactions.length) {
182
+ return nextTransaction(index);
183
+ }
184
+ // });
185
+ isDone = true;
186
+ paymentsAmount += transaction.amount;
187
+ break;
188
+ }
189
+ }
190
+
191
+ if (isDone) {
192
+ countDone += 1;
193
+ if (countDone === transactions.length) {
194
+ if (amount.total / paymentsAmount > 1.01) {
195
+ return cancelOrder(
196
+ 'Transaction amounts doesn\'t match (is lower) order total value',
197
+ orderId,
198
+ accessToken,
199
+ isOrderCancelled,
200
+ res,
201
+ usrMsg,
202
+ );
203
+ }
204
+ }
205
+ // return
206
+ return checkoutRespond(res, orderId, orderNumber);
207
+ }
208
+
209
+ // unexpected response object from create transaction module
210
+ const firstResult = listTransactions && listTransactions[0];
211
+ let errorMessage: string | undefined;
212
+ if (firstResult) {
213
+ const { response } = firstResult;
214
+ if (response) {
215
+ // send devMsg with app response
216
+ if (response.message) {
217
+ errorMessage = response.message;
218
+ if (response.error) {
219
+ errorMessage += ` (${response.error})`;
220
+ }
221
+ } else {
222
+ errorMessage = JSON.stringify(response);
223
+ }
224
+ } else {
225
+ errorMessage = firstResult.error_message;
226
+ }
227
+ }
228
+ if (isFirstTransaction) {
229
+ return sendError(
230
+ res,
231
+ 409,
232
+ 'CKT704',
233
+ errorMessage || 'No valid transaction object',
234
+ usrMsg,
235
+ );
236
+ }
237
+ return cancelOrder(
238
+ 'Error trying to create transaction',
239
+ orderId,
240
+ accessToken,
241
+ isOrderCancelled,
242
+ res,
243
+ usrMsg,
244
+ errorMessage,
245
+ );
246
+ }
247
+ // send error no exist transaction
248
+ return sendError(
249
+ res,
250
+ 409,
251
+ 'CKT704',
252
+ 'There was a problem saving your order, please try again later',
253
+ usrMsg,
254
+ );
255
+ };
256
+
257
+ return nextTransaction();
258
+ }
259
+ // send error
260
+ const userMessage = {
261
+ en_us: 'There was a problem saving your order, please try again later',
262
+ pt_br: 'Houve um problema ao salvar o pedido, por favor tente novamente mais tarde',
263
+ };
264
+ return sendError(
265
+ res,
266
+ 409,
267
+ 'CKT701',
268
+ 'There was a problem saving your order, please try again later',
269
+ userMessage,
270
+ );
271
+ };
272
+
273
+ export default createOrder;
@@ -0,0 +1,76 @@
1
+ import { logger } from 'firebase-functions';
2
+ import axios from 'axios';
3
+
4
+ // handle other modules endpoints directly
5
+ export default async (
6
+ checkoutBody: {[key:string]:any},
7
+ hostname: string,
8
+ label: string,
9
+ ) => {
10
+ const locationId = process.env.FIREBASE_CONFIG
11
+ ? (JSON.parse(process.env.FIREBASE_CONFIG).locationId || 'southamerica-east1')
12
+ : 'southamerica-east1';
13
+
14
+ const baseUrl = hostname !== 'localhost' ? `https://${hostname}`
15
+ : `http://localhost:5001/${process.env.GCLOUD_PROJECT}/${locationId}/modules`; // To LocalTest
16
+
17
+ let moduleBody: {[key:string]:any} | undefined;
18
+ let modName: string | undefined;
19
+ switch (label) {
20
+ case 'shipping':
21
+ modName = 'calculate_shipping';
22
+ moduleBody = checkoutBody.shipping;
23
+ break;
24
+ case 'payment':
25
+ modName = 'list_payments';
26
+ moduleBody = checkoutBody.transaction;
27
+ break;
28
+ case 'discount':
29
+ modName = 'apply_discount';
30
+ moduleBody = checkoutBody.discount;
31
+ break;
32
+ case 'transaction':
33
+ modName = 'create_transaction';
34
+ moduleBody = checkoutBody.transaction;
35
+ break;
36
+ default:
37
+ break;
38
+ }
39
+
40
+ if (moduleBody && moduleBody.app_id && modName) {
41
+ // mask request objects
42
+ const url = `${baseUrl}/${modName}?app_id=${moduleBody.app_id}`;
43
+ // mount request body with received checkout body object
44
+ const body = {
45
+ ...checkoutBody,
46
+ ...moduleBody,
47
+ is_checkout_confirmation: true,
48
+ };
49
+ try {
50
+ console.log('> ', JSON.stringify({ url, body }));
51
+ const resp = (await axios.post(
52
+ url,
53
+ body,
54
+ )).data;
55
+ if (Array.isArray(resp.result)) {
56
+ let countAppErro = 0;
57
+ for (let i = 0; i < resp.result.length; i++) {
58
+ const result = resp.result[i];
59
+ if (!result.validated || result.error) {
60
+ countAppErro += 1;
61
+ console.error(result.response);
62
+ logger.error(result.response);
63
+ }
64
+ }
65
+ if (resp.result.length === countAppErro) {
66
+ return null;
67
+ }
68
+ }
69
+ return resp.result;
70
+ } catch (e) {
71
+ logger.error('>>erro: ', e);
72
+ return null;
73
+ }
74
+ }
75
+ return null;
76
+ };