@@ -11,7 +11,6 @@ export async function POST(req: NextRequest) {
1111 const signature = headers . get ( "Stripe-Signature" ) as string ;
1212
1313 let event : Stripe . Event | undefined = undefined ;
14-
1514 try {
1615 event = stripe . webhooks . constructEvent (
1716 body ,
@@ -28,9 +27,10 @@ export async function POST(req: NextRequest) {
2827 | Stripe . Checkout . Session
2928 | Stripe . Invoice ;
3029
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+ ) {
3434 const session = eventData as Stripe . Checkout . Session ;
3535 // Retrieve the subscription details from Stripe.
3636 const subscription = await stripe . subscriptions . retrieve (
@@ -40,36 +40,49 @@ export async function POST(req: NextRequest) {
4040 // Update the user stripe into in our database.
4141 // Since this is the initial subscription, we need to update
4242 // 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" ) {
5761 const invoice = eventData as Stripe . Invoice ;
62+
63+ const subscriptionId = invoice . parent ?. subscription_details ?. subscription ;
64+
5865 // Retrieve the subscription details from Stripe.
5966 const subscription = await stripe . subscriptions . retrieve (
60- invoice . parent ?. subscription_details ?. subscription as string
67+ subscriptionId as string
6168 ) ;
6269
6370 // 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+ }
7386 }
7487
7588 return new Response ( null , { status : 200 } ) ;
0 commit comments