PhoenixKit.Modules.Billing (phoenix_kit v1.7.71)

Copy Markdown View Source

Main context for PhoenixKit Billing system.

Provides comprehensive billing functionality including currencies, billing profiles, orders, and invoices with manual bank transfer payments (Phase 1).

Features

  • Currencies: Multi-currency support with exchange rates
  • Billing Profiles: User billing information (individuals & companies)
  • Orders: Order management with line items and status tracking
  • Invoices: Invoice generation with receipt functionality
  • Bank Payments: Manual bank transfer workflow

System Enable/Disable

# Check if billing is enabled
PhoenixKit.Modules.Billing.enabled?()

# Enable/disable billing system
PhoenixKit.Modules.Billing.enable_system()
PhoenixKit.Modules.Billing.disable_system()

Order Workflow

# Create order
{:ok, order} = Billing.create_order(user, %{...})

# Confirm order
{:ok, order} = Billing.confirm_order(order)

# Generate invoice
{:ok, invoice} = Billing.create_invoice_from_order(order)

# Send invoice
{:ok, invoice} = Billing.send_invoice(invoice)

# Mark as paid (generates receipt)
{:ok, invoice} = Billing.mark_invoice_paid(invoice)

Summary

Functions

Returns list of available payment methods for manual recording. Bank transfer is always available, plus any enabled providers (Stripe/PayPal/Razorpay).

Calculates the total paid amount for an invoice from all transactions.

Calculates the current receipt status based on invoice state and transactions.

Returns a changeset for billing profile form.

Returns an order changeset for form building.

Returns a changeset for tracking payment option changes.

Confirms an order.

Creates a billing profile.

Creates a checkout session for paying an invoice.

Creates a currency.

Creates a standalone invoice (without order).

Creates an invoice from an order.

Creates an order from attributes (user_uuid included in attrs).

Creates an order for a user.

Creates a payment method record.

Creates a new payment option.

Creates a setup session for saving a payment method.

Creates a new subscription for a user.

Creates a subscription type.

Deletes a billing profile.

Deletes a currency.

Deletes an order (only drafts).

Deletes a payment option.

Deletes a subscription type.

Disables the billing system.

Enables the billing system.

Checks if the billing system is enabled.

Generates a receipt for an invoice.

Generates a unique transaction number.

Gets a billing profile by ID or UUID, returns nil if not found.

Gets a billing profile by ID or UUID, raises if not found.

Returns the current billing configuration.

Gets a currency by ID or UUID.

Gets a currency by ID or UUID, raises if not found.

Gets a currency by code.

Returns dashboard statistics.

Gets the default billing profile for a user.

Gets the default currency.

Gets the default payment method for a user.

Gets an invoice by ID or UUID with optional preloads.

Gets an invoice by ID or UUID.

Gets an invoice by invoice number.

Gets the remaining amount for an invoice.

Gets an order by ID or UUID with optional preloads.

Gets an order by ID or UUID.

Gets an order by order number.

Gets an order by UUID with optional preloads. Used for public-facing URLs to prevent ID enumeration.

Gets a payment method by ID or UUID.

Gets a payment option by ID.

Gets a payment option by code.

Gets a subscription by ID or UUID.

Gets a subscription by ID or UUID, raises if not found.

Gets a subscription type by ID or UUID.

Gets a subscription type by slug.

Gets a transaction by ID or UUID.

Gets a transaction by ID or UUID, raises if not found.

Gets a transaction by number.

Lists active payment options for checkout.

Lists billing profiles with optional filters.

Lists billing profiles with count for pagination.

Lists all currencies with optional filters.

Lists enabled currencies.

Gets transactions for a specific invoice.

Lists all invoices with optional filters.

Lists invoices for a specific order.

Lists invoices with count for pagination.

Lists all orders with optional filters.

Lists orders with count for pagination.

Lists saved payment methods for a user.

Lists all payment options.

Lists all subscription types.

Lists all subscriptions for a user.

Lists all transactions with optional filters.

Lists transactions with count for pagination.

Lists billing profiles for a user (shorthand).

Lists invoices for a specific user.

Lists orders for a specific user.

Lists all subscriptions for a specific user.

Marks an invoice as paid (generates receipt).

Marks an order as paid.

Marks an order as refunded.

Marks overdue invoices.

Pauses a subscription.

Checks if a payment option requires a billing profile.

Records a payment for an invoice.

Records a refund for an invoice.

Removes a payment method.

Resumes a paused subscription.

Sends a credit note email for a refund transaction.

Sends credit note email to the customer.

Sends an invoice (marks as sent and sends email).

Sends invoice email to the customer.

Sends a payment confirmation email for an individual payment transaction.

Sends payment confirmation email to the customer.

Sends receipt for a paid invoice.

