首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Mollie Api与火基云函数结合使用

如何将Mollie Api与火基云函数结合使用
EN

Stack Overflow用户
提问于 2018-03-25 17:58:04
回答 1查看 922关注 0票数 0

我可能需要一些帮助,用防火墙云函数从node.js中建立mollie。我试图使用部分云功能,设置paypal指南,但没有让它发挥作用。我对云函数和node.js很陌生,所以我很难完成它。我正在使用硬编码支付属性进行测试。我正在使用blaze订阅来完成对非Google服务的请求-我到目前为止的代码:

代码语言:javascript
复制
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指南:https://github.com/mollie/mollie-api-node

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

更新:我更新了代码。我现在遇到的错误是,支付变量的所有属性都未在procces函数(web钩子)中定义。payment.isPaid()函数为false,而应该为true。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-27 09:31:57

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

代码语言:javascript
复制
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();
  }
})

没有必要。

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

代码语言:javascript
复制
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();
});
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49479053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档