gpt4 book ai didi

ios - Google Cloud 功能中仅接收部分 Apple 订阅通知

转载 作者:行者123 更新时间:2023-11-29 05:10:22 25 4
gpt4 key购买 nike

我正在尝试设置 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);
});
});

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

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

我从未在 console.log 中看到任何 block 。

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

最佳答案

我解决了。就是这么简单。我只需使用 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 发送的类型...

关于ios - Google Cloud 功能中仅接收部分 Apple 订阅通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59742537/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com