【问题标题】:function for USAePay API in TWilio functions returns 504 codeTWilio 函数中 USAePay API 的函数返回 504 代码
【发布时间】:2021-11-04 03:44:21
【问题描述】:

我正在尝试在 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 || ""
            });
        
        }
    });
        });
        
};

【问题讨论】:

  • 您好!您说您在 Twilio Functions 中执行此操作,但这似乎是一个 Express 应用程序,根本没有显示 callback 的使用情况。这是正确的代码示例吗?
  • @philnash 感谢您的快速回复。是的,它在 Twilio 函数中我试图从 github 复制这段代码,希望它可以在 Twilio 中工作并添加这些依赖项。我删除了回调,因为它会立即触发而不执行。如果您能指出发布到外部 API 的最佳方式是什么,那就太好了!

标签: node.js twilio twilio-functions usaepay


【解决方案1】:

这里是 Twilio 开发者宣传员。

我建议您阅读有关 how Twilio Functions works 的信息。您不只是导入 Node 应用程序而不进行更改。您需要导出一个名为handler 的函数。

当对 Twilio 函数的 URL 发出请求时,将调用 handler 函数。

该函数接收三个参数,contexteventcallback 函数。 context 包含环境变量等,event 包含来自 HTTP 请求的所有参数(查询字符串参数或请求正文中的参数),callback 用于返回响应。

一个基本的 Twilio 函数如下所示:

exports.handler = function (context, event, callback) {
    return callback(null, { hello: "World!" });
}

在这种情况下,向函数的 URL 发出请求将收到 { "hello": "World!" } 的 JSON 响应。

现在,在您的情况下,您需要向外部 API 发出请求,作为对函数的请求的一部分。首先,我建议您在环境变量中设置诸如 API Key 之类的秘密。然后可以从context 对象访问它们。您的 API 调用将是异步的,因此重要的是仅在完成所有异步调用后才调用 callback 函数。

这样的事情可能会奏效:

const request = require("request");
const sha256 = require("sha256");

exports.handler = function (context, event, callback) {
  const seed = context.SEED;
  const apikey = context.API_KEY;
  const prehash = apikey + seed;
  const apihash = "s2/" + seed + "/" + sha256(prehash);
  const authKey = Buffer.from(apikey + ":" + apihash).toString("base64");
  const authorization = "Basic " + authKey;

  const 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) {
      const response = new Twilio.Response();
      response.setStatusCode(500);
      response.appendHeader("Content-Type", "application/json");
      response.setBody({ message: "internal server error" });
      callback(null, response);
    } else {
      callback(null, {
        result: body.result,
        error: body.error || "",
      });
    }
  });
};

request 包已被弃用一段时间,因此您可能需要考虑将其更新为仍在维护的内容。

但最重要的是学习how Twilio Functions work by reading the documentation

【讨论】:

  • 谢谢!那行得通。尽管我过去确实使用过函数,并且我了解callback() 函数的重要性,但第 3 方 API 调用有点让我失望。您建议哪些库最适合在 Twilio 函数中发出发布请求?
  • 我喜欢使用gotnode-fetch 以及axios 之类的其他工具(这也是Twilio 包下的HTTP 客户端)。
猜你喜欢
  • 1970-01-01
  • 2019-02-20
  • 2013-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-15
  • 1970-01-01
相关资源
最近更新 更多