Only Receiving Part of Apple Subscription Notification in Google Cloud Function

依然范特西╮ 提交于 2020-01-25 08:07:33

问题


I am trying to set up a Google Cloud Function (GCF) to handle Subscription Notifications from Apple. I am familiar with GCF, but not very familiar with writing my own REST API and the Nodejs methods of handling the data Apple sends with the notification. I am receiving the Apple notification, but only a "chunk" of it. Here's my code (using express and body-parser frameworks). I put my whole function here to help people since there is absolutely nothing about how to use GCF for Subscription Notifications anywhere I could find on the web (note this code is very much a work in progress and I am new to Nodejs):

// Set up express object
const express = require('express');
const app = express();
const bodyParser = require('body-parser');

exports.iosNotification = functions.https.onRequest((req, res) => {
console.log("We are receiving a request from Apple.");
app.use(bodyParser.json());
let receipt = req.body.latest_receipt;

console.log(req.body);

const chunks = [];
req.on('data', chunk => {
    chunks.push(chunk);
    console.log('A chunk of data has arrived:', chunk);
});
req.on('end', () => {
    const data = Buffer.concat(chunks);
    console.log('Data: ', data);
    console.log('No more data');
});

const type = req.body.notification_type;
console.log("Notification type: ", type);
const lri = req.body.latest_receipt_info;
console.log(lri, receipt);
// Verify the receipt.
validateAppleReceipt(receipt)
        .then((appleResponse) => {
            console.log("Receipt from App Store server validated.", appleResponse);
                res.sendStatus(200);
            const oTxId = appleResponse.latest_receipt_info[0].original_transaction_id;
            // Receipt is valid and we let Apple know.  Let's process the notification.
            switch (type) {
                case 'CANCEL':
                    // User canceled the subscription (like on accidental purchase).
                    console.log("User canceled a subscription.");

                    break;
                case 'DID_CHANGE_RENEWAL_PREF':
                    console.log("The subscriber downgraded.  Effective on next renewal.  Handle.");

                    break;
                case 'DID_CHANGE_RENEWAL_STATUS':
                    console.log("The subscriber downgraded or upgraded.  Effective on next renewal.  Handle.");

                    break;
                case 'DID_FAIL_TO_RENEW':
                    console.log("Subscription has a billing issue.  Check if in billing retry period.");

                    break;
                case 'DID_RECOVER':
                    console.log("Renewal of expired subscription that failed to renew.");

                    break;
                case 'INITIAL_BUY':
                    console.log("Initial purchase.  Ignored because we already handled with another function.");
                    break;
                case 'INTERACTIVE_RENEWAL':
                    console.log("Interactive renewal.  Not sure if we'll ever see this.");
                    break;
                case 'RENEWAL':
                    console.log("Renewal after failure.  Same as DID_RECOVER.  Handle there.");
                    break;
                default:
                    console.log("Hit default.");
                    break;
            };
        })
        .catch((error) => {
            console.log("Error validating receipt from App Store server.", error);      
        });
    });

This is the output I'm getting (which is only a portion of the notification Apple says it is sending). I don't get notification_type or any of the other parts of the JSON file the Apple docs say I should be receiving:

{ latest_receipt: 'ewoJInNpZ25hdHVyZSIgPSAiQTNVM0FjaDJpbXRPMG53cEtrQW9 <<shortened for this post>>  

I never see the console.log for any chunks.

What can I do to make sure I receive all the "chunks" and put them together into the complete JSON file that Apple is sending me so I can work with it?


回答1:


I solved it. It was so simple. I just had to use req.body instead of req. Here's the code for anyone who is trying to use Google Cloud Functions to handle Server to Server Notifications for subscriptions from Apple.

exports.iosNotification = functions.https.onRequest((req, res) => {
console.log("We are receiving a request from Apple.");
let receipt = req.body.latest_receipt;

const type = req.body.notification_type;
console.log("Notification type: ", type);
const lri = req.body.latest_receipt_info;
console.log(type, lri, receipt);
// Verify the receipt.
validateAppleReceipt(receipt)

See code above for how to handle the types Apple sends...



来源:https://stackoverflow.com/questions/59742537/only-receiving-part-of-apple-subscription-notification-in-google-cloud-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!