NodeJS 加入 promise 与请求函数

NodeJS joining promise with request functions

我有以下用于三叶草付款的代码 api,虽然代码运行良好,但当我从邮递员那里调用 api 时,我没有得到回复。我知道这是因为服务中有多个请求,我试图找到一种更简洁的方法来做到这一点,但一直未能让它发挥作用。我正在尝试发回最终响应,即 postPayment() 函数中请求的响应。任何帮助将不胜感激。

我的服务代码是:

const db = require('../_helpers/db');
const crypto = require('crypto');
const request = require("request-promise");


module.exports = {
    getAll
};

var targetEnv = 'https://sandbox.dev.clover.com/v2/merchant/';
var cardNumber = '6011361000006668';

async function getAll(data) {
  var url = targetEnv + data.merchant_id + '/pay/key';

  var options = {
    url: url,
    method: 'GET',
    headers: {
      Authorization: 'Bearer ' + data.api_token
    }
  };

  request(options, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      console.log('getAll ' +data);
      processEncryption(JSON.parse(body), JSON.stringify(data));
    }
  });
}

// Process the encryption information received by the pay endpoint.
function processEncryption(jsonResponse, data) {
  console.log('processEncryption ' +data);
  var prefix = jsonResponse['prefix'];
  var pem = jsonResponse['pem'];

  // create a cipher from the RSA key and use it to encrypt the card number, prepended with the prefix from GET /v2/merchant/{mId}/pay/key
  var encrypted = crypto.publicEncrypt(pem, Buffer(prefix + cardNumber));

  // Base64 encode the resulting encrypted data into a string to Clover as the 'cardEncrypted' property.
  var cardEncrypted = new Buffer(encrypted).toString('base64');

  return postPayment(cardEncrypted, data);
}

// Post the payment to the pay endpoint with the encrypted card information.
async function postPayment(cardEncrypted, body) {
  // POST to /v2/merchant/{mId}/pay
  console.log('mid ' +JSON.parse(body));
  var posturl = targetEnv + '9ZQTAJSQKZ391/pay';
  var postData = {
    "orderId": "4N3RBF33EBEGT",
    "currency": "usd",
    "amount": 2,
    "tipAmount": 0,
    "taxAmount": 0,
    "expMonth": 12,
    "cvv": 123,
    "expYear": 2018,
    "cardEncrypted": cardEncrypted,
    "last4": 6668,
    "first6": 601136,
    "streetAddress": "123 Fake street",
    "zip": "94080",
    "merchant_id": "9ZQTAJSQKZ391",
    "order_id": "4N3RBF33EBEGT",
    "api_token": "4792a281-38a9-868d-b33d-e36ecbad66f5"
  }

  var options = {
    url: posturl,
    method: 'POST',
    headers: {
      'Authorization': 'Bearer ' + "4792a281-38a9-868d-b33d-e36ecbad66f5",
    },
    json: postData
  };

   request(options, (error, response, body) => {
    if (!error && response.statusCode === 200) {
      //console.log(response);  
      return response;   <---- this response is what i need to show in postman
    }
  });
   console.log(response);
}

我的控制器是:

const express = require('express');
const router = express.Router();
const tableOrderService = require('./cloverPayment.service');

// routes
router.post('/getAll', getAll);


module.exports = router;


function getAll(req, res, next) {
    tableOrderService.getAll(req.body)
        .then(users => res.json(users))
        .catch(err => next(err));
}

您的异步函数 getAll()postPayment() 未正确返回异步值(通过回调或承诺)。

我建议将所有内容都转换为 promise,并从 getAll()postPayment() 返回一个 promise。而且,由于转换为承诺,我将删除已弃用的 request-promise 库以支持 got() 库。然后,您可以调用 getAll(),获得承诺并使用已解析的值或拒绝从实际请求处理程序发送您的响应:

const db = require('../_helpers/db');
const crypto = require('crypto');
const got = require('got');

module.exports = {
    getAll
};

var targetEnv = 'https://sandbox.dev.clover.com/v2/merchant/';
var cardNumber = '6011361000006668';

async function getAll(data) {
    console.log('getAll ', data);
    const url = targetEnv + data.merchant_id + '/pay/key';

    const options = {
        url: url,
        method: 'GET',
        headers: {
            Authorization: 'Bearer ' + data.api_token
        }
    };
    const response = await got(options);
    return processEncryption(response, data);
}

// Process the encryption information received by the pay endpoint.
function processEncryption(jsonResponse, data) {
    console.log('processEncryption ' + data);
    const prefix = jsonResponse.prefix;
    const pem = jsonResponse.pem;

    // create a cipher from the RSA key and use it to encrypt the card number, prepended with the prefix from GET /v2/merchant/{mId}/pay/key
    const encrypted = crypto.publicEncrypt(pem, Buffer(prefix + cardNumber));

    // Base64 encode the resulting encrypted data into a string to Clover as the 'cardEncrypted' property.
    const cardEncrypted = Buffer.from(encrypted).toString('base64');
    return postPayment(cardEncrypted, data);
}

// Post the payment to the pay endpoint with the encrypted card information.
function postPayment(cardEncrypted, body) {
    // POST to /v2/merchant/{mId}/pay
    console.log('mid ', body);
    const posturl = targetEnv + '9ZQTAJSQKZ391/pay';
    const postData = {
        "orderId": "4N3RBF33EBEGT",
        "currency": "usd",
        "amount": 2,
        "tipAmount": 0,
        "taxAmount": 0,
        "expMonth": 12,
        "cvv": 123,
        "expYear": 2018,
        "cardEncrypted": cardEncrypted,
        "last4": 6668,
        "first6": 601136,
        "streetAddress": "123 Fake street",
        "zip": "94080",
        "merchant_id": "9ZQTAJSQKZ391",
        "order_id": "4N3RBF33EBEGT",
        "api_token": "4792a281-38a9-868d-b33d-e36ecbad66f5"
    }

    const options = {
        url: posturl,
        method: 'POST',
        headers: {
            'Authorization': 'Bearer ' + "4792a281-38a9-868d-b33d-e36ecbad66f5",
        },
        json: postData
    };

    return got(options);
}

然后,你的控制器:

const express = require('express');
const router = express.Router();
const tableOrderService = require('./cloverPayment.service');

// routes
router.post('/getAll', (req, res) => {
    tableOrderService.getAll(req.body)
        .then(users => res.json(users))
        .catch(err => next(err));
});

module.exports = router;