File size: 14,871 Bytes
7856e60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use crate::domain::error::{AppError, AppResult};
use crate::domain::models::{
    CreatePlanRequest, CreateSubscriptionRequest, Subscription, SubscriptionBillingEvent,
    SubscriptionPlan, SubscriptionSummary,
};
use crate::infrastructure::db::DbPool;
use chrono::Utc;
use uuid::Uuid;

pub struct SubscriptionService {
    pool: DbPool,
}

impl SubscriptionService {
    pub fn new(pool: DbPool) -> Self {
        Self { pool }
    }

    // ─── Plans ────────────────────────────────────────────────────────────────

    pub async fn create_plan(
        &self,
        merchant_id: &str,
        req: CreatePlanRequest,
    ) -> AppResult<SubscriptionPlan> {
        if req.price_inr <= 0.0 {
            return Err(AppError::Validation("Price must be positive".into()));
        }
        if req.interval_days <= 0 {
            return Err(AppError::Validation("interval_days must be >= 1".into()));
        }

        let id = Uuid::new_v4().to_string();
        let trial_days = req.trial_days.unwrap_or(0);

        let plan = sqlx::query_as::<_, SubscriptionPlan>(
            r#"INSERT INTO subscription_plans
               (id, merchant_id, name, description, price_inr, interval_days, trial_days)
               VALUES ($1, $2, $3, $4, $5, $6, $7)
               RETURNING *"#,
        )
        .bind(&id)
        .bind(merchant_id)
        .bind(&req.name)
        .bind(&req.description)
        .bind(req.price_inr)
        .bind(req.interval_days)
        .bind(trial_days)
        .fetch_one(&self.pool)
        .await
        .map_err(AppError::Database)?;

        tracing::info!(merchant_id, plan_id = %id, "Created subscription plan: {}", req.name);
        Ok(plan)
    }

    pub async fn list_plans(&self, merchant_id: &str) -> AppResult<Vec<SubscriptionPlan>> {
        sqlx::query_as::<_, SubscriptionPlan>(
            "SELECT * FROM subscription_plans WHERE merchant_id = $1 AND is_active = TRUE ORDER BY created_at DESC",
        )
        .bind(merchant_id)
        .fetch_all(&self.pool)
        .await
        .map_err(AppError::Database)
    }

    pub async fn deactivate_plan(&self, merchant_id: &str, plan_id: &str) -> AppResult<()> {
        let affected = sqlx::query(
            "UPDATE subscription_plans SET is_active = FALSE, updated_at = NOW() WHERE id = $1 AND merchant_id = $2",
        )
        .bind(plan_id)
        .bind(merchant_id)
        .execute(&self.pool)
        .await
        .map_err(AppError::Database)?
        .rows_affected();

        if affected == 0 {
            return Err(AppError::NotFound("Plan not found".into()));
        }
        Ok(())
    }

    // ─── Subscriptions ───────────────────────────────────────────────────────

    pub async fn create_subscription(
        &self,
        merchant_id: &str,
        req: CreateSubscriptionRequest,
    ) -> AppResult<Subscription> {
        // Verify plan belongs to merchant and is active
        let plan = sqlx::query_as::<_, SubscriptionPlan>(
            "SELECT * FROM subscription_plans WHERE id = $1 AND merchant_id = $2 AND is_active = TRUE",
        )
        .bind(&req.plan_id)
        .bind(merchant_id)
        .fetch_optional(&self.pool)
        .await
        .map_err(AppError::Database)?
        .ok_or_else(|| AppError::NotFound("Plan not found or inactive".into()))?;

        let id = Uuid::new_v4().to_string();
        let now = Utc::now();
        let trial_end = now + chrono::Duration::days(plan.trial_days as i64);
        let period_end = if plan.trial_days > 0 {
            trial_end
        } else {
            now + chrono::Duration::days(plan.interval_days as i64)
        };
        let initial_status = if plan.trial_days > 0 {
            "TRIAL"
        } else {
            "ACTIVE"
        };

        let sub = sqlx::query_as::<_, Subscription>(
            r#"INSERT INTO subscriptions
               (id, merchant_id, plan_id, subscriber_email, subscriber_phone, subscriber_name,
                status, current_period_start, current_period_end, metadata)
               VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
               RETURNING *"#,
        )
        .bind(&id)
        .bind(merchant_id)
        .bind(&req.plan_id)
        .bind(&req.subscriber_email)
        .bind(&req.subscriber_phone)
        .bind(&req.subscriber_name)
        .bind(initial_status)
        .bind(now)
        .bind(period_end)
        .bind(&req.metadata)
        .fetch_one(&self.pool)
        .await
        .map_err(AppError::Database)?;

        tracing::info!(
            merchant_id, subscription_id = %id,
            "New subscription: {} -> plan {}", req.subscriber_email, plan.name
        );
        Ok(sub)
    }

