Skip to content

Commit f78438d

Browse files
committed
fix: build error
1 parent f833422 commit f78438d

File tree

1 file changed

+67
-100
lines changed
  • apps/masterbots.ai/app/api/payment/subscription

1 file changed

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

4+
export const runtime = "edge"
45

56
const stripeSecretKey = process.env.STRIPE_SECRET_KEY;
67

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

14-
// # Get Subscription Details by Payment Intent ID
16+
// Get Subscription Details by Payment Intent ID
1517
export async function GET(req: NextRequest) {
1618
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');
1921

2022
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+
});
2827
}
2928

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+
});
3632

3733
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+
});
4538
}
4639

47-
const invoice = await stripe.invoices.retrieve(
48-
paymentIntent.invoice as string
49-
)
40+
const invoice = await stripe.invoices.retrieve(paymentIntent.invoice as string);
5041

5142
if (!invoice) {
52-
return new Response(JSON.stringify({ error: 'Invoice not found' }), {
43+
return new NextResponse(JSON.stringify({ error: 'Invoice not found' }), {
5344
status: 404,
54-
headers: { 'Content-Type': 'application/json' }
55-
})
45+
headers: { 'Content-Type': 'application/json' },
46+
});
5647
}
5748

58-
const subscriptionId = invoice.subscription
49+
const subscriptionId = invoice.subscription;
5950

6051
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+
});
6856
}
6957

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+
});
7761

7862
const card = paymentIntent.payment_method;
7963

80-
81-
return new Response(JSON.stringify(
82-
{
83-
card,
84-
subscription,
85-
}
86-
), {
64+
return new NextResponse(JSON.stringify({ card, subscription }), {
8765
status: 200,
88-
headers: { 'Content-Type': 'application/json' }
89-
})
66+
headers: { 'Content-Type': 'application/json' },
67+
});
9068
} 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 }), {
9472
status: stripeError?.statusCode || 500,
95-
headers: { 'Content-Type': 'application/json' }
96-
})
73+
headers: { 'Content-Type': 'application/json' },
74+
});
9775
}
9876
}
9977

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
10179
export async function PUT(req: NextRequest) {
10280
try {
103-
const { email } = await req.json()
81+
const { email } = await req.json();
10482
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+
});
11287
}
11388

11489
// 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 });
11991

120-
let customer
92+
let customer;
12193
if (customers.data.length > 0) {
122-
// Use the existing customer
123-
customer = customers.data[0]
94+
customer = customers.data[0];
12495
} 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+
});
132100
}
133101

134102
const subscriptions = await stripe.subscriptions.list({
135103
customer: customer.id,
136104
status: 'active',
137-
limit: 1
138-
})
105+
limit: 1,
106+
});
139107

140108
if (subscriptions.data.length > 0) {
141-
return new Response(JSON.stringify({ active: true }), {
109+
return new NextResponse(JSON.stringify({ active: true }), {
142110
status: 200,
143-
headers: { 'Content-Type': 'application/json' }
144-
})
111+
headers: { 'Content-Type': 'application/json' },
112+
});
145113
} else {
146-
return new Response(JSON.stringify({ active: false }), {
114+
return new NextResponse(JSON.stringify({ active: false }), {
147115
status: 200,
148-
headers: { 'Content-Type': 'application/json' }
149-
})
116+
headers: { 'Content-Type': 'application/json' },
117+
});
150118
}
151119
} 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 }), {
155123
status: stripeError?.statusCode || 500,
156-
headers: { 'Content-Type': 'application/json' }
157-
})
124+
headers: { 'Content-Type': 'application/json' },
125+
});
158126
}
159127
}
160-

0 commit comments

Comments
 (0)