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