Sends receipt email to the customer.

Sets a billing profile as default.

Sets a currency as default.

Sets a payment method as the default for a user.

Toggles the active status of a payment option.

Updates a billing profile.

Updates a currency.

Updates an invoice.

Updates an invoice's paid_amount based on its transactions.

Updates an order.

Updates the receipt status based on current invoice state. Call this after refunds to update the receipt status.

Updates a subscription type.

Functions

available_payment_methods()

Returns list of available payment methods for manual recording. Bank transfer is always available, plus any enabled providers (Stripe/PayPal/Razorpay).

Examples

iex> Billing.available_payment_methods()
["bank"]  # Only bank if no providers enabled

iex> Billing.available_payment_methods()
["bank", "stripe", "paypal"]  # Bank + enabled providers

calculate_invoice_paid_amount(invoice_uuid)

Calculates the total paid amount for an invoice from all transactions.

calculate_receipt_status(invoice, transactions \\ nil)

Calculates the current receipt status based on invoice state and transactions.

cancel_order(order, reason \\ nil)

Cancels an order.

cancel_subscription(subscription, opts \\ [])

Cancels a subscription.

Options

  • immediately: true - Cancel immediately instead of at period end

Examples

Billing.cancel_subscription(subscription)
Billing.cancel_subscription(subscription, immediately: true)

change_billing_profile(profile, attrs \\ %{})

Returns a changeset for billing profile form.

change_order(order, attrs \\ %{})

Returns an order changeset for form building.

change_payment_option(payment_option, attrs \\ %{})

Returns a changeset for tracking payment option changes.

change_subscription_type(subscription, new_type_uuid, opts \\ [])

Changes a subscription's type.

By default, the new type takes effect at the next billing cycle.

confirm_order(order)

Confirms an order.

create_billing_profile(user_or_uuid, attrs)

Creates a billing profile.

create_checkout_session(invoice, provider, opts \\ [])

Creates a checkout session for paying an invoice.

Returns the checkout URL to redirect the user to.

Parameters

  • invoice - The invoice to pay
  • provider - Payment provider atom (:stripe, :paypal, :razorpay)
  • opts - Options:
    • :success_url - URL to redirect after success
    • :cancel_url - URL to redirect if cancelled

Examples

{:ok, url} = Billing.create_checkout_session(invoice, :stripe, success_url: "/success")

create_currency(attrs)

Creates a currency.

create_invoice(user_or_uuid, attrs)

Creates a standalone invoice (without order).

create_invoice_from_order(order, opts \\ [])

Creates an invoice from an order.

create_order(attrs)

Creates an order from attributes (user_uuid included in attrs).

create_order(user_or_uuid, attrs)

Creates an order for a user.

create_payment_method(attrs)

Creates a payment method record.

Usually called after a successful setup session webhook.

create_payment_option(attrs)

Creates a new payment option.

create_setup_session(user_uuid, provider, opts \\ [])

Creates a setup session for saving a payment method.

Returns the setup URL to redirect the user to.

Parameters

  • user_uuid - The user saving the payment method
  • provider - Payment provider atom
  • opts - Options (success_url required)

create_subscription(user_uuid, attrs)

Creates a new subscription for a user.

This creates the master subscription record. The first payment should be processed separately via checkout session.

Parameters

  • user_uuid - The user creating the subscription (UUID)
  • attrs - Subscription attributes:
    • :subscription_type_uuid - Required: subscription type UUID
    • :billing_profile_uuid - Optional: billing profile UUID to use
    • :payment_method_uuid - Optional: saved payment method UUID for renewals
    • :trial_days - Optional: override type's trial days
    • :plan_uuid - Alternative: can use :plan_uuid instead of :subscription_type_uuid

Examples

Billing.create_subscription(user.uuid, %{subscription_type_uuid: type.uuid})
Billing.create_subscription(user.uuid, %{subscription_type_uuid: type.uuid, trial_days: 14})

# Using plan_uuid parameter
Billing.create_subscription(user.uuid, %{plan_uuid: type.uuid})

create_subscription_type(attrs)

Creates a subscription type.

delete_billing_profile(profile)

Deletes a billing profile.

delete_currency(currency)

Deletes a currency.

The default currency and currencies referenced by orders cannot be deleted.

delete_order(order)

Deletes an order (only drafts).

delete_payment_option(payment_option)

Deletes a payment option.

delete_subscription_type(type)

Deletes a subscription type.

Types with active subscriptions cannot be deleted.

disable_system()

Disables the billing system.

enable_system()

Enables the billing system.

enabled?()

Checks if the billing system is enabled.

generate_receipt(invoice)

Generates a receipt for an invoice.

Receipts can be generated:

  • When invoice is fully paid (status: "paid")
  • When invoice has any payment (paid_amount > 0) - partial receipt

