File size: 5,196 Bytes
b5e5eac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { Request, Response } from 'express';
import { AppDataSource } from '../config/database';
import { Agent, AgentStatus } from '../entities/Agent';
import { Subscription, SubscriptionStatus } from '../entities/Subscription';
import { User } from '../entities/User';
import crypto from 'crypto';

const agentRepository = AppDataSource.getRepository(Agent);
const subscriptionRepository = AppDataSource.getRepository(Subscription);
const userRepository = AppDataSource.getRepository(User);

export class SubscriptionController {
  /**
   * Create subscription/purchase agent access
   */
  async createSubscription(req: Request, res: Response) {
    try {
      const { agentId } = req.params;
      const userId = (req as any).user.id;

      // Get agent
      const agent = await agentRepository.findOne({
        where: { id: agentId, status: AgentStatus.APPROVED },
      });

      if (!agent) {
        return res.status(404).json({ error: 'Agent not found or not approved' });
      }

      // Check if user already has active subscription
      const existing = await subscriptionRepository.findOne({
        where: {
          userId,
          agentId,
          status: SubscriptionStatus.ACTIVE,
        },
      });

      if (existing) {
        return res.status(400).json({
          error: 'You already have an active subscription to this agent',
        });
      }

      // Calculate expiry
      const expiresAt = agent.isSubscription
        ? new Date(
            Date.now() + (agent.subscriptionDuration || 30) * 24 * 60 * 60 * 1000
          )
        : new Date('2099-12-31'); // One-time purchase = lifetime (or very long)

      // Create subscription (payment verification happens via webhook)
      const subscription = subscriptionRepository.create({
        userId,
        agentId,
        expiresAt,
        status: SubscriptionStatus.ACTIVE,
        isPaymentVerified: false, // Will be verified via webhook
      });

      const savedSubscription = await subscriptionRepository.save(subscription);

      // Generate payment reference (for Paystack)
      const paymentReference = `sub_${savedSubscription.id}_${Date.now()}`;
      savedSubscription.paymentReference = paymentReference;
      await subscriptionRepository.save(savedSubscription);

      res.status(201).json({
        subscription: savedSubscription,
        payment: {
          reference: paymentReference,
          amount: agent.price,
          currency: 'NGN', // Adjust based on your setup
        },
      });
    } catch (error) {
      console.error('Create subscription error:', error);
      res.status(500).json({ error: 'Failed to create subscription' });
    }
  }

  /**
   * Get user's subscriptions
   */
  async getMySubscriptions(req: Request, res: Response) {
    try {
      const userId = (req as any).user.id;
      const subscriptions = await subscriptionRepository.find({
        where: { userId },
        relations: ['agent'],
        order: { createdAt: 'DESC' },
      });

      res.json(subscriptions);
    } catch (error) {
      console.error('Get subscriptions error:', error);
      res.status(500).json({ error: 'Failed to get subscriptions' });
    }
  }

  /**
   * Cancel subscription
   */
  async cancelSubscription(req: Request, res: Response) {
    try {
      const { id } = req.params;
      const userId = (req as any).user.id;

      const subscription = await subscriptionRepository.findOne({
        where: { id, userId },
      });

      if (!subscription) {
        return res.status(404).json({ error: 'Subscription not found' });
      }

      subscription.status = SubscriptionStatus.CANCELLED;
      await subscriptionRepository.save(subscription);

      res.json({ message: 'Subscription cancelled', subscription });
    } catch (error) {
      console.error('Cancel subscription error:', error);
      res.status(500).json({ error: 'Failed to cancel subscription' });
    }
  }

  /**
   * Verify payment via Paystack webhook
   */
  async verifyPayment(req: Request, res: Response) {
    try {
      const secret = process.env.PAYSTACK_SECRET;
      if (!secret) {
        throw new Error('Paystack secret not configured');
      }

      const hash = crypto
        .createHmac('sha512', secret)
        .update(JSON.stringify(req.body))
        .digest('hex');

      if (hash !== req.headers['x-paystack-signature']) {
        return res.status(401).json({ error: 'Invalid signature' });
      }

      const event = req.body;

      if (event.event === 'charge.success') {
        const { reference, metadata } = event.data;

        // Find subscription by payment reference
        const subscription = await subscriptionRepository.findOne({
          where: { paymentReference: reference },
        });

        if (subscription) {
          subscription.isPaymentVerified = true;
          subscription.status = SubscriptionStatus.ACTIVE;
          await subscriptionRepository.save(subscription);
        }
      }

      res.json({ received: true });
    } catch (error) {
      console.error('Payment verification error:', error);
      res.status(500).json({ error: 'Payment verification failed' });
    }
  }
}