0

我可能需要一些帮助来设置来自 mollie 的 node.js api 以及 firebase 云功能。我尝试使用部分 cloudfunction 设置贝宝指南,但还没有让它工作。我是云功能和 node.js 的新手,所以我很难完成它。我正在使用硬编码的支付属性进行测试。我正在使用 blaze 订阅能够向非 Google 服务发出请求到目前为止我拥有的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
Mollie = require("mollie-api-node");
mollie = new Mollie.API.Client;
mollie.setApiKey("test_GhQyK7Gkkkkkk**********");
querystring = require("querystring");
fs = require("fs");

exports.pay = functions.https.onRequest((req, res) => {

    console.log('1. response', res)
mollie.payments.create({
    amount:      10.00,
    method: Mollie.API.Object.Method.IDEAL,
    description: "My first iDEAL payment",
    redirectUrl: "https://dummyse-afgerond",
    webhookUrl:  "https://us-c90e9d.cloudfunctions.net/process",
    testmode: true
}, (payment)=> {
    if (payment.error) {
        console.error('errrr' , payment.error);
        return response.end();
      }
    console.log('3. payment.getPaymentUrl()', payment.getPaymentUrl());
    res.redirect(302, payment.getPaymentUrl());
});

});

exports.process = functions.https.onRequest((req, res) => {

let _this = this;
this.body = "";
req.on("data", (data)=> {
    console.log('_this.body += data', _this.body += data)
  return _this.body += data;
});
req.on("end", ()=> {
    console.log('hier dan?')
  let mollie, _ref;
  _this.body = querystring.parse(_this.body);
  if (!((_ref = _this.body) !== null ? _ref.id : void 0)) {
    console.log('res.end()', res.end())
    return res.end();
  }
})

    mollie.payments.get(
        _this.body.id
    , (payment) => {

        if (payment.error) {
            console.error('3a. err', payment.error);
            return response.end();
          }

        console.log('4a. payment', payment);
        console.log('5a. payment.isPaid()', payment.isPaid());


        if (payment.isPaid()) {
          /*
            At this point you'd probably want to start the process of delivering the
            product to the customer.
          */
            console.log('6a. payment is payed!!!!!!!!!!')
        } else if (!payment.isOpen()) {
          /*
            The payment isn't paid and isn't open anymore. We can assume it was
            aborted.
          */
         console.log('6a. payment is aborted!!!!!!!!!!')
        }
        res.end();
    });
});

这是 mollies api 指南: https ://github.com/mollie/mollie-api-node

这是贝宝云功能指南: https ://github.com/firebase/functions-samples/tree/master/paypal

更新:我更新了代码。我现在得到的错误是付款变量的所有属性都未在 procces 函数(webhook)中定义。并且 payment.isPaid() 函数说假,而它应该说真。

4

1 回答 1

2

当我第一次尝试让 mollie 在 firebase 云函数中工作时,我也做了同样的事情,这个:

let _this = this;
this.body = "";
req.on("data", (data)=> {
    console.log('_this.body += data', _this.body += data)
  return _this.body += data;
});
req.on("end", ()=> {
    console.log('hier dan?')
  let mollie, _ref;
  _this.body = querystring.parse(_this.body);
  if (!((_ref = _this.body) !== null ? _ref.id : void 0)) {
    console.log('res.end()', res.end())
    return res.end();
  }
})

没有必要。

我的 webhook 端点要简单得多,直接使用函数提供的请求和响应:

exports.paymentsWebhook = functions.https.onRequest((request, response) => {
// ...
console.log("request.body: ", request.body);
console.log("request.query: ", request.query);
mollie.payments.get(request.body.id, function (payment) {
    console.log("payment", payment);
    if (payment.error) {
        console.error('payment error: ', payment.error);
        response.end();
    }
    //checking/processing the payment goes here...
    response.end();
});
});
于 2018-03-27T09:31:57.430 回答