google-api-nodejs-client icon indicating copy to clipboard operation
google-api-nodejs-client copied to clipboard

Add support for `androidpublisher.subscriptionsv2`

Open j3k0 opened this issue 3 years ago • 0 comments

The androidpublisher API now includes a subscriptionsv2 resource that has to be used to validate purchases made with Android Billing v5 API released this month (has to or I get crashes when using the v1 endpoint) .

https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptionsv2/get

For information, as a workaround I use a "quick&dirty" injected method inspired by the existing subscriptions.get, that I set as subscriptions.getv2... Obviously this should be part of this library. Here's the code.

import * as googleapis_common_1 from 'googleapis-common';

androidpublisher.purchases.subscriptions.getv2 = function(paramsOrCallback, optionsOrCallback, callback) {
  let params = (paramsOrCallback ||
      {});
  let options = (optionsOrCallback || {});
  if (typeof paramsOrCallback === 'function') {
      callback = paramsOrCallback;
      params = {};
      options = {};
  }
  if (typeof optionsOrCallback === 'function') {
      callback = optionsOrCallback;
      options = {};
  }
  if (params.subscriptionId) delete params.subscriptionId;
  const rootUrl = options.rootUrl || 'https://androidpublisher.googleapis.com/';
  const parameters = {
      options: Object.assign({
          url: (rootUrl +
              '/androidpublisher/v3/applications/{packageName}/purchases/subscriptionsv2/tokens/{token}').replace(/([^:]\/)\/+/g, '$1'),
          method: 'GET',
      }, options),
      params,
      requiredParams: ['packageName', 'token'],
      pathParams: ['packageName', 'token'],
      context: this.context,
  };
  if (callback) {
      googleapis_common_1.createAPIRequest(parameters, callback);
  }
  else {
      return googleapis_common_1.createAPIRequest(parameters);
  }
}

And types:

export interface Params$Resource$Purchases$SubscriptionsV2$Get {
  // just because I cannot extend StandardParams (which is private)
  /**
    * Auth client or API Key for the request
    */
  auth?: any;
  /**
   * OAuth access token.
   */
  access_token?: string;

  /**
   * The package name of the application for which this subscription was purchased (for example, 'com.some.thing').
   */
  packageName?: string;
  /**
   * The token provided to the user's device when the subscription was purchased.
   */
  token?: string;
}
/**
 * A SubscriptionPurchase resource indicates the status of a user's subscription purchase.
 */

export interface Schema$SubscriptionPurchaseV2 {
  /**
   * The acknowledgement state of the subscription.
   */
  acknowledgementState?: Schema$AcknowledgementState | null;
  /**
   * Additional context around canceled subscriptions. Only present if the subscription currently has subscriptionState SUBSCRIPTION_STATE_CANCELED.
   */
  canceledStateContext?: Schema$CanceledStateContext | null;
  /**
   * User account identifier in the third-party service.
   */
  externalAccountIdentifiers?: Schema$ExternalAccountIdentifiers | null;
  /**
   * This kind represents a subscriptionPurchase object in the androidpublisher service.
   */
  kind?: string | null;
  /**
   * The order id of the latest order associated with the purchase of the subscription. For autoRenewing subscription, this is the order id of signup order if it is not renewed yet, or the last recurring order id (success, pending, or declined order). For prepaid subscription, this is the order id associated with the queried purchase token.
   */
  latestOrderId?: string | null;
  /**
   * Item-level info for a subscription purchase. The items in the same purchase should be either all with AutoRenewingPlan or all with PrepaidPlan.
   */
  lineItems?: Schema$SubscriptionPurchaseLineItem[] | null;
  /**
   * The purchase token of the old subscription if this subscription is one of the following:
   * - Re-signup of a canceled but non-lapsed subscription
   * - Upgrade/downgrade from a previous subscription.
   * - Convert from prepaid to auto renewing subscription.
   * - Convert from an auto renewing subscription to prepaid.
   * - Topup a prepaid subscription.
   */
  linkedPurchaseToken?: string | null;
  /**
   * Additional context around paused subscriptions. Only present if the subscription currently has subscriptionState SUBSCRIPTION_STATE_PAUSED.
   */
  pausedStateContext?: Schema$PausedStateContext | null;
  /**
   * ISO 3166-1 alpha-2 billing country/region code of the user at the time the subscription was granted.
   */
  regionCode?: string | null;
  /**
   * Time at which the subscription was granted. Not set for pending subscriptions (subscription was created but awaiting payment during signup).
   *
   * A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
   */
  startTime?: string | null;
  /**
   * User profile associated with purchases made with 'Subscribe with Google'.
   */
  subscribeWithGoogleInfo?: Schema$SubscribeWithGoogleInfo | null;
  /**
   * The current state of the subscription.
   */
  subscriptionState?: Schema$SubscriptionState | null;
  /**
   * Only present if this subscription purchase is a test purchase.
   */
  testPurchase?: Schema$TestPurchase | null;
}
/**
 * The potential states a subscription can be in, for example whether it is active or canceled.
 *
 * The items within a subscription purchase can either be all auto renewing plans or prepaid plans.
 */