    pub async fn list_subscriptions(
        &self,
        merchant_id: &str,
        status_filter: Option<&str>,
    ) -> AppResult<Vec<Subscription>> {
        let subs = if let Some(status) = status_filter {
            sqlx::query_as::<_, Subscription>(
                "SELECT * FROM subscriptions WHERE merchant_id = $1 AND status = $2 ORDER BY created_at DESC LIMIT 100",
            )
            .bind(merchant_id)
            .bind(status)
            .fetch_all(&self.pool)
            .await
        } else {
            sqlx::query_as::<_, Subscription>(
                "SELECT * FROM subscriptions WHERE merchant_id = $1 ORDER BY created_at DESC LIMIT 100",
            )
            .bind(merchant_id)
            .fetch_all(&self.pool)
            .await
        }
        .map_err(AppError::Database)?;

        Ok(subs)
    }

    pub async fn cancel_subscription(
        &self,
        merchant_id: &str,
        subscription_id: &str,
        reason: Option<String>,
    ) -> AppResult<Subscription> {
        let sub = sqlx::query_as::<_, Subscription>(
            r#"UPDATE subscriptions
               SET status = 'CANCELLED', cancelled_at = NOW(), cancel_reason = $3, updated_at = NOW()
               WHERE id = $1 AND merchant_id = $2 AND status IN ('ACTIVE', 'TRIAL', 'PAST_DUE')
               RETURNING *"#,
        )
        .bind(subscription_id)
        .bind(merchant_id)
        .bind(&reason)
        .fetch_optional(&self.pool)
        .await
        .map_err(AppError::Database)?
        .ok_or_else(|| AppError::NotFound("Subscription not found or already cancelled".into()))?;

        tracing::info!(merchant_id, subscription_id, "Subscription cancelled");
        Ok(sub)
    }

    pub async fn get_summary(&self, merchant_id: &str) -> AppResult<SubscriptionSummary> {
        use sqlx::Row;

        let counts = sqlx::query(
            r#"SELECT
                COUNT(*) FILTER (WHERE status = 'ACTIVE') as active,
                COUNT(*) FILTER (WHERE status = 'TRIAL') as trial,
                COUNT(*) FILTER (WHERE status = 'CANCELLED') as cancelled,
                COUNT(*) FILTER (WHERE status = 'PAST_DUE') as past_due
               FROM subscriptions WHERE merchant_id = $1"#,
        )
        .bind(merchant_id)
        .fetch_one(&self.pool)
        .await
        .map_err(AppError::Database)?;

        // MRR = sum of monthly-normalised active subscription prices
        let mrr: f64 = sqlx::query_scalar(
            r#"SELECT COALESCE(SUM(p.price_inr * (30.0 / p.interval_days)), 0.0)
               FROM subscriptions s
               JOIN subscription_plans p ON s.plan_id = p.id
               WHERE s.merchant_id = $1 AND s.status = 'ACTIVE'"#,
        )
        .bind(merchant_id)
        .fetch_one(&self.pool)
        .await
        .map_err(AppError::Database)?;

        Ok(SubscriptionSummary {
            active_count: counts.get("active"),
            trial_count: counts.get("trial"),
            cancelled_count: counts.get("cancelled"),
            past_due_count: counts.get("past_due"),
            mrr_inr: mrr,
            arr_inr: mrr * 12.0,
        })
    }

    pub async fn get_billing_history(
        &self,
        merchant_id: &str,
        subscription_id: &str,
    ) -> AppResult<Vec<SubscriptionBillingEvent>> {
        sqlx::query_as::<_, SubscriptionBillingEvent>(
            r#"SELECT * FROM subscription_billing_events
               WHERE merchant_id = $1 AND subscription_id = $2
               ORDER BY billed_at DESC LIMIT 50"#,
        )
        .bind(merchant_id)
        .bind(subscription_id)
        .fetch_all(&self.pool)
        .await
        .map_err(AppError::Database)
    }

