How to implement json object in cloud functions for Firebase?

I want to check the apple on the application purchase receipt by sending the original receipt to the App Store server for verification and receive a confirmed receipt using the functions of the Firebase cloud.

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.');
            }
        });

This is a logical code. How can I enter the JSON receipt object (which is in the Firebase database) and how to integrate this code into the http function? I am using the npm library for query.

+4
source share
1 answer

, , HTTPS. Firebase Admin SDK. , . , . :

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
+3

Source: https://habr.com/ru/post/1686315/


All Articles