我可能需要一些帮助设置来自Mollie的Node.js API,并具有Firebase Cloud函数。我试图使用云功能的一部分设置PayPal指南,但尚未使其正常工作。我是云功能和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(中均未定义。付款.ispaid((函数说false,虽然应该为true。
我第一次试图让莫利在firebase cloud函数中工作时也做了同样的事情,这是:
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();
});
});