All files / src/payment getStatusPayment.js

66.67% Statements 18/27
40% Branches 6/15
100% Functions 2/2
66.67% Lines 18/27

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             3x 3x                         3x 3x             3x 3x       3x   3x       3x   3x                                                       3x 3x 3x          
import axios from 'axios'
import { nequiHeaders } from '../utils'
 
//Definición de constantes
const RestEndpoint = '/payments/v2/-services-paymentservice-getstatuspayment'
const PENDIENTE = '33'
const REALIZADO = '35'
const SUCCESS = '0'
 
//variable de configuración
let config = {}
 
/**
 *
 * @returns {Promise<{phoneNumber: string, status: string}>}
 */
async function init () {
  const { headers, RequestHeader, endpoint } = nequiHeaders(config, 'getStatusPayment', RestEndpoint)
  const data = {
    RequestMessage: {
      RequestHeader,
      RequestBody: {
        any: {
          getStatusPaymentRQ: {
            codeQR: config.code,
          }
        }
      }
    }
  }
  
  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
      
      Eif (statusCode === SUCCESS) {
        const {
          status = '',
          phoneNumber = '',
        } = data.ResponseMessage.ResponseBody.any.getStatusPaymentRS
        
        return { status, phoneNumber }
        
      } else {
        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<{phoneNumber: string, status: string}>}
 * @param userOptions
 * @param paymentOptions
 */
export async function getStatusPayment (userOptions, paymentOptions) {
  config = { ...userOptions, ...paymentOptions }
  try {
    return await init()
  } catch (error) {
    console.error(`Error verificando el estado del pago por QR -> '${ error.message }'`)
  }
}