mirror of
https://github.com/HugeFrog24/shakethefrog.git
synced 2026-03-02 08:24:33 +00:00
Fuckaway Windows line endings
This commit is contained in:
107
app/api/checkout/route.ts
Normal file
107
app/api/checkout/route.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createCheckout } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { initializeLemonSqueezy, getLemonSqueezyConfig } from '../../config/lemonsqueezy';
|
||||
import { getFeatureFlags } from '../../config/features';
|
||||
import { appConfig } from '../../config/app';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { paymentsEnabled } = getFeatureFlags();
|
||||
if (!paymentsEnabled) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Payments are currently disabled' },
|
||||
{ status: 503 }
|
||||
);
|
||||
}
|
||||
|
||||
// Initialize Lemon Squeezy SDK
|
||||
initializeLemonSqueezy();
|
||||
|
||||
const { skinId, locale } = await request.json();
|
||||
|
||||
if (!skinId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Skin ID is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!locale) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Locale is required' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Get skin configuration
|
||||
const skin = appConfig.skins[skinId as keyof typeof appConfig.skins];
|
||||
|
||||
if (!skin) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid skin ID' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!skin.isPremium) {
|
||||
return NextResponse.json(
|
||||
{ error: 'This skin is not premium' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
if (!skin.variantId) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Variant ID not configured for this skin' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// Create checkout session
|
||||
const config = getLemonSqueezyConfig();
|
||||
const checkout = await createCheckout(config.storeId, skin.variantId!, {
|
||||
productOptions: {
|
||||
name: `Premium ${skin.name} Skin`,
|
||||
description: `Unlock the premium ${skin.name} skin for Shake the Frog!`,
|
||||
redirectUrl: `${config.baseUrl}/${locale}/checkout/success?skin=${skinId}`,
|
||||
receiptButtonText: 'Go to App',
|
||||
receiptThankYouNote: 'Thank you for your purchase! Your premium skin is now available.',
|
||||
},
|
||||
checkoutOptions: {
|
||||
embed: false,
|
||||
media: false,
|
||||
logo: true,
|
||||
desc: true,
|
||||
discount: true,
|
||||
subscriptionPreview: true,
|
||||
buttonColor: '#16a34a'
|
||||
},
|
||||
checkoutData: {
|
||||
custom: {
|
||||
skin_id: skinId,
|
||||
},
|
||||
},
|
||||
testMode: process.env.NODE_ENV !== 'production',
|
||||
});
|
||||
|
||||
if (checkout.error) {
|
||||
console.error('Checkout creation error:', checkout.error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to create checkout session' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
checkoutUrl: checkout.data?.data.attributes.url,
|
||||
checkoutId: checkout.data?.data.id,
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Checkout API error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Internal server error' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
33
app/api/prices/route.ts
Normal file
33
app/api/prices/route.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getVariant } from '@lemonsqueezy/lemonsqueezy.js';
|
||||
import { initializeLemonSqueezy } from '../../config/lemonsqueezy';
|
||||
import { getFeatureFlags } from '../../config/features';
|
||||
import { appConfig } from '../../config/app';
|
||||
|
||||
export async function GET() {
|
||||
const { paymentsEnabled } = getFeatureFlags();
|
||||
if (!paymentsEnabled) {
|
||||
return NextResponse.json({ prices: {}, enabled: false });
|
||||
}
|
||||
|
||||
// Initialize Lemon Squeezy SDK
|
||||
initializeLemonSqueezy();
|
||||
|
||||
const prices: Record<string, string> = {};
|
||||
|
||||
// Fetch prices for all premium skins
|
||||
for (const [skinId, skin] of Object.entries(appConfig.skins)) {
|
||||
if (skin.isPremium && skin.variantId) {
|
||||
const variant = await getVariant(skin.variantId);
|
||||
|
||||
if (!variant.data) {
|
||||
throw new Error(`No variant data found for ${skinId}`);
|
||||
}
|
||||
|
||||
const priceInCents = variant.data.data.attributes.price;
|
||||
prices[skinId] = `$${(priceInCents / 100).toFixed(2)}`;
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({ prices });
|
||||
}
|
||||
148
app/api/webhooks/lemonsqueezy/route.ts
Normal file
148
app/api/webhooks/lemonsqueezy/route.ts
Normal file
@@ -0,0 +1,148 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createHmac } from 'crypto';
|
||||
import { getLemonSqueezyConfig } from '../../../config/lemonsqueezy';
|
||||
|
||||
// Webhook payload interface using proper typing
|
||||
interface WebhookPayload {
|
||||
meta: {
|
||||
event_name: string;
|
||||
custom_data?: Record<string, unknown>;
|
||||
};
|
||||
data: {
|
||||
type: string;
|
||||
id: string;
|
||||
attributes: Record<string, unknown>;
|
||||
relationships?: Record<string, unknown>;
|
||||
};
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.text();
|
||||
const signature = request.headers.get('x-signature');
|
||||
|
||||
if (!signature) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Missing signature' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// Verify webhook signature
|
||||
const config = getLemonSqueezyConfig();
|
||||
const secret = config.webhookSecret;
|
||||
|
||||
const hmac = createHmac('sha256', secret);
|
||||
hmac.update(body);
|
||||
const digest = hmac.digest('hex');
|
||||
|
||||
if (signature !== digest) {
|
||||
console.error('Invalid webhook signature');
|
||||
return NextResponse.json(
|
||||
{ error: 'Invalid signature' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// Parse webhook payload
|
||||
const payload = JSON.parse(body);
|
||||
const eventName = payload.meta?.event_name;
|
||||
|
||||
console.log('Received webhook:', eventName);
|
||||
|
||||
// Handle different webhook events
|
||||
switch (eventName) {
|
||||
case 'order_created':
|
||||
await handleOrderCreated(payload);
|
||||
break;
|
||||
case 'subscription_created':
|
||||
await handleSubscriptionCreated(payload);
|
||||
break;
|
||||
case 'subscription_updated':
|
||||
await handleSubscriptionUpdated(payload);
|
||||
break;
|
||||
case 'subscription_cancelled':
|
||||
await handleSubscriptionCancelled(payload);
|
||||
break;
|
||||
default:
|
||||
console.log('Unhandled webhook event:', eventName);
|
||||
}
|
||||
|
||||
return NextResponse.json({ received: true });
|
||||
|
||||
} catch (error) {
|
||||
console.error('Webhook processing error:', error);
|
||||
return NextResponse.json(
|
||||
{ error: 'Webhook processing failed' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleOrderCreated(payload: WebhookPayload) {
|
||||
const order = payload.data;
|
||||
const attributes = order.attributes as Record<string, unknown>;
|
||||
const firstOrderItem = attributes.first_order_item as Record<string, unknown> | undefined;
|
||||
const customData = firstOrderItem?.product_name;
|
||||
|
||||
console.log('Order created:', {
|
||||
orderId: order.id,
|
||||
customerEmail: attributes.user_email,
|
||||
total: attributes.total_formatted,
|
||||
status: attributes.status,
|
||||
customData: customData,
|
||||
});
|
||||
|
||||
// Here you could:
|
||||
// - Send confirmation email
|
||||
// - Update user permissions in your database
|
||||
// - Log the purchase for analytics
|
||||
// - Grant access to premium features
|
||||
}
|
||||
|
||||
async function handleSubscriptionCreated(payload: WebhookPayload) {
|
||||
const subscription = payload.data;
|
||||
const attributes = subscription.attributes as Record<string, unknown>;
|
||||
|
||||
console.log('Subscription created:', {
|
||||
subscriptionId: subscription.id,
|
||||
customerEmail: attributes.user_email,
|
||||
status: attributes.status,
|
||||
productName: attributes.product_name,
|
||||
});
|
||||
|
||||
// Handle subscription creation
|
||||
// - Update user subscription status
|
||||
// - Send welcome email
|
||||
// - Grant premium access
|
||||
}
|
||||
|
||||
async function handleSubscriptionUpdated(payload: WebhookPayload) {
|
||||
const subscription = payload.data;
|
||||
const attributes = subscription.attributes as Record<string, unknown>;
|
||||
|
||||
console.log('Subscription updated:', {
|
||||
subscriptionId: subscription.id,
|
||||
status: attributes.status,
|
||||
endsAt: attributes.ends_at,
|
||||
});
|
||||
|
||||
// Handle subscription updates
|
||||
// - Update user access based on status
|
||||
// - Handle plan changes
|
||||
}
|
||||
|
||||
async function handleSubscriptionCancelled(payload: WebhookPayload) {
|
||||
const subscription = payload.data;
|
||||
const attributes = subscription.attributes as Record<string, unknown>;
|
||||
|
||||
console.log('Subscription cancelled:', {
|
||||
subscriptionId: subscription.id,
|
||||
customerEmail: attributes.user_email,
|
||||
endsAt: attributes.ends_at,
|
||||
});
|
||||
|
||||
// Handle subscription cancellation
|
||||
// - Schedule access removal for end date
|
||||
// - Send cancellation confirmation
|
||||
}
|
||||
Reference in New Issue
Block a user