export type Schema$SubscriptionState =
  | "SUBSCRIPTION_STATE_UNSPECIFIED" // Unspecified subscription state.
  | "SUBSCRIPTION_STATE_PENDING" // Subscription was created but awaiting payment during signup. In this state, all items are awaiting payment.
  | "SUBSCRIPTION_STATE_ACTIVE" // Subscription is active. - (1) If the subscription is an auto renewing plan, at least one item is autoRenewEnabled and not expired. - (2) If the subscription is a prepaid plan, at least one item is not expired.
  | "SUBSCRIPTION_STATE_PAUSED" // Subscription is paused. The state is only available when the subscription is an auto renewing plan. In this state, all items are in paused state.
  | "SUBSCRIPTION_STATE_IN_GRACE_PERIOD" // Subscription is in grace period. The state is only available when the subscription is an auto renewing plan. In this state, all items are in grace period.
  | "SUBSCRIPTION_STATE_ON_HOLD" // Subscription is on hold (suspended). The state is only available when the subscription is an auto renewing plan. In this state, all items are on hold.
  | "SUBSCRIPTION_STATE_CANCELED" // Subscription is canceled but not expired yet. The state is only available when the subscription is an auto renewing plan. All items have autoRenewEnabled set to false.
  | "SUBSCRIPTION_STATE_EXPIRED" // Subscription is expired. All items have expiryTime in the past.
  ;
/**
 * Information specific to a subscription in paused state.
 */

export interface Schema$PausedStateContext {
  /**
   * Time at which the subscription will be automatically resumed.
   *
   * A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
   */
  autoResumeTime?: string | null;
}
/** Information specific to a subscription in canceled state. */

export type Schema$CanceledStateContext =
  // Union field cancellation_reason can be only one of the following:
  {
    /** Subscription was canceled by user. */
    userInitiatedCancellation: Schema$UserInitiatedCancellation;
  } |
  {
    /** Subscription was canceled by the system, for example because of a billing problem. */
    systemInitiatedCancellation: Schema$SystemInitiatedCancellation;
  } |
  {
    /** Subscription was canceled by the developer. */
    developerInitiatedCancellation: Schema$DeveloperInitiatedCancellation;
  } |
  {
    /** Subscription was replaced by a new subscription. */
    replacementCancellation: Schema$ReplacementCancellation;
  };
// End of list of possible types for union field cancellation_reason.
/** Information specific to cancellations initiated by users. */
export interface Schema$UserInitiatedCancellation {
  /** Information provided by the user when they complete the subscription cancellation flow (cancellation reason survey). */
  cancelSurveyResult?: Schema$CancelSurveyResult;
  /**
   * The time at which the subscription was canceled by the user. The user might still have access to the subscription after this time. Use lineItems.expiry_time to determine if a user still has access.
   *
   * A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
   */
  cancelTime?: string | null;
}
/** Result of the cancel survey when the subscription was canceled by the user. */

export interface Schema$CancelSurveyResult {
  /**
   * The reason the user selected in the cancel survey.
   */
  reason?: Schema$CancelSurveyReason | null;
  /**
   * Only set for CANCEL_SURVEY_REASON_OTHERS. This is the user's freeform response to the survey.
   */
  reasonUserInput?: string | null;
}
/** The reason the user selected in the cancel survey. */

export type Schema$CancelSurveyReason = "CANCEL_SURVEY_REASON_UNSPECIFIED" // Unspecified cancel survey reason.
  |
  "CANCEL_SURVEY_REASON_NOT_ENOUGH_USAGE" // Not enough usage of the subscription.
  |
  "CANCEL_SURVEY_REASON_TECHNICAL_ISSUES" // Technical issues while using the app.
  |
  "CANCEL_SURVEY_REASON_COST_RELATED" // Cost related issues.
  |
  "CANCEL_SURVEY_REASON_FOUND_BETTER_APP" // The user found a better app.
  |
  "CANCEL_SURVEY_REASON_OTHERS" // Other reasons.
  ;
/** Information specific to cancellations initiated by Google system. */

export type Schema$SystemInitiatedCancellation = unknown;
/** Information specific to cancellations initiated by developers. */

export type Schema$DeveloperInitiatedCancellation = unknown;
/** Information specific to cancellations caused by subscription replacement. */

export type Schema$ReplacementCancellation = unknown;
/** Whether this subscription purchase is a test purchase. */

export type Schema$TestPurchase = unknown;
/** The possible acknowledgement states for a subscription. */

export type Schema$AcknowledgementState = "ACKNOWLEDGEMENT_STATE_UNSPECIFIED" // Unspecified acknowledgement state.
  |
  "ACKNOWLEDGEMENT_STATE_PENDING" // The subscription is not acknowledged yet.
  |
  "ACKNOWLEDGEMENT_STATE_ACKNOWLEDGED"; // The subscription is acknowledged.

/** User account identifier in the third-party service. */
export interface Schema$ExternalAccountIdentifiers {
  /**
   * User account identifier in the third-party service. Only present if account linking happened as part of the subscription purchase flow.
   */
  externalAccountId?: string | null;
  /**
   * An obfuscated version of the id that is uniquely associated with the user's account in your app. Present for the following purchases: * If account linking happened as part of the subscription purchase flow. * It was specified using https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.Builder#setobfuscatedaccountid when the purchase was made.
   */
  obfuscatedExternalAccountId?: string | null;

  /**
   * An obfuscated version of the id that is uniquely associated with the user's profile in your app. Only present if specified using https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.Builder#setobfuscatedprofileid when the purchase was made.
   */
  obfuscatedExternalProfileId?: string | null;
}
/**
 * Information associated with purchases made with 'Subscribe with Google'.
 */

export interface Schema$SubscribeWithGoogleInfo {
  /**
   * The Google profile id of the user when the subscription was purchased.
   */
  profileId?: string | null;
  /**
   * The profile name of the user when the subscription was purchased.
   */
  profileName?: string | null;
  /**
   * The email address of the user when the subscription was purchased.
   */
  emailAddress?: string | null;
  /**
   * The given name of the user when the subscription was purchased.
   */
  givenName?: string | null;
  /**
   * The family name of the user when the subscription was purchased.
   */
  familyName?: string | null;
}
/**
 * Item-level info for a subscription purchase.
 */

export type Schema$SubscriptionPurchaseLineItem = {
  /**
   * The purchased product ID (for example, 'monthly001').
   */
  productId?: string | null;
  /**
   * Time at which the subscription expired or will expire unless the access is extended (ex. renews).
   *
   * A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
   */
  expiryTime?: string | null;
} & ({
  /**
   * The item is auto renewing.
   */
  autoRenewingPlan?: Schema$AutoRenewingPlan | null;
} | {
  /**
   * The item is prepaid.
   */
  prepaidPlan?: Schema$PrepaidPlan | null;
});
/** Information related to an auto renewing plan. */

export interface Schema$AutoRenewingPlan {
  /**
   * If the subscription is currently set to auto-renew, e.g. the user has not canceled the subscription
   */
  autoRenewEnabled?: boolean;
}
/**
 * Information related to a prepaid plan.
 */

export interface Schema$PrepaidPlan {
  /**
   * After this time, the subscription is allowed for a new top-up purchase. Not present if the subscription is already extended by a top-up purchase.
   *
   * A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits. Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".
   */
  allowExtendAfterTime?: string | null;
}

j3k0 avatar May 30 '22 09:05 j3k0