Skip to content

Commit a9cf6ac

Browse files
committed
fix: build error update
1 parent f78438d commit a9cf6ac

File tree

2 files changed

+102
-67
lines changed

2 files changed

+102
-67
lines changed
Lines changed: 102 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,162 @@
1-
import { NextRequest, NextResponse } from 'next/server';
2-
import { Stripe } from 'stripe';
1+
import type { NextRequest } from 'next/server'
2+
import { Stripe } from 'stripe'
33

4-
export const runtime = "edge"
54

65
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
76

8-
if (!stripeSecretKey) {
7+
if (!stripeSecretKey) {
98
throw new Error('Stripe secret key is not set.');
109
}
11-
12-
const stripe = new Stripe(stripeSecretKey, {
10+
const stripe = new Stripe(stripeSecretKey|| '', {
1311
apiVersion: '2024-04-10'
14-
});
12+
})
1513

16-
// Get Subscription Details by Payment Intent ID
14+
export const runtime = "edge"
15+
16+
// # Get Subscription Details by Payment Intent ID
1717
export async function GET(req: NextRequest) {
1818
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')
2121

2222
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+
)
2730
}
2831

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+
);
3238

3339
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+
)
3847
}
3948

40-
const invoice = await stripe.invoices.retrieve(paymentIntent.invoice as string);
49+
const invoice = await stripe.invoices.retrieve(
50+
paymentIntent.invoice as string
51+
)
4152

4253
if (!invoice) {
43-
return new NextResponse(JSON.stringify({ error: 'Invoice not found' }), {
54+
return new Response(JSON.stringify({ error: 'Invoice not found' }), {
4455
status: 404,
45-
headers: { 'Content-Type': 'application/json' },
46-
});
56+
headers: { 'Content-Type': 'application/json' }
57+
})
4758
}
4859

49-
const subscriptionId = invoice.subscription;
60+
const subscriptionId = invoice.subscription
5061

5162
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+
)
5670
}
5771

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+
6179

6280
const card = paymentIntent.payment_method;
6381

64-
return new NextResponse(JSON.stringify({ card, subscription }), {
82+
83+
return new Response(JSON.stringify(
84+
{
85+
card,
86+
subscription,
87+
}
88+
), {
6589
status: 200,
66-
headers: { 'Content-Type': 'application/json' },
67-
});
90+
headers: { 'Content-Type': 'application/json' }
91+
})
6892
} 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 }), {
7296
status: stripeError?.statusCode || 500,
73-
headers: { 'Content-Type': 'application/json' },
74-
});
97+
headers: { 'Content-Type': 'application/json' }
98+
})
7599
}
76100
}
77101

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
79103
export async function PUT(req: NextRequest) {
80104
try {
81-
const { email } = await req.json();
105+
const { email } = await req.json()
82106
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+
)
87114
}
88115

89116
// 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+
})
91121

92-
let customer;
122+
let customer
93123
if (customers.data.length > 0) {
94-
customer = customers.data[0];
124+
// Use the existing customer
125+
customer = customers.data[0]
95126
} 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+
)
100134
}
101135

102136
const subscriptions = await stripe.subscriptions.list({
103137
customer: customer.id,
104138
status: 'active',
105-
limit: 1,
106-
});
139+
limit: 1
140+
})
107141

108142
if (subscriptions.data.length > 0) {
109-
return new NextResponse(JSON.stringify({ active: true }), {
143+
return new Response(JSON.stringify({ active: true }), {
110144
status: 200,
111-
headers: { 'Content-Type': 'application/json' },
112-
});
145+
headers: { 'Content-Type': 'application/json' }
146+
})
113147
} else {
114-
return new NextResponse(JSON.stringify({ active: false }), {
148+
return new Response(JSON.stringify({ active: false }), {
115149
status: 200,
116-
headers: { 'Content-Type': 'application/json' },
117-
});
150+
headers: { 'Content-Type': 'application/json' }
151+
})
118152
}
119153
} 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 }), {
123157
status: stripeError?.statusCode || 500,
124-
headers: { 'Content-Type': 'application/json' },
125-
});
158+
headers: { 'Content-Type': 'application/json' }
159+
})
126160
}
127161
}
162+

bun.lockb

4.34 KB
Binary file not shown.

0 commit comments

Comments
 (0)