Scaffold Stripe-backed DashCaddy license server
This commit is contained in:
107
src/server.js
Normal file
107
src/server.js
Normal file
@@ -0,0 +1,107 @@
|
||||
import express from 'express';
|
||||
import { config } from './config.js';
|
||||
import { getPlan, listPlans, PREMIUM_FEATURES } from './plans.js';
|
||||
import { getStripe } from './stripe.js';
|
||||
|
||||
const app = express();
|
||||
|
||||
app.use('/api/stripe/webhook', express.raw({ type: 'application/json' }));
|
||||
app.use(express.json());
|
||||
|
||||
app.get('/health', (_req, res) => {
|
||||
res.json({ ok: true, service: 'dashcaddy-license-server' });
|
||||
});
|
||||
|
||||
app.get('/api/public/config', (_req, res) => {
|
||||
res.json({
|
||||
ok: true,
|
||||
publishableKeyPresent: Boolean(config.stripePublishableKey),
|
||||
websiteUrl: config.websiteUrl
|
||||
});
|
||||
});
|
||||
|
||||
app.get('/api/public/plans', (_req, res) => {
|
||||
res.json({
|
||||
ok: true,
|
||||
tier: 'premium',
|
||||
features: PREMIUM_FEATURES,
|
||||
plans: listPlans()
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/checkout/session', async (req, res) => {
|
||||
try {
|
||||
const { planCode, customerEmail } = req.body || {};
|
||||
const plan = getPlan(planCode);
|
||||
|
||||
if (!plan) {
|
||||
return res.status(400).json({ ok: false, error: 'Invalid planCode' });
|
||||
}
|
||||
|
||||
const stripe = getStripe();
|
||||
|
||||
const session = await stripe.checkout.sessions.create({
|
||||
mode: 'subscription',
|
||||
payment_method_types: ['card'],
|
||||
line_items: [
|
||||
{
|
||||
price_data: {
|
||||
currency: 'usd',
|
||||
product_data: {
|
||||
name: plan.label,
|
||||
description: 'DashCaddy Premium subscription'
|
||||
},
|
||||
recurring: {
|
||||
interval: plan.interval,
|
||||
interval_count: plan.intervalCount
|
||||
},
|
||||
unit_amount: plan.amountUsd * 100
|
||||
},
|
||||
quantity: 1
|
||||
}
|
||||
],
|
||||
success_url: `${config.websiteUrl}/success?session_id={CHECKOUT_SESSION_ID}`,
|
||||
cancel_url: `${config.websiteUrl}/pricing`,
|
||||
allow_promotion_codes: true,
|
||||
billing_address_collection: 'required',
|
||||
customer_email: customerEmail || undefined,
|
||||
metadata: {
|
||||
source: 'dashcaddy.net',
|
||||
planCode: plan.code,
|
||||
tier: plan.tier
|
||||
},
|
||||
subscription_data: {
|
||||
metadata: {
|
||||
source: 'dashcaddy.net',
|
||||
planCode: plan.code,
|
||||
tier: plan.tier
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return res.json({ ok: true, url: session.url, sessionId: session.id });
|
||||
} catch (error) {
|
||||
console.error('Checkout session error:', error);
|
||||
return res.status(500).json({ ok: false, error: error.message || 'Checkout failed' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/stripe/webhook', async (req, res) => {
|
||||
try {
|
||||
return res.json({ ok: true, message: 'Webhook endpoint scaffolded, handler next' });
|
||||
} catch (error) {
|
||||
return res.status(500).json({ ok: false, error: error.message || 'Webhook failed' });
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/license/validate', async (_req, res) => {
|
||||
return res.status(501).json({ ok: false, error: 'License validation not implemented yet' });
|
||||
});
|
||||
|
||||
app.post('/api/license/deactivate', async (_req, res) => {
|
||||
return res.status(501).json({ ok: false, error: 'License deactivation not implemented yet' });
|
||||
});
|
||||
|
||||
app.listen(config.port, () => {
|
||||
console.log(`dashcaddy-license-server listening on :${config.port}`);
|
||||
});
|
||||
Reference in New Issue
Block a user