@mathrunet/masamune
Version:
Manages packages for the server portion (NodeJS) of the Masamune framework.
306 lines • 19 kB
JavaScript
;
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 verifier = __importStar(require("../lib/functions/verify_android"));
const utils = __importStar(require("../lib/utils"));
const firebase_loader_1 = require("../lib/src/firebase_loader");
/**
* This is a webhook endpoint for Android. you can create a `purchasing` topic in GCP's pub/sub and set the principal to "google-play-developer-notifications@system.gserviceaccount.com" to receive notifications.
*
* Android用のWebhookのエンドポイントです。GCPのpub/subに`purchasing`のトピックを作成しプリンシパルに「google-play-developer-notifications@system.gserviceaccount.com」を設定することで通知を受け取ることができるようになります。
*
* @param process.env.PURCHASE_ANDROID_SERVICEACCOUNT_EMAIL
* The email address of your Google service account.
* Create an OAuth consent screen from the URL below.
* https://console.cloud.google.com/apis/credentials/consent
* It is then created from the service account.
* https://console.cloud.google.com/iam-admin/serviceaccounts
*
* Googleのサービスアカウントのメールアドレス。
* 下記のURLからOAuthの同意画面を作成します。
* https://console.cloud.google.com/apis/credentials/consent
* その後、サービスアカウントから作成します。
* https://console.cloud.google.com/iam-admin/serviceaccounts
*
* @param process.env.PURCHASE_ANDROID_SERVICEACCOUNT_PRIVATE_KEY
* A private key for your Google service account.
* Create an OAuth consent screen from the URL below.
* https://console.cloud.google.com/apis/credentials/consent
* It is then created from the service account.
* https://console.cloud.google.com/iam-admin/serviceaccounts
* After creating a service account, create a key in Json format from the Key tab.
* The private key is described there.
*
* Googleのサービスアカウントのプライベートキー。
* 下記のURLからOAuthの同意画面を作成します。
* https://console.cloud.google.com/apis/credentials/consent
* その後、サービスアカウントから作成します。
* https://console.cloud.google.com/iam-admin/serviceaccounts
* サービスアカウント作成後、キーのタブからJson形式でキーを作成します。
* プライベートキーはそこに記述されています。
*
* @param process.env.PURCHASE_SUBSCRIPTIONPATH
* Describes the path to the collection of subscriptions.
*
* サブスクリプションのコレクションのパスを記述します。
*/
module.exports = (regions, options, data) => {
var _a, _b, _c;
return functions.pubsub.onMessagePublished({
topic: (_a = options.topic) !== null && _a !== void 0 ? _a : "purchasing",
region: (_b = options.region) !== null && _b !== void 0 ? _b : regions[0],
timeoutSeconds: options.timeoutSeconds,
memory: options.memory,
minInstances: options.minInstances,
concurrency: options.concurrency,
maxInstances: options.maxInstances,
serviceAccount: (_c = options.serviceAccount) !== null && _c !== void 0 ? _c : undefined,
}, (message) => __awaiter(void 0, void 0, void 0, function* () {
var _a;
try {
const messageBody = message.data.message.data ?
JSON.parse(Buffer.from(message.data.message.data, "base64").toString()) :
null;
if (messageBody) {
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;
const androidServiceAccountEmail = process.env.PURCHASE_ANDROID_SERVICEACCOUNT_EMAIL;
const androidServiceAccountPrivateKey = process.env.PURCHASE_ANDROID_SERVICEACCOUNT_PRIVATE_KEY;
if (!androidServiceAccountEmail || !androidServiceAccountPrivateKey || !targetPath) {
throw new Error("The data is invalid.");
}
const { subscriptionNotification, packageName, } = messageBody;
if (subscriptionNotification) {
const { notificationType, purchaseToken, subscriptionId, } = subscriptionNotification;
if (!purchaseToken || !packageName || !subscriptionId) {
throw new Error("The data is invalid.");
}
const res = yield verifier.verifyAndroid({
type: "subscriptions",
serviceAccountEmail: androidServiceAccountEmail,
serviceAccountPrivateKey: androidServiceAccountPrivateKey,
packageName: packageName,
productId: subscriptionId,
purchaseToken: purchaseToken,
});
const search = yield firestoreInstance.collection(targetPath).where("token", "==", purchaseToken).get();
if (search.empty) {
console.error("The purchased data is not found.");
return;
}
const doc = search.docs[0];
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) {
console.error("The purchased data is not found.");
return;
}
const user = data["userId"];
console.log(`notificationType: ${notificationType}`);
switch (notificationType) {
case SubscriptionNotificationTypes.SUBSCRIPTION_RECOVERED:
case SubscriptionNotificationTypes.SUBSCRIPTION_RESTARTED:
case SubscriptionNotificationTypes.SUBSCRIPTION_RENEWED:
case SubscriptionNotificationTypes.SUBSCRIPTION_IN_GRACE_PERIOD: {
for (const key in res) {
if (!data[key]) {
continue;
}
data[key] = utils.parse(res[key]);
}
data["expired"] = false;
data["paused"] = false;
data["expiredTime"] = parseInt(res["expiryTimeMillis"]);
data["orderId"] = res["orderId"];
data["@time"] = new Date();
yield firestoreInstance.doc(path).set(data);
console.log(`Updated subscription: ${data["productId"]}:${user}`);
break;
}
case SubscriptionNotificationTypes.SUBSCRIPTION_DEFERRED:
case SubscriptionNotificationTypes.SUBSCRIPTION_PRICE_CHANGE_CONFIRMED:
case SubscriptionNotificationTypes.SUBSCRIPTION_PAUSE_SCHEDULE_CHANGED: {
for (const key in res) {
if (!data[key]) {
continue;
}
data[key] = utils.parse(res[key]);
}
data["expiredTime"] = parseInt(res["expiryTimeMillis"]);
data["orderId"] = res["orderId"];
data["@time"] = new Date();
yield firestoreInstance.doc(path).set(data);
console.log(`Updated subscription: ${data["productId"]}:${user}`);
break;
}
case SubscriptionNotificationTypes.SUBSCRIPTION_CANCELED: {
for (const key in res) {
if (!data[key]) {
continue;
}
data[key] = utils.parse(res[key]);
}
const time = new Date().getTime();
const expiryTimeMillis = data["expiredTime"] = parseInt(res["expiryTimeMillis"]);
data["orderId"] = res["orderId"];
data["@time"] = new Date();
if (expiryTimeMillis <= time) {
data["expired"] = true;
data["paused"] = false;
yield firestoreInstance.doc(path).set(data);
console.log(`Expired subscription: ${data["productId"]}:${user}`);
}
else {
data["expired"] = false;
data["paused"] = false;
yield firestoreInstance.doc(path).set(data);
console.log(`Updated subscription: ${data["productId"]}:${user}`);
}
break;
}
case SubscriptionNotificationTypes.SUBSCRIPTION_REVOKED:
case SubscriptionNotificationTypes.SUBSCRIPTION_EXPIRED:
case SubscriptionNotificationTypes.SUBSCRIPTION_PAUSED:
case SubscriptionNotificationTypes.SUBSCRIPTION_ON_HOLD: {
for (const key in res) {
if (!data[key]) {
continue;
}
data[key] = utils.parse(res[key]);
}
data["expired"] = true;
if (notificationType === SubscriptionNotificationTypes.SUBSCRIPTION_PAUSED || notificationType === SubscriptionNotificationTypes.SUBSCRIPTION_ON_HOLD) {
data["paused"] = true;
yield firestoreInstance.doc(path).set(data);
console.log(`Paused subscription: ${data["productId"]}:${user}`);
}
else {
data["paused"] = false;
yield firestoreInstance.doc(path).set(data);
console.log(`Expired subscription: ${data["productId"]}:${user}`);
}
break;
}
default:
break;
}
if (res["linkedPurchaseToken"]) {
const linkedPurchaseToken = res["linkedPurchaseToken"];
const search = yield firestoreInstance.collection(targetPath).where("token", "==", linkedPurchaseToken).get();
if (search.empty) {
return;
}
const doc = search.docs[0];
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"];
data["expired"] = true;
data["paused"] = false;
data["@time"] = new Date();
yield firestoreInstance.doc(path).set(data);
console.log(`Expired subscription: ${data["productId"]}:${user}`);
}
}
}
catch (err) {
error = err;
}
}
if (error) {
console.error(error);
throw new functions.https.HttpsError("unknown", "Unknown error.");
}
}
}
catch (err) {
console.error(err);
throw new functions.https.HttpsError("unknown", "Unknown error.");
}
}));
};
/**
* Notification Type.
*
* 通知タイプ。
*/
var SubscriptionNotificationTypes;
(function (SubscriptionNotificationTypes) {
// 定期購入がアカウントの一時停止から復帰した。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_RECOVERED"] = 1] = "SUBSCRIPTION_RECOVERED";
// アクティブな定期購入が更新された。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_RENEWED"] = 2] = "SUBSCRIPTION_RENEWED";
// 定期購入が自発的または非自発的にキャンセルされた。
// 自発的なキャンセルの場合、ユーザーがキャンセルしたときに送信されます。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_CANCELED"] = 3] = "SUBSCRIPTION_CANCELED";
// 新しい定期購入が購入された。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_PURCHASED"] = 4] = "SUBSCRIPTION_PURCHASED";
// 定期購入でアカウントが一時停止された(有効な場合)。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_ON_HOLD"] = 5] = "SUBSCRIPTION_ON_HOLD";
// 定期購入が猶予期間に入った(有効な場合)。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_IN_GRACE_PERIOD"] = 6] = "SUBSCRIPTION_IN_GRACE_PERIOD";
// ユーザーが [Play] > [アカウント] > [定期購入] から
// 定期購入を再有効化した(定期購入の再開にはオプトインが必要)。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_RESTARTED"] = 7] = "SUBSCRIPTION_RESTARTED";
// 定期購入の料金変更がユーザーによって確認された。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_PRICE_CHANGE_CONFIRMED"] = 8] = "SUBSCRIPTION_PRICE_CHANGE_CONFIRMED";
// 定期購入の契約期間が延長された。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_DEFERRED"] = 9] = "SUBSCRIPTION_DEFERRED";
// 定期購入が一時停止された。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_PAUSED"] = 10] = "SUBSCRIPTION_PAUSED";
// 定期購入の一時停止スケジュールが変更された。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_PAUSE_SCHEDULE_CHANGED"] = 11] = "SUBSCRIPTION_PAUSE_SCHEDULE_CHANGED";
// 有効期限前にユーザーが定期購入を取り消した。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_REVOKED"] = 12] = "SUBSCRIPTION_REVOKED";
// 定期購入が期限切れになった。
SubscriptionNotificationTypes[SubscriptionNotificationTypes["SUBSCRIPTION_EXPIRED"] = 13] = "SUBSCRIPTION_EXPIRED";
})(SubscriptionNotificationTypes || (SubscriptionNotificationTypes = {}));
//# sourceMappingURL=purchase_webhook_android.js.map