import {z} from 'zod' export type EntitleSubscriptionRequestLineItemEntitlementDetails = {lineItemIndex?: number | undefined, products?: Array | undefined}; export type EntitleSubscriptionRequest = {lineItemEntitlementDetails?: Array | undefined}; export type CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdEntitleArgs = {partnersId: string, subscriptionsId: string, body: EntitleSubscriptionRequest}; export type CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdEntitleOptions = {baseUrl?: string | undefined, Oauth2?: string | undefined, Oauth2c?: string | undefined, fetch?: typeof fetch | undefined}; export const subscriptionCancellationDetails = z.object({reason: z.enum(["CANCELLATION_REASON_UNSPECIFIED", "CANCELLATION_REASON_FRAUD", "CANCELLATION_REASON_REMORSE", "CANCELLATION_REASON_ACCIDENTAL_PURCHASE", "CANCELLATION_REASON_PAST_DUE", "CANCELLATION_REASON_ACCOUNT_CLOSED", "CANCELLATION_REASON_UPGRADE_DOWNGRADE", "CANCELLATION_REASON_USER_DELINQUENCY", "CANCELLATION_REASON_SYSTEM_ERROR", "CANCELLATION_REASON_SYSTEM_CANCEL", "CANCELLATION_REASON_BILLING_SYSTEM_SWITCH", "CANCELLATION_REASON_OTHER"]).optional()}); export const amount = z.object({amountMicros: z.string().optional(), currencyCode: z.string().optional()}); export const subscriptionLineItemBundleDetailsBundleElementDetails = z.object({product: z.string().optional(), userAccountLinkedTime: z.string().optional()}); export const subscriptionLineItemBundleDetails = z.object({bundleElementDetails: z.array(subscriptionLineItemBundleDetailsBundleElementDetails).optional()}); export const finiteBillingCycleDetails = z.object({billingCycleCountLimit: z.string().optional()}); export const duration = z.object({count: z.number().int().optional(), unit: z.enum(["UNIT_UNSPECIFIED", "MONTH", "DAY", "HOUR"]).optional()}); export const promotionIntroductoryPricingDetailsIntroductoryPricingSpec = z.object({discountAmount: amount.optional(), discountRatioMicros: z.string().optional(), recurrenceCount: z.number().int().optional(), regionCode: z.string().optional()}); export const promotionIntroductoryPricingDetails = z.object({introductoryPricingSpecs: z.array(promotionIntroductoryPricingDetailsIntroductoryPricingSpec).optional()}); export const subscriptionPromotionSpec = z.object({freeTrialDuration: duration.optional(), introductoryPricingDetails: promotionIntroductoryPricingDetails.optional(), promotion: z.string().optional(), type: z.enum(["PROMOTION_TYPE_UNSPECIFIED", "PROMOTION_TYPE_FREE_TRIAL", "PROMOTION_TYPE_INTRODUCTORY_PRICING"]).optional()}); export const servicePeriod = z.object({endTime: z.string().optional(), startTime: z.string().optional()}); export const subscriptionLineItemOneTimeRecurrenceDetails = z.object({servicePeriod: servicePeriod.optional()}); export const googleHomePayload = z.object({attachedToGoogleStructure: z.boolean().optional(), googleStructureId: z.string().optional(), partnerStructureId: z.string().optional()}); export const googleOnePayload = z.object({campaigns: z.array(z.string()).optional(), offering: z.enum(["OFFERING_UNSPECIFIED", "OFFERING_VAS_BUNDLE", "OFFERING_VAS_STANDALONE", "OFFERING_HARD_BUNDLE", "OFFERING_SOFT_BUNDLE"]).optional(), salesChannel: z.enum(["CHANNEL_UNSPECIFIED", "CHANNEL_RETAIL", "CHANNEL_ONLINE_WEB", "CHANNEL_ONLINE_ANDROID_APP", "CHANNEL_ONLINE_IOS_APP"]).optional(), storeId: z.string().optional()}); export const youtubePayload = z.object({accessEndTime: z.string().optional(), partnerEligibilityIds: z.array(z.string()).optional(), partnerPlanType: z.enum(["PARTNER_PLAN_TYPE_UNSPECIFIED", "PARTNER_PLAN_TYPE_STANDALONE", "PARTNER_PLAN_TYPE_HARD_BUNDLE", "PARTNER_PLAN_TYPE_SOFT_BUNDLE"]).optional()}); export const productPayload = z.object({googleHomePayload: googleHomePayload.optional(), googleOnePayload: googleOnePayload.optional(), youtubePayload: youtubePayload.optional()}); export const subscriptionLineItem = z.object({amount: amount.optional(), bundleDetails: subscriptionLineItemBundleDetails.optional(), description: z.string().optional(), finiteBillingCycleDetails: finiteBillingCycleDetails.optional(), lineItemFreeTrialEndTime: z.string().optional(), lineItemIndex: z.number().int().optional(), lineItemPromotionSpecs: z.array(subscriptionPromotionSpec).optional(), name: z.string().optional(), oneTimeRecurrenceDetails: subscriptionLineItemOneTimeRecurrenceDetails.optional(), planType: z.enum(["LINE_ITEM_PLAN_TYPE_UNSPECIFIED", "LINE_ITEM_PLAN_TYPE_BASE", "LINE_ITEM_PLAN_TYPE_ADDON"]).optional(), product: z.string().optional(), productPayload: productPayload.optional(), recurrenceType: z.enum(["LINE_ITEM_RECURRENCE_TYPE_UNSPECIFIED", "LINE_ITEM_RECURRENCE_TYPE_PERIODIC", "LINE_ITEM_RECURRENCE_TYPE_ONE_TIME"]).optional(), state: z.enum(["LINE_ITEM_STATE_UNSPECIFIED", "LINE_ITEM_STATE_ACTIVE", "LINE_ITEM_STATE_INACTIVE", "LINE_ITEM_STATE_NEW", "LINE_ITEM_STATE_ACTIVATING", "LINE_ITEM_STATE_DEACTIVATING", "LINE_ITEM_STATE_WAITING_TO_DEACTIVATE", "LINE_ITEM_STATE_OFF_CYCLE_CHARGING"]).optional()}); export const subscriptionMigrationDetails = z.object({legacyCreationTime: z.string().optional(), migratedSubscriptionId: z.string().optional()}); export const location = z.object({postalCode: z.string().optional(), regionCode: z.string().optional()}); export const subscriptionUpgradeDowngradeDetails = z.object({billingCycleSpec: z.enum(["BILLING_CYCLE_SPEC_UNSPECIFIED", "BILLING_CYCLE_SPEC_ALIGN_WITH_PREVIOUS_SUBSCRIPTION", "BILLING_CYCLE_SPEC_START_IMMEDIATELY", "BILLING_CYCLE_SPEC_DEFERRED_TO_NEXT_RECURRENCE"]).optional(), previousSubscriptionId: z.string().optional()}); export const subscription = z.object({cancellationDetails: subscriptionCancellationDetails.optional(), createTime: z.string().optional(), cycleEndTime: z.string().optional(), endUserEntitled: z.boolean().optional(), freeTrialEndTime: z.string().optional(), lineItems: z.array(subscriptionLineItem).optional(), migrationDetails: subscriptionMigrationDetails.optional(), name: z.string().optional(), partnerUserToken: z.string().optional(), processingState: z.enum(["PROCESSING_STATE_UNSPECIFIED", "PROCESSING_STATE_CANCELLING", "PROCESSING_STATE_RECURRING", "PROCESSING_STATE_RESUMING", "PROCESSING_STATE_SUSPENDING"]).optional(), products: z.array(z.string()).optional(), promotionSpecs: z.array(subscriptionPromotionSpec).optional(), promotions: z.array(z.string()).optional(), purchaseTime: z.string().optional(), redirectUri: z.string().optional(), renewalTime: z.string().optional(), serviceLocation: location.optional(), state: z.enum(["STATE_UNSPECIFIED", "STATE_CREATED", "STATE_ACTIVE", "STATE_CANCELLED", "STATE_IN_GRACE_PERIOD", "STATE_CANCEL_AT_END_OF_CYCLE", "STATE_SUSPENDED"]).optional(), updateTime: z.string().optional(), upgradeDowngradeDetails: subscriptionUpgradeDowngradeDetails.optional()}); export const entitleSubscriptionResponse = z.object({subscription: subscription.optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Entitles a previously provisioned subscription to the current end user. The end user identity is inferred from the authorized credential of the request. This API must be authorized by the end user using OAuth. * * POST /v1/partners/{partnersId}/subscriptions/{subscriptionsId}:entitle */ export const createApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdEntitle = async (args: CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdEntitleArgs, options?: CreateApiV1PartnersPartnersIdSubscriptionsSubscriptionsIdEntitleOptions) => { const {baseUrl = "https://paymentsresellersubscription.googleapis.com", fetch: fetchFn = fetch, Oauth2, Oauth2c} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (Oauth2 !== undefined) headers.set('authorization', `Bearer ${Oauth2}`) if (Oauth2c !== undefined) headers.set('authorization', `Bearer ${Oauth2c}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/partners/${encodeURIComponent(String(args.partnersId))}/subscriptions/${encodeURIComponent(String(args.subscriptionsId))}:entitle${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(entitleSubscriptionResponse, await res.json()) };