Skip to content

Commit 18dfdad

Browse files
committed
Allow async checkout purchases in UserProduct
1 parent 6b09e91 commit 18dfdad

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

  • packages/prisma-client/prisma/migrations/20260502000000_user_product_async_checkout
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
-- Treat async-paid Checkout Sessions as purchase-granting events.
2+
-- Stripe can emit checkout.session.completed before async payment confirmation;
3+
-- checkout.session.async_payment_succeeded is the paid signal for that flow.
4+
CREATE OR REPLACE VIEW "UserProduct" AS (
5+
SELECT
6+
"userId",
7+
"subscriptionId",
8+
"productId",
9+
"customerId",
10+
"customerEmail"
11+
FROM
12+
"TransactionLog" AS tl
13+
WHERE
14+
"status" = 'complete'
15+
AND "eventData"#>>'{data,object,payment_status}' = 'paid'
16+
AND "eventType" IN (
17+
'checkout.session.completed',
18+
'checkout.session.async_payment_succeeded'
19+
)
20+
-- If both events were logged for one async Checkout Session, prefer the
21+
-- paid async signal so the same subscription is not granted twice.
22+
AND NOT (
23+
tl."eventType" = 'checkout.session.completed'
24+
AND tl."subscriptionId" IS NOT NULL
25+
AND EXISTS (
26+
SELECT
27+
1
28+
FROM
29+
"TransactionLog" AS async_success
30+
WHERE
31+
async_success."subscriptionId" = tl."subscriptionId"
32+
AND async_success."eventType" =
33+
'checkout.session.async_payment_succeeded'
34+
AND async_success."status" = 'complete'
35+
AND async_success."eventData"#>>'{data,object,payment_status}' =
36+
'paid')
37+
)
38+
AND NOT EXISTS (
39+
SELECT
40+
1
41+
FROM
42+
"TransactionLog" AS tlexsists
43+
WHERE
44+
tlexsists."subscriptionId" = tl."subscriptionId"
45+
AND tlexsists."eventType" = 'customer.subscription.deleted'
46+
AND tlexsists."status" = 'canceled'
47+
AND tlexsists."eventCreated" > tl."eventCreated")
48+
AND NOT EXISTS (
49+
SELECT
50+
1
51+
FROM
52+
"TransactionLog" AS tlexsists
53+
WHERE
54+
tlexsists."paymentIntent" = tl."paymentIntent"
55+
AND tlexsists."eventType" = 'charge.refunded'
56+
AND tlexsists."status" = 'succeeded'
57+
AND tlexsists."eventCreated" > tl."eventCreated")
58+
ORDER BY
59+
"userId",
60+
"eventCreated" DESC)
61+
UNION ALL (
62+
SELECT
63+
"userId",
64+
"subscriptionId",
65+
"productId",
66+
"customerId",
67+
"customerEmail"
68+
FROM
69+
"TransactionLog"
70+
WHERE
71+
"eventType" = 'appsumo.activate');

0 commit comments

Comments
 (0)