PhoenixKit.Modules.Billing.Workers.SubscriptionRenewalWorker (phoenix_kit v1.7.71)

Copy Markdown View Source

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:

config :my_app, Oban,
  queues: [default: 10, billing: 5],
  plugins: [
    {Oban.Plugins.Cron,
     crontab: [
       {"0 6 * * *", PhoenixKit.Modules.Billing.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:

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