@cloudcommerce/app-pagarme-v5 2.46.0 → 2.46.2

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.
@@ -19,7 +19,7 @@ const updateTransaction = (orderId, body, transactionId) => {
19
19
 
20
20
  const getOrderIntermediatorTransactionId = async (invoiceId) => {
21
21
  let queryString = `transactions.intermediator.transaction_id=${invoiceId}`;
22
- queryString += '&fields=transactions,financial_status,status';
22
+ queryString += '&fields=transactions,financial_status,status,payments_history';
23
23
  const { data } = await api.get(`orders?${queryString}`);
24
24
  return data.result[0] || null;
25
25
  };
@@ -1,6 +1,6 @@
1
1
  import { getFirestore } from 'firebase-admin/firestore';
2
2
  import api from '@cloudcommerce/api';
3
- import { logger } from '@cloudcommerce/firebase/lib/config';
3
+ import config, { logger } from '@cloudcommerce/firebase/lib/config';
4
4
  import getAppData from '@cloudcommerce/firebase/lib/helpers/get-app-data';
5
5
  import axios from './functions-lib/pagarme/create-axios.mjs';
6
6
  import {
@@ -17,9 +17,16 @@ import { parserChangeStatusToEcom } from './functions-lib/pagarme/parses-utils.m
17
17
  const handleWehook = async (req, res) => {
18
18
  const colletionFirebase = getFirestore().collection('pagarmeV5Subscriptions');
19
19
  const { body } = req;
20
+ const type = body?.type;
21
+ if (!type || !body.data) {
22
+ return res.sendStatus(400);
23
+ }
24
+ const { storeId } = config.get();
25
+ if (body.data.metadata?.store_id && Number(body.data.metadata?.store_id) !== storeId) {
26
+ return res.sendStatus(204);
27
+ }
20
28
 
21
29
  try {
22
- const type = body.type;
23
30
  const appData = await getAppData('pagarMeV5');
24
31
 
25
32
  if (!process.env.PAGARMEV5_API_TOKEN) {
@@ -151,7 +158,6 @@ const handleWehook = async (req, res) => {
151
158
  .send({ message: !subscription ? 'Not found subscription' : 'Subscription not canceled' });
152
159
  } else if (type.startsWith('charge.')) {
153
160
  const { data: charge } = await pagarmeAxios.get(`/charges/${body.data.id}`);
154
- logger.log('>> Charge ', JSON.stringify(charge));
155
161
  if (charge.invoice) {
156
162
  const { invoice, status } = charge;
157
163
  const order = await getOrderIntermediatorTransactionId(invoice.id);
@@ -163,7 +169,7 @@ const handleWehook = async (req, res) => {
163
169
  (transactionFind) => transactionFind.intermediator.transaction_id === invoice.id,
164
170
  );
165
171
  const transactionPagarme = charge.last_transaction;
166
- let notificationCode = `${type};${body.id};`;
172
+ let notificationCode = `${type};${(body.id || body.data.id)};`;
167
173
  if (transactionPagarme.transaction_type === 'credit_card') {
168
174
  notificationCode += `${transactionPagarme.gateway_id || ''};`;
169
175
  notificationCode += `${transactionPagarme.acquirer_tid || ''};`;
@@ -189,7 +195,7 @@ const handleWehook = async (req, res) => {
189
195
  return res.sendStatus(200);
190
196
  }
191
197
 
192
- if (status === 'paid') {
198
+ if (status === 'paid' && invoice.subscriptionId) {
193
199
  logger.log('>> Try create new order for recurrence');
194
200
  const { data: subscription } = await pagarmeAxios.get(`/subscriptions/${invoice.subscriptionId}`);
195
201
  const orderOriginal = await getOrderById(subscription.code);
@@ -243,8 +249,8 @@ const handleWehook = async (req, res) => {
243
249
 
244
250
  if (charge.order) {
245
251
  // payment update (order in pagarme)
246
- logger.log('>> Try update status order');
247
252
  const { order: orderPagarme, status } = charge;
253
+ logger.info(`Pagar.me charge ${orderPagarme.id} ${status}`);
248
254
  const order = await getOrderIntermediatorTransactionId(orderPagarme.id);
249
255
  if (order) {
250
256
  if (order.financial_status?.current !== parserChangeStatusToEcom(status)) {
@@ -255,7 +261,7 @@ const handleWehook = async (req, res) => {
255
261
  (transactionFind) => transactionFind.intermediator.transaction_id === orderPagarme.id,
256
262
  );
257
263
  const transactionPagarme = charge.last_transaction;
258
- let notificationCode = `${type};${body.id};`;
264
+ let notificationCode = `${type};${(body.id || body.data.id)};`;
259
265
  if (transactionPagarme.transaction_type === 'credit_card') {
260
266
  notificationCode += `${transactionPagarme.gateway_id || ''};`;
261
267
  notificationCode += `${transactionPagarme.acquirer_tid || ''};`;
@@ -271,8 +277,14 @@ const handleWehook = async (req, res) => {
271
277
  transactionBody = { notes };
272
278
  isUpdateTransaction = true;
273
279
  }
280
+ let statusDateTime;
281
+ if (order.payments_history?.some(({ flags }) => flags?.includes('pagarme-expired'))) {
282
+ statusDateTime = new Date().toISOString();
283
+ } else {
284
+ statusDateTime = transactionPagarme.updated_at || new Date().toISOString();
285
+ }
274
286
  const bodyPaymentHistory = {
275
- date_time: transactionPagarme.updated_at || new Date().toISOString(),
287
+ date_time: statusDateTime,
276
288
  status: parserChangeStatusToEcom(status),
277
289
  notification_code: notificationCode,
278
290
  flags: ['PagarMe'],
@@ -285,7 +297,7 @@ const handleWehook = async (req, res) => {
285
297
  await updateTransaction(order._id, transactionBody, transaction._id)
286
298
  .catch(logger.error);
287
299
  }
288
- logger.log(`>> Status update to ${parserChangeStatusToEcom(status)}`);
300
+ logger.info(`${order._id} update to ${parserChangeStatusToEcom(status)}`);
289
301
  return res.sendStatus(201);
290
302
  }
291
303
  return res.sendStatus(200);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@cloudcommerce/app-pagarme-v5",
3
3
  "type": "module",
4
- "version": "2.46.0",
4
+ "version": "2.46.2",
5
5
  "description": "e-com.plus Cloud Commerce app to integrate Pagar.me API v5 with recurring payments",
6
6
  "main": "lib/index.js",
7
7
  "exports": {
@@ -31,12 +31,12 @@
31
31
  "axios": "^1.10.0",
32
32
  "firebase-admin": "^13.4.0",
33
33
  "firebase-functions": "^6.3.2",
34
- "@cloudcommerce/firebase": "2.46.0",
35
- "@cloudcommerce/api": "2.46.0"
34
+ "@cloudcommerce/api": "2.46.2",
35
+ "@cloudcommerce/firebase": "2.46.2"
36
36
  },
37
37
  "devDependencies": {
38
- "@cloudcommerce/test-base": "2.46.0",
39
- "@cloudcommerce/types": "2.46.0"
38
+ "@cloudcommerce/test-base": "2.46.2",
39
+ "@cloudcommerce/types": "2.46.2"
40
40
  },
41
41
  "scripts": {
42
42
  "build": "bash scripts/build.sh",