All files / src/reverse reverseTransaction.js

64.29% Statements 18/28
41.18% Branches 7/17
100% Functions 2/2
65.38% Lines 17/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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          1x 1x 1x 1x     1x                     1x 1x                                 1x 1x             1x 1x       1x   1x 1x                                               1x 1x 1x          
import axios from 'axios'
import { nequiHeaders } from '../utils'
 
 
//Definición de constantes
const RestEndpoint = '/payments/v2/-services-reverseservices-reversetransaction'
const SUCCESS = '0'
const NO_EXISTE = '2-CCSB000079' //No se encontro un dato en el core financiero, puede que la transacción o messageId no exista
const ERROR_TECNICO = '20-07A' //No se encontro un dato en el core financiero, puede que la transacción o messageId no exista
 
//Variable de configuración
let config = {}
 
/**
 *
 * @returns {Promise<boolean>}
 */
async function init () {
  const {
    headers,
    RequestHeader,
    endpoint
  } = nequiHeaders(config, 'reverseTransaction', RestEndpoint, 'reverseServices')
  const data = {
    RequestMessage: {
      RequestHeader,
      RequestBody: {
        any: {
          reversionRQ: {
            phoneNumber: config.phoneNumber,
            value: config.value,
            code: 'NIT_'+process.env.NEQUI_CLIENT_NIT,
            messageId: config.messageID,
            type: 'payment'
          }
        }
      }
    }
  }
  
  try {
    const response = await axios.request({
      url: endpoint,
      method: 'POST',
      headers,
      data
    })
    
    Eif (!!response && response.status === 200 && response.data) {
      const { data } = response
      const {
        StatusCode: statusCode = '',
        StatusDesc: statusDesc = ''
      } = data.ResponseMessage.ResponseHeader.Status
      
      Iif (statusCode === SUCCESS) return statusCode === SUCCESS
      Eif (statusCode === NO_EXISTE || statusCode === ERROR_TECNICO) return false
      throw new Error(`Error ${ statusCode } = ${ statusDesc }`)
    } else {
      throw new Error('Unable to connect to Nequi, please check the information sent.')
    }
  } catch (error) {
    let msgError = ''
    if (error.isAxiosError) {
      const { status = 'Undefined', statusText = 'Undefined' } = error.response
      msgError = `Axios error ${ status } -> ${ statusText }`
      throw new Error(msgError)
    } else {
      throw error
    }
  }
}
 
/**
 *
 * @returns {Promise<boolean>}
 * @param userOptions
 * @param paymentOptions
 */
export async function reverseTransaction (userOptions, paymentOptions) {
  config = { ...userOptions, ...paymentOptions }
  try {
    return await init()
  } catch (error) {
    console.error(`Error generando la reversión del pago por QR -> '${ error.message }'`)
  }
}