【问题标题】:Only Receiving Part of Apple Subscription Notification in Google Cloud Function谷歌云功能只接收部分苹果订阅通知
【发布时间】:2020-05-01 16:42:27
【问题描述】:

我正在尝试设置 Google Cloud Function (GCF) 来处理来自 Apple 的订阅通知。我熟悉 GCF,但不太熟悉编写自己的 REST API 和处理 Apple 通过通知发送的数据的 Nodejs 方法。我收到了 Apple 通知,但只是其中的一个“块”。这是我的代码(使用 express 和 body-parser 框架)。我把我的整个功能都放在这里是为了帮助人们,因为我在网上可以找到的任何地方都没有关于如何使用 GCF 进行订阅通知的内容(请注意,这段代码正在进行中,我是 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 文件的任何其他部分:

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

我从来没有看到任何块的 console.log。

我可以做些什么来确保我收到所有“块”并将它们组合到 Apple 发送给我的完整 JSON 文件中,以便我可以使用它?

【问题讨论】:

  • 请编辑问题以显示整个功能代码。现在,我看到的只是一个函数片段,完全不清楚appbodyParser 是什么。我们应该有足够的信息来重现您所看到的行为。
  • @Doug Stevenson 。我添加了代码来展示框架 express 和 bodyParser 以及 app 如何成为 express 对象。
  • 为什么要声明一个新的 express 应用实例?这在这里似乎完全没有必要。 HTTP 触发器已经为您提供了请求和响应。该应用没有做任何在运行时产生影响的事情。
  • 简短的回答是我不知道如何在 Nodejs 中编写 API。这是我尝试使用从互联网上收集到的其他信息来编写一个获取 Apple 数据并将其转换为可用于检查状态等的格式的尝试。我愿意接受任何和所有建议。

标签: ios node.js firebase google-cloud-functions


【解决方案1】:

我解决了。就这么简单。我只需要使用 req.body 而不是 req。这是任何尝试使用 Google Cloud Functions 处理来自 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 发送的类型...

【讨论】:

    猜你喜欢
    • 2023-01-01
    • 1970-01-01
    • 2021-10-31
    • 2020-02-29
    • 2021-12-03
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多