A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
// SPDX-License-Identifier: AGPL-3.0-or-later
import {BillingDocumentResponse} from '@fluxer/schema/src/domains/billing_documents/BillingDocumentSchemas';
import {
DistributionQueryParam,
PriceDistributionResponse,
} from '@fluxer/schema/src/domains/premium/DistributionSchemas';
import {PremiumStoreSubscriptionState} from '@fluxer/schema/src/domains/premium/StoreBillingSchemas';
import {createStringType, withOpenApiType} from '@fluxer/schema/src/primitives/SchemaPrimitives';
import {UserPremiumTypesSchema} from '@fluxer/schema/src/primitives/UserSettingsValidators';
import {z} from 'zod';
export const PremiumCurrency = z.string().regex(/^[A-Z]{3}$/);
export type PremiumCurrency = z.infer<typeof PremiumCurrency>;
const BillingCycleSchema = z.enum(['monthly', 'yearly']);
export const WebhookReceivedResponse = z.object({
received: z.boolean().describe('Whether the webhook was successfully received'),
});
export type WebhookReceivedResponse = z.infer<typeof WebhookReceivedResponse>;
export const UrlResponse = z.object({
url: z.string().describe('The URL to redirect to'),
});
export type UrlResponse = z.infer<typeof UrlResponse>;
export const PriceIdsResponse = z.object({
monthly: z.string().nullish().describe('Stripe price ID for the monthly subscription'),
yearly: z.string().nullish().describe('Stripe price ID for the yearly subscription'),
gift_1_month: z.string().nullish().describe('Stripe price ID for the 1 month gift'),
gift_1_year: z.string().nullish().describe('Stripe price ID for the 1 year gift'),
monthly_amount_minor: z.number().int().nullish().describe('Monthly price amount in the currency minor unit'),
yearly_amount_minor: z.number().int().nullish().describe('Yearly price amount in the currency minor unit'),
gift_1_month_amount_minor: z
.number()
.int()
.nullish()
.describe('Gift 1 month price amount in the currency minor unit'),
gift_1_year_amount_minor: z.number().int().nullish().describe('Gift 1 year price amount in the currency minor unit'),
currency: PremiumCurrency.describe('Currency for the prices'),
gift_currency: PremiumCurrency.nullable().describe(
'Currency for gift prices, null when no gift prices are configured',
),
distribution: PriceDistributionResponse.nullable().describe(
'Distribution whose channel price the amounts include, or null for the standard price',
),
});
export type PriceIdsResponse = z.infer<typeof PriceIdsResponse>;
export const PriceIdsQueryRequest = z.object({
country_code: createStringType(2, 2)
.optional()
.describe(
'Two-letter country code for regional pricing. Only used when the server cannot geolocate the request; otherwise the request GeoIP country wins.',
),
distribution: DistributionQueryParam,
});
export type PriceIdsQueryRequest = z.infer<typeof PriceIdsQueryRequest>;
export const CurrentSubscriptionPriceResponse = z
.object({
price_id: z.string().describe('The Stripe price ID the user is currently billed against'),
amount_minor: z.number().int().describe('The amount the user is actually charged, in the currency minor unit'),
currency: PremiumCurrency.describe('Currency of the charged amount'),
billing_cycle: BillingCycleSchema.describe('The recurring billing cycle of the active subscription'),
is_grandfathered: z
.boolean()
.describe('Whether the user is on a legacy price that no longer matches the current list price'),
list_amount_minor: z
.number()
.int()
.nullable()
.describe('The current list price for the same cycle/currency, in the currency minor unit'),
list_price_id: z.string().nullable().describe('The current list Stripe price ID for the same cycle/currency'),
})
.nullable();
export type CurrentSubscriptionPriceResponse = z.infer<typeof CurrentSubscriptionPriceResponse>;
export const PendingSubscriptionChangeKind = z.enum(['billing_cycle', 'price']);
export type PendingSubscriptionChangeKind = z.infer<typeof PendingSubscriptionChangeKind>;
export const PendingSubscriptionChangeResponse = z
.object({
schedule_id: z.string().describe('Stripe subscription schedule ID managing the pending change'),
change_kind: PendingSubscriptionChangeKind.describe(
'Whether the pending change moves the billing cycle or only the price within the same cycle',
),
current_billing_cycle: BillingCycleSchema.nullable().describe('Current recurring billing cycle'),
target_billing_cycle: BillingCycleSchema.describe('Recurring billing cycle that will start later'),
effective_at: z.string().describe('ISO timestamp when the pending change takes effect'),
current_price_id: z.string().nullable().describe('Current Stripe price ID, when known'),
target_price_id: z.string().nullable().describe('Stripe price ID that will be used after the change'),
target_amount_minor: z
.number()
.int()
.nullable()
.describe('Unit amount of the price that will be used after the change, in the currency minor unit'),
currency: PremiumCurrency.nullable().describe('Currency for the pending change'),
initial_amount_minor: z
.number()
.int()
.nullable()
.describe('Estimated first invoice amount after one-time credits, in the currency minor unit'),
recurring_amount_minor: z
.number()
.int()
.nullable()
.describe('Estimated normal recurring amount after the first invoice, in the currency minor unit'),
credit_amount_minor: z
.number()
.int()
.nullable()
.describe('One-time credit applied to the first invoice, in the currency minor unit'),
})
.nullable();
export type PendingSubscriptionChangeResponse = z.infer<typeof PendingSubscriptionChangeResponse>;
export const ChangeSubscriptionRequest = z.object({
billing_cycle: BillingCycleSchema.describe('The recurring billing cycle to switch the active subscription to'),
effective_at: z
.enum(['now', 'period_end'])
.optional()
.describe(
'When the billing cycle change should take effect. A switch from yearly to monthly always takes effect at the end of the paid year',
),
});
export type ChangeSubscriptionRequest = z.infer<typeof ChangeSubscriptionRequest>;
export const ListPriceSwitchIneligibilityReason = z.enum([
'feature_unavailable',
'no_active_subscription',
'subscription_not_chargeable',
'unsupported_subscription',
'no_list_price',
'already_on_list_price',
'not_a_price_decrease',
'subscription_cancelling',
'cancellation_managed_by_schedule',
'conflicting_pending_change',
'missing_period_end',
'switch_in_progress',
]);
export type ListPriceSwitchIneligibilityReason = z.infer<typeof ListPriceSwitchIneligibilityReason>;
export const ListPriceSwitchState = z.object({
available: z
.boolean()
.describe('Whether the authenticated user can move their subscription down to the current list price right now'),
reason: ListPriceSwitchIneligibilityReason.nullable().describe(
'Why the switch is unavailable, when available is false',
),
pending: z.boolean().describe('Whether a switch to the current list price is already scheduled'),
current_price_id: z.string().nullable().describe('Stripe price ID the subscription is billed against today'),
current_amount_minor: z
.number()
.int()
.nullable()
.describe('Amount the subscription is billed today, in the currency minor unit'),
list_price_id: z.string().nullable().describe('Current list Stripe price ID for the same cycle and currency'),
list_amount_minor: z
.number()
.int()
.nullable()
.describe('Current list price for the same cycle and currency, in the currency minor unit'),
currency: PremiumCurrency.nullable().describe('Currency of both the current and the list amount'),
billing_cycle: BillingCycleSchema.nullable().describe('Recurring billing cycle the switch applies to'),
effective_at: z
.string()
.nullable()
.describe('ISO timestamp the switch takes effect, which is the end of the current billing period'),
});
export type ListPriceSwitchState = z.infer<typeof ListPriceSwitchState>;
const ScheduledListPriceSwitch = z.object({
effective_at: z.string().describe('ISO timestamp the switch takes effect'),
target_price_id: z.string().describe('Stripe price ID the subscription will be billed against after the switch'),
target_amount_minor: z.number().int().describe('Amount billed after the switch, in the currency minor unit'),
current_amount_minor: z.number().int().describe('Amount billed before the switch, in the currency minor unit'),
currency: PremiumCurrency.describe('Currency of both amounts'),
});
export const SwitchToListPriceResponse = z.discriminatedUnion('status', [
ScheduledListPriceSwitch.extend({
status: z.literal('scheduled').describe('The switch was scheduled for the end of the current billing period'),
}),
ScheduledListPriceSwitch.extend({
status: z.literal('already_scheduled').describe('The switch was already scheduled by an earlier request'),
}),
z.object({
status: z.literal('ineligible').describe('The subscription cannot be moved to the current list price'),
reason: ListPriceSwitchIneligibilityReason.describe('Why the switch was refused'),
}),
]);
export type SwitchToListPriceResponse = z.infer<typeof SwitchToListPriceResponse>;
const PremiumBillingCycle = BillingCycleSchema.nullable();
const PremiumActualState = z.object({
premium_type: UserPremiumTypesSchema.nullable().describe('Actual subscription type before local perk disabling'),
premium_since: z.string().nullable().describe('ISO timestamp when actual premium access first started'),
premium_until: z
.string()
.nullable()
.describe('ISO timestamp when actual premium access ends, including stacked gift time'),
premium_will_cancel: z.boolean().describe('Whether the subscription is set to cancel at period end'),
premium_billing_cycle: PremiumBillingCycle.describe('The actual recurring billing cycle, when known'),
premium_lifetime_sequence: z.number().int().nullable().describe('Visionary sequence number, when applicable'),
premium_grace_ends_at: z.string().nullable().describe('ISO timestamp when grace access ends, when applicable'),
has_active_paid_premium: z
.boolean()
.describe('Whether paid premium access is currently active before local disabling'),
is_visionary: z.boolean().describe('Whether the actual premium entitlement is lifetime Visionary access'),
has_ever_purchased: z.boolean().describe('Whether the user has ever completed a premium purchase'),
});
const PremiumEffectiveState = z.object({
is_premium: z.boolean().describe('Whether premium perks are currently effective for product gating'),
premium_type: UserPremiumTypesSchema.nullable().describe('Effective premium type used by product gates'),
premium_since: z.string().nullable().describe('Effective premium start timestamp exposed to the client'),
premium_until: z
.string()
.nullable()
.describe('Effective premium end timestamp exposed to the client, including stacked gift time'),
premium_will_cancel: z.boolean().describe('Effective cancellation status exposed to the client'),
premium_billing_cycle: PremiumBillingCycle.describe('Effective recurring billing cycle, when known'),
premium_lifetime_sequence: z
.number()
.int()
.nullable()
.describe('Effective Visionary sequence number, when applicable'),
premium_grace_ends_at: z.string().nullable().describe('Effective grace timestamp exposed to the client'),
premium_enabled_override: z.boolean().describe('Whether backend premium override is enabled'),
premium_purchase_disabled: z.boolean().describe('Whether premium purchase is disabled for this account'),
payment_dispute_open: z
.boolean()
.describe('Whether a payment dispute this account made is open, which blocks every purchase until it closes'),
premium_perks_disabled: z.boolean().describe('Whether the user temporarily disabled premium perks'),
self_hosted: z.boolean().describe('Whether the instance treats all users as premium because it is self-hosted'),
bot: z.boolean().describe('Whether the account is a bot account with premium-equivalent service access'),
});
export const PremiumBillingSubscriptionResponse = z.object({
id: z.string(),
status: z.string().nullable(),
current_period_start: z.string().nullable(),
current_period_end: z.string().nullable(),
cancel_at_period_end: z.boolean(),
cancel_at: z.string().nullable(),
canceled_at: z.string().nullable(),
plan_interval: z.string().nullable(),
plan_amount_minor: z.number().int().nullable(),
plan_currency: z.string().nullable(),
default_payment_method_id: z.string().nullable(),
});
export type PremiumBillingSubscriptionResponse = z.infer<typeof PremiumBillingSubscriptionResponse>;
export const PremiumBillingPaymentMethodResponse = z.object({
id: z.string(),
type: z.string().nullable(),
card_brand: z.string().nullable(),
card_last4: z.string().nullable(),
card_exp_month: z.number().int().nullable(),
card_exp_year: z.number().int().nullable(),
is_default: z.boolean(),
});
export type PremiumBillingPaymentMethodResponse = z.infer<typeof PremiumBillingPaymentMethodResponse>;
export const SelfServeRefundIneligibilityReason = z.enum([
'no_refundable_purchase',
'outside_refund_window',
'cooldown_active',
'feature_unavailable',
]);
export type SelfServeRefundIneligibilityReason = z.infer<typeof SelfServeRefundIneligibilityReason>;
export const SelfServeRefundProvider = withOpenApiType(z.enum(['stripe', 'google_play']), 'SelfServeRefundProvider');
export type SelfServeRefundProvider = z.infer<typeof SelfServeRefundProvider>;
export const SelfServeRefundEligibilityResponse = z.object({
eligible: z
.boolean()
.describe('Whether the authenticated user can self-serve refund their recent purchases right now'),
reason: SelfServeRefundIneligibilityReason.nullable().describe(
'Why the user is not eligible, when eligible is false',
),
invoice_id: z.string().nullable().describe('Latest paid invoice considered for refund eligibility'),
invoice_amount_paid_cents: z
.number()
.int()
.nullable()
.describe(
'Amount a self-serve refund would return in the currency minor unit: every payment made in the last 3 days when they share a currency, otherwise the latest one',
),
currency: z.string().nullable().describe('Currency of the latest paid invoice'),
paid_at: z.string().nullable().describe('ISO timestamp the latest invoice was paid'),
refund_window_expires_at: z.string().nullable().describe('ISO timestamp after which the 3-day refund window closes'),
cooldown_expires_at: z.string().nullable().describe('ISO timestamp the 30-day cooldown ends, if currently active'),
cancels_subscription: z.boolean().describe('Whether issuing the refund will also cancel the active subscription'),
provider: SelfServeRefundProvider.nullable().describe('Who took the payment for the purchase considered for refund'),
});
export type SelfServeRefundEligibilityResponse = z.infer<typeof SelfServeRefundEligibilityResponse>;
export const PlutoniumWithdrawalResponse = z.object({
id: z
.string()
.describe(
'Stripe subscription id of a subscription, or Checkout Session id of a gift, to send to the withdraw route',
),
kind: z.enum(['subscription', 'gift']).describe('Whether the purchase is a subscription or a gift'),
amount_minor: z
.number()
.int()
.describe(
'Amount a withdrawal at quoted_at refunds in the currency minor unit. It is the payments the withdrawal covers, less the share for the days of Plutonium used when the purchase was made with the immediate-start consent, and in full otherwise',
),
paid_minor: z.number().int().describe('Sum of the payments the withdrawal covers, in the currency minor unit'),
quoted_at: z
.string()
.describe('ISO timestamp amount_minor was worked out for. Send it back as quoted_at when withdrawing'),
currency: z.string().describe('Currency of the payments'),
purchased_at: z
.string()
.describe(
'ISO timestamp of the payment that started the withdrawal period: the first payment, a plan switch, a renewal at a new price, or a renewal of an EU or EEA subscription bought without the immediate-start consent',
),
withdrawal_ends_at: z.string().describe('ISO timestamp after which the withdrawal period is over'),
withdrawal_ends_on: z.string().describe('Last day of the withdrawal period as YYYY-MM-DD'),
});
export type PlutoniumWithdrawalResponse = z.infer<typeof PlutoniumWithdrawalResponse>;
export const PlutoniumWithdrawalListResponse = z
.array(PlutoniumWithdrawalResponse)
.describe('Plutonium purchases paid through Stripe that the account can withdraw from right now, newest first');
export type PlutoniumWithdrawalListResponse = z.infer<typeof PlutoniumWithdrawalListResponse>;
export const PlutoniumWithdrawRequest = z.object({
name: z.string().trim().min(1).max(200).describe('Name of the buyer who withdraws'),
quoted_at: z
.string()
.datetime()
.optional()
.describe(
'quoted_at of the withdrawal list entry the buyer saw. A refund less the days used uses the amount quoted then, if it is at most one day old',
),
});
export type PlutoniumWithdrawRequest = z.infer<typeof PlutoniumWithdrawRequest>;
export const PlutoniumWithdrawalIdParam = z.object({
purchase_id: z
.string()
.max(255)
.regex(/^(sub|cs)_[A-Za-z0-9_]+$/)
.describe('The id of a purchase from the withdrawals list of the premium state'),
});
const PremiumBillingState = z.object({
stripe_customer_id: z.string().nullable(),
current_subscription_price: CurrentSubscriptionPriceResponse,
pending_subscription_change: PendingSubscriptionChangeResponse,
list_price_switch: ListPriceSwitchState,
subscription: PremiumBillingSubscriptionResponse.nullable(),
documents: z
.array(BillingDocumentResponse)
.describe('Invoices, receipts, credit notes and refund notes Fluxer issued to the account, newest first'),
payment_methods: z.array(PremiumBillingPaymentMethodResponse),
refund_eligibility: SelfServeRefundEligibilityResponse,
});
export const PremiumPricingState = z.object({
country_code: createStringType(2, 2).nullable().describe('Country code used to resolve localized prices'),
localized: PriceIdsResponse.nullable().describe('Localized checkout prices resolved from mirrored billing data'),
});
export type PremiumPricingState = z.infer<typeof PremiumPricingState>;
export const PremiumSubscriptionProvider = withOpenApiType(
z.enum(['stripe', 'app_store', 'google_play']),
'PremiumSubscriptionProvider',
);
export type PremiumSubscriptionProvider = z.infer<typeof PremiumSubscriptionProvider>;
export const PremiumStateResponse = z.object({
actual: PremiumActualState,
effective: PremiumEffectiveState,
billing: PremiumBillingState,
pricing: PremiumPricingState,
store: PremiumStoreSubscriptionState.nullish().describe(
'Active App Store or Google Play subscription, null when no store subscription is active',
),
subscription_provider: PremiumSubscriptionProvider.nullable().describe(
'Billing platform that owns the current recurring subscription, null for gift, lifetime or no subscription. When a Stripe and a store subscription are both active, the one paid through later',
),
});
export type PremiumStateResponse = z.infer<typeof PremiumStateResponse>;
export const PremiumStateQueryRequest = PriceIdsQueryRequest;
export type PremiumStateQueryRequest = z.infer<typeof PremiumStateQueryRequest>;
export const UpdatePremiumPerksDisabledRequest = z.object({
disabled: z.boolean().describe('Whether premium perks should be temporarily disabled'),
});
export type UpdatePremiumPerksDisabledRequest = z.infer<typeof UpdatePremiumPerksDisabledRequest>;
export const SelfServeRefundResponse = z.object({
invoice_id: z.string(),
payment_intent_id: z.string().nullable(),
charge_id: z.string().nullable(),
refund_id: z.string().nullable(),
refunded_amount_cents: z
.number()
.int()
.nullable()
.describe(
'Amount actually refunded so far across every refunded payment, in the currency minor unit; 0 until the provider confirms success, null when the store does not report it',
),
invoice_amount_paid_cents: z.number().int().nullable(),
currency: z.string().nullable(),
subscription_id: z
.string()
.nullable()
.describe('Subscription that was cancelled along with the refund, when applicable'),
status: z
.string()
.nullable()
.describe('Provider status of the refund (e.g. pending, succeeded, failed); money only moved once succeeded'),
provider: SelfServeRefundProvider.describe('Who took the payment that was refunded'),
});
export type SelfServeRefundResponse = z.infer<typeof SelfServeRefundResponse>;