    /// Background engine: find ACTIVE subscriptions whose billing period has expired
    /// and mark them PAST_DUE, then record a billing event. In production this
    /// would be wired to a UPI AutoPay or payment gateway trigger.
    ///
    /// # Hardening
    /// - **Advisory lock**: Uses `pg_try_advisory_lock` keyed on the current billing
    ///   hour so only one server replica executes the cycle per hour. Returns 0 if
    ///   another instance has the lock.
    /// - **Audit trail**: Inserts a `SubscriptionBillingEvent` row for every
    ///   transition — `BILLING_FAILED` for ACTIVE→PAST_DUE and `TRIAL_ENDED` for
    ///   TRIAL→ACTIVE — giving merchants a complete billing history.
    pub async fn run_billing_cycle(&self) -> AppResult<u64> {
        // ─── Advisory Lock — idempotency across replicas ──────────────────────
        // Key: hash of "billing_cycle" + current UTC hour. This ensures only one
        // replica runs the cycle per hour even on multi-instance deployments.
        let lock_acquired: bool = sqlx::query_scalar(
            "SELECT pg_try_advisory_lock(hashtext('billing_cycle_' || to_char(NOW() AT TIME ZONE 'UTC', 'YYYY-MM-DD-HH24')))"
        )
        .fetch_one(&self.pool)
        .await
        .map_err(AppError::Database)?;

        if !lock_acquired {
            tracing::debug!(
                "Billing cycle: advisory lock held by another instance — skipping this tick"
            );
            return Ok(0);
        }

        tracing::info!("Billing cycle: advisory lock acquired — running cycle");

        // ─── Step 1: ACTIVE → PAST_DUE ───────────────────────────────────────
        // Fetch affected rows BEFORE the bulk UPDATE so we can write billing events.
        let past_due_candidates: Vec<(String, String, f64)> = sqlx::query_as(
            r#"SELECT s.id, s.merchant_id, p.price_inr
               FROM subscriptions s
               JOIN subscription_plans p ON s.plan_id = p.id
               WHERE s.status = 'ACTIVE' AND s.current_period_end < NOW()"#,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(AppError::Database)?;

        let past_due_count = past_due_candidates.len() as u64;

        if past_due_count > 0 {
            // Bulk transition
            sqlx::query(
                "UPDATE subscriptions SET status = 'PAST_DUE', updated_at = NOW() WHERE status = 'ACTIVE' AND current_period_end < NOW()"
            )
            .execute(&self.pool)
            .await
            .map_err(AppError::Database)?;

            // Write one BILLING_FAILED event per affected subscription
            for (sub_id, merchant_id, price_inr) in &past_due_candidates {
                let event_id = Uuid::new_v4().to_string();
                if let Err(e) = sqlx::query(
                    r#"INSERT INTO subscription_billing_events
                       (id, subscription_id, merchant_id, amount_inr, status, failure_reason)
                       VALUES ($1, $2, $3, $4, 'FAILED', 'Billing period expired — subscription moved to PAST_DUE')"#,
                )
                .bind(&event_id)
                .bind(sub_id)
                .bind(merchant_id)
                .bind(price_inr)
                .execute(&self.pool)
                .await
                {
                    tracing::error!(
                        subscription_id = %sub_id,
                        "Billing cycle: failed to record BILLING_FAILED event: {}", e
                    );
                }
            }

            tracing::warn!(
                "Billing cycle: {} subscriptions moved to PAST_DUE — billing events recorded",
                past_due_count
            );
        }

        // ─── Step 2: TRIAL → ACTIVE ───────────────────────────────────────────
        // Fetch candidates before the transition to record TRIAL_ENDED events.
        let trial_ended_candidates: Vec<(String, String, f64)> = sqlx::query_as(
            r#"SELECT s.id, s.merchant_id, p.price_inr
               FROM subscriptions s
               JOIN subscription_plans p ON s.plan_id = p.id
               WHERE s.status = 'TRIAL' AND s.current_period_end < NOW()"#,
        )
        .fetch_all(&self.pool)
        .await
        .map_err(AppError::Database)?;

        if !trial_ended_candidates.is_empty() {
            sqlx::query(
                r#"UPDATE subscriptions
                   SET status = 'ACTIVE',
                       current_period_start = NOW(),
                       current_period_end = NOW() + (SELECT interval_days * INTERVAL '1 day' FROM subscription_plans WHERE id = plan_id),
                       updated_at = NOW()
                   WHERE status = 'TRIAL' AND current_period_end < NOW()"#,
            )
            .execute(&self.pool)
            .await
            .map_err(AppError::Database)?;

            for (sub_id, merchant_id, price_inr) in &trial_ended_candidates {
                let event_id = Uuid::new_v4().to_string();
                if let Err(e) = sqlx::query(
                    r#"INSERT INTO subscription_billing_events
                       (id, subscription_id, merchant_id, amount_inr, status, failure_reason)
                       VALUES ($1, $2, $3, $4, 'PENDING', 'Trial period ended — first billing cycle initiated')"#,
                )
                .bind(&event_id)
                .bind(sub_id)
                .bind(merchant_id)
                .bind(price_inr)
                .execute(&self.pool)
                .await
                {
                    tracing::error!(
                        subscription_id = %sub_id,
                        "Billing cycle: failed to record TRIAL_ENDED event: {}", e
                    );
                }
            }

            tracing::info!(
                "Billing cycle: {} trials graduated to ACTIVE — TRIAL_ENDED events recorded",
                trial_ended_candidates.len()
            );
        }

        Ok(past_due_count)
    }
}