Skip to content

Instantly share code, notes, and snippets.

@Masterxilo
Created November 26, 2022 23:31
Show Gist options
  • Save Masterxilo/492c5c169ef6be54f43fc5e8a9a01232 to your computer and use it in GitHub Desktop.
Save Masterxilo/492c5c169ef6be54f43fc5e8a9a01232 to your computer and use it in GitHub Desktop.
Typescript @types for Google Play Billing/Google Play Developer Console Real-time developer notifications rtdn (Cloud Pub Sub Messages) https://developer.android.com/google/play/billing/rtdn-reference
// couldn't find this anywhere, so here you go
// it's ugly, you can clean it up
/** https://developer.android.com/google/play/billing/rtdn-reference */
// TODO no one made typescript @types package for these?
interface CloudPubSubPublishMessage {
subscription: string;
message: pubsub_v1.Schema$PubsubMessage;
}
interface DeveloperNotification {
"version": string,
"packageName": string,
"eventTimeMillis": number,
"oneTimeProductNotification": OneTimeProductNotification,
"subscriptionNotification": SubscriptionNotification,
"testNotification": TestNotification
}
/** The type of notification. It can have the following values: */
enum OneTimeProductNotification$NotificationType {
ONE_TIME_PRODUCT_PURCHASED = 1, // (1) ONE_TIME_PRODUCT_PURCHASED - A one-time product was successfully purchased by a user.
ONE_TIME_PRODUCT_CANCELED = 2, // (2) ONE_TIME_PRODUCT_CANCELED - A pending one-time product purchase has been canceled by the user.
}
interface OneTimeProductNotification {
"version": string,
"notificationType": OneTimeProductNotification$NotificationType,
"purchaseToken": string,
"sku": string
}
/**The notificationType for a subscription can have the following values:*/
enum SubscriptionNotification$NotificationType {
SUBSCRIPTION_RECOVERED = 1, // (1) SUBSCRIPTION_RECOVERED - A subscription was recovered from account hold.
SUBSCRIPTION_RENEWED = 2, // (2) SUBSCRIPTION_RENEWED - An active subscription was renewed.
SUBSCRIPTION_CANCELED = 3, // (3) SUBSCRIPTION_CANCELED - A subscription was either voluntarily or involuntarily cancelled. For voluntary cancellation, sent when the user cancels.
SUBSCRIPTION_PURCHASED = 4, // (4) SUBSCRIPTION_PURCHASED - A new subscription was purchased.
SUBSCRIPTION_ON_HOLD = 5, // (5) SUBSCRIPTION_ON_HOLD - A subscription has entered account hold (if enabled).
SUBSCRIPTION_IN_GRACE_PERIOD = 6, // (6) SUBSCRIPTION_IN_GRACE_PERIOD - A subscription has entered grace period (if enabled).
SUBSCRIPTION_RESTARTED = 7, // (7) SUBSCRIPTION_RESTARTED - User has restored their subscription from Play > Account > Subscriptions. The subscription was canceled but had not expired yet when the user restores. For more information, see [Restorations](/google/play/billing/subscriptions#restore).
SUBSCRIPTION_PRICE_CHANGE_CONFIRMED = 8, // (8) SUBSCRIPTION_PRICE_CHANGE_CONFIRMED - A subscription price change has successfully been confirmed by the user.
SUBSCRIPTION_DEFERRED = 9, // (9) SUBSCRIPTION_DEFERRED - A subscription's recurrence time has been extended.
SUBSCRIPTION_PAUSED = 10, // (10) SUBSCRIPTION_PAUSED - A subscription has been paused.
SUBSCRIPTION_PAUSE_SCHEDULE_CHANGED = 11, // (11) SUBSCRIPTION_PAUSE_SCHEDULE_CHANGED - A subscription pause schedule has been changed.
SUBSCRIPTION_REVOKED = 12, // (12) SUBSCRIPTION_REVOKED - A subscription has been revoked from the user before the expiration time.
SUBSCRIPTION_EXPIRED = 13, // (13) SUBSCRIPTION_EXPIRED - A subscription has expired.
}
interface SubscriptionNotification {
"version": string,
"notificationType": SubscriptionNotification$NotificationType,
"purchaseToken": string,
"subscriptionId": string
}
interface TestNotification {
"version": string
}
@Masterxilo
Copy link
Author

usage in node.js with express:

app.post('/google-play-store-realtime-developer-notifications-rtdn-push-subscription-endpoint', express.json(), async (req, res) => {

    // formats: https://developer.android.com/google/play/billing/rtdn-reference#one-time
    // https://proandroiddev.com/implement-account-hold-part-2-a-complete-guide-for-subscriptions-in-google-play-f8aab44a9818
    const m = req.body as CloudPubSubPublishMessage;
    const message_data: string | undefined = decodeBase64AsStringOrUndefined(m.message?.data!);

    const developerNotification: DeveloperNotification = parseJsonOrUndefined(message_data);

    console.log({
        m,
        message_data,
        developerNotification,
        headers: req.headers
    });
    res.sendStatus(200);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment