Skip to content

Commit

Permalink
chore(auth): WIP - add tests for Apple IAP modules
Browse files Browse the repository at this point in the history
* add AppleIAP module test
  • Loading branch information
biancadanforth committed Apr 14, 2022
1 parent bb52af3 commit e0a25f0
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

export { AppleIAP } from './apple-iap';
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import {
decodeRenewalInfo,
decodeTransaction,
} from 'app-store-server-api';
import { TransactionType } from 'app-store-server-api/dist/types/Models';
import { Logger } from 'mozlog';
// TODO: Why is this import throwing an error when this module is loaded?
// (e.g. in a local test: yarn test test/local/payments/iap/apple-iap.js)
// import { TransactionType } from 'app-store-server-api/dist/types/Models';
import { TransactionType } from './types';
import Container from 'typedi';

import { AuthLogger } from '../../../types';
import {
APPLE_APP_STORE_FORM_OF_PAYMENT,
mergePurchaseWithFirestorePurchaseRecord,
Expand All @@ -25,7 +28,7 @@ import { PurchaseQueryError, PurchaseUpdateError } from './types';
* https://developer.apple.com/documentation/appstoreserverapi
*/
export class PurchaseManager {
private log: Logger;
private log: AuthLogger;

/*
* This class is intended to be initialized by the library.
Expand All @@ -37,7 +40,7 @@ export class PurchaseManager {
[key: string]: AppStoreServerAPI;
}
) {
this.log = Container.get(Logger);
this.log = Container.get(AuthLogger);
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
StatusResponse,
SubscriptionStatus,
TransactionType,
} from 'app-store-server-api/dist/types/Models';
} from './types';
// TODO: Why is this import throwing an error when this module is loaded?
// (e.g. in a local test: yarn test test/local/payments/iap/apple-iap.js)
// } from 'app-store-server-api/dist/types/Models';

const FIRESTORE_OBJECT_INTERNAL_KEYS = ['formOfPayment'];
export const APPLE_APP_STORE_FORM_OF_PAYMENT = 'APPLE_APP_STORE';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
/**
* MIT License
*
* Copyright (c) 2021 August Heegaard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

// TODO: Below is copied over from app-store-server-api/dist/types/Models
// as an module load error occurs if imported from that path e.g. in
// subscription-purchase.ts and running a local test:
// yarn test test/local/payments/iap/apple-iap.js.
export declare enum Environment {
Production = 'Production',
Sandbox = 'Sandbox',
}
export interface HistoryResponse {
appAppleId: string;
bundleId: string;
environment: Environment;
hasMore: boolean;
revision: string;
signedTransactions: JWSTransaction[];
}
export declare type JWSTransaction = string;
export interface JWSDecodedHeader {
alg: string;
kid: string;
x5c: string[];
}
export interface JWSTransactionDecodedPayload {
appAccountToken?: string;
bundleId: string;
expiresDate?: number;
inAppOwnershipType: OwnershipType;
isUpgraded?: boolean;
offerIdentifier?: string;
offerType?: OfferType;
originalPurchaseDate: number;
originalTransactionId: string;
productId: string;
purchaseDate: number;
quantity: number;
revocationDate?: number;
revocationReason?: number;
signedDate: number;
subscriptionGroupIdentifier?: string;
transactionId: string;
type: TransactionType;
webOrderLineItemId: string;
}
export declare enum OwnershipType {
Purchased = 'PURCHASED',
FamilyShared = 'FAMILY_SHARED',
}
export declare enum TransactionType {
AutoRenewableSubscription = 'Auto-Renewable Subscription',
NonConsumable = 'Non-Consumable',
Consumable = 'Consumable',
NonRenewingSubscription = 'Non-Renewing Subscription',
}
export interface StatusResponse {
data: SubscriptionGroupIdentifierItem[];
environment: Environment;
appAppleId: string;
bundleId: string;
}
export interface SubscriptionGroupIdentifierItem {
subscriptionGroupIdentifier: string;
lastTransactions: LastTransactionsItem[];
}
export interface LastTransactionsItem {
originalTransactionId: string;
status: SubscriptionStatus;
signedRenewalInfo: JWSRenewalInfo;
signedTransactionInfo: JWSTransaction;
}
export declare type JWSRenewalInfo = string;
export declare enum SubscriptionStatus {
Active = 1,
Expired = 2,
InBillingRetry = 3,
InBillingGracePeriod = 4,
Revoked = 5,
}
export interface JWSRenewalInfoDecodedPayload {
autoRenewProductId: string;
autoRenewStatus: AutoRenewStatus;
expirationIntent?: ExpirationIntent;
gracePeriodExpiresDate?: number;
isInBillingRetryPeriod?: boolean;
offerIdentifier?: string;
offerType?: OfferType;
originalTransactionId: string;
priceIncreaseStatus?: PriceIncreaseStatus;
productId: string;
signedDate: number;
}
export declare enum AutoRenewStatus {
Off = 0,
On = 1,
}
export declare enum ExpirationIntent {
Canceled = 1,
BillingError = 2,
RejectedPriceIncrease = 3,
ProductUnavailable = 4,
}
export declare enum OfferType {
Introductory = 1,
Promotional = 2,
SubscriptionOfferCode = 3,
}
export declare enum PriceIncreaseStatus {
NoResponse = 0,
Consented = 1,
}
export interface OrderLookupResponse {
orderLookupStatus: OrderLookupStatus;
signedTransactions: JWSTransaction[];
}
export declare enum OrderLookupStatus {
Valid = 0,
Invalid = 1,
}
export interface DecodedNotificationPayload {
notificationType: NotificationType;
subtype?: NotificationSubtype;
notificationUUID: string;
version: string;
data: NotificationData;
}
export interface NotificationData {
appAppleId: string;
bundleId: string;
bundleVersion: number;
environment: Environment;
signedRenewalInfo: JWSRenewalInfo;
signedTransactionInfo: JWSTransaction;
}
export declare enum NotificationType {
ConsumptionRequest = 'CONSUMPTION_REQUEST',
DidChangeRenewalPref = 'DID_CHANGE_RENEWAL_PREF',
DidChangeRenewalStatus = 'DID_CHANGE_RENEWAL_STATUS',
DidFailToRenew = 'DID_FAIL_TO_RENEW',
DidRenew = 'DID_RENEW',
Expired = 'EXPIRED',
GracePeriodExpired = 'GRACE_PERIOD_EXPIRED',
OfferRedeemed = 'OFFER_REDEEMED',
PriceIncrease = 'PRICE_INCREASE',
Refund = 'REFUND',
RefundDeclined = 'REFUND_DECLINED',
RenewalExtended = 'RENEWAL_EXTENDED',
Revoke = 'REVOKE',
Subscribed = 'SUBSCRIBED',
}
export declare enum NotificationSubtype {
InitialBuy = 'INITIAL_BUY',
Resubscribe = 'RESUBSCRIBE',
Downgrade = 'DOWNGRADE',
Upgrade = 'UPGRADE',
AutoRenewEnabled = 'AUTO_RENEW_ENABLED',
AutoRenewDisabled = 'AUTO_RENEW_DISABLED',
Voluntary = 'VOLUNTARY',
BillingRetry = 'BILLING_RETRY',
PriceIncrease = 'PRICE_INCREASE',
GracePeriod = 'GRACE_PERIOD',
BillingRecovery = 'BILLING_RECOVERY',
Pending = 'PENDING',
Accepted = 'ACCEPTED',
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
export { PurchaseQueryError, PurchaseUpdateError } from './errors';
export {
AutoRenewStatus,
JWSRenewalInfoDecodedPayload,
JWSTransactionDecodedPayload,
LastTransactionsItem,
StatusResponse,
SubscriptionStatus,
TransactionType,
} from './api-client';
Loading

0 comments on commit e0a25f0

Please sign in to comment.