Provider registry and helper functions for payment providers.
This module serves as the central point for working with payment providers. It handles provider lookup, availability checking, and configuration.
Available Providers
:stripe- Stripe payments (cards, wallets):paypal- PayPal payments:razorpay- Razorpay payments (India)
Usage
# Get a provider module
provider = Providers.get_provider(:stripe)
provider.create_checkout_session(invoice, opts)
# List available providers
Providers.list_available_providers()
#=> [:stripe, :paypal]
# Check if provider is available
Providers.provider_enabled?(:stripe)
#=> true
Summary
Functions
Returns a list of all provider names.
Charges a saved payment method using the appropriate provider.
Creates a checkout session using the specified provider.
Creates a refund using the appropriate provider.
Creates a setup session using the specified provider.
Gets the setting key for a provider's enabled status.
Returns the provider module for the given provider name.
Handles a webhook event for the specified provider.
Returns a list of available (enabled and configured) provider names.
Checks if a provider is enabled and available.
Checks if a provider exists (regardless of availability).
Returns display information for a provider.
Checks if a provider is enabled in settings.
Verifies a webhook signature for the specified provider.
Functions
@spec all_providers() :: [atom()]
Returns a list of all provider names.
Examples
iex> Providers.all_providers()
[:stripe, :paypal, :razorpay]
@spec charge_payment_method(map(), Decimal.t(), keyword()) :: {:ok, PhoenixKit.Modules.Billing.Providers.Provider.charge_result()} | {:error, term()}
Charges a saved payment method using the appropriate provider.
Parameters
payment_method- Saved payment method record (must include :provider)amount- Amount to chargeopts- Options passed to provider
Returns
{:ok, charge_result}- Charge successful{:error, reason}- Charge failed
@spec create_checkout_session(atom() | String.t(), map(), keyword()) :: {:ok, PhoenixKit.Modules.Billing.Providers.Provider.checkout_session()} | {:error, term()}
Creates a checkout session using the specified provider.
Convenience function that looks up the provider and calls
create_checkout_session/2.
Parameters
provider- Provider nameinvoice- Invoice to payopts- Options passed to provider
Returns
{:ok, checkout_session}- Session created{:error, :provider_not_found}- Provider doesn't exist{:error, :provider_not_available}- Provider not configured{:error, reason}- Provider-specific error
@spec create_refund(atom() | String.t(), String.t(), Decimal.t() | nil, keyword()) :: {:ok, PhoenixKit.Modules.Billing.Providers.Provider.refund_result()} | {:error, term()}
Creates a refund using the appropriate provider.
Parameters
provider- Provider nameprovider_transaction_id- Provider's transaction IDamount- Amount to refund (nil for full refund)opts- Options
Returns
{:ok, refund_result}- Refund created{:error, reason}- Refund failed
@spec create_setup_session(atom() | String.t(), map(), keyword()) :: {:ok, PhoenixKit.Modules.Billing.Providers.Provider.setup_session()} | {:error, term()}
Creates a setup session using the specified provider.
Parameters
provider- Provider nameuser- User to save payment method foropts- Options passed to provider
Returns
{:ok, setup_session}- Session created{:error, reason}- Failed
Gets the setting key for a provider's enabled status.
Examples
iex> Providers.enabled_setting_key(:stripe)
"billing_stripe_enabled"
Returns the provider module for the given provider name.
Parameters
name- Provider name as atom or string
Returns
- Provider module if found
nilif provider not found
Examples
iex> Providers.get_provider(:stripe)
PhoenixKit.Modules.Billing.Providers.Stripe
iex> Providers.get_provider("paypal")
PhoenixKit.Modules.Billing.Providers.PayPal
iex> Providers.get_provider(:unknown)
nil
@spec handle_webhook_event(atom() | String.t(), map()) :: {:ok, PhoenixKit.Modules.Billing.Providers.Provider.webhook_event()} | {:error, term()}
Handles a webhook event for the specified provider.
Parameters
provider- Provider namepayload- Decoded JSON payload
Returns
{:ok, webhook_event}- Event parsed{:error, reason}- Failed to parse
@spec list_available_providers() :: [atom()]
Returns a list of available (enabled and configured) provider names.
Checks each provider's available?/0 callback to determine availability.
Examples
iex> Providers.list_available_providers()
[:stripe, :paypal]
Checks if a provider is enabled and available.
Parameters
name- Provider name as atom or string
Returns
trueif provider is availablefalseif provider is not available or not found
Examples
iex> Providers.provider_enabled?(:stripe)
true
iex> Providers.provider_enabled?(:unknown)
false
Checks if a provider exists (regardless of availability).
Examples
iex> Providers.provider_exists?(:stripe)
true
iex> Providers.provider_exists?(:bitcoin)
false
@spec provider_info(atom()) :: PhoenixKit.Modules.Billing.Providers.Types.ProviderInfo.t()
Returns display information for a provider.
Examples
iex> Providers.provider_info(:stripe)
%{name: "Stripe", icon: "stripe", color: "#635BFF"}
Checks if a provider is enabled in settings.
This is a lower-level check that only looks at the setting, not whether the provider is fully configured.
Examples
iex> Providers.setting_enabled?(:stripe)
true
@spec verify_webhook_signature(atom() | String.t(), binary(), String.t(), String.t()) :: :ok | {:error, term()}
Verifies a webhook signature for the specified provider.
Parameters
provider- Provider namepayload- Raw request bodysignature- Signature from headerssecret- Webhook secret
Returns
:ok- Signature valid{:error, :invalid_signature}- Signature invalid{:error, :provider_not_found}- Provider doesn't exist