【问题标题】:Not able to get Apple Pay payment session无法获得 Apple Pay 付款会话
【发布时间】:2020-04-16 21:59:48
【问题描述】:

我们正在尝试在我的项目中在网络上实施 ApplePay。根据苹果文档,我从 applejs api onvalidatemerchant 函数获取验证 url,并且我将此验证 url 传递给节点 js 路由以获取苹果支付会话。这是我为获得苹果付款会话(https://developer.apple.com/documentation/apple_pay_on_the_web/apple_pay_js_api/requesting_an_apple_pay_payment_session)而遵循的文档。

以下是我为获取苹果支付会话而编写的自定义节点 js 代码。传递给该节点的验证url 即req.query.url js 路由代码为https://apple-pay-gateway-cert.apple.com/paymentservices/startSession

app.get('/getAppleSession2', function (req, res) {


  var endpointURL = req.query.url;
  var cert_path = './apple_pay.pem';
  var cert = fs.readFileSync(cert_path);
  console.log('endpointURL is ' + endpointURL);
  console.log('cert_path is ' + cert_path);
  console.log('cert is'+cert);
  const options = {
    url: endpointURL,
    method: 'post',
    cert: cert,
    key: cert,
    body: {
      merchantIdentifier: "xxxxx.xxxxx.xxxxx.xxxxx.xxxxx",
      displayName: "xxxxx.xxxxx.xxxxx.xxxxx.xxxxx",
      initiative: "web",
      initiativeContext: "xxxxx.xxxx.xxxx.xxxx.com"
    },
    json: true,
  };
  //console.log("body" + body);
  request(options, function (error, response, body) {
    console.log('body of  getAppleSession' + body);
    console.log('Response from getAppleSession' + response);
    console.error('Error object ' + error);

    res.send(body);
  });

});

但这是我对这条路线的回应

body of  getAppleSession undefined
Response from getAppleSession undefined
Error object Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

不知道这里出了什么问题,因为我按照苹果的文档这样做。我怀疑它是否与我如何将证书(商家身份证书)传递给这个 nodejs 路由有关。我通过从 Apple Development 门户下载 .cer 格式的商家身份证书来生成证书,并通过在我的 mac 中的 KeyChain 访问中导入 .cer 文件并将其导出到 .pem 中,将我从 Apple 门户下载的证书转换为 .pem 格式钥匙串访问。然后我将 .pem 文件('./apple_pay.pem')放在我的节点 js 路由的同一目录中。我如何生成证书或在我的节点 js 路由中传递它们有什么问题吗?

不知道这里出了什么问题。任何代码示例或指针都会很有帮助。

【问题讨论】:

    标签: node.js applepay applepayjs


    【解决方案1】:

    似乎这是由于证书有效性相关问题。请您确定自签名证书是否有效。

    希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      我可能会迟到,但在这里为遇到同样问题的其他人留下一个可行的解决方案:

      在开始创建用于请求Apple Pay Session 的api 之前,您需要创建payment processingmerchant identifier certificates。详细解释可以看这个link

      在商户标识符证书流程结束时,您将留下一个.cer 文件。 Double click 将此文件添加到您的 keychain 应用程序中。

      在此之后,转到您的keychain,右键单击证书,并将其导出为 PKCS #12 (.p12)。然后,您可以使用 openssl 将其转换为 .pem 文件,或者您可以直接使用 .p12(在带有请求的 nodejs 中。您需要将 agentOptions 设置为 {pfx: p12File 和密码:'***'}。

      我对 NodeJS 的工作解决方案如下:

      async validateMerchant(ctx) {
        let response = {};
        try {
          const options = {
            url: ctx.query.validationURL,
            agentOptions: {
              pfx: fs.readFileSync(
                path.resolve(__dirname, 'MerchantIDCertificate.p12')
              ),
              passphrase: '********',
            },
            method: 'post',
            body: {
              merchantIdentifier: 'merchant.***.***.*****',
              displayName: 'Your Store Name',
              initiative: 'web',
              initiativeContext: 'Verified.domain.com',
            },
            json: true,
          };
          response = await this.promisifyRequest(options);
        } catch (error) {
          logger.error(error);
        }
        return response;
      }
      
      promisifyRequest(options) {
        return new Promise((resolve, reject) => {
          request(options, (error, response, body) => {
            if (body) {
              console.log(response);
              return resolve(body);
            }
            if (error) {
              return reject(error);
            }
          });
        });
      }
      
      

      【讨论】:

        猜你喜欢
        • 2014-12-20
        • 2014-12-19
        • 2017-05-13
        • 2021-03-26
        • 2018-12-31
        • 2019-04-05
        • 2019-10-28
        • 2021-04-07
        • 2015-03-02
        相关资源
        最近更新 更多