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

Oban worker for processing subscription renewals.

This worker runs daily and handles:
- Finding subscriptions due for renewal (within 24 hours of period end)
- Creating invoices for the renewal
- Charging saved payment methods via providers
- Updating subscription periods on success
- Moving to past_due status on failure

## Scheduling

The worker should be scheduled to run daily via Oban crontab:

```elixir
config :my_app, Oban,
  queues: [default: 10, billing: 5],
  plugins: [
    {Oban.Plugins.Cron,
     crontab: [
       {"0 6 * * *", PhoenixKitBilling.Workers.SubscriptionRenewalWorker}
     ]}
  ]
```

## Process Flow

1. Query subscriptions where `current_period_end` is within 24 hours
2. For each subscription:
   a. Skip if cancel_at_period_end is true
   b. Create renewal invoice
   c. Charge saved payment method
   d. On success: extend period_end, update invoice as paid
   e. On failure: set past_due, schedule dunning

## Manual Trigger

Can be triggered manually for a specific subscription:

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

---

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