增加支付逻辑

This commit is contained in:
andy committed 2026-10-12 13:57:23 +08:00
1 parent 4ac786aafb
commit dfb2c91b8d
77 files changed
+6370 -45

No files matched your search

@@ -0,0 +1,32 @@
-- Orders are written before calling WeChat. Keep them when a user or
-- organization is deleted so paid money cannot lose its audit trail.
create table if not exists public.payment_orders (
id text primary key check (length(id) between 1 and 32 and id ~ '^[A-Za-z0-9_-]+$'),
organization_id text not null references public.platform_organizations(id) on delete restrict,
account_id text not null references public.platform_users(id) on delete restrict,
request_key text not null check (length(request_key) between 1 and 64 and request_key ~ '^[A-Za-z0-9_-]+$'),
mch_id text not null check (length(mch_id) between 1 and 64),
app_id text not null check (length(app_id) between 1 and 64),
amount_fen bigint not null check (amount_fen between 1 and 1000000),
currency text not null check (currency = 'CNY'),
status text not null default 'pending' check (status in ('pending', 'paid', 'closed')),
code_url text,
transaction_id text,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
expires_at timestamptz not null,
paid_at timestamptz,
constraint payment_orders_paid_fields_check check (
(status = 'paid' and transaction_id is not null and length(transaction_id) > 0 and paid_at is not null)
or (status <> 'paid' and transaction_id is null and paid_at is null)
),
constraint payment_orders_expiry_check check (expires_at > created_at),
constraint payment_orders_org_request_unique unique (organization_id, request_key)
);
create unique index if not exists payment_orders_provider_transaction_unique
on public.payment_orders(mch_id, transaction_id) where transaction_id is not null;
create index if not exists payment_orders_org_created_idx
on public.payment_orders(organization_id, created_at desc);
create index if not exists payment_orders_pending_check_idx
on public.payment_orders(updated_at, created_at) where status = 'pending';
@@ -0,0 +1,34 @@
-- Reserve each requested refund against its original, successful payment.
-- A successful external refund can exceed the current wallet balance.
alter table public.billing_wallets drop constraint if exists billing_wallets_balance_fen_check;
alter table public.billing_ledger drop constraint if exists billing_ledger_balance_after_fen_check;
alter table public.billing_ledger drop constraint if exists billing_ledger_kind_check;
alter table public.billing_ledger add constraint billing_ledger_kind_check
check (kind in ('recharge', 'charge', 'refund', 'adjustment', 'recharge_refund'));
create table public.payment_refunds (
id text primary key check (length(id) between 1 and 32 and id ~ '^[A-Za-z0-9_-]+$'),
order_id text not null references public.payment_orders(id) on delete restrict,
organization_id text not null references public.platform_organizations(id) on delete restrict,
operator_id text not null references public.platform_users(id) on delete restrict,
request_key text not null check (length(request_key) between 1 and 64 and request_key ~ '^[A-Za-z0-9_-]+$'),
mch_id text not null check (length(mch_id) between 1 and 64),
transaction_id text not null check (length(transaction_id) > 0),
provider_id text,
amount_fen bigint not null check (amount_fen > 0),
total_fen bigint not null check (total_fen > 0 and total_fen >= amount_fen),
currency text not null check (currency = 'CNY'),
reason text not null,
status text not null default 'pending' check (status in ('pending', 'processing', 'success', 'closed', 'abnormal', 'rejected')),
failure_reason text not null default '',
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
succeeded_at timestamptz,
constraint payment_refunds_request_unique unique (order_id, request_key),
constraint payment_refunds_success_check check ((status = 'success') = (succeeded_at is not null))
);
create unique index payment_refunds_provider_unique on public.payment_refunds(mch_id, provider_id)
where provider_id is not null;
create index payment_refunds_order_idx on public.payment_refunds(order_id, created_at desc, id desc);
create index payment_refunds_pending_idx on public.payment_refunds(updated_at, created_at, id)
where status in ('pending', 'processing', 'abnormal');