@@ -11,7 +11,6 @@ export async function POST(req: NextRequest) {
11
11
const signature = headers . get ( "Stripe-Signature" ) as string ;
12
12
13
13
let event : Stripe . Event | undefined = undefined ;
14
-
15
14
try {
16
15
event = stripe . webhooks . constructEvent (
17
16
body ,
@@ -28,9 +27,10 @@ export async function POST(req: NextRequest) {
28
27
| Stripe . Checkout . Session
29
28
| Stripe . Invoice ;
30
29
31
- console . log ( { event, eventData } ) ;
32
-
33
- if ( event ?. type === "checkout.session.completed" ) {
30
+ if (
31
+ event ?. type === "checkout.session.completed" ||
32
+ event ?. type === "checkout.session.async_payment_succeeded"
33
+ ) {
34
34
const session = eventData as Stripe . Checkout . Session ;
35
35
// Retrieve the subscription details from Stripe.
36
36
const subscription = await stripe . subscriptions . retrieve (
@@ -40,36 +40,49 @@ export async function POST(req: NextRequest) {
40
40
// Update the user stripe into in our database.
41
41
// Since this is the initial subscription, we need to update
42
42
// the subscription id and customer id.
43
- await prisma . user . update ( {
44
- where : {
45
- id : session ?. metadata ?. userId ,
46
- } ,
47
- data : {
48
- stripeSubscriptionId : subscription . id ,
49
- stripeCustomerId : subscription . customer as string ,
50
- stripePriceId : subscription . items . data [ 0 ] . price . id ,
51
- stripeCurrentPeriodEnd : new Date ( subscription . ended_at ! * 1000 ) ,
52
- } ,
53
- } ) ;
54
- }
55
-
56
- if ( event ?. type === "invoice.payment_succeeded" ) {
43
+ try {
44
+ await prisma . user . update ( {
45
+ where : {
46
+ id : session ?. metadata ?. userId ,
47
+ } ,
48
+ data : {
49
+ stripeSubscriptionId : subscription . id ,
50
+ stripeCustomerId : subscription . customer as string ,
51
+ stripePriceId : subscription . items . data [ 0 ] . price . id ,
52
+ stripeCurrentPeriodEnd : new Date (
53
+ subscription . items . data [ 0 ] . current_period_end ! * 1000
54
+ ) ,
55
+ } ,
56
+ } ) ;
57
+ } catch ( error ) {
58
+ console . error ( error ) ;
59
+ }
60
+ } else if ( event ?. type === "invoice.payment_succeeded" ) {
57
61
const invoice = eventData as Stripe . Invoice ;
62
+
63
+ const subscriptionId = invoice . parent ?. subscription_details ?. subscription ;
64
+
58
65
// Retrieve the subscription details from Stripe.
59
66
const subscription = await stripe . subscriptions . retrieve (
60
- invoice . parent ?. subscription_details ?. subscription as string
67
+ subscriptionId as string
61
68
) ;
62
69
63
70
// Update the price id and set the new period end.
64
- await prisma . user . update ( {
65
- where : {
66
- stripeSubscriptionId : subscription . id ,
67
- } ,
68
- data : {
69
- stripePriceId : subscription . items . data [ 0 ] . price . id ,
70
- stripeCurrentPeriodEnd : new Date ( subscription . ended_at ! * 1000 ) ,
71
- } ,
72
- } ) ;
71
+ try {
72
+ await prisma . user . update ( {
73
+ where : {
74
+ stripeSubscriptionId : subscription . id ,
75
+ } ,
76
+ data : {
77
+ stripePriceId : subscription . items . data [ 0 ] . price . id ,
78
+ stripeCurrentPeriodEnd : new Date (
79
+ subscription . items . data [ 0 ] . current_period_end ! * 1000
80
+ ) ,
81
+ } ,
82
+ } ) ;
83
+ } catch ( error ) {
84
+ console . error ( error ) ;
85
+ }
73
86
}
74
87
75
88
return new Response ( null , { status : 200 } ) ;
0 commit comments