6

I want to verify apple in app purchase receipt by sending the original receipt to App store server for validation and get validated receipt back, using Firebase cloud functions.

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 the logic code. How do i input the receipt JSON object (which is sitting in the Firebase database) and how do i integrate this code in an http function? I am using 'request' npm library.

3
  • What specific problem are you running into trying to create an HTTP function? Commented Sep 25, 2017 at 4:35
  • I am not sure how to fetch the receipt that i have stored for each user on the realtime firebase database. If i use database cloud function .onwrite() or .onupdate() i am able to access the receipt using db .ref('Customers/{uid}/receiptData') and use event.data to get the receipt and then pass it in to this logic. however, with http function, I am not able to find documentation to do such thing. Also, as i am new to node.js, i am struggling to understand the flow and structure of the http cloud function. Specifically, how can i implement this logic code. Commented Sep 25, 2017 at 4:44
  • 1
    You'll typically use the Firebase Admin SDK in your Cloud Functions code to access the database. See stackoverflow.com/questions/43650472/… Commented Sep 25, 2017 at 4:51

1 Answer 1

3

Based on your comment, it sounds as if you don't know how to query the database from within an HTTPS type function. You can use the Firebase Admin SDK for this. There are lots of examples of how to do this. In particular, this uppercase quickstart sample illustrates it. You start like this:

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
Sign up to request clarification or add additional context in comments.

1 Comment

referring to this: 'exports.makeUppercase = functions.database.ref('/messages/{pushId}/original') .onWrite(event => { const getSomethingPromise = admin.database().ref(/important/messages/{pushId}).once('value'); return getSomethingPromise.then(results => { const somethingSnapshot = results[0]; // Do something with the snapshot }) }) ' I am a bit confused. Doesnt .onwrite() mean access of data when the event of writing data takes place?? Am i able to query db in http func?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.