简体   繁体   English

带有 .p12 和 pem 证书的 Node.js POST 请求输出“错误:读取 ECONNRESET”

[英]Node.js POST request with .p12 and pem certificates outputs "Error: read ECONNRESET"

I'm setting up a function using node on Firebase to test a Swish integration.我正在使用 Firebase 上的节点设置一个函数来测试 Swish 集成。 Swish is a mobile application for money transfer between wallets (bank accounts). Swish 是一款用于在钱包(银行账户)之间转账的移动应用程序。 Currently Im testing their Merchant Swish Simulator test environment that is available for merchants to test their integration with the Swish Commerce API.目前我正在测试他们的 Merchant Swish Simulator 测试环境,可供商家测试他们与 Swish Commerce API 的集成。

There is a available manual on their developer website: https://developer.getswish.se/merchants/他们的开发者网站上有可用的手册: https : //developer.getswish.se/merchants/

Example request that can be done through a unix terminal (to test one of their endpoints) which works perfectly for me (getting 201 respond) is:可以通过 unix 终端(以测试其端点之一)完成的示例请求对我来说非常有效(获得 201 响应)是:

curl -s -S -i --cert ./Swish_Merchant_TestCertificate_1231181189.p12:swish --cert-type p12 --cacert ./Swish_TLS_RootCA.pem --tlsv1.1 --header "Content-Type: application/json" https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests --data '{ "payeePaymentReference" : "0123456789", "callbackUrl" : "https://myfakehost.se/swishcallback.cfm", "payerAlias" : "46766268608", "payeeAlias" : "1231181189", "amount" : "100", "currency" : "SEK", "message" : "Kingston USB Flash Drive 8 GB" }'

The issue is im not sure of how to make a similar request with including the certs using firebase.问题是我不确定如何通过包含使用 firebase 的证书来提出类似的请求。

const functions = require('firebase-functions');
const express = require('express');
const app = express();
let fs = require('fs');
let request = require('request');

app.post('/request-payment', async (req, res) => {
    const url = 'https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests';

    let data = {
        payeePaymentReference : '0123456789',
        message: 'Kingston USB Flash Drive 8 GB',
        callbackUrl: 'https://myfakehost.se/swishcallback.cfm',
        amount: '100',
        currency: 'SEK',
        payeeAlias: '1231181189',
        payerAlias: '46766268608'
    };

    let options = {
        url: url,
        headers: {
            "content-type": "application/json",
        },
        agentOptions: {
            pfx: fs.readFileSync('certificates/Swish_Merchant_TestCertificate_1231181189.p12'),
            passphrase: 'swish',
        },
        ca: fs.readFileSync('certificates/Swish_TLS_RootCA.pem'),
        body: data,
        json: true
    };

    return await request.post(options, async (error, response, body) => {
        if (!error && response.statusCode === 201) {
            return res.status(200).json({
                url: response.body.url,
            })
        } else {
            console.log('error', error);
            return res.status(404).json({
                message: 'Error: ' + error,
            })
        }
    });
});

exports.swish = functions.https.onRequest(app);

I've been trying to pass the .p12 and .pem files when making a request to the endpoint above and im sending the exact same data as above.在向上面的端点发出请求时,我一直在尝试传递 .p12 和 .pem 文件,并且我正在发送与上面完全相同的数据。 I still get this error:我仍然收到此错误:

error { Error: read ECONNRESET at TLSWrap.onread (net.js:622:25) errno: 'ECONNRESET', code: 'ECONNRESET', syscall: 'read' }错误 { 错误:在 TLSWrap.onread (net.js:622:25) 处读取 ECONNRESET 错误号:'ECONNRESET',代码:'ECONNRESET',系统调用:'read'}

Can anyone see anything wrong I do?谁能看出我做错了什么?

I've also tried to set a "binary" option to the .pem file.我还尝试为 .pem 文件设置“二进制”选项。 I do then get a crash from node saying that the header is to long....然后我确实从节点那里崩溃了,说标题太长了....

Here is the way I populate options for request to make it work:这是我为request填充选项以使其工作的方式:

const options = {
            url: "https://mss.cpc.getswish.net/swish-cpcapi/api/v1/paymentrequests",
            json: true,
            cert: fs.readFileSync("Swish_Merchant_TestCertificate_1231181189.pem", "ascii"),
            passphrase: "swish",
            body: data,
            ca: fs.readFileSync("Swish_TLS_RootCA.pem", "ascii"),
            key: fs.readFileSync("Swish_Merchant_TestCertificate_1231181189.key", "ascii")
        };

Note the important differences:注意重要的区别:

  • "ascii" encoding to read the files “ascii”编码读取文件
  • usage of the "pem" certificate instead of "p12".使用“pem”证书而不是“p12”。

Anyone? 任何人? Im still stucked with this :/ 我仍然坚持这个:/

Note that Swish has now released API v2.请注意,Swish 现在已经发布了 API v2。 I got the payment initiation endpoint working like this:我让付款启动端点像这样工作:

import { readFileSync } from "fs"
import { Agent, AgentOptions } from "https"
import fetch from "node-fetch"

const url = `https://mss.cpc.getswish.net/swish-cpcapi/api/v2/paymentrequests/${randomUUID}`

const data = {
      callbackUrl: "https://myfakehost.se/swishcallback.cfm",
      amount: "100.00",
      currency: "SEK",
      payeeAlias: "1234679304",
      payeePaymentReference: "foo"
    }

const cert = {
      cert: readFileSync(`./Swish_Merchant_TestCertificate_1234679304.pem`, "utf-8"),
      key: readFileSync(`./Swish_Merchant_TestCertificate_1234679304.key`, "utf-8"),
      passphrase: "swish",
    }    

const options = {
      agent: new Agent({ ...cert, minVersion: "TLSv1.2", maxVersion: "TLSv1.2" }),
      method: "PUT",
      body: JSON.stringify(data),
      headers: { "Content-Type": "application/json" }
    }

fetch(url, options)
   .then(handleToken)
   .catch(handleError)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM