如何使用Mollie Api和firebase云功能

时间:2018-03-25 17:58:05

标签: firebase google-cloud-functions mollie

我可能需要一些帮助来设置来自mollie的node.js api和firebase云功能。我试图使用部分云功能设置paypal指南,但还没有让它工作。我是云功能和node.js的新手,所以我很难完成它。我使用硬编码支付属性进行测试。我正在使用大火订阅能够向非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

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

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

1 个答案:

答案 0 :(得分: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();
});
});