# `PhoenixKitBilling.Workers.SubscriptionDunningWorker`
[🔗](https://github.com/BeamLabEU/phoenix_kit_billing/blob/0.5.1/lib/phoenix_kit_billing/workers/subscription_dunning_worker.ex#L1)

Oban worker for dunning (failed payment recovery).

When a subscription payment fails, the subscription enters `past_due` status
and this worker handles retry attempts during the grace period.

## Dunning Process

1. Initial payment fails → subscription status = `past_due`
2. Grace period starts (configurable, default 3 days)
3. This worker retries payment at intervals
4. If payment succeeds → status = `active`
5. If max attempts reached or grace period ends → status = `cancelled`

## Retry Schedule

Default retry schedule (can be configured):
- Attempt 1: Immediate (handled by RenewalWorker)
- Attempt 2: 24 hours later
- Attempt 3: 48 hours later (2 days)
- Attempt 4: 72 hours later (3 days, grace period ends)

## Configuration

```elixir
# Settings (stored in database)
billing_subscription_grace_days: 3
billing_dunning_max_attempts: 3
```

## Manual Trigger

```elixir
%{subscription_uuid: "019145a1-0000-7000-8000-000000000001"}
|> SubscriptionDunningWorker.new()
|> Oban.insert()
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
