Payment Integration
Paddle SDK Integration
Sitepins uses Paddle for subscription management and payment processing:
// src/lib/paddle/get-paddle-instance.ts
export function getPaddleInstance() {
const paddleOptions: PaddleOptions = {
environment:
process.env.NODE_ENV === "production"
? Environment.production
: Environment.sandbox,
logLevel: LogLevel.error,
};
return new Paddle(process.env.PADDLE_API_KEY!, paddleOptions);
}Checkout Flow
- Price Selection: User selects plan and billing cycle
- Paddle Initialization: Initialize Paddle with user context
- Checkout Process: Handle payment flow with event callbacks
- Webhook Processing: Process payment confirmation webhooks
Webhook Processing
// src/app/api/paddlehooks/route.ts
export async function POST(request: NextRequest) {
const signature = request.headers.get("paddle-signature") || "";
const privateKey = process.env.PADDLE_NOTIFICATION_WEBHOOK_SECRET || "";
const rawRequestBody = await request.text();
try {
const paddle = getPaddleInstance();
const eventData = await paddle.webhooks.unmarshal(
rawRequestBody,
privateKey,
signature
);
if (eventData) {
await webhookProcessor.processEvent(eventData);
}
return Response.json({ status: 200, eventName: eventData?.eventType });
} catch (error) {
return Response.json({ status: 500, error: "Webhook processing failed" });
}
}Subscription Management
- Plan Creation: Dynamic price generation
- Billing Cycles: Monthly and yearly options
- Usage Tracking: Monitor API usage and limits
- Upgrade/Downgrade: Seamless plan changes
Last updated on