Receipt status:

  • "paid" - fully paid
  • "partially_paid" - partial payment received
  • "refunded" - fully refunded after payment

generate_transaction_number()

Generates a unique transaction number.

get_billing_profile(id)

Gets a billing profile by ID or UUID, returns nil if not found.

get_billing_profile!(id)

Gets a billing profile by ID or UUID, raises if not found.

get_config()

Returns the current billing configuration.

get_currency(id)

Gets a currency by ID or UUID.

get_currency!(id)

Gets a currency by ID or UUID, raises if not found.

get_currency_by_code(code)

Gets a currency by code.

get_dashboard_stats()

Returns dashboard statistics.

get_default_billing_profile(user_uuid)

Gets the default billing profile for a user.

get_default_currency()

Gets the default currency.

get_default_payment_method(user_uuid)

Gets the default payment method for a user.

get_invoice(id, opts \\ [])

Gets an invoice by ID or UUID with optional preloads.

get_invoice!(id)

Gets an invoice by ID or UUID.

get_invoice_by_number(invoice_number)

Gets an invoice by invoice number.

get_invoice_remaining_amount(invoice)

Gets the remaining amount for an invoice.

get_order(id, opts \\ [])

Gets an order by ID or UUID with optional preloads.

get_order!(id)

Gets an order by ID or UUID.

get_order_by_number(order_number)

Gets an order by order number.

get_order_by_uuid(uuid, opts \\ [])

Gets an order by UUID with optional preloads. Used for public-facing URLs to prevent ID enumeration.

get_payment_method(id)

Gets a payment method by ID or UUID.

get_payment_option(uuid)

Gets a payment option by ID.

get_payment_option_by_code(code)

Gets a payment option by code.

get_subscription(id, opts \\ [])

Gets a subscription by ID or UUID.

Options

  • :preload - list of associations to preload (default: [])

get_subscription!(id)

Gets a subscription by ID or UUID, raises if not found.

get_subscription_type(id)

Gets a subscription type by ID or UUID.

get_subscription_type_by_slug(slug)

Gets a subscription type by slug.

get_transaction(id, opts \\ [])

Gets a transaction by ID or UUID.

get_transaction!(id, opts \\ [])

Gets a transaction by ID or UUID, raises if not found.

get_transaction_by_number(number)

Gets a transaction by number.

list_active_payment_options()

Lists active payment options for checkout.

list_billing_profiles(opts \\ [])

Lists billing profiles with optional filters.

Options

  • :user_uuid - Filter by user UUID
  • :type - Filter by type ("individual" or "company")
  • :search - Search in name/email/company fields
  • :page - Page number
  • :per_page - Items per page
  • :preload - Associations to preload

list_billing_profiles_with_count(opts \\ [])

Lists billing profiles with count for pagination.

list_currencies(opts \\ [])

Lists all currencies with optional filters.

Options

  • :enabled - Filter by enabled status
  • :order_by - Custom ordering

list_enabled_currencies()

Lists enabled currencies.

list_invoice_transactions(invoice_uuid)

Gets transactions for a specific invoice.

list_invoices(filters \\ %{})

Lists all invoices with optional filters.

list_invoices_for_order(order_uuid)

Lists invoices for a specific order.

list_invoices_with_count(opts \\ [])

Lists invoices with count for pagination.

list_orders(filters \\ %{})

Lists all orders with optional filters.

list_orders_with_count(opts \\ [])

Lists orders with count for pagination.

list_payment_methods(user_uuid, opts \\ [])

Lists saved payment methods for a user.

list_payment_options()

Lists all payment options.

list_subscription_types(opts \\ [])

Lists all subscription types.

Options

  • :active_only - Only return active types (default: true)

list_subscriptions(opts \\ [])

Lists all subscriptions for a user.

Options

  • :status - Filter by status (e.g., "active", "cancelled")
  • :preload - Associations to preload (default: [:subscription_type])

Examples

Billing.list_subscriptions(user_uuid)
Billing.list_subscriptions(user_uuid, status: "active")

list_transactions(opts \\ [])

Lists all transactions with optional filters.

Options

  • :invoice_uuid - Filter by invoice UUID
  • :user_uuid - Filter by user who created the transaction
  • :payment_method - Filter by payment method
  • :type - Filter by type: "payment" (amount > 0) or "refund" (amount < 0)
  • :search - Search by transaction number
  • :limit - Limit results
  • :offset - Offset for pagination
  • :preload - Associations to preload

Examples

Billing.list_transactions(invoice_uuid: "some-uuid")
Billing.list_transactions(type: "payment", limit: 10)

list_transactions_with_count(opts \\ [])

Lists transactions with count for pagination.

list_user_billing_profiles(user_uuid)

