Skip to content

Commit

Permalink
Android promotion info is pulled through
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwadeson committed Mar 13, 2024
1 parent 8036806 commit 3abe9bd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
23 changes: 19 additions & 4 deletions typescript/src/pubsub/google.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ interface SubscriptionNotification {
}

interface MetaData {
freeTrial: boolean
freeTrial: boolean,
promotionType: number | null,
promotionCode: string | null
}

export function parsePayload(body: Option<string>): Error | DeveloperNotification {
Expand Down Expand Up @@ -62,8 +64,11 @@ async function fetchMetadata(notification: DeveloperNotification): Promise<MetaD
notification.subscriptionNotification.purchaseToken,
notification.packageName
);

return {
freeTrial : subscription?.paymentState === GOOGLE_PAYMENT_STATE.FREE_TRIAL
freeTrial : subscription?.paymentState === GOOGLE_PAYMENT_STATE.FREE_TRIAL,
promotionType : subscription?.promotionType ?? null,
promotionCode : subscription?.promotionCode ?? null
}
} catch (exception) {
// here we really don't want to stop the processing of that event if we can't fetch metadata,
Expand All @@ -90,6 +95,16 @@ export function toDynamoEvent(notification: DeveloperNotification, metaData?: Me
console.warn(`Unknown package name ${notification.packageName}`)
}

// See: https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptions#SubscriptionPurchase
let promotionType
if (metaData?.promotionType === 0) {
promotionType = "ONE_TIME_CODE"
} else if (metaData?.promotionType === 1) {
promotionType = "VANITY_CODE"
} else {
promotionType = null
}

return new SubscriptionEvent(
notification.subscriptionNotification.purchaseToken,
eventTimestamp + "|" + eventTypeString,
Expand All @@ -102,8 +117,8 @@ export function toDynamoEvent(notification: DeveloperNotification, metaData?: Me
notification,
null,
dateToSecondTimestamp(thirtyMonths(eventTime)),
null, // string | null ; Introduced during the Apple extension of SubscriptionEvent [2023-11-03]
null, // string | null ; Introduced during the Apple extension of SubscriptionEvent [2023-11-03]
metaData?.promotionCode ?? null, // `promotionCode` (only present when the `promotionType` is a "VANITY_CODE") is taken as the `promotional_offer_id`
promotionType ?? null, // `promotionType` is taken as the `promotional_offer_name`
undefined, // any ; Introduced during the Apple extension of SubscriptionEvent [2023-11-03]
undefined, // any ; Introduced during the Apple extension of SubscriptionEvent [2023-11-03]
undefined // any ; Introduced during the Apple extension of SubscriptionEvent [2023-11-03]
Expand Down
4 changes: 3 additions & 1 deletion typescript/src/services/google-play.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ export interface GoogleResponseBody {
expiryTimeMillis: string,
userCancellationTimeMillis: string,
autoRenewing: boolean,
paymentState: 0 | 1 | 2 | 3
paymentState: 0 | 1 | 2 | 3,
promotionType: number,
promotionCode: string
}

export async function fetchGoogleSubscription(subscriptionId: string, purchaseToken: string, packageName: string): Promise<GoogleResponseBody | null> {
Expand Down
6 changes: 3 additions & 3 deletions typescript/tests/pubsub/pubsub.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe("The google pubsub", () => {

const mockSqsFunction: Mock<Promise<any>, [string, {purchaseToken: string}]> = jest.fn((queurl, event) => Promise.resolve({}));

const mockFetchMetadataFunction: Mock<Promise<any>> = jest.fn(event => Promise.resolve({freeTrial: true}));
const mockFetchMetadataFunction: Mock<Promise<any>> = jest.fn(event => Promise.resolve({freeTrial: true, promotionType: 1, promotionCode: "100"}));

const receivedEvent = {
"version":"1.0",
Expand Down Expand Up @@ -89,8 +89,8 @@ describe("The google pubsub", () => {
},
null,
1582319167,
null,
null,
"100",
"VANITY_CODE",
undefined,
undefined,
undefined
Expand Down

0 comments on commit 3abe9bd

Please sign in to comment.