-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy path20260722090000_paid_community.sql
More file actions
503 lines (455 loc) · 15.7 KB
/
Copy path20260722090000_paid_community.sql
File metadata and controls
503 lines (455 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
create table if not exists public.community_orders (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users(id) on delete cascade,
status text not null default 'PENDING'
check (status in ('PENDING', 'PAID', 'CLOSED', 'REFUNDED', 'REVOKED')),
amount_cents integer not null default 990 check (amount_cents = 990),
currency text not null default 'CNY' check (currency = 'CNY'),
subject text not null default 'GPT-Image2 付费交流群长期资格',
terms_version text not null,
alipay_app_id text,
alipay_seller_id text,
alipay_trade_no text,
refund_request_no text,
refund_status text not null default 'NONE'
check (refund_status in ('NONE', 'PROCESSING', 'SUCCEEDED', 'FAILED')),
refund_failure_code text,
paid_amount_cents integer check (paid_amount_cents is null or paid_amount_cents = 990),
paid_currency text check (paid_currency is null or paid_currency = 'CNY'),
metadata jsonb not null default '{}'::jsonb,
expires_at timestamptz not null default (now() + interval '30 minutes'),
paid_at timestamptz,
refund_requested_at timestamptz,
refund_checked_at timestamptz,
refunded_at timestamptz,
revoked_at timestamptz,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
create unique index if not exists community_orders_one_active_per_user_idx
on public.community_orders (user_id)
where status in ('PENDING', 'PAID');
create unique index if not exists community_orders_alipay_trade_no_idx
on public.community_orders (alipay_trade_no)
where alipay_trade_no is not null;
create unique index if not exists community_orders_refund_request_no_idx
on public.community_orders (refund_request_no)
where refund_request_no is not null;
create index if not exists community_orders_created_at_idx
on public.community_orders (created_at desc);
create index if not exists community_orders_status_idx
on public.community_orders (status, created_at desc);
create table if not exists public.community_alipay_notify_events (
id bigint generated by default as identity primary key,
order_id uuid not null references public.community_orders(id) on delete cascade,
notify_id text not null unique,
trade_no text not null,
trade_status text not null,
payload jsonb not null default '{}'::jsonb,
created_at timestamptz not null default now()
);
create unique index if not exists community_alipay_notify_order_trade_event_idx
on public.community_alipay_notify_events (order_id, trade_no, trade_status, notify_id);
create table if not exists public.community_group_qr_assets (
id uuid primary key default gen_random_uuid(),
media_type text not null check (media_type in ('image/png', 'image/jpeg', 'image/webp')),
qr_bytes bytea not null,
size_bytes integer not null check (size_bytes > 0 and size_bytes <= 2097152),
is_current boolean not null default false,
uploaded_by uuid not null references auth.users(id),
created_at timestamptz not null default now(),
retired_at timestamptz
);
create unique index if not exists community_group_qr_one_current_idx
on public.community_group_qr_assets ((true))
where is_current;
drop trigger if exists community_orders_set_updated_at on public.community_orders;
create trigger community_orders_set_updated_at
before update on public.community_orders
for each row
execute function public.set_updated_at();
alter table public.community_orders enable row level security;
alter table public.community_alipay_notify_events enable row level security;
alter table public.community_group_qr_assets enable row level security;
revoke all on table public.community_orders from public, anon, authenticated;
revoke all on table public.community_alipay_notify_events from public, anon, authenticated;
revoke all on table public.community_group_qr_assets from public, anon, authenticated;
grant all on table public.community_orders to service_role;
grant all on table public.community_alipay_notify_events to service_role;
grant all on table public.community_group_qr_assets to service_role;
grant usage, select on sequence public.community_alipay_notify_events_id_seq to service_role;
create or replace function public.create_or_reuse_community_order(
p_user_id uuid,
p_terms_version text
)
returns public.community_orders
language plpgsql
security definer
set search_path = public
as $$
declare
v_order public.community_orders%rowtype;
begin
if p_user_id is null then
raise exception 'COMMUNITY_USER_REQUIRED' using errcode = 'P0001';
end if;
if p_terms_version is null or btrim(p_terms_version) = '' then
raise exception 'COMMUNITY_TERMS_REQUIRED' using errcode = 'P0001';
end if;
perform pg_advisory_xact_lock(hashtext(p_user_id::text));
select co.*
into v_order
from public.community_orders as co
where co.user_id = p_user_id
and co.status in ('PENDING', 'PAID')
order by co.created_at desc
limit 1
for update;
if found then
return v_order;
end if;
insert into public.community_orders (
user_id,
status,
amount_cents,
currency,
subject,
terms_version
)
values (
p_user_id,
'PENDING',
990,
'CNY',
'GPT-Image2 付费交流群长期资格',
p_terms_version
)
returning * into v_order;
return v_order;
end;
$$;
create or replace function public.mark_community_order_paid(
p_order_id uuid,
p_trade_no text,
p_paid_amount_cents integer,
p_paid_currency text,
p_notify_id text default null,
p_notify_payload jsonb default '{}'::jsonb
)
returns table (
current_status text,
transitioned boolean
)
language plpgsql
security definer
set search_path = public
as $$
declare
v_order public.community_orders%rowtype;
begin
select co.*
into v_order
from public.community_orders as co
where co.id = p_order_id
for update;
if not found then
raise exception 'COMMUNITY_ORDER_NOT_FOUND' using errcode = 'P0001';
end if;
if p_trade_no is null or btrim(p_trade_no) = '' then
raise exception 'ALIPAY_TRADE_NO_REQUIRED' using errcode = 'P0001';
end if;
if p_paid_amount_cents <> v_order.amount_cents or p_paid_amount_cents <> 990 then
raise exception 'COMMUNITY_AMOUNT_MISMATCH' using errcode = 'P0001';
end if;
if p_paid_currency <> v_order.currency or p_paid_currency <> 'CNY' then
raise exception 'COMMUNITY_CURRENCY_MISMATCH' using errcode = 'P0001';
end if;
if v_order.alipay_trade_no is not null and v_order.alipay_trade_no <> p_trade_no then
raise exception 'ALIPAY_TRADE_NO_MISMATCH' using errcode = 'P0001';
end if;
if p_notify_id is not null and btrim(p_notify_id) <> '' then
insert into public.community_alipay_notify_events (
order_id,
notify_id,
trade_no,
trade_status,
payload
)
values (
v_order.id,
p_notify_id,
p_trade_no,
coalesce(nullif(p_notify_payload ->> 'trade_status', ''), 'TRADE_SUCCESS'),
coalesce(p_notify_payload, '{}'::jsonb)
)
on conflict (notify_id) do nothing;
end if;
if v_order.status in ('PAID', 'REFUNDED', 'REVOKED') then
return query select v_order.status, false;
return;
end if;
if v_order.status <> 'PENDING' then
raise exception 'COMMUNITY_ORDER_NOT_PAYABLE' using errcode = 'P0001';
end if;
update public.community_orders as co
set status = 'PAID',
alipay_trade_no = p_trade_no,
paid_amount_cents = p_paid_amount_cents,
paid_currency = p_paid_currency,
paid_at = coalesce(co.paid_at, now())
where co.id = v_order.id;
return query select 'PAID'::text, true;
end;
$$;
create or replace function public.close_community_order(p_order_id uuid)
returns table (
current_status text,
transitioned boolean
)
language plpgsql
security definer
set search_path = public
as $$
declare
v_order public.community_orders%rowtype;
begin
select co.*
into v_order
from public.community_orders as co
where co.id = p_order_id
for update;
if not found then
raise exception 'COMMUNITY_ORDER_NOT_FOUND' using errcode = 'P0001';
end if;
if v_order.status <> 'PENDING' then
return query select v_order.status, false;
return;
end if;
update public.community_orders as co
set status = 'CLOSED'
where co.id = v_order.id;
return query select 'CLOSED'::text, true;
end;
$$;
create or replace function public.prepare_community_refund(
p_order_id uuid,
p_refund_request_no text
)
returns table (
refund_request_no text,
refund_amount_cents integer,
already_prepared boolean
)
language plpgsql
security definer
set search_path = public
as $$
declare
v_order public.community_orders%rowtype;
begin
select co.*
into v_order
from public.community_orders as co
where co.id = p_order_id
for update;
if not found then
raise exception 'COMMUNITY_ORDER_NOT_FOUND' using errcode = 'P0001';
end if;
if v_order.status = 'REFUNDED' then
return query select v_order.refund_request_no, v_order.amount_cents, true;
return;
end if;
if v_order.status <> 'PAID' then
raise exception 'COMMUNITY_ORDER_NOT_REFUNDABLE' using errcode = 'P0001';
end if;
if p_refund_request_no is null or btrim(p_refund_request_no) = '' then
raise exception 'REFUND_REQUEST_NO_REQUIRED' using errcode = 'P0001';
end if;
if v_order.refund_request_no is not null and v_order.refund_request_no <> p_refund_request_no then
raise exception 'REFUND_REQUEST_NO_MISMATCH' using errcode = 'P0001';
end if;
update public.community_orders as co
set refund_request_no = p_refund_request_no,
refund_status = 'PROCESSING',
refund_failure_code = null,
refund_requested_at = coalesce(co.refund_requested_at, now())
where co.id = v_order.id;
return query select p_refund_request_no, v_order.amount_cents,
v_order.refund_status = 'PROCESSING';
end;
$$;
create or replace function public.set_community_refund_state(
p_order_id uuid,
p_refund_status text,
p_failure_code text default null
)
returns void
language plpgsql
security definer
set search_path = public
as $$
begin
if p_refund_status not in ('PROCESSING', 'FAILED') then
raise exception 'INVALID_REFUND_STATUS' using errcode = 'P0001';
end if;
update public.community_orders
set refund_status = p_refund_status,
refund_failure_code = nullif(left(coalesce(p_failure_code, ''), 120), ''),
refund_checked_at = now()
where id = p_order_id and status = 'PAID';
if not found then
raise exception 'COMMUNITY_ORDER_NOT_REFUNDABLE' using errcode = 'P0001';
end if;
end;
$$;
create or replace function public.finalize_community_refund(
p_order_id uuid,
p_trade_no text,
p_refund_request_no text,
p_refund_amount_cents integer
)
returns table (
current_status text,
transitioned boolean
)
language plpgsql
security definer
set search_path = public
as $$
declare
v_order public.community_orders%rowtype;
begin
select co.*
into v_order
from public.community_orders as co
where co.id = p_order_id
for update;
if not found then
raise exception 'COMMUNITY_ORDER_NOT_FOUND' using errcode = 'P0001';
end if;
if v_order.status = 'REFUNDED' then
return query select 'REFUNDED'::text, false;
return;
end if;
if v_order.status <> 'PAID' or v_order.refund_status <> 'PROCESSING' then
raise exception 'COMMUNITY_REFUND_NOT_PROCESSING' using errcode = 'P0001';
end if;
if v_order.alipay_trade_no <> p_trade_no
or v_order.refund_request_no <> p_refund_request_no
or p_refund_amount_cents <> v_order.amount_cents then
raise exception 'COMMUNITY_REFUND_RESULT_MISMATCH' using errcode = 'P0001';
end if;
update public.community_orders as co
set status = 'REFUNDED',
refund_status = 'SUCCEEDED',
refund_failure_code = null,
refund_checked_at = now(),
refunded_at = coalesce(co.refunded_at, now())
where co.id = v_order.id;
return query select 'REFUNDED'::text, true;
end;
$$;
create or replace function public.revoke_community_access(p_order_id uuid)
returns table (
current_status text,
transitioned boolean
)
language plpgsql
security definer
set search_path = public
as $$
declare
v_order public.community_orders%rowtype;
begin
select co.*
into v_order
from public.community_orders as co
where co.id = p_order_id
for update;
if not found then
raise exception 'COMMUNITY_ORDER_NOT_FOUND' using errcode = 'P0001';
end if;
if v_order.status = 'REVOKED' then
return query select 'REVOKED'::text, false;
return;
end if;
if v_order.status <> 'PAID' or v_order.refund_status = 'PROCESSING' then
raise exception 'COMMUNITY_ORDER_NOT_REVOKABLE' using errcode = 'P0001';
end if;
update public.community_orders as co
set status = 'REVOKED',
revoked_at = coalesce(co.revoked_at, now())
where co.id = v_order.id;
return query select 'REVOKED'::text, true;
end;
$$;
create or replace function public.replace_community_group_qr_asset(
p_media_type text,
p_qr_bytes bytea,
p_uploaded_by uuid
)
returns table (
id uuid,
media_type text,
size_bytes integer,
created_at timestamptz
)
language plpgsql
security definer
set search_path = public
as $$
declare
v_asset public.community_group_qr_assets%rowtype;
v_size integer;
begin
v_size := octet_length(p_qr_bytes);
if p_media_type not in ('image/png', 'image/jpeg', 'image/webp') then
raise exception 'COMMUNITY_QR_INVALID_TYPE' using errcode = 'P0001';
end if;
if v_size is null or v_size < 1 or v_size > 2097152 then
raise exception 'COMMUNITY_QR_INVALID_SIZE' using errcode = 'P0001';
end if;
if p_uploaded_by is null then
raise exception 'COMMUNITY_QR_UPLOADER_REQUIRED' using errcode = 'P0001';
end if;
perform pg_advisory_xact_lock(hashtext('community_group_qr_current'));
update public.community_group_qr_assets
set is_current = false,
retired_at = coalesce(retired_at, now())
where is_current;
insert into public.community_group_qr_assets (
media_type,
qr_bytes,
size_bytes,
is_current,
uploaded_by
)
values (p_media_type, p_qr_bytes, v_size, true, p_uploaded_by)
returning * into v_asset;
return query select v_asset.id, v_asset.media_type, v_asset.size_bytes, v_asset.created_at;
end;
$$;
revoke execute on function public.create_or_reuse_community_order(uuid, text)
from public, anon, authenticated;
revoke execute on function public.mark_community_order_paid(uuid, text, integer, text, text, jsonb)
from public, anon, authenticated;
revoke execute on function public.close_community_order(uuid)
from public, anon, authenticated;
revoke execute on function public.prepare_community_refund(uuid, text)
from public, anon, authenticated;
revoke execute on function public.set_community_refund_state(uuid, text, text)
from public, anon, authenticated;
revoke execute on function public.finalize_community_refund(uuid, text, text, integer)
from public, anon, authenticated;
revoke execute on function public.revoke_community_access(uuid)
from public, anon, authenticated;
revoke execute on function public.replace_community_group_qr_asset(text, bytea, uuid)
from public, anon, authenticated;
grant execute on function public.create_or_reuse_community_order(uuid, text) to service_role;
grant execute on function public.mark_community_order_paid(uuid, text, integer, text, text, jsonb) to service_role;
grant execute on function public.close_community_order(uuid) to service_role;
grant execute on function public.prepare_community_refund(uuid, text) to service_role;
grant execute on function public.set_community_refund_state(uuid, text, text) to service_role;
grant execute on function public.finalize_community_refund(uuid, text, text, integer) to service_role;
grant execute on function public.revoke_community_access(uuid) to service_role;
grant execute on function public.replace_community_group_qr_asset(text, bytea, uuid) to service_role;