UNPKG

@mathrunet/masamune

Version:

Manages packages for the server portion (NodeJS) of the Masamune framework.

182 lines 10.4 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const functions = __importStar(require("firebase-functions/v2")); const utils = __importStar(require("../lib/utils")); const firebase_loader_1 = require("../lib/src/firebase_loader"); /** * Webhook endpoint for IOS, which allows you to receive notifications by setting the endpoint in AppStoreConnect's [App]->[App Information]->[App Store Server Notification]. * * IOS用のWebhookのエンドポイントです。AppStoreConnectの[App]->[App情報]->[App Storeサーバ通知]にエンドポイントを設定することで通知を受け取ることができるようになります。 * * @param process.env.PURCHASE_IOS_SHAREDSECRET * SharedSecret for AppStore, obtained from [Apps]->[App Info]->[Shared Secret for App] in the AppStore. * * AppStoreのSharedSecret。AppStoreの[アプリ]->[App情報]->[App用共有シークレット]から取得します。 * * @param process.env.PURCHASE_SUBSCRIPTIONPATH * Describes the path to the collection of subscriptions. * * サブスクリプションのコレクションのパスを記述します。 */ module.exports = (regions, options, data) => { var _a, _b; return functions.https.onRequest({ region: (_a = options.region) !== null && _a !== void 0 ? _a : regions, timeoutSeconds: options.timeoutSeconds, memory: options.memory, minInstances: options.minInstances, concurrency: options.concurrency, maxInstances: options.maxInstances, serviceAccount: (_b = options.serviceAccount) !== null && _b !== void 0 ? _b : undefined, }, (req, res) => __awaiter(void 0, void 0, void 0, function* () { var _a; try { const message = req.body; const signedPayload = message.signedPayload; if (signedPayload) { const signedPayloadBody = signedPayload.replace(/-/g, "+").replace(/_/g, "/").split(".")[1]; const messageBody = JSON.parse(Buffer.from(signedPayloadBody, "base64").toString()); if (messageBody) { const { notificationType, data, } = messageBody; const { signedTransactionInfo, } = data; const signedTransactionInfoBody = signedTransactionInfo.replace(/-/g, "+").replace(/_/g, "/").split(".")[1]; const transactionInfo = JSON.parse(Buffer.from(signedTransactionInfoBody, "base64").toString()); let error = null; const firestoreDatabaseIds = (_a = options.firestoreDatabaseIds) !== null && _a !== void 0 ? _a : [""]; for (const databaseId of firestoreDatabaseIds) { try { const firestoreInstance = (0, firebase_loader_1.firestoreLoader)(databaseId); const targetPath = process.env.PURCHASE_SUBSCRIPTIONPATH; if (transactionInfo) { const transactionId = transactionInfo.originalTransactionId; const doc = yield firestoreInstance.doc(`${targetPath}/${transactionId}`).get(); const data = doc === null || doc === void 0 ? void 0 : doc.data(); const path = doc === null || doc === void 0 ? void 0 : doc.ref.path; if (!data) { throw new Error("The purchased data is not found."); } const user = data["userId"]; console.log(`notificationType: ${notificationType}`); switch (notificationType) { case "CONSUMPTION_REQUEST": case "DID_CHANGE_RENEWAL_STATUS": case "DID_FAIL_TO_RENEW": case "PRICE_INCREASE": case "REFUND_DECLINED": { for (const key in transactionInfo) { if (!data[key]) { continue; } data[key] = utils.parse(transactionInfo[key]); } data["expiredTime"] = parseInt(transactionInfo["expiresDate"]); data["productId"] = data["product_id"] = transactionInfo["productId"]; data["orderId"] = transactionInfo["transactionId"]; yield firestoreInstance.doc(path).set(data); console.log(`Updated subscription: ${data["productId"]}:${user}`); break; } case "SUBSCRIBED": case "DID_CHANGE_RENEWAL_PREF": case "DID_RENEW": case "OFFER_REDEEMED": case "RENEWAL_EXTENDED": { for (const key in transactionInfo) { if (!data[key]) { continue; } data[key] = utils.parse(transactionInfo[key]); } data["expired"] = false; data["paused"] = false; data["expiredTime"] = parseInt(transactionInfo["expiresDate"]); data["productId"] = data["product_id"] = transactionInfo["productId"]; data["orderId"] = transactionInfo["transactionId"]; yield firestoreInstance.doc(path).set(data); console.log(`Updated subscription: ${data["productId"]}:${user}`); break; } case "EXPIRED": case "REVOKE": case "GRACE_PERIOD_EXPIRED": case "REFUND": { for (const key in transactionInfo) { if (!data[key]) { continue; } data[key] = utils.parse(transactionInfo[key]); } data["expired"] = true; data["paused"] = false; data["productId"] = data["product_id"] = transactionInfo["productId"]; data["orderId"] = transactionInfo["transactionId"]; yield firestoreInstance.doc(path).set(data); console.log(`Expired subscription: ${data["productId"]}:${user}`); break; } default: break; } } } catch (err) { error = err; } } if (error) { console.error(error); throw new functions.https.HttpsError("unknown", "Unknown error."); } } } res.send({ "status": 1 }); } catch (err) { console.error(err); res.end(); } })); }; //# sourceMappingURL=purchase_webhook_ios.js.map