Google Cloud Function中仅接收部分Apple订阅通知 [英] Only Receiving Part of Apple Subscription Notification in Google Cloud Function

查看:140
本文介绍了Google Cloud Function中仅接收部分Apple订阅通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置Google Cloud Function(GCF)来处理来自Apple的订阅通知.我熟悉GCF,但对编写我自己的REST API和处理Apple随通知发送的数据的Nodejs方法不是很熟悉.我收到了Apple通知,但只有一个大块".这是我的代码(使用express和body-parser框架).我将整个功能放在这里是为了帮助人们,因为绝对没有关于如何在Web上可以找到的任何地方使用GCF进行订阅通知的提示(请注意,此代码正在开发中,并且对Node.js还是陌生的):

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

这是我得到的输出(这只是Apple所说的发送通知的一部分).我没有得到notification_type或Apple文档说我应该收到的JSON文件的其他任何部分:

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>>  

我从没看到console.log中的任何块.

I never see the console.log for any chunks.

我该怎么做才能确保收到所有块"并将它们放到Apple发送给我的完整JSON文件中,以便我可以使用它?

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?

推荐答案

我解决了.太简单了.我只需要使用req.body而不是req.这是所有尝试使用Google Cloud Functions来处理从Apple订阅的服务器到服务器通知的代码.

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)

有关如何处理Apple发送的类型的信息,请参见上面的代码...

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

这篇关于Google Cloud Function中仅接收部分Apple订阅通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