BuilderKit provides seamless integration with two popular payment gateways: Stripe and Lemon Squeezy. This allows you to easily handle subscriptions and process payments within your application.
Database Setup
The give code atset of SQL queries to set up the necessary database tables and functions for handling subscriptions. You can run the following code in your PostgreSQL database:
-- Create a table for User Subscription
-- Enum Type for Subscription Status
create type subscriptiontype as enum ('free', 'standard', 'premium');
create type billingcycle as enum ('month', 'year');
-- Table
create table
public.subscriptions (
id uuid not null default gen_random_uuid (),
created_at timestamp with time zone not null default now(),
type public.subscriptiontype not null default 'free'::subscriptiontype,
subscription_id text null,
amount int null,
interval public.billingcycle null,
start_date date null,
active boolean null,
user_email text not null,
user_id uuid not null,
constraint subscriptions_pkey primary key (id),
constraint subscriptions_user_email_fkey foreign key (user_email) references users (email),
constraint subscriptions_user_id_fkey foreign key (user_id) references users (id)
) tablespace pg_default;
-- Set up Row Level Security (RLS)
-- See https://supabase.com/docs/guides/auth/row-level-security for more details.
alter table subscriptions
enable row level security;
create policy "Users can insert their own row." on subscriptions
for insert with check (auth.uid() = id);
create policy "Users can read own row" on subscriptions
for select using (auth.uid() = id);
-- Function to handle new subscription for users
create function public.handle_new_subscription()
returns trigger as $$
BEGIN
-- Insert a new subscription record for the new user
insert into public.subscriptions (user_id, user_email)
values (new.id, new.email);
return new;
END;
$$ language plpgsql security definer;
-- Trigger to execute the function after a new user is created
create trigger on_user_created
after insert on auth.users
for each row execute procedure public.handle_new_subscription();
This code creates a subscriptions
table to store user subscription data, sets up row-level security policies, and creates a function and trigger to automatically insert a new subscription record for each new user.
Payment Methods Overview
Stripe is a widely used payment gateway that offers a robust set of features for handling online payments and subscriptions.
Configuration
To enable Stripe in your BuilderKit application, you need to set the following environment variables in your .env
file:
STRIPE_SECRET_KEY=your_stripe_secret_key
STRIPE_WEBHOOK_SECRET=your_stripe_webhook_secret
Webhook Handling
BuilderKit automatically handles incoming webhook events from Stripe. The webhook route is defined in /api/webhooks/stripe/route.ts
. It processes events such as customer.subscription.created
and customer.subscription.updated
to keep your application's subscription data in sync with Stripe.
Payment Button
BuilderKit provides a reusable payment button component (ButtonPayment
) that handles the payment flow for different subscription plans and frequencies.
For more detailed information on Stripe integration, please refer to the Builderkit Stripe Documentation.
By leveraging the payment gateway integrations provided by BuilderKit, you can easily implement subscription-based functionality in your application without the hassle of setting up the payment infrastructure from scratch.