expo-popcore-app/lib/plugins/stripe.d.ts

423 lines
11 KiB
TypeScript

import { GenericEndpointContext, User, Session, InferOptionSchema, BetterAuthPlugin } from 'better-auth';
import Stripe from 'stripe';
declare const subscriptions: {
subscription: {
fields: {
plan: {
type: "string";
required: true;
};
referenceId: {
type: "string";
required: true;
};
stripeCustomerId: {
type: "string";
required: false;
};
stripeSubscriptionId: {
type: "string";
required: false;
unique: true;
};
status: {
type: "string";
defaultValue: string;
};
periodStart: {
type: "date";
required: false;
};
periodEnd: {
type: "date";
required: false;
};
trialStart: {
type: "date";
required: false;
};
trialEnd: {
type: "date";
required: false;
};
cancelAtPeriodEnd: {
type: "boolean";
required: false;
defaultValue: false;
};
seats: {
type: "number";
required: false;
};
};
};
};
declare const user: {
user: {
fields: {
stripeCustomerId: {
type: "string";
required: false;
};
metadata: {
type: "json";
required: false;
};
};
};
};
type StripePlan = {
/**
* Monthly price id
*/
priceId?: string;
/**
* To use lookup key instead of price id
*
* https://docs.stripe.com/products-prices/
* manage-prices#lookup-keys
*/
lookupKey?: string;
/**
* A yearly discount price id
*
* useful when you want to offer a discount for
* yearly subscription
*/
annualDiscountPriceId?: string;
/**
* To use lookup key instead of price id
*
* https://docs.stripe.com/products-prices/
* manage-prices#lookup-keys
*/
annualDiscountLookupKey?: string;
/**
* Plan name
*/
name: string;
/**
* Limits for the plan
*/
limits?: Record<string, number>;
/**
* Plan group name
*
* useful when you want to group plans or
* when a user can subscribe to multiple plans.
*/
group?: string;
/**
* Free trial days
*/
freeTrial?: {
/**
* Number of days
*/
days: number;
/**
* A function that will be called when the trial
* starts.
*
* @param subscription
* @returns
*/
onTrialStart?: (subscription: Subscription) => Promise<void>;
/**
* A function that will be called when the trial
* ends
*
* @param subscription - Subscription
* @returns
*/
onTrialEnd?: (data: {
subscription: Subscription;
}, ctx: GenericEndpointContext) => Promise<void>;
/**
* A function that will be called when the trial
* expired.
* @param subscription - Subscription
* @returns
*/
onTrialExpired?: (subscription: Subscription, ctx: GenericEndpointContext) => Promise<void>;
};
};
type actionType = 'upgrade-subscription' | 'list-subscription' | 'cancel-subscription' | 'restore-subscription' | 'meter-event' | 'credit-topup' | 'adjust-meter-event' | 'meter-event-summary' | 'create-subscription' | 'get-pricing-table' | 'billing-portal';
interface Subscription {
/**
* Database identifier
*/
id: string;
/**
* The plan name
*/
plan: string;
/**
* Stripe customer id
*/
stripeCustomerId?: string;
/**
* Stripe subscription id
*/
stripeSubscriptionId?: string;
/**
* Trial start date
*/
trialStart?: Date;
/**
* Trial end date
*/
trialEnd?: Date;
/**
* Price Id for the subscription
*/
priceId?: string;
/**
* To what reference id the subscription belongs to
* @example
* - userId for a user
* - workspace id for a saas platform
* - website id for a hosting platform
*
* @default - userId
*/
referenceId: string;
/**
* Subscription status
*/
status: 'active' | 'canceled' | 'incomplete' | 'incomplete_expired' | 'past_due' | 'paused' | 'trialing' | 'unpaid';
/**
* The billing cycle start date
*/
periodStart?: Date;
/**
* The billing cycle end date
*/
periodEnd?: Date;
/**
* Cancel at period end
*/
cancelAtPeriodEnd?: boolean;
/**
* A field to group subscriptions so you can have multiple subscriptions
* for one reference id
*/
groupId?: string;
/**
* Number of seats for the subscription (useful for team plans)
*/
seats?: number;
}
interface AlipayNotifyData {
notify_time: string;
notify_type: string;
notify_id: string;
app_id: string;
charset: string;
version: string;
sign_type: string;
sign: string;
trade_no: string;
out_trade_no: string;
out_biz_no?: string;
buyer_id?: string;
buyer_logon_id?: string;
seller_id?: string;
seller_email?: string;
trade_status: 'WAIT_BUYER_PAY' | 'TRADE_CLOSED' | 'TRADE_SUCCESS' | 'TRADE_FINISHED';
total_amount: string;
receipt_amount?: string;
invoice_amount?: string;
buyer_pay_amount?: string;
point_amount?: string;
refund_fee?: string;
subject?: string;
body?: string;
gmt_create?: string;
gmt_payment?: string;
gmt_refund?: string;
gmt_close?: string;
fund_bill_list?: string;
passback_params?: string;
voucher_detail_list?: string;
[key: string]: any;
}
interface StripeOptions {
/**
* Stripe Client
*/
stripeClient: Stripe;
/**
* Stripe Webhook Secret
*
* @description Stripe webhook secret key
*/
stripeWebhookSecret: string;
/**
* Enable customer creation when a user signs up
*/
createCustomerOnSignUp?: boolean;
/**
* A callback to run after a customer has been created
* @param customer - Customer Data
* @param stripeCustomer - Stripe Customer Data
* @returns
*/
onCustomerCreate?: (data: {
stripeCustomer: Stripe.Customer;
user: User & {
stripeCustomerId: string;
};
}, ctx: GenericEndpointContext) => Promise<void>;
/**
* A custom function to get the customer create
* params
* @param data - data containing user and session
* @returns
*/
getCustomerCreateParams?: (user: User, ctx: GenericEndpointContext) => Promise<Partial<Stripe.CustomerCreateParams>>;
/**
* Subscriptions
*/
subscription?: {
enabled: boolean;
/**
* Subscription Configuration
*/
/**
* List of plan
*/
plans: StripePlan[] | (() => StripePlan[] | Promise<StripePlan[]>);
/**
* Require email verification before a user is allowed to upgrade
* their subscriptions
*
* @default false
*/
requireEmailVerification?: boolean;
/**
* A callback to run after a user has subscribed to a package
* @param event - Stripe Event
* @param subscription - Subscription Data
* @returns
*/
onSubscriptionComplete?: (data: {
event: Stripe.Event;
stripeSubscription: Stripe.Subscription;
subscription: Subscription;
plan: StripePlan;
}, ctx: GenericEndpointContext) => Promise<void>;
/**
* A callback to run after a user is about to cancel their subscription
* @returns
*/
onSubscriptionUpdate?: (data: {
event: Stripe.Event;
subscription: Subscription;
}) => Promise<void>;
/**
* A callback to run after a user is about to cancel their subscription
* @returns
*/
onSubscriptionCancel?: (data: {
event?: Stripe.Event;
subscription: Subscription;
stripeSubscription: Stripe.Subscription;
cancellationDetails?: Stripe.Subscription.CancellationDetails | null;
}) => Promise<void>;
/**
* A function to check if the reference id is valid
* and belongs to the user
*
* @param data - data containing user, session and referenceId
* @param ctx - the context object
* @returns
*/
authorizeReference?: (data: {
user: User & Record<string, any>;
session: Session & Record<string, any>;
referenceId: string;
action: actionType;
}, ctx: GenericEndpointContext) => Promise<boolean>;
/**
* A callback to run after a user has deleted their subscription
* @returns
*/
onSubscriptionDeleted?: (data: {
event: Stripe.Event;
stripeSubscription: Stripe.Subscription;
subscription: Subscription;
}) => Promise<void>;
/**
* parameters for session create params
*
* @param data - data containing user, session and plan
* @param ctx - the context object
*/
getCheckoutSessionParams?: (data: {
user: User & Record<string, any>;
session: Session & Record<string, any>;
plan: StripePlan;
subscription: Subscription;
}, ctx: GenericEndpointContext) => Promise<{
params?: Stripe.Checkout.SessionCreateParams;
options?: Stripe.RequestOptions;
}> | {
params?: Stripe.Checkout.SessionCreateParams;
options?: Stripe.RequestOptions;
};
/**
* Enable organization subscription
*/
organization?: {
enabled: boolean;
};
};
/**
* A callback to run after a stripe event is received
* @param event - Stripe Event
* @returns
*/
onEvent?: (event: Stripe.Event) => Promise<void>;
/**
* A callback to run after a one-time payment checkout is completed
* (e.g., credit top-up)
* @param checkoutSession - Stripe Checkout Session
* @param ctx - Generic Endpoint Context
* @returns
*/
onCheckoutComplete?: (data: {
session: Stripe.Checkout.Session;
}, ctx: GenericEndpointContext) => Promise<void>;
/**
* Schema for the stripe plugin
*/
schema?: InferOptionSchema<typeof subscriptions & typeof user>;
/**
* Pricing table config
*/
pricingTable?: {
id: string;
key: string;
meteredPriceId: string;
};
/**
* Alipay Configuration
*/
alipay?: {
endpoint: string;
/**
* Callback when Alipay sends a webhook notification
* @param data The notification parameters from Alipay
*/
beforeNotify?: (data: AlipayNotifyData, ctx: GenericEndpointContext) => Promise<void>;
};
}
declare const stripe: <O extends StripeOptions>(options: O) => BetterAuthPlugin;
export { stripe };
export type { StripePlan, Subscription };