import {z} from 'zod' export type SubscriptionCancellationDetails = {reason?: ('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') | undefined}; export type Amount = {amountMicros?: unknown | undefined, currencyCode?: string | undefined}; export type SubscriptionLineItemBundleDetailsBundleElementDetails = {product?: string | undefined, userAccountLinkedTime?: unknown | undefined}; export type SubscriptionLineItemBundleDetails = {bundleElementDetails?: Array | undefined}; export type FiniteBillingCycleDetails = {billingCycleCountLimit?: unknown | undefined}; export type Duration = {count?: number | undefined, unit?: ('UNIT_UNSPECIFIED' | 'MONTH' | 'DAY' | 'HOUR') | undefined}; export type PromotionIntroductoryPricingDetailsIntroductoryPricingSpec = {discountAmount?: Amount | undefined, discountRatioMicros?: unknown | undefined, recurrenceCount?: number | undefined, regionCode?: string | undefined}; export type PromotionIntroductoryPricingDetails = {introductoryPricingSpecs?: Array | undefined}; export type SubscriptionPromotionSpec = {freeTrialDuration?: Duration | undefined, introductoryPricingDetails?: PromotionIntroductoryPricingDetails | undefined, promotion?: string | undefined, type?: ('PROMOTION_TYPE_UNSPECIFIED' | 'PROMOTION_TYPE_FREE_TRIAL' | 'PROMOTION_TYPE_INTRODUCTORY_PRICING') | undefined}; export type ServicePeriod = {endTime?: unknown | undefined, startTime?: unknown | undefined}; export type SubscriptionLineItemOneTimeRecurrenceDetails = {servicePeriod?: ServicePeriod | undefined}; export type GoogleHomePayload = {attachedToGoogleStructure?: boolean | undefined, googleStructureId?: string | undefined, partnerStructureId?: string | undefined}; export type GoogleOnePayload = {campaigns?: Array | undefined, offering?: ('OFFERING_UNSPECIFIED' | 'OFFERING_VAS_BUNDLE' | 'OFFERING_VAS_STANDALONE' | 'OFFERING_HARD_BUNDLE' | 'OFFERING_SOFT_BUNDLE') | undefined, salesChannel?: ('CHANNEL_UNSPECIFIED' | 'CHANNEL_RETAIL' | 'CHANNEL_ONLINE_WEB' | 'CHANNEL_ONLINE_ANDROID_APP' | 'CHANNEL_ONLINE_IOS_APP') | undefined, storeId?: string | undefined}; export type YoutubePayload = {accessEndTime?: unknown | undefined, partnerEligibilityIds?: Array | undefined, partnerPlanType?: ('PARTNER_PLAN_TYPE_UNSPECIFIED' | 'PARTNER_PLAN_TYPE_STANDALONE' | 'PARTNER_PLAN_TYPE_HARD_BUNDLE' | 'PARTNER_PLAN_TYPE_SOFT_BUNDLE') | undefined}; export type ProductPayload = {googleHomePayload?: GoogleHomePayload | undefined, googleOnePayload?: GoogleOnePayload | undefined, youtubePayload?: YoutubePayload | undefined}; export type SubscriptionLineItem = {amount?: Amount | undefined, bundleDetails?: SubscriptionLineItemBundleDetails | undefined, description?: string | undefined, finiteBillingCycleDetails?: FiniteBillingCycleDetails | undefined, lineItemFreeTrialEndTime?: unknown | undefined, lineItemIndex?: number | undefined, lineItemPromotionSpecs?: Array | undefined, name?: string | undefined, oneTimeRecurrenceDetails?: SubscriptionLineItemOneTimeRecurrenceDetails | undefined, planType?: ('LINE_ITEM_PLAN_TYPE_UNSPECIFIED' | 'LINE_ITEM_PLAN_TYPE_BASE' | 'LINE_ITEM_PLAN_TYPE_ADDON') | undefined, product?: string | undefined, productPayload?: ProductPayload | undefined, recurrenceType?: ('LINE_ITEM_RECURRENCE_TYPE_UNSPECIFIED' | 'LINE_ITEM_RECURRENCE_TYPE_PERIODIC' | 'LINE_ITEM_RECURRENCE_TYPE_ONE_TIME') | undefined, state?: ('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') | undefined}; export type SubscriptionMigrationDetails = {legacyCreationTime?: unknown | undefined, migratedSubscriptionId?: string | undefined}; export type Location = {postalCode?: string | undefined, regionCode?: string | undefined}; export type SubscriptionUpgradeDowngradeDetails = {billingCycleSpec?: ('BILLING_CYCLE_SPEC_UNSPECIFIED' | 'BILLING_CYCLE_SPEC_ALIGN_WITH_PREVIOUS_SUBSCRIPTION' | 'BILLING_CYCLE_SPEC_START_IMMEDIATELY' | 'BILLING_CYCLE_SPEC_DEFERRED_TO_NEXT_RECURRENCE') | undefined, previousSubscriptionId?: string | undefined}; export type Subscription = {cancellationDetails?: SubscriptionCancellationDetails | undefined, createTime?: unknown | undefined, cycleEndTime?: unknown | undefined, endUserEntitled?: boolean | undefined, freeTrialEndTime?: unknown | undefined, lineItems?: Array | undefined, migrationDetails?: SubscriptionMigrationDetails | undefined, name?: string | undefined, partnerUserToken?: string | undefined, processingState?: ('PROCESSING_STATE_UNSPECIFIED' | 'PROCESSING_STATE_CANCELLING' | 'PROCESSING_STATE_RECURRING' | 'PROCESSING_STATE_RESUMING' | 'PROCESSING_STATE_SUSPENDING') | undefined, products?: Array | undefined, promotionSpecs?: Array | undefined, promotions?: Array | undefined, purchaseTime?: unknown | undefined, redirectUri?: string | undefined, renewalTime?: unknown | undefined, serviceLocation?: Location | undefined, state?: ('STATE_UNSPECIFIED' | 'STATE_CREATED' | 'STATE_ACTIVE' | 'STATE_CANCELLED' | 'STATE_IN_GRACE_PERIOD' | 'STATE_CANCEL_AT_END_OF_CYCLE' | 'STATE_SUSPENDED') | undefined, updateTime?: unknown | undefined, upgradeDowngradeDetails?: SubscriptionUpgradeDowngradeDetails | undefined}; export type CreateApiV1PartnersPartnersIdSubscriptionsArgs = {partnersId: string, subscriptionId?: string | undefined, body: Subscription}; export type CreateApiV1PartnersPartnersIdSubscriptionsOptions = {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 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 }; /** * Used by partners to create a subscription for their customers. The created subscription is associated with the end user inferred from the end user credentials. This API must be authorized by the end user using OAuth. * * POST /v1/partners/{partnersId}/subscriptions */ export const createApiV1PartnersPartnersIdSubscriptions = async (args: CreateApiV1PartnersPartnersIdSubscriptionsArgs, options?: CreateApiV1PartnersPartnersIdSubscriptionsOptions) => { 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 (args.subscriptionId != null) query.append("subscriptionId", String(args.subscriptionId)) 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${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(subscription, await res.json()) };