我正在尝试在 Twilio 函数中发出发布请求,以使用 USAePay 网关 API 处理费用,但我的代码似乎在某处跳闸。任何见解都值得赞赏。我认为这可能是callback()
功能在错误的地方。
我还收到一个折旧的警告,buffer
我该如何解决这个问题?
这是我的代码:
exports.handler = function(context, event, callback) {
//setup dependencies
const express = require('express');
const bodyParser = require('body-parser');
const request = require('request');
const sha256 = require('sha256');
const app = express();
app.use(express.static('public'));
app.use(bodyParser.json());
//setup authorization for API request
var seed = "abcdefghijklmnop";
var apikey = "xxxxxxxxxxxxxxxxxxxxxxxx";
var prehash = apikey + seed;
var apihash = 's2/'+ seed + '/' + sha256(prehash);
var authKey = new Buffer(apikey + ":" + apihash).toString('base64');
var authorization = "Basic " + authKey;
//POST endpoint for API request
app.post('/', (req, res) => {
//setup request for API using provided info from user
let options = {
url: 'https://sandbox.usaepay.com/api/v2/transactions',
method: 'POST',
json: true,
headers: {
"Authorization": authorization
},
body: {
"command": "cc:sale",
"amount": "5.00",
"amount_detail": {
"tax": "1.00",
"tip": "0.50"
},
"creditcard": {
"cardholder": "John doe",
"number": "4000100011112224",
"expiration": "0919",
"cvc": "123",
"avs_street": "1234 Main",
"avs_zip": "12345"
}
}
};
//make request and handle response
request(options, (err, apiRes, body) => {
if(err) {
res.status(500).json({message: "internal server error"});
}
else{
res.status(200).json({
result: body.result,
error: body.error || ""
});
}
});
});
};