Lists billing profiles for a user (shorthand).

list_user_invoices(user_uuid, filters \\ %{})

Lists invoices for a specific user.

list_user_orders(user_uuid, filters \\ %{})

Lists orders for a specific user.

list_user_subscriptions(user_uuid, opts \\ [])

Lists all subscriptions for a specific user.

Options

  • :status - filter by status (e.g., "active", "cancelled")
  • :preload - list of associations to preload (default: [:subscription_type])

Examples

Billing.list_user_subscriptions(user.uuid)
Billing.list_user_subscriptions(user.uuid, status: "active")

mark_invoice_paid(invoice)

Marks an invoice as paid (generates receipt).

mark_order_paid(order, opts \\ [])

Marks an order as paid.

Options

  • :payment_method - The payment method used (e.g., "bank", "stripe", "paypal")

mark_order_refunded(order)

Marks an order as refunded.

mark_overdue_invoices()

Marks overdue invoices.

pause_subscription(subscription)

Pauses a subscription.

Paused subscriptions don't renew until resumed.

payment_option_requires_billing?(arg1)

Checks if a payment option requires a billing profile.

record_payment(invoice, attrs, admin_user)

Records a payment for an invoice.

Creates a transaction with positive amount and updates invoice's paid_amount. If paid_amount >= total, marks invoice as paid and generates receipt.

Parameters

  • invoice - The invoice to pay
  • attrs - Transaction attributes including :amount, :payment_method, :description
  • admin_user - The admin user recording the payment

Examples

{:ok, transaction} = Billing.record_payment(invoice, %{amount: "100.00", payment_method: "bank"}, admin)

record_refund(invoice, attrs, admin_user)

Records a refund for an invoice.

Creates a transaction with negative amount and updates invoice's paid_amount.

Parameters

  • invoice - The invoice to refund
  • attrs - Transaction attributes including :amount (positive value), :description (reason)
  • admin_user - The admin user recording the refund

Examples

{:ok, transaction} = Billing.record_refund(invoice, %{amount: "50.00", description: "Partial refund"}, admin)

remove_payment_method(payment_method)

Removes a payment method.

Marks as removed in database. Should also delete from provider.

resume_subscription(subscription)

Resumes a paused subscription.

send_credit_note(invoice, transaction, opts \\ [])

Sends a credit note email for a refund transaction.

Parameters

  • invoice - The invoice associated with the refund
  • transaction - The refund transaction
  • opts - Options:
    • :to_email - Override recipient email
    • :credit_note_url - URL to view credit note online

Examples

{:ok, invoice} = Billing.send_credit_note(invoice, transaction, credit_note_url: "https://...")

send_credit_note_email(invoice, transaction, opts \\ [])

Sends credit note email to the customer.

send_invoice(invoice, opts \\ [])

Sends an invoice (marks as sent and sends email).

Options:

  • :send_email - Whether to send email (default: true)
  • :invoice_url - URL to view invoice online (optional)

send_invoice_email(invoice, opts \\ [])

Sends invoice email to the customer.

send_payment_confirmation(invoice, transaction, opts \\ [])

Sends a payment confirmation email for an individual payment transaction.

Parameters

  • invoice - The invoice associated with the payment
  • transaction - The payment transaction
  • opts - Options including:
    • :to_email - Override recipient email address
    • :payment_url - URL to view payment confirmation online
    • :send_email - Whether to send email (default: true)

send_payment_confirmation_email(invoice, transaction, opts \\ [])

Sends payment confirmation email to the customer.

send_receipt(invoice, opts \\ [])

Sends receipt for a paid invoice.

Options:

  • :send_email - Whether to send email (default: true)
  • :to_email - Override recipient email address
  • :receipt_url - URL to view receipt online (optional)

send_receipt_email(invoice, opts \\ [])

Sends receipt email to the customer.

set_default_billing_profile(profile)

Sets a billing profile as default.

set_default_currency(currency)

Sets a currency as default.

set_default_payment_method(payment_method)

Sets a payment method as the default for a user.

Unsets any existing default.

toggle_payment_option_active(payment_option)

Toggles the active status of a payment option.

update_billing_profile(profile, attrs)

Updates a billing profile.

update_currency(currency, attrs)

Updates a currency.

update_invoice(invoice, attrs)

Updates an invoice.

update_invoice_paid_amount(invoice)

Updates an invoice's paid_amount based on its transactions.

update_order(order, attrs)

Updates an order.

update_payment_option(payment_option, attrs)

Updates a payment option.

update_receipt_status(invoice)

Updates the receipt status based on current invoice state. Call this after refunds to update the receipt status.

update_subscription_type(type, attrs)

Updates a subscription type.

void_invoice(invoice, reason \\ nil)

Voids an invoice.