Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android promotion info is pulled through #1393

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading