in typescript/src/subscription-status/googleSubStatus.ts [45:105]
export async function handler(
request: APIGatewayProxyEvent,
): Promise<APIGatewayProxyResult> {
const purchaseToken = getPurchaseToken(request.headers);
const subscriptionId = getSubscriptionId(request.pathParameters);
const packageName = googlePackageName(request.headers);
if (purchaseToken && subscriptionId) {
// We're testing the new implementation in production, but want to limit traffic through this codepath
// This was turned off 2024-11-06 in an attempt to reduce our API quota usage
// const roll = Math.floor(Math.random() * 100 + 1)
// if (roll <= 0) {
// 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 {
const subscriptionStatus =
(await getSubscriptionStatusFromDynamo(
purchaseToken,
purchaseTokenHash,
)) ??
(await getSubscriptionStatusFromGoogle(
subscriptionId,
purchaseToken,
packageName,
purchaseTokenHash,
));
if (subscriptionStatus !== null) {
return { statusCode: 200, body: JSON.stringify(subscriptionStatus) };
} else {
console.log(
`No subscription found for purchaseToken hash: ${purchaseTokenHash}`,
);
return HTTPResponses.NOT_FOUND;
}
} catch (error: any) {
if (error.statusCode == 410) {
console.log(
`No subscription found for purchaseToken hash: ${purchaseTokenHash} (410-Gone from upstream API)`,
);
return HTTPResponses.NOT_FOUND;
} else {
console.log(
`Serving an Internal Server Error due to: ${
error.toString().split('/tokens/')[0]
}`,
);
return HTTPResponses.INTERNAL_ERROR;
}
}
} else {
return HTTPResponses.INVALID_REQUEST;
}
}