Skip to content

Commit

Permalink
Write V2-based lookups to parallel table
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwadeson committed Feb 20, 2024
1 parent 86c5726 commit b19dbab
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
8 changes: 6 additions & 2 deletions typescript/src/models/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export class Subscription {
@attribute()
ttl?: number;

tableName: string

constructor(
subscriptionId: string,
startTimestamp: string,
Expand All @@ -57,7 +59,8 @@ export class Subscription {
googlePayload?: any,
receipt?: string,
applePayload?: any,
ttl?:number
ttl?:number,
tableName: string = "subscriptions"
) {
this.subscriptionId = subscriptionId;
this.startTimestamp = startTimestamp;
Expand All @@ -72,6 +75,7 @@ export class Subscription {
this.receipt = receipt;
this.applePayload = applePayload;
this.ttl = ttl;
this.tableName = tableName;
}

get [DynamoDbTable]() {
Expand All @@ -91,7 +95,7 @@ export class ReadSubscription extends Subscription {
}

get [DynamoDbTable]() {
return `${App}-${Stage}-subscriptions`
return `${App}-${Stage}-${this.tableName}`
}
}

2 changes: 1 addition & 1 deletion typescript/src/services/google-play-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type GoogleSubscription = {
// 3. Looking up detailed information about the purchased subscription product from the `android-publisher` API:
// https://developers.google.com/android-publisher/api-ref/rest/v3/monetization.subscriptions/get
// 4. Applying heuristics to attempt to determine whether the subscription is currently beneffiting from a free trial (see detailed discussion below.)
export async function fetchGoogleSubscription(
export async function fetchGoogleSubscriptionV2(
purchaseToken: string,
packageName: string
): Promise<GoogleSubscription> {
Expand Down
41 changes: 40 additions & 1 deletion typescript/src/subscription-status/googleSubStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import {
} from '../models/apiGatewayHttp';
import {APIGatewayProxyEvent, APIGatewayProxyResult} from "aws-lambda";
import {fetchGoogleSubscription} from "../services/google-play";
import {optionalMsToDate} from "../utils/dates";
import {fetchGoogleSubscriptionV2} from "../services/google-play-v2";
import {Subscription} from '../models/subscription';
import {fromGooglePackageName} from "../services/appToPlatform";
import {dateToSecondTimestamp, optionalMsToDate, thirtyMonths} from "../utils/dates";
import {ReadSubscription} from "../models/subscription";
import {dynamoMapper} from "../utils/aws";
import {createHash} from 'crypto'
Expand Down Expand Up @@ -40,6 +43,13 @@ export async function handler(request: APIGatewayProxyEvent): Promise<APIGateway
const packageName = googlePackageName(request.headers);

if (purchaseToken && subscriptionId) {

// We're testing the new implementation in production, but want to limit traffic through this codepath
const roll = Math.floor(Math.random() * 100 + 1)
if (roll <= 100) {
await updateParallelTestTable(purchaseToken, packageName)
}

const purchaseTokenHash = createHash('sha256').update(purchaseToken).digest('hex')
console.log(`Searching for valid ${subscriptionId} subscription for Android app with package name: ${packageName}, for purchaseToken hash: ${purchaseTokenHash}`)
try {
Expand Down Expand Up @@ -98,6 +108,35 @@ async function getSubscriptionStatusFromDynamo(purchaseToken: string, purchaseTo
}
}

async function updateParallelTestTable(purchaseToken: string, packageName: string) {
try {
const googleSubscription =
await fetchGoogleSubscriptionV2(purchaseToken, packageName)

const subscription =
new Subscription(
purchaseToken,
googleSubscription.startTime?.toISOString() ?? "",
googleSubscription.expiryTime.toISOString(),
googleSubscription.userCancellationTime?.toISOString(),
googleSubscription.autoRenewing,
googleSubscription.productId,
fromGooglePackageName(packageName),
googleSubscription.freeTrial,
googleSubscription.billingPeriodDuration,
googleSubscription,
undefined,
null,
dateToSecondTimestamp(thirtyMonths(googleSubscription.expiryTime)),
"subscriptions-parallel-test"
)

await dynamoMapper.put({item: subscription})
} catch (err) {
console.log(`ANDROID-PARALLEL-TEST: Error: ${JSON.stringify(err)}`)
}
}

function subscriptionStatus(expiryDate: Date): SubscriptionStatus {
const now = new Date(Date.now())
return {
Expand Down

0 comments on commit b19dbab

Please sign in to comment.