cloudcommerce 0.0.112 → 0.0.113

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 (172) hide show
  1. package/.gitmodules +1 -1
  2. package/CHANGELOG.md +9 -0
  3. package/{.ecomplus/stores → ecomplus-stores}/monocard/.firebaserc +0 -0
  4. package/{.ecomplus/stores → ecomplus-stores}/monocard/.github/renovate.json +0 -0
  5. package/{.ecomplus/stores → ecomplus-stores}/monocard/.github/workflows/build-and-deploy.yml +0 -0
  6. package/{.ecomplus/stores → ecomplus-stores}/monocard/.github/workflows/calibreapp-image-actions.yml +0 -0
  7. package/{.ecomplus/stores → ecomplus-stores}/monocard/.vscode/extensions.json +0 -0
  8. package/{.ecomplus/stores → ecomplus-stores}/monocard/.vscode/settings.json +0 -0
  9. package/{.ecomplus/stores → ecomplus-stores}/monocard/LICENSE.md +0 -0
  10. package/{.ecomplus/stores → ecomplus-stores}/monocard/README.md +0 -0
  11. package/{.ecomplus/stores → ecomplus-stores}/monocard/README.pt-BR.md +0 -0
  12. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/config.json +0 -0
  13. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/core/index.js +0 -0
  14. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/core/package.json +1 -1
  15. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/events/index.js +0 -0
  16. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/events/package.json +2 -2
  17. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/example.env +0 -0
  18. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/modules/index.js +0 -0
  19. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/modules/package.json +2 -2
  20. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/passport/index.js +0 -0
  21. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/passport/package.json +2 -2
  22. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/astro.config.mjs +0 -0
  23. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/blog.json +0 -0
  24. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/brands.json +0 -0
  25. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/categories.json +0 -0
  26. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/code.json +0 -0
  27. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/collections.json +0 -0
  28. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/contacts.json +0 -0
  29. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/footer.json +0 -0
  30. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/header.json +0 -0
  31. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/home.json +0 -0
  32. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/info.json +0 -0
  33. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/maintenance.json +0 -0
  34. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/menu.json +0 -0
  35. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/contato.json +0 -0
  36. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/entrega.json +0 -0
  37. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/faq.json +0 -0
  38. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/pagamentos.json +0 -0
  39. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/privacidade.json +0 -0
  40. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/sobre-nos.json +0 -0
  41. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/termos.json +0 -0
  42. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/pages/trocas.json +0 -0
  43. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/posts/esta-loja-e-um-pwa.json +0 -0
  44. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/products.json +0 -0
  45. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/search.json +0 -0
  46. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/settings.json +7 -7
  47. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/social.json +0 -0
  48. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/analytics.json +0 -0
  49. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/compre-confie.json +0 -0
  50. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/ebit.json +0 -0
  51. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/fb-pixel.json +0 -0
  52. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/gmc-ratings.json +0 -0
  53. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/minicart.json +0 -0
  54. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/offers-notification.json +0 -0
  55. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/opinioes-verificadas.json +0 -0
  56. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/product-card.json +0 -0
  57. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/product.json +0 -0
  58. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/search-engine.json +0 -0
  59. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/search.json +0 -0
  60. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/tag-manager.json +0 -0
  61. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/tawkto.json +0 -0
  62. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/trustvox.json +0 -0
  63. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/content/widgets/user.json +0 -0
  64. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/index.js +0 -0
  65. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/package.json +4 -4
  66. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/admin/config.json +0 -0
  67. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/cms-preview.css +0 -0
  68. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/cms.css +0 -0
  69. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/cvv.png +0 -0
  70. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/icons/bootstrap-icons/font/storefront-icons.woff2 +0 -0
  71. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/icons/feather-icons/font/storefront-icons.woff2 +0 -0
  72. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/icons/font-awesome/font/storefront-icons.woff2 +0 -0
  73. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/icons/line-awesome/font/storefront-icons.woff2 +0 -0
  74. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/icons/tabler-icons/font/storefront-icons.woff2 +0 -0
  75. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/img-placeholder.png +0 -0
  76. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/payments.png +0 -0
  77. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/assets/ssl-safe.png +0 -0
  78. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/icon.png +0 -0
  79. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/large-icon.png +0 -0
  80. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/banner1.png +0 -0
  81. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/banner2.png +0 -0
  82. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/banner2.webp +0 -0
  83. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/favicon.png +0 -0
  84. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/headless.png +0 -0
  85. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/headphone.png +0 -0
  86. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/headphone.webp +0 -0
  87. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/icon.png +0 -0
  88. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/large-icon.png +0 -0
  89. package/ecomplus-stores/monocard/functions/ssr/public/img/uploads/logo.png +0 -0
  90. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/og-image.png +0 -0
  91. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/passion.png +0 -0
  92. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/passion.webp +0 -0
  93. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/pwa-reliable.png +0 -0
  94. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/rect8589.png +0 -0
  95. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/rect859.png +0 -0
  96. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/rect89.png +0 -0
  97. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/rect89.webp +0 -0
  98. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/img/uploads/ssl-safe.png +0 -0
  99. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/public/robots.txt +0 -0
  100. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/src/env.d.ts +0 -0
  101. package/ecomplus-stores/monocard/functions/ssr/src/layouts/Pages.astro +23 -0
  102. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/src/pages/[...slug].astro +0 -0
  103. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/src/pages/fallback.astro +0 -0
  104. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/src/pages/index.astro +1 -3
  105. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/src/scripts/InlineScripts.astro +0 -0
  106. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/tailwind.config.cjs +0 -0
  107. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/tsconfig.json +0 -0
  108. package/{.ecomplus/stores → ecomplus-stores}/monocard/functions/ssr/uno.config.ts +0 -0
  109. package/{.ecomplus/stores → ecomplus-stores}/monocard/package.json +1 -1
  110. package/{.ecomplus/stores → ecomplus-stores}/monocard/scripts/install.sh +0 -0
  111. package/package.json +2 -1
  112. package/packages/api/package.json +1 -1
  113. package/packages/apps/correios/package.json +1 -1
  114. package/packages/apps/custom-shipping/package.json +1 -1
  115. package/packages/apps/discounts/package.json +1 -1
  116. package/packages/apps/frenet/package.json +1 -1
  117. package/packages/apps/mercadopago/CHANGELOG.md +1 -0
  118. package/packages/apps/mercadopago/README.md +1 -0
  119. package/packages/apps/mercadopago/assets/onload-expression.js +138 -0
  120. package/packages/apps/mercadopago/lib/index.d.ts +1 -0
  121. package/packages/apps/mercadopago/lib/index.js +3 -0
  122. package/packages/apps/mercadopago/lib/index.js.map +1 -0
  123. package/packages/apps/mercadopago/lib/mercadopago.d.ts +86 -0
  124. package/packages/apps/mercadopago/lib/mercadopago.js +15 -0
  125. package/packages/apps/mercadopago/lib/mercadopago.js.map +1 -0
  126. package/packages/apps/mercadopago/lib/mp-create-transaction.d.ts +85 -0
  127. package/packages/apps/mercadopago/lib/mp-create-transaction.js +270 -0
  128. package/packages/apps/mercadopago/lib/mp-create-transaction.js.map +1 -0
  129. package/packages/apps/mercadopago/lib/mp-list-payments.d.ts +7 -0
  130. package/packages/apps/mercadopago/lib/mp-list-payments.js +175 -0
  131. package/packages/apps/mercadopago/lib/mp-list-payments.js.map +1 -0
  132. package/packages/apps/mercadopago/lib/mp-webhook.d.ts +5 -0
  133. package/packages/apps/mercadopago/lib/mp-webhook.js +119 -0
  134. package/packages/apps/mercadopago/lib/mp-webhook.js.map +1 -0
  135. package/packages/apps/mercadopago/package.json +36 -0
  136. package/packages/apps/mercadopago/scripts/build.sh +5 -0
  137. package/packages/apps/mercadopago/src/index.ts +2 -0
  138. package/packages/apps/mercadopago/src/mercadopago.ts +16 -0
  139. package/packages/apps/mercadopago/src/mp-create-transaction.ts +290 -0
  140. package/packages/apps/mercadopago/src/mp-list-payments.ts +198 -0
  141. package/packages/apps/mercadopago/src/mp-webhook.ts +136 -0
  142. package/packages/apps/mercadopago/tsconfig.json +6 -0
  143. package/packages/apps/mercadopago/webhook.js +1 -0
  144. package/packages/apps/tiny-erp/package.json +1 -1
  145. package/packages/cli/lib/index.js +4 -0
  146. package/packages/cli/package.json +1 -1
  147. package/packages/cli/src/index.ts +5 -0
  148. package/packages/config/package.json +1 -1
  149. package/packages/events/lib/firebase.js +2 -0
  150. package/packages/events/lib/firebase.js.map +1 -1
  151. package/packages/events/package.json +2 -1
  152. package/packages/events/src/firebase.ts +2 -0
  153. package/packages/firebase/lib/config.d.ts +3 -0
  154. package/packages/firebase/lib/config.js +3 -0
  155. package/packages/firebase/lib/config.js.map +1 -1
  156. package/packages/firebase/package.json +1 -1
  157. package/packages/firebase/src/config.ts +3 -0
  158. package/packages/i18n/package.json +1 -1
  159. package/packages/modules/lib/firebase/call-app-module.js +14 -0
  160. package/packages/modules/lib/firebase/call-app-module.js.map +1 -1
  161. package/packages/modules/package.json +2 -1
  162. package/packages/modules/src/firebase/call-app-module.ts +14 -0
  163. package/packages/passport/package.json +1 -1
  164. package/packages/ssr/package.json +1 -1
  165. package/packages/storefront/dist/server/entry.mjs +6 -4
  166. package/packages/storefront/package.json +1 -1
  167. package/packages/storefront/src/lib/layouts/Pages.astro +4 -3
  168. package/packages/storefront/src/lib/layouts/PagesHeader.astro +3 -0
  169. package/packages/types/package.json +1 -1
  170. package/pnpm-workspace.yaml +3 -3
  171. package/.ecomplus/stores/monocard/functions/ssr/public/img/uploads/logo.png +0 -0
  172. package/.ecomplus/stores/monocard/functions/ssr/public/img/uploads/logo.webp +0 -0
@@ -0,0 +1,175 @@
1
+ import * as path from 'path';
2
+ import * as fs from 'fs';
3
+ import url from 'url';
4
+
5
+ const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
6
+
7
+ export default (data) => {
8
+ const { application } = data;
9
+ const { params } = data;
10
+ // https://apx-mods.e-com.plus/api/v1/list_payments/schema.json?store_id=100
11
+ const amount = params.amount || { total: undefined };
12
+ // const initialTotalAmount = amount.total;
13
+ const config = {
14
+ ...application.data,
15
+ ...application.hidden_data,
16
+ };
17
+ if (!config.mp_public_key || !config.mp_access_token) {
18
+ // must have configured PayPal app ID and secret
19
+ return {
20
+ error: 'LIST_PAYMENTS_ERR',
21
+ message: 'MP Public Key or Access Token is unset on app hidden data (merchant must configure the app)',
22
+ };
23
+ }
24
+ // start mounting response body
25
+ // https://apx-mods.e-com.plus/api/v1/list_payments/response_schema.json?store_id=100
26
+ const response = {
27
+ payment_gateways: [],
28
+ };
29
+ // calculate discount value
30
+ const { discount } = config;
31
+ if (discount && discount.value) {
32
+ if (discount.apply_at !== 'freight') {
33
+ // default discount option
34
+ const { value } = discount;
35
+ response.discount_option = {
36
+ label: config.discount_option_label,
37
+ value,
38
+ };
39
+ // specify the discount type and min amount is optional
40
+ const discountTypeMinAmount = ['type', 'min_amount'];
41
+ discountTypeMinAmount.forEach((prop) => {
42
+ if (response.discount_option && discount[prop]) {
43
+ response.discount_option[prop] = discount[prop];
44
+ }
45
+ });
46
+ }
47
+ if (amount.total) {
48
+ // check amount value to apply discount
49
+ if (amount.total < discount.min_amount) {
50
+ discount.value = 0;
51
+ } else {
52
+ delete discount.min_amount;
53
+ // fix local amount object
54
+ const maxDiscount = amount[discount.apply_at || 'subtotal'];
55
+ let discountValue;
56
+ if (discount.type === 'percentage') {
57
+ discountValue = (maxDiscount * discount.value) / 100;
58
+ } else {
59
+ discountValue = discount.value;
60
+ if (discountValue > maxDiscount) {
61
+ discountValue = maxDiscount;
62
+ }
63
+ }
64
+ if (discountValue) {
65
+ amount.discount = (amount.discount || 0) + discountValue;
66
+ amount.total -= discountValue;
67
+ if (amount.total < 0) {
68
+ amount.total = 0;
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ // setup payment gateway objects
75
+ const intermediator = {
76
+ code: 'mercadopago',
77
+ link: 'https://www.mercadopago.com.br',
78
+ name: 'Mercado Pago',
79
+ };
80
+ const listPaymentMethods = ['banking_billet', 'credit_card'];
81
+ if (config.account_deposit && config.account_deposit.key_pix) {
82
+ // pix Configured
83
+ listPaymentMethods.push('account_deposit');
84
+ }
85
+ listPaymentMethods.forEach((paymentMethod) => {
86
+ const isCreditCard = paymentMethod === 'credit_card';
87
+ const methodConfig = isCreditCard ? config : config[paymentMethod];
88
+ const minAmount = methodConfig.min_amount || 0;
89
+ const methodEnable = methodConfig.enable || (isCreditCard && !methodConfig.disable);
90
+ if (methodConfig && methodEnable && (amount.total === undefined || amount.total >= minAmount)) {
91
+ let label;
92
+ if (methodConfig.label) {
93
+ label = methodConfig.label;
94
+ } else if (isCreditCard) {
95
+ label = 'Cartão de crédito';
96
+ } else {
97
+ label = paymentMethod === 'account_deposit' ? 'Pix' : 'Boleto bancário';
98
+ }
99
+ const gateway = {
100
+ label,
101
+ icon: methodConfig.icon,
102
+ text: methodConfig.text,
103
+ payment_method: {
104
+ code: paymentMethod,
105
+ name: `${label} - ${intermediator.name}`,
106
+ },
107
+ intermediator,
108
+ type: 'payment',
109
+ };
110
+ if (isCreditCard) {
111
+ if (!gateway.icon) {
112
+ // gateway.icon = `${baseUri}/checkout-stamp.png`; // TODO: baseUri
113
+ gateway.icon = 'https://us-central1-mercadopago-ecom.cloudfunctions.net/app/checkout-stamp.png';
114
+ }
115
+ gateway.js_client = {
116
+ script_uri: 'https://secure.mlstatic.com/sdk/javascript/v1/mercadopago.js',
117
+ onload_expression: `window.Mercadopago.setPublishableKey("${config.mp_public_key}");${fs.readFileSync(path.join(__dirname, '../../assets/onload-expression.min.js'), 'utf8')}`,
118
+ cc_brand: {
119
+ function: '_mpBrand',
120
+ is_promise: true,
121
+ },
122
+ cc_hash: {
123
+ function: '_mpHash',
124
+ is_promise: true,
125
+ },
126
+ cc_installments: {
127
+ function: '_mpInstallments',
128
+ is_promise: true,
129
+ },
130
+ };
131
+ // default configured default installments option
132
+ const installmentsOption = config.installments_option;
133
+ if (installmentsOption && installmentsOption.max_number) {
134
+ response.installments_option = installmentsOption;
135
+ const minInstallment = installmentsOption.min_installment;
136
+ // optional configured installments list
137
+ if (amount.total && Array.isArray(config.installments) && config.installments.length) {
138
+ gateway.installment_options = [];
139
+ config.installments.forEach(({ number, interest }) => {
140
+ if (number >= 2) {
141
+ const value = amount.total / number;
142
+ if (gateway.installment_options && value >= minInstallment) {
143
+ gateway.installment_options.push({
144
+ number,
145
+ value: interest > 0 ? (value + value * interest) / 100 : value,
146
+ tax: Boolean(interest),
147
+ });
148
+ }
149
+ }
150
+ });
151
+ }
152
+ }
153
+ }
154
+ // check available discount by payment method
155
+ if (discount && discount.value && discount[paymentMethod] !== false) {
156
+ gateway.discount = {
157
+ apply_at: 'total',
158
+ type: 'fixed',
159
+ value: 0,
160
+ };
161
+ ['apply_at', 'type', 'value'].forEach((field) => {
162
+ if (gateway.discount && discount[field]) {
163
+ gateway.discount[field] = discount[field];
164
+ }
165
+ });
166
+ if (response.discount_option && !response.discount_option.label) {
167
+ response.discount_option.label = label;
168
+ }
169
+ }
170
+ response.payment_gateways.push(gateway);
171
+ }
172
+ });
173
+ return response;
174
+ };
175
+ // # sourceMappingURL=mp-list-payments.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mp-list-payments.js","sourceRoot":"","sources":["../src/mp-list-payments.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAC7B,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,GAAG,MAAM,KAAK,CAAC;AAKtB,MAAM,SAAS,GAAG,GAAG,CAAC,aAAa,CAAC,IAAI,GAAG,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;AAEnE,eAAe,CAAC,IAAmB,EAAE,EAAE;IACrC,MAAM,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;IAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,MAA4B,CAAC;IACjD,4EAA4E;IAC5E,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,EAAE,KAAK,EAAE,SAAS,EAAE,CAAC;IACrD,2CAA2C;IAE3C,MAAM,MAAM,GAAG;QACb,GAAG,WAAW,CAAC,IAAI;QACnB,GAAG,WAAW,CAAC,WAAW;KAC3B,CAAC;IAEF,IAAI,CAAC,MAAM,CAAC,aAAa,IAAI,CAAC,MAAM,CAAC,eAAe,EAAE;QACpD,gDAAgD;QAChD,OAAO;YACL,KAAK,EAAE,mBAAmB;YAC1B,OAAO,EAAE,6FAA6F;SACvG,CAAC;KACH;IAED,+BAA+B;IAC/B,qFAAqF;IACrF,MAAM,QAAQ,GAAyB;QACrC,gBAAgB,EAAE,EAAE;KACrB,CAAC;IAEF,2BAA2B;IAC3B,MAAM,EAAE,QAAQ,EAAE,GAAG,MAAM,CAAC;IAC5B,IAAI,QAAQ,IAAI,QAAQ,C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C,eAAe,IAAI,CAAC,QAAQ,CAAC,eAAe,CAAC,KAAK,EAAE;oBAC/D,QAAQ,CAAC,eAAe,CAAC,KAAK,GAAG,KAAK,CAAC;iBACxC;aACF;YAED,QAAQ,CAAC,gBAAgB,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACzC;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC"}
@@ -0,0 +1,5 @@
1
+ import '@cloudcommerce/firebase/lib/init';
2
+ import * as functions from 'firebase-functions/v1';
3
+ export declare const mercadopago: {
4
+ webhook: functions.HttpsFunction;
5
+ };
@@ -0,0 +1,119 @@
1
+ /* eslint-disable import/prefer-default-export */
2
+ import '@cloudcommerce/firebase/lib/init';
3
+ import { logger } from 'firebase-functions';
4
+ import axios from 'axios';
5
+ import api from '@cloudcommerce/api';
6
+ // eslint-disable-next-line import/no-unresolved
7
+ import { getFirestore } from 'firebase-admin/firestore';
8
+ // eslint-disable-next-line import/no-unresolved
9
+ import * as functions from 'firebase-functions/v1';
10
+ import config from '@cloudcommerce/firebase/lib/config';
11
+ import { parsePaymentStatus } from './mp-create-transaction.js';
12
+
13
+ const ECHO_SKIP = 'SKIP';
14
+ let ECHO_SUCCESS = 'OK';
15
+
16
+ export const mercadopago = {
17
+ webhook: functions
18
+ .region(config.get().httpsFunctionOptions.region)
19
+ .https.onRequest(async (req, res) => {
20
+ const { method } = req;
21
+ if (method === 'POST') {
22
+ const { body } = req;
23
+ logger.log('>> Webhook MP #', JSON.stringify(body), ' <<');
24
+ try {
25
+ const app = (await api.get('applications?app_id=111223&fields=hidden_data')).data.result;
26
+ const accessTokenMp = app[0].hidden_data?.mp_access_token;
27
+ if (accessTokenMp) {
28
+ const notification = req.body;
29
+ if (notification.type !== 'payment' || !notification.data || !notification.data.id) {
30
+ res.status(404).send(ECHO_SKIP);
31
+ }
32
+ // setTimeout(() => {
33
+ logger.log('> MP Notification for Payment #', notification.data.id);
34
+ const docRef = getFirestore().collection('mp_payments')
35
+ .doc(String(notification.data.id));
36
+ docRef.get()
37
+ .then(async (doc) => {
38
+ if (doc.exists) {
39
+ const data = doc.data();
40
+ const orderId = data?.order_id;
41
+ const order = (await api.get(`orders/${orderId}`)).data;
42
+ logger.log('>order ', JSON.stringify(order), '<');
43
+ if (order && order.transactions) {
44
+ const payment = (await axios.get(`https://api.mercadopago.com/v1/payments/${notification.data.id}`, {
45
+ headers: {
46
+ Authorization: `Bearer ${accessTokenMp}`,
47
+ 'Content-Type': 'application/json',
48
+ },
49
+ })).data;
50
+ logger.log('>payment ', JSON.stringify(payment), ' <');
51
+ const methodPayment = payment.payment_method_id;
52
+ const transaction = order.transactions.find(({ intermediator }) => {
53
+ return intermediator
54
+ && intermediator.transaction_code === notification.data.id;
55
+ });
56
+ const status = parsePaymentStatus(payment.status);
57
+ if (transaction) {
58
+ const bodyPaymentHistory = {
59
+ transaction_id: transaction._id,
60
+ date_time: new Date().toISOString(),
61
+ status,
62
+ notification_code: String(notification.id),
63
+ flags: [
64
+ 'mercadopago',
65
+ ],
66
+ }; // TODO: incompatible type=> amount and status
67
+ if (status !== order.financial_status?.current) {
68
+ // avoid unnecessary API request
69
+ await api.post(`orders/${orderId}/payments_history`, bodyPaymentHistory);
70
+ const updatedAt = new Date().toISOString();
71
+ docRef.set({ status, updatedAt }, { merge: true }).catch(logger.error);
72
+ }
73
+ if ((status === 'paid' && methodPayment === 'pix' && transaction)) {
74
+ let { notes } = transaction;
75
+ notes = notes?.replace('display:block', 'display:none'); // disable QR Code
76
+ notes = `${notes} # PIX Aprovado`;
77
+ // orders/${order._id}/transactions/${transactionId}.json { notes }
78
+ // Update to disable QR Code
79
+ try {
80
+ await api.patch(`orders/${order._id}/transactions/${transaction._id}`, { notes });
81
+ ECHO_SUCCESS = 'SUCCESS';
82
+ } catch (e) {
83
+ logger.error(e);
84
+ }
85
+ }
86
+ res.status(200).send(ECHO_SUCCESS);
87
+ } else {
88
+ // transaction not found
89
+ logger.log('> Transaction not found #', notification.data.id);
90
+ res.sendStatus(404);
91
+ }
92
+ } else {
93
+ // order or order transaction not found
94
+ logger.log('> Order Not Found #', orderId);
95
+ res.sendStatus(404);
96
+ }
97
+ } else {
98
+ logger.log('> Payment not found in Firestore #', notification.data.id);
99
+ res.sendStatus(404);
100
+ }
101
+ })
102
+ .catch((err) => {
103
+ logger.error(err);
104
+ res.sendStatus(503);
105
+ });
106
+ // }, 3000);
107
+ } else {
108
+ res.sendStatus(406);
109
+ }
110
+ } catch (e) {
111
+ logger.error(e);
112
+ res.sendStatus(500);
113
+ }
114
+ } else {
115
+ res.sendStatus(405);
116
+ }
117
+ }),
118
+ };
119
+ // # sourceMappingURL=mp-webhook.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"mp-webhook.js","sourceRoot":"","sources":["../src/mp-webhook.ts"],"names":[],"mappings":"AAAA,iDAAiD;AACjD,OAAO,kCAAkC,CAAC;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,GAAG,MAAM,oBAAoB,CAAC;AACrC,gDAAgD;AAChD,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,gDAAgD;AAChD,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAC;AACnD,OAAO,MAAM,MAAM,oCAAoC,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAC;AAE7D,MAAM,SAAS,GAAG,MAAM,CAAC;AACzB,IAAI,YAAY,GAAG,IAAI,CAAC;AAExB,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB,OAAO,EAAE,SAAS;SACf,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,MAAM,CAAC;SAChD,KAAK,CAAC,SAAS,CAAC,KAAK,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE;QAClC,MAAM,EAAE,MAAM,EAAE,GAAG,G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AC,UAAU,CAAC,GAAG,CAAC,CAAC;6BACrB;yBACF;6BAAM;4BACL,MAAM,CAAC,GAAG,CAAC,oCAAoC,EAAE,YAAY,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;4BACvE,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;yBACrB;oBACH,CAAC,CAAC;yBACD,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;wBACb,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;wBAClB,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;oBACtB,CAAC,CAAC,CAAC;oBACP,YAAY;iBACX;qBAAM;oBACL,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;iBACrB;aACF;YAAC,OAAO,CAAC,EAAE;gBACV,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAChB,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;aACrB;SACF;aAAM;YACL,GAAG,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC;SACrB;IACH,CAAC,CAAC;CACL,CAAC"}
@@ -0,0 +1,36 @@
1
+ {
2
+ "name": "@cloudcommerce/app-mercadopago",
3
+ "type": "module",
4
+ "version": "0.0.113",
5
+ "description": "E-Com Plus Cloud Commerce app to integrate Mercado Pago",
6
+ "main": "lib/mercadopago.js",
7
+ "exports": {
8
+ ".": "./lib/mercadopago.js",
9
+ "./webhook": "./lib/mp-webhook.js"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/ecomplus/cloud-commerce.git",
14
+ "directory": "packages/apps/mercadopago"
15
+ },
16
+ "author": "E-Com Club Softwares para E-commerce <ti@e-com.club>",
17
+ "license": "Apache 2.0 with Commons Clause",
18
+ "bugs": {
19
+ "url": "https://github.com/ecomplus/cloud-commerce/issues"
20
+ },
21
+ "homepage": "https://github.com/ecomplus/cloud-commerce/tree/main/packages/packages/apps/mercadopago#readme",
22
+ "scripts": {
23
+ "build": "sh scripts/build.sh"
24
+ },
25
+ "dependencies": {
26
+ "@cloudcommerce/api": "workspace:*",
27
+ "@cloudcommerce/firebase": "workspace:*",
28
+ "axios": "^0.27.2",
29
+ "firebase-admin": "^11.0.1",
30
+ "firebase-functions": "^3.24.0"
31
+ },
32
+ "devDependencies": {
33
+ "@cloudcommerce/types": "workspace:*",
34
+ "@firebase/app-types": "^0.7.0"
35
+ }
36
+ }
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ node ../../../scripts/assets-minification.mjs
4
+ sh ../../../scripts/build-lib.sh
5
+
@@ -0,0 +1,2 @@
1
+ // eslint-disable-next-line import/prefer-default-export
2
+ export * from './mercadopago';
@@ -0,0 +1,16 @@
1
+ import '@cloudcommerce/firebase/lib/init';
2
+ /* eslint-disable import/prefer-default-export */
3
+ import type { AppModuleBody } from '@cloudcommerce/types';
4
+ // eslint-disable-next-line import/no-unresolved
5
+ import { getFirestore } from 'firebase-admin/firestore';
6
+ // eslint-disable-next-line import/no-unresolved
7
+ import handleListPayments from './mp-list-payments';
8
+ import handleCreateTransaction from './mp-create-transaction';
9
+
10
+ export const listPayments = async (modBody: AppModuleBody) => {
11
+ return handleListPayments(modBody);
12
+ };
13
+
14
+ export const createTransaction = async (modBody: AppModuleBody) => {
15
+ return handleCreateTransaction(modBody, getFirestore());
16
+ };
@@ -0,0 +1,290 @@
1
+ import type { AppModuleBody } from '@cloudcommerce/types';
2
+ import type{ CreateTransactionParams } from '@cloudcommerce/types/modules/create_transaction:params';
3
+ import type{ CreateTransactionResponse } from '@cloudcommerce/types/modules/create_transaction:response';
4
+ import type { Firestore } from 'firebase-admin/firestore';
5
+ import { logger } from 'firebase-functions';
6
+ import config from '@cloudcommerce/firebase/lib/config';
7
+ import axios from 'axios';
8
+
9
+ const parsePaymentStatus = (status: string) => {
10
+ switch (status) {
11
+ case 'rejected':
12
+ return 'voided';
13
+ case 'in_process':
14
+ return 'under_analysis';
15
+ case 'approved':
16
+ return 'paid';
17
+ default:
18
+ return 'pending';
19
+ }
20
+ };
21
+
22
+ export default async (appData: AppModuleBody, firestore:Firestore) => {
23
+ const locationId = config.get().httpsFunctionOptions.region;
24
+ const baseUri = `https://${locationId}-${process.env.GCLOUD_PROJECT}.cloudfunctions.net`;
25
+ // body was already pre-validated on @/bin/web.js
26
+ // treat module request body
27
+ const { application, storeId } = appData;
28
+ const params = appData.params as CreateTransactionParams;
29
+ // app configured options
30
+ const configApp = { ...application.data, ...application.hidden_data };
31
+ const notificationUrl = `${baseUri}/mercadopago-webhook`;
32
+
33
+ let token: string| undefined;
34
+ let paymentMethodId: string;
35
+ const isPix = params.payment_method.code === 'account_deposit';
36
+ if (params.credit_card && params.credit_card.hash) {
37
+ const hashParts = params.credit_card.hash.split(' // ');
38
+ [token] = hashParts;
39
+ try {
40
+ paymentMethodId = JSON.parse(hashParts[1]).payment_method_id;
41
+ } catch (e) {
42
+ paymentMethodId = params.credit_card.company || 'visa';
43
+ }
44
+ } else if (params.payment_method.code === 'banking_billet') {
45
+ paymentMethodId = 'bolbradesco';
46
+ } else if (params.payment_method.code === 'account_deposit') {
47
+ paymentMethodId = 'pix';
48
+ } else {
49
+ return {
50
+ status: 400,
51
+ error: 'NO_CARD_ERR',
52
+ message: 'Credit card hash is required',
53
+ };
54
+ }
55
+
56
+ const { buyer } = params;
57
+ const orderId = params.order_id;
58
+ logger.log('> MP Transaction #', storeId, orderId);
59
+
60
+ // https://www.mercadopago.com.br/developers/pt/reference/payments/_payments/post/
61
+ const payerPhone = {
62
+ area_code: buyer.phone.number.substring(0, 2),
63
+ number: buyer.phone.number.substring(2, 11),
64
+ };
65
+ const additionalInfo: {[key:string]: any} = {
66
+ items: [],
67
+ payer: {
68
+ first_name: buyer.fullname.replace(/\s.*/, ''),
69
+ last_name: buyer.fullname.replace(/[^\s]+\s/, ''),
70
+ phone: payerPhone,
71
+ },
72
+ };
73
+
74
+ if (params.to && params.to.street) {
75
+ additionalInfo.shipments = {
76
+ receiver_address: {
77
+ zip_code: params.to.zip,
78
+ street_name: params.to.street,
79
+ street_number: params.to.number || 0,
80
+ },
81
+ };
82
+ }
83
+ if (params.billing_address && params.billing_address.street) {
84
+ additionalInfo.payer.address = {
85
+ zip_code: params.billing_address.zip,
86
+ street_name: params.billing_address.street,
87
+ street_number: params.billing_address.number || 0,
88
+ };
89
+ } else if (additionalInfo.shipments) {
90
+ additionalInfo.payer.address = additionalInfo.shipments.receiver_address;
91
+ }
92
+
93
+ if (Array.isArray(params.items)) {
94
+ params.items.forEach((item) => {
95
+ additionalInfo.items.push({
96
+ id: item.sku || item.variation_id || item.product_id,
97
+ title: item.name || item.sku,
98
+ description: item.name || item.sku,
99
+ quantity: item.quantity,
100
+ unit_price: item.final_price || item.price,
101
+ });
102
+ });
103
+ }
104
+
105
+ const payerOrBuyer = {
106
+ ...buyer,
107
+ ...params.payer,
108
+ };
109
+
110
+ Object.keys(payerOrBuyer).forEach(
111
+ (field) => {
112
+ if (!payerOrBuyer[field] && buyer[field]) {
113
+ payerOrBuyer[field] = buyer[field];
114
+ }
115
+ },
116
+ );
117
+
118
+ const payment = {
119
+ payer: {
120
+ type: 'customer',
121
+ email: buyer.email,
122
+ first_name: payerOrBuyer.fullname.replace(/\s.*/, ''),
123
+ last_name: payerOrBuyer.fullname.replace(/[^\s]+\s/, ''),
124
+ identification: {
125
+ type: payerOrBuyer.registry_type === 'j' ? 'CNPJ' : 'CPF',
126
+ number: String(payerOrBuyer.doc_number),
127
+ },
128
+ },
129
+ external_reference: String(params.order_number),
130
+ transaction_amount: params.amount.total,
131
+ description: `Pedido #${params.order_number} de ${buyer.fullname}`.substring(0, 60),
132
+ payment_method_id: paymentMethodId,
133
+ token,
134
+ statement_descriptor: configApp.statement_descriptor || `${params.domain}_MercadoPago`,
135
+ installments: params.installments_number || 1,
136
+ notification_url: notificationUrl,
137
+ additional_info: additionalInfo,
138
+ metadata: {
139
+ ecom_store_id: storeId,
140
+ ecom_order_id: orderId,
141
+ },
142
+ };
143
+ logger.log('>data: ', JSON.stringify(payment));
144
+
145
+ try {
146
+ // https://www.mercadopago.com.br/developers/pt/reference/payments/_payments/post
147
+ const { data } = await axios({
148
+ url: 'https://api.mercadopago.com/v1/payments',
149
+ method: 'post',
150
+ headers: {
151
+ Authorization: `Bearer ${configApp.mp_access_token}`,
152
+ 'Content-Type': 'application/json',
153
+ },
154
+ data: payment,
155
+ });
156
+ if (data) {
157
+ logger.log('> MP Checkout #', storeId, orderId);
158
+
159
+ const statusPayment = parsePaymentStatus(data.status);
160
+ let isSaveRetry = false;
161
+ const saveToDb = () => {
162
+ return new Promise((resolve, reject) => {
163
+ firestore.collection('mp_payments')
164
+ .doc(String(data.id))
165
+ .set({
166
+ transaction_code: data.id,
167
+ store_id: storeId,
168
+ order_id: orderId,
169
+ status: statusPayment,
170
+ paymentMethod: paymentMethodId,
171
+ notificationUrl,
172
+ }, {
173
+ merge: true,
174
+ })
175
+ .then(() => {
176
+ logger.log('> Payment #', String(data.id));
177
+ resolve(true);
178
+ })
179
+ .catch((err) => {
180
+ if (err.code === 13 && !isSaveRetry) {
181
+ isSaveRetry = true;
182
+ setTimeout(saveToDb, 500);
183
+ } else {
184
+ logger.error('PAYMENT_SAVE_ERR', err);
185
+ reject(err);
186
+ }
187
+ });
188
+ });
189
+ };
190
+ await saveToDb();
191
+
192
+ const transaction: CreateTransactionResponse['transaction'] = {
193
+ amount: data.transaction_details.total_paid_amount,
194
+ currency_id: data.currency_id,
195
+ intermediator: {
196
+ payment_method: {
197
+ code: paymentMethodId || params.payment_method.code,
198
+ },
199
+ transaction_id: String(data.id),
200
+ transaction_code: String(data.id),
201
+ transaction_reference: data.external_reference,
202
+ },
203
+ status: {
204
+ current: statusPayment,
205
+ },
206
+ };
207
+
208
+ if (params.payment_method.code === 'credit_card') {
209
+ if (data.card) {
210
+ transaction.credit_card = {
211
+ holder_name: data.card.cardholder.name,
212
+ last_digits: data.card.last_four_digits,
213
+ token,
214
+ };
215
+ }
216
+ if (data.installments) {
217
+ transaction.installments = {
218
+ number: data.installments,
219
+ tax: (data.transaction_details.total_paid_amount > data.transaction_amount),
220
+ total: data.transaction_details.total_paid_amount,
221
+ value: data.transaction_details.installment_amount,
222
+ };
223
+ }
224
+ } else if (!isPix && data.transaction_details
225
+ && data.transaction_details.external_resource_url) {
226
+ transaction.payment_link = data.transaction_details.external_resource_url;
227
+ transaction.banking_billet = {
228
+ link: transaction.payment_link,
229
+ };
230
+ if (data.date_of_expiration) {
231
+ const dateValidThru = new Date(data.date_of_expiration);
232
+ if (dateValidThru.getTime() > 0) {
233
+ transaction.banking_billet.valid_thru = dateValidThru.toISOString();
234
+ }
235
+ }
236
+ } else if (isPix && data.point_of_interaction && data.point_of_interaction.transaction_data) {
237
+ // https://www.mercadopago.com.br/developers/pt/docs/checkout-api/integration-configuration/integrate-with-pix#bookmark_visualiza%C3%A7%C3%A3o_de_pagamento
238
+ const qrCode = data.point_of_interaction.transaction_data.qr_code;
239
+ const qrCodeBase64 = data.point_of_interaction.transaction_data.qr_code_base64;
240
+ transaction.notes = '<div style="display:block;margin:0 auto"> '
241
+ + `<img width="280" height="280" style="margin:5px auto" src='data:image/jpeg;base64,${qrCodeBase64}'/> `
242
+ + `<lable> ${qrCode} </label></div>`;
243
+ }
244
+
245
+ return {
246
+ status: 200,
247
+ redirect_to_payment: false,
248
+ transaction,
249
+ };
250
+ }
251
+ return {
252
+ status: 409,
253
+ message: `CREATE_TRANSACTION_ERR #${storeId} - ${orderId} => MP not response`,
254
+ };
255
+ } catch (error: any) {
256
+ let { message } = error;
257
+ //
258
+ const err = {
259
+ message: `CREATE_TRANSACTION_ERR #${storeId} - ${orderId} => ${message}`,
260
+ payment: '',
261
+ status: 0,
262
+ response: '',
263
+ };
264
+ if (error.response) {
265
+ const { status, data } = error.response;
266
+ if (status !== 401 && status !== 403) {
267
+ err.payment = JSON.stringify(payment);
268
+ err.status = status;
269
+ if (typeof data === 'object' && data) {
270
+ err.response = JSON.stringify(data);
271
+ } else {
272
+ err.response = data;
273
+ }
274
+ if (typeof data.message === 'string' && data.message) {
275
+ message = data.message;
276
+ }
277
+ }
278
+ }
279
+ logger.error(err);
280
+ return {
281
+ status: 409,
282
+ error: 'CREATE_TRANSACTION_ERR',
283
+ message,
284
+ };
285
+ }
286
+ };
287
+
288
+ export {
289
+ parsePaymentStatus,
290
+ };