1

I can't seem to parse the response of an API call I'm making in a Twilio function. I know the call is successful since I get the response in the success function but when i try to filter out the different results it always defaults to the else statement.

Here is my code:

exports.handler = function(context, event, callback) {

const name = event.name;
const rawCc = event.cc;
const cc = rawCc.replace(/[- /\,.]/g, "");

const rawExp = event.exp;
const exp = rawExp.replace(/[- /\,.]/g, "");

const cvc_code = event.cvc;

const rawAmount = event.amount;
const pos = rawAmount.indexOf('$');
const subst = rawAmount.substring(pos+1);
const pos2 = subst.indexOf(' ');
const cleanAmount = subst.substring(0,pos2);
const chargeAmount = cleanAmount + "00";

var chargeType;
if(rawAmount.includes('donation')){
    chargeType = 'donation';
}else{
    chargeType = 'raffle purchase';
}

const chargeDescription = event.description;

const num = event.number;
const donorNumber = num.slice(-10,num.length-7)+"-"+num.slice(-7,num.length-4)+"-"+num.slice(-4,num.length);

//import http request library
const axios = require('axios');

//post Authorization
const un = 'testing'; //process.env.CARDPOINTE_UN;
const pw = 'testing123'; //process.env.CARDPOINTE_PW;
const merchid = '496160873888'; //process.env.CARDPOINTE_BYHS_MERCHID;
const authKey = Buffer.from(un + ":" + pw).toString("base64");
const authorization = "Basic " + authKey;

// post url  
const url = "https://fts-uat.cardconnect.com/cardconnect/rest/auth";
  
// post parameters  

    
const payload = JSON.stringify({
  "merchid" : merchid,
  "account": cc,
  "expiry": exp,
  "cvv2": cvc_code,
  "amount": chargeAmount,
  "phone": donorNumber,
  "name": name,
  "capture": "y",
  "ecomind": "t"
  });

//make post request with axios
  axios.post(url, payload,  {
  headers: {
    'Authorization' : authorization,
    'Content-Type' : 'application/json'
  }
  })
  

  //handle success  
  .then(function (response) {
     const resultCode = response.respstat;
    var res;
     if(resultCode.toLowerCase() == 'a'){
        res = 'Your ' + chargeType + ' of $' + cleanAmount + ' was successfully processed.';
     }else if(resultCode.toLowerCase() == 'c'){
       const errMsg = data.resptext;
         res = 'Sorry your ' + chargeType + ' could not be processed, due to the following issue: ' + errMsg;
     }else{
         res = 'Sorry there was an issue processing your ' + chargeType + '.';
     }
    callback(null, res);
})
  
  
  
//handle error  
  .catch(function (error) {
    const errRes = 'Sorry. There was an error while proccessing your '+chargeType+'.';
    error.reply = errRes;
    callback(null, errRes);
  });
//end function
};
jack
  • 330
  • 3
  • 12

1 Answers1

0

Ok I figured it out after much googling. In a axios post request you need to parse the data object. so response.data.respstat gave me what i was looking for.

jack
  • 330
  • 3
  • 12