如何在 Cloud Functions for Firebase 中实现 json 对象?

Jac*_*her 6 javascript node.js firebase google-cloud-functions

我想通过使用 Firebase 云功能将原始收据发送到应用商店服务器进行验证并获取经过验证的收据来验证应用程序购买收据中的苹果。

var jsonObject = {
            'receipt-data': receiptData,
            password: functions.config().apple.iappassword
        };
        var jsonData = JSON.stringify(jsonObject);
        var firebaseRef = '/' + fbRefHelper.getUserPaymentInfo(currentUser);

        let url = "https://sandbox.itunes.apple.com/verifyReceipt";//or production  
        request.post({
            headers: { 'content-type': 'application/x-www-form-urlencoded' },
            url: url,
            body: jsonData
        }, function (error, response, body) {
            if (error) {

            } else {
                var jsonResponse = JSON.parse(body);
                if (jsonResponse.status === 0) {
                    console.log('Recipt Valid!');

                } else {

                    console.log('Recipt Invalid!.');

                }
                if (jsonResponse.status === 0 && jsonResponse.environment !== 'Sandbox') {
                    console.log('Response is in Production!');
                }
                console.log('Done.');
            }
        });
Run Code Online (Sandbox Code Playgroud)

这是逻辑代码。如何输入收据 JSON 对象(位于 Firebase 数据库中)以及如何将此代码集成到 http 函数中?我正在使用“请求”npm 库。

Dou*_*son 3

根据您的评论,听起来好像您不知道如何从 HTTPS 类型函数中查询数据库。为此,您可以使用Firebase Admin SDK 。很多关于如何执行此操作的示例。特别是,这个大写快速入门示例说明了这一点。你这样开始:

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
Run Code Online (Sandbox Code Playgroud)