|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | +import { Firestore } from '@google-cloud/firestore'; |
| 5 | +import { AppStoreServerAPI, Environment } from 'app-store-server-api'; |
| 6 | +import { Container } from 'typedi'; |
| 7 | +import { TypedCollectionReference } from 'typesafe-node-firestore'; |
| 8 | + |
| 9 | +import { AppConfig, AuthFirestore, AuthLogger } from '../../types'; |
| 10 | +// TODO: promote this to a shared dir |
| 11 | +import { IapConfig } from '../google-play/types'; |
| 12 | +import { PurchaseManager } from './purchase-manager'; |
| 13 | + |
| 14 | +export class AppleIAP { |
| 15 | + private firestore: Firestore; |
| 16 | + private log: AuthLogger; |
| 17 | + private prefix: string; |
| 18 | + private iapConfigDbRef: TypedCollectionReference<IapConfig>; |
| 19 | + |
| 20 | + public purchaseManager: PurchaseManager; |
| 21 | + constructor() { |
| 22 | + const { |
| 23 | + authFirestore, |
| 24 | + env, |
| 25 | + subscriptions: { appStore }, |
| 26 | + } = Container.get(AppConfig); |
| 27 | + this.prefix = `${authFirestore.prefix}iap-`; |
| 28 | + this.firestore = Container.get(AuthFirestore); |
| 29 | + this.iapConfigDbRef = this.firestore.collection( |
| 30 | + `${this.prefix}iap-config` |
| 31 | + ) as TypedCollectionReference<IapConfig>; |
| 32 | + this.log = Container.get(AuthLogger); |
| 33 | + |
| 34 | + // Initialize App Store Server API client per bundle ID |
| 35 | + const environment = |
| 36 | + env === 'prod' ? Environment.Production : Environment.Sandbox; |
| 37 | + const appStoreServerApiClients = {}; |
| 38 | + for (const [bundleIdWithUnderscores, credentials] of Object.entries( |
| 39 | + appStore.credentials |
| 40 | + )) { |
| 41 | + // Cannot use an actual bundleId (e.g. 'org.mozilla.ios.FirefoxVPN') as the key |
| 42 | + // due to https://github.com/mozilla/node-convict/issues/250 |
| 43 | + const bundleId = bundleIdWithUnderscores.replace('_', '.'); |
| 44 | + const { serverApiKey, serverApiKeyId, issuerId } = credentials; |
| 45 | + appStoreServerApiClients[bundleId] = new AppStoreServerAPI( |
| 46 | + serverApiKey, |
| 47 | + serverApiKeyId, |
| 48 | + issuerId, |
| 49 | + bundleId, |
| 50 | + environment |
| 51 | + ); |
| 52 | + } |
| 53 | + const purchasesDbRef = this.firestore.collection( |
| 54 | + `${this.prefix}app-store-purchases` |
| 55 | + ); |
| 56 | + this.purchaseManager = new PurchaseManager( |
| 57 | + purchasesDbRef, |
| 58 | + appStoreServerApiClients |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Fetch the Apple plans for iOS client usage. |
| 64 | + */ |
| 65 | + public async plans(appName: string) { |
| 66 | + const doc = await this.iapConfigDbRef.doc(appName).get(); |
| 67 | + if (doc.exists) { |
| 68 | + return doc.data()?.plans; |
| 69 | + } else { |
| 70 | + throw Error(`IAP Plans document does not exist for ${appName}`); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Fetch the App Store bundleId for the given appName. |
| 76 | + */ |
| 77 | + public async getBundleId(appName: string) { |
| 78 | + const doc = await this.iapConfigDbRef.doc(appName).get(); |
| 79 | + if (doc.exists) { |
| 80 | + return doc.data()?.bundleId; |
| 81 | + } else { |
| 82 | + throw Error(`IAP Plans document does not exist for ${appName}`); |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments