firebasegoogle-cloud-functionsmollie

How to use Mollie Api with firebase cloud functions


I could need some help setting up the node.js api from mollie with firebase cloud functions. I tried to use parts of the cloudfunction setting up paypal guide but didn't get it to work yet. I'm new to cloud functions and node.js so i have a hard time getting it done. I am using hard coded payment properties for testing purposes. I am using a blaze subscribtion to be able to do requests to non-Google services The code i have so far:

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();
    });
});

this is mollies api guide: https://github.com/mollie/mollie-api-node

this is paypal cloud function guide: https://github.com/firebase/functions-samples/tree/master/paypal

UPDATE: I updated the code. The error i get now is that all properties of the payment variable are undefined in the procces function (webhook). and the payment.isPaid() function says false while it should say true.


Solution

  • I did the same when I first tried to get mollie working in firebase cloud functions, this:

    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();
      }
    })
    

    is not necessary.

    my webhook endpoint is much simpler, directly using the request & response that functions provides:

    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();
    });
    });