[英]Parse a response of an API call in Twilio functions
我似乎無法解析我在 Twilio function 中進行的 API 調用的響應。 我知道調用是成功的,因為我在成功 function 中得到響應,但是當我嘗試過濾掉不同的結果時,它總是默認為else
語句。
這是我的代碼:
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
};
好的,經過多次谷歌搜索,我想通了。 在 axios post
請求中,您需要解析data
object。 所以response.data.respstat
給了我我想要的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.