feat: implement merchant dashboard, secure auth, and short_id system
- Added dedicated merchant dashboard with analytics and transactions - Implemented API Key based authentication for merchants - Introduced 8-character Short IDs for merchants to use in URLs - Refactored checkout and payment intent APIs to support multi-gateway - Enhanced Landing Page with Merchant Portal access and marketing copy - Fixed Next.js 15 async params build issues - Updated internal branding to P2CGateway - Added AyrisTech credits to footer
This commit is contained in:
@@ -5,8 +5,14 @@ CREATE TABLE admin_users (
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Register initial admin (User should replace this or add via dashboard)
|
||||
-- INSERT INTO admin_users (email) VALUES ('your-email@example.com');
|
||||
-- Merchants (Firms) table
|
||||
CREATE TABLE merchants (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
api_key TEXT UNIQUE DEFAULT encode(gen_random_bytes(32), 'hex'),
|
||||
webhook_url TEXT,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Transactions table
|
||||
CREATE TABLE transactions (
|
||||
@@ -20,18 +26,27 @@ CREATE TABLE transactions (
|
||||
customer_name TEXT,
|
||||
customer_phone TEXT,
|
||||
callback_url TEXT,
|
||||
merchant_id UUID REFERENCES merchants(id),
|
||||
metadata JSONB DEFAULT '{}'::jsonb
|
||||
);
|
||||
|
||||
-- Enable RLS
|
||||
ALTER TABLE transactions ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE merchants ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- Create policy for admins to read all
|
||||
CREATE POLICY "Admins can read all transactions" ON transactions
|
||||
FOR SELECT
|
||||
USING (auth.jwt() ->> 'email' IN (SELECT email FROM admin_users));
|
||||
|
||||
CREATE POLICY "Admins can manage merchants" ON merchants
|
||||
USING (auth.jwt() ->> 'email' IN (SELECT email FROM admin_users));
|
||||
|
||||
-- Create policy for service role to manage all
|
||||
CREATE POLICY "Service role can manage all" ON transactions
|
||||
CREATE POLICY "Service role can manage all transactions" ON transactions
|
||||
USING (true)
|
||||
WITH CHECK (true);
|
||||
|
||||
CREATE POLICY "Service role can manage all merchants" ON merchants
|
||||
USING (true)
|
||||
WITH CHECK (true);
|
||||
|
||||
17
docs/update_schema_merchants.sql
Normal file
17
docs/update_schema_merchants.sql
Normal file
@@ -0,0 +1,17 @@
|
||||
-- Create merchants table
|
||||
CREATE TABLE merchants (
|
||||
id UUID DEFAULT gen_random_uuid() PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
api_key TEXT UNIQUE DEFAULT encode(gen_random_bytes(32), 'hex'),
|
||||
webhook_url TEXT,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW()
|
||||
);
|
||||
|
||||
-- Add merchant_id to transactions
|
||||
ALTER TABLE transactions ADD COLUMN merchant_id UUID REFERENCES merchants(id);
|
||||
|
||||
-- Update RLS for transactions to support merchants (future proofing)
|
||||
-- For now, we'll just keep the admin policy as is, but we'll add more later.
|
||||
|
||||
-- Optional: Add index for performance
|
||||
CREATE INDEX idx_transactions_merchant_id ON transactions(merchant_id);
|
||||
11
docs/update_schema_providers.sql
Normal file
11
docs/update_schema_providers.sql
Normal file
@@ -0,0 +1,11 @@
|
||||
-- Add payment provider configuration to merchants table
|
||||
ALTER TABLE merchants ADD COLUMN payment_provider TEXT NOT NULL DEFAULT 'stripe';
|
||||
ALTER TABLE merchants ADD COLUMN provider_config JSONB DEFAULT '{}'::jsonb;
|
||||
|
||||
-- Add provider info to transactions to track which one was used
|
||||
ALTER TABLE transactions ADD COLUMN provider TEXT NOT NULL DEFAULT 'stripe';
|
||||
ALTER TABLE transactions ADD COLUMN provider_tx_id TEXT;
|
||||
ALTER TABLE transactions ADD COLUMN provider_status TEXT;
|
||||
|
||||
-- Update status constraint if needed (ours was already quite flexible, but let's be sure)
|
||||
-- Currently: CHECK (status IN ('pending', 'succeeded', 'failed'))
|
||||
25
docs/update_schema_short_ids.sql
Normal file
25
docs/update_schema_short_ids.sql
Normal file
@@ -0,0 +1,25 @@
|
||||
-- Add short_id column to merchants
|
||||
ALTER TABLE merchants ADD COLUMN IF NOT EXISTS short_id TEXT UNIQUE;
|
||||
|
||||
-- Function to generate a random short ID
|
||||
CREATE OR REPLACE FUNCTION generate_short_id() RETURNS TEXT AS $$
|
||||
DECLARE
|
||||
chars TEXT := 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
result TEXT := '';
|
||||
i INTEGER := 0;
|
||||
BEGIN
|
||||
FOR i IN 1..8 LOOP
|
||||
result := result || substr(chars, floor(random() * length(chars) + 1)::integer, 1);
|
||||
END LOOP;
|
||||
RETURN result;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Update existing merchants with a short_id
|
||||
UPDATE merchants SET short_id = generate_short_id() WHERE short_id IS NULL;
|
||||
|
||||
-- Make short_id required for further inserts
|
||||
-- ALTER TABLE merchants ALTER COLUMN short_id SET NOT NULL; -- Can do this after update
|
||||
|
||||
-- Add owner_id to merchants to link with Supabase Auth users
|
||||
ALTER TABLE merchants ADD COLUMN IF NOT EXISTS owner_id UUID REFERENCES auth.users(id);
|
||||
Reference in New Issue
Block a user