All files / src/payment/QR generateQR.js

64% Statements 16/25
37.5% Branches 6/16
100% Functions 2/2
64% Lines 16/25

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        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-paymentservice-generatecodeqr'
const SUCCESS = '0'
 
//variable de configuración
let config = {}
 
/**
 *
 * @returns {Promise<string>}
 */
async function init () {
  const { headers, RequestHeader, endpoint } = nequiHeaders(config, 'generateCodeQR', restEndpoint)
  const data = {
    RequestMessage: {
      RequestHeader,
      RequestBody: {
        any: {
          generateCodeQRRQ: {
            code: 'NIT_'+process.env.NEQUI_CLIENT_NIT,
            value: config.value,
            reference1: config.reference ? config.reference : 'reference1',
          }
        }
      }
    }
  }
  
  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 {
          codeQR = ''
        } = data.ResponseMessage.ResponseBody.any.generateCodeQRRS
        
        return codeQR
      } 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<string>}
 * @param userOptions
 * @param paymentOptions
 */
export async function generateQR (userOptions, paymentOptions) {
  config = { ...userOptions, ...paymentOptions }
  try {
    return await init()
  } catch (error) {
    console.error(`Error generando código -> '${ error.message }'`)
  }
}