【发布时间】:2022-01-26 05:22:36
【问题描述】:
我似乎无法解析我在 Twilio 函数中进行的 API 调用的响应。我知道调用是成功的,因为我在成功函数中得到了响应,但是当我尝试过滤掉不同的结果时,它总是默认为 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
};
【问题讨论】:
标签: json api twilio-functions