UNPKG

@gsb-core/core

Version:

GSB core services and classes for platform-independent web applications

131 lines 5.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SubscriptionService = void 0; const gsb_subscription_model_1 = require("../../models/gsb-subscription.model"); const gsb_config_1 = require("../../config/gsb-config"); /** * Subscription service that handles all Paddle integration and subscription management */ class SubscriptionService { constructor() { // Initialize with environment variables this.paddleApiKey = (0, gsb_config_1.getEnvVar)('PADDLE_API_KEY', ''); this.paddleWebhookSecret = (0, gsb_config_1.getEnvVar)('PADDLE_WEBHOOK_SECRET', ''); this.paddleClientToken = (0, gsb_config_1.getEnvVar)('NEXT_PUBLIC_PADDLE_CLIENT_TOKEN', ''); this.paddleEnvironment = (0, gsb_config_1.getEnvVar)('NEXT_PUBLIC_PADDLE_ENV', 'sandbox') || 'sandbox'; if (!this.paddleApiKey || !this.paddleWebhookSecret || !this.paddleClientToken) { console.warn('Paddle configuration is incomplete. Some features may not work correctly.'); } } static getInstance() { if (!SubscriptionService.instance) { SubscriptionService.instance = new SubscriptionService(); } return SubscriptionService.instance; } /** * Create a new subscription plan */ async createSubscriptionPlan(userId, tenantId, title, paymentType = gsb_subscription_model_1.PaymentType.CreditCardOnline, status = gsb_subscription_model_1.SubscriptionPlanStatus.Active) { // In a real implementation, you would call Paddle API to create a new plan // For now, we'll create a mock plan that would be saved to GSB const plan = { title, tenant_id: tenantId, createdBy_id: userId, lastUpdatedBy_id: userId, paymentType, status, nextPaymentDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30 days from now }; // This would be replaced with an actual API call console.log('Creating subscription plan:', plan); return plan; } /** * Create a new subscription */ async createSubscription(userId, planId, productVariantId, quantity = 1) { // In a real implementation, you would call Paddle API to create a new subscription // For now, we'll create a mock subscription that would be saved to GSB const subscription = { plan_id: planId, productVariant_id: productVariantId, createdBy_id: userId, lastUpdatedBy_id: userId, quantity, active: true, startDate: new Date(), endDate: new Date(Date.now() + 365 * 24 * 60 * 60 * 1000), // 1 year from now }; // This would be replaced with an actual API call console.log('Creating subscription:', subscription); return subscription; } /** * Process a Paddle webhook event */ async processWebhookEvent(event) { try { // Verify the webhook signature - in a real implementation // you would use a library like crypto to verify the HMAC signature console.log('Processing webhook event:', event); // Depending on the event type, update subscription information switch (event.event_type) { case 'subscription_created': await this.handleSubscriptionCreated(event); break; case 'subscription_updated': await this.handleSubscriptionUpdated(event); break; case 'subscription_cancelled': await this.handleSubscriptionCancelled(event); break; case 'payment_succeeded': await this.handlePaymentSucceeded(event); break; case 'payment_failed': await this.handlePaymentFailed(event); break; default: console.log(`Unhandled webhook event type: ${event.event_type}`); } return true; } catch (error) { console.error('Error processing webhook event:', error); return false; } } async handleSubscriptionCreated(event) { // Logic to handle subscription created event console.log('Subscription created:', event); } async handleSubscriptionUpdated(event) { // Logic to handle subscription updated event console.log('Subscription updated:', event); } async handleSubscriptionCancelled(event) { // Logic to handle subscription cancelled event console.log('Subscription cancelled:', event); } async handlePaymentSucceeded(event) { // Logic to handle payment succeeded event console.log('Payment succeeded:', event); } async handlePaymentFailed(event) { // Logic to handle payment failed event console.log('Payment failed:', event); } /** * Get Paddle client configuration */ getPaddleConfig() { return { environment: this.paddleEnvironment, token: this.paddleClientToken, }; } } exports.SubscriptionService = SubscriptionService; //# sourceMappingURL=subscription.service.js.map