|
1 | | -import { NextRequest, NextResponse } from 'next/server'; |
2 | | -import { Stripe } from 'stripe'; |
| 1 | +import type { NextRequest } from 'next/server' |
| 2 | +import { Stripe } from 'stripe' |
3 | 3 |
|
4 | | -export const runtime = "edge" |
5 | 4 |
|
6 | 5 | const stripeSecretKey = process.env.STRIPE_SECRET_KEY; |
7 | 6 |
|
8 | | -if (!stripeSecretKey) { |
| 7 | + if (!stripeSecretKey) { |
9 | 8 | throw new Error('Stripe secret key is not set.'); |
10 | 9 | } |
11 | | - |
12 | | -const stripe = new Stripe(stripeSecretKey, { |
| 10 | +const stripe = new Stripe(stripeSecretKey|| '', { |
13 | 11 | apiVersion: '2024-04-10' |
14 | | -}); |
| 12 | +}) |
15 | 13 |
|
16 | | -// Get Subscription Details by Payment Intent ID |
| 14 | +export const runtime = "edge" |
| 15 | + |
| 16 | +// # Get Subscription Details by Payment Intent ID |
17 | 17 | export async function GET(req: NextRequest) { |
18 | 18 | try { |
19 | | - const { searchParams } = new URL(req.url); |
20 | | - const paymentIntentId = searchParams.get('paymentIntentId'); |
| 19 | + const { searchParams } = new URL(req.url) |
| 20 | + const paymentIntentId = searchParams.get('paymentIntentId') |
21 | 21 |
|
22 | 22 | if (!paymentIntentId) { |
23 | | - return new NextResponse(JSON.stringify({ error: 'paymentIntentId is required' }), { |
24 | | - status: 400, |
25 | | - headers: { 'Content-Type': 'application/json' }, |
26 | | - }); |
| 23 | + return new Response( |
| 24 | + JSON.stringify({ error: 'paymentIntentId is required' }), |
| 25 | + { |
| 26 | + status: 400, |
| 27 | + headers: { 'Content-Type': 'application/json' } |
| 28 | + } |
| 29 | + ) |
27 | 30 | } |
28 | 31 |
|
29 | | - const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId, { |
30 | | - expand: ['payment_method'], |
31 | | - }); |
| 32 | + const paymentIntent = await stripe.paymentIntents.retrieve(paymentIntentId, |
| 33 | + // expand card details |
| 34 | + { |
| 35 | + expand: ['payment_method'] |
| 36 | + } |
| 37 | + ); |
32 | 38 |
|
33 | 39 | if (!paymentIntent) { |
34 | | - return new NextResponse(JSON.stringify({ error: 'Payment Intent not found' }), { |
35 | | - status: 404, |
36 | | - headers: { 'Content-Type': 'application/json' }, |
37 | | - }); |
| 40 | + return new Response( |
| 41 | + JSON.stringify({ error: 'Payment Intent not found' }), |
| 42 | + { |
| 43 | + status: 404, |
| 44 | + headers: { 'Content-Type': 'application/json' } |
| 45 | + } |
| 46 | + ) |
38 | 47 | } |
39 | 48 |
|
40 | | - const invoice = await stripe.invoices.retrieve(paymentIntent.invoice as string); |
| 49 | + const invoice = await stripe.invoices.retrieve( |
| 50 | + paymentIntent.invoice as string |
| 51 | + ) |
41 | 52 |
|
42 | 53 | if (!invoice) { |
43 | | - return new NextResponse(JSON.stringify({ error: 'Invoice not found' }), { |
| 54 | + return new Response(JSON.stringify({ error: 'Invoice not found' }), { |
44 | 55 | status: 404, |
45 | | - headers: { 'Content-Type': 'application/json' }, |
46 | | - }); |
| 56 | + headers: { 'Content-Type': 'application/json' } |
| 57 | + }) |
47 | 58 | } |
48 | 59 |
|
49 | | - const subscriptionId = invoice.subscription; |
| 60 | + const subscriptionId = invoice.subscription |
50 | 61 |
|
51 | 62 | if (!subscriptionId) { |
52 | | - return new NextResponse(JSON.stringify({ error: 'Subscription ID not found in invoice' }), { |
53 | | - status: 404, |
54 | | - headers: { 'Content-Type': 'application/json' }, |
55 | | - }); |
| 63 | + return new Response( |
| 64 | + JSON.stringify({ error: 'Subscription ID not found in invoice' }), |
| 65 | + { |
| 66 | + status: 404, |
| 67 | + headers: { 'Content-Type': 'application/json' } |
| 68 | + } |
| 69 | + ) |
56 | 70 | } |
57 | 71 |
|
58 | | - const subscription = await stripe.subscriptions.retrieve(subscriptionId as string, { |
59 | | - expand: ['items.data.plan', 'customer'], |
60 | | - }); |
| 72 | + const subscription = await stripe.subscriptions.retrieve( |
| 73 | + subscriptionId as string, |
| 74 | + { |
| 75 | + expand: ['items.data.plan', 'customer'] // Expand the plan details |
| 76 | + } |
| 77 | + ) |
| 78 | + |
61 | 79 |
|
62 | 80 | const card = paymentIntent.payment_method; |
63 | 81 |
|
64 | | - return new NextResponse(JSON.stringify({ card, subscription }), { |
| 82 | + |
| 83 | + return new Response(JSON.stringify( |
| 84 | + { |
| 85 | + card, |
| 86 | + subscription, |
| 87 | + } |
| 88 | + ), { |
65 | 89 | status: 200, |
66 | | - headers: { 'Content-Type': 'application/json' }, |
67 | | - }); |
| 90 | + headers: { 'Content-Type': 'application/json' } |
| 91 | + }) |
68 | 92 | } catch (error: any) { |
69 | | - console.error('Error creating subscription:', error); |
70 | | - const stripeError = error?.raw || error; |
71 | | - return new NextResponse(JSON.stringify({ error: stripeError?.message }), { |
| 93 | + console.error('Error creating subscription:', error) |
| 94 | + const stripeError = error?.raw || error |
| 95 | + return new Response(JSON.stringify({ error: stripeError?.message }), { |
72 | 96 | status: stripeError?.statusCode || 500, |
73 | | - headers: { 'Content-Type': 'application/json' }, |
74 | | - }); |
| 97 | + headers: { 'Content-Type': 'application/json' } |
| 98 | + }) |
75 | 99 | } |
76 | 100 | } |
77 | 101 |
|
78 | | -// Use PUT to check if a customer has an active subscription or not by email address |
| 102 | +// Use PUT to check if a customer has an active subscription or not by email address |
79 | 103 | export async function PUT(req: NextRequest) { |
80 | 104 | try { |
81 | | - const { email } = await req.json(); |
| 105 | + const { email } = await req.json() |
82 | 106 | if (!email) { |
83 | | - return new NextResponse(JSON.stringify({ error: 'Email is required' }), { |
84 | | - status: 400, |
85 | | - headers: { 'Content-Type': 'application/json' }, |
86 | | - }); |
| 107 | + return new Response( |
| 108 | + JSON.stringify({ error: 'Email is required' }), |
| 109 | + { |
| 110 | + status: 400, |
| 111 | + headers: { 'Content-Type': 'application/json' } |
| 112 | + } |
| 113 | + ) |
87 | 114 | } |
88 | 115 |
|
89 | 116 | // Search for an existing customer by email |
90 | | - const customers = await stripe.customers.list({ email, limit: 1 }); |
| 117 | + const customers = await stripe.customers.list({ |
| 118 | + email, |
| 119 | + limit: 1 |
| 120 | + }) |
91 | 121 |
|
92 | | - let customer; |
| 122 | + let customer |
93 | 123 | if (customers.data.length > 0) { |
94 | | - customer = customers.data[0]; |
| 124 | + // Use the existing customer |
| 125 | + customer = customers.data[0] |
95 | 126 | } else { |
96 | | - return new NextResponse(JSON.stringify({ error: 'Customer not found' }), { |
97 | | - status: 404, |
98 | | - headers: { 'Content-Type': 'application/json' }, |
99 | | - }); |
| 127 | + return new Response( |
| 128 | + JSON.stringify({ error: 'Customer not found' }), |
| 129 | + { |
| 130 | + status: 404, |
| 131 | + headers: { 'Content-Type': 'application/json' } |
| 132 | + } |
| 133 | + ) |
100 | 134 | } |
101 | 135 |
|
102 | 136 | const subscriptions = await stripe.subscriptions.list({ |
103 | 137 | customer: customer.id, |
104 | 138 | status: 'active', |
105 | | - limit: 1, |
106 | | - }); |
| 139 | + limit: 1 |
| 140 | + }) |
107 | 141 |
|
108 | 142 | if (subscriptions.data.length > 0) { |
109 | | - return new NextResponse(JSON.stringify({ active: true }), { |
| 143 | + return new Response(JSON.stringify({ active: true }), { |
110 | 144 | status: 200, |
111 | | - headers: { 'Content-Type': 'application/json' }, |
112 | | - }); |
| 145 | + headers: { 'Content-Type': 'application/json' } |
| 146 | + }) |
113 | 147 | } else { |
114 | | - return new NextResponse(JSON.stringify({ active: false }), { |
| 148 | + return new Response(JSON.stringify({ active: false }), { |
115 | 149 | status: 200, |
116 | | - headers: { 'Content-Type': 'application/json' }, |
117 | | - }); |
| 150 | + headers: { 'Content-Type': 'application/json' } |
| 151 | + }) |
118 | 152 | } |
119 | 153 | } catch (error: any) { |
120 | | - console.error('Error checking subscription:', error); |
121 | | - const stripeError = error?.raw || error; |
122 | | - return new NextResponse(JSON.stringify({ error: stripeError?.message }), { |
| 154 | + console.error('Error checking subscription:', error) |
| 155 | + const stripeError = error?.raw || error |
| 156 | + return new Response(JSON.stringify({ error: stripeError?.message }), { |
123 | 157 | status: stripeError?.statusCode || 500, |
124 | | - headers: { 'Content-Type': 'application/json' }, |
125 | | - }); |
| 158 | + headers: { 'Content-Type': 'application/json' } |
| 159 | + }) |
126 | 160 | } |
127 | 161 | } |
| 162 | + |
0 commit comments