MPP.Client.MultiProvider (mpp v0.6.0)

Copy Markdown View Source

Wraps multiple payment providers and dispatches to the first match.

MultiProvider holds a list of {module, config} tuples, each implementing MPP.Client.PaymentProvider. When pay/2 is called, it iterates through providers and delegates to the first whose supports?/3 returns true.

Usage

provider = MPP.Client.MultiProvider.new([
  {MyApp.TempoProvider, %{rpc_url: "https://rpc.tempo.xyz", private_key: key}},
  {MyApp.StripeProvider, %{api_key: "sk_test_..."}}
])

# Dispatches to TempoProvider for tempo challenges, StripeProvider for stripe
{:ok, credential} = MPP.Client.MultiProvider.pay(provider, challenge)

Design

Providers are tried in order — the first that supports the challenge's method+intent wins. This means provider ordering matters when multiple providers support the same method+intent (first wins).

API Functions

FunctionArityDescriptionParam Kinds
pay2Execute payment by dispatching to the first matching provider.multi: value, challenge: value
supports?3Check if any provider in this MultiProvider supports the method+intent.multi: value, method: value, intent: value
add3Add a provider to an existing MultiProvider.multi: value, module: value, config: value
new1Create a MultiProvider from a list of {module, config} tuples.providers: value

Summary

Functions

Add a provider to an existing MultiProvider.

Create a MultiProvider from a list of {module, config} tuples.

Execute payment by dispatching to the first matching provider.

Check if any provider in this MultiProvider supports the method+intent.

Types

provider_entry()

@type provider_entry() :: {module(), map()}

t()

@type t() :: %MPP.Client.MultiProvider{providers: [provider_entry()]}

Functions

add(multi, module, config)

@spec add(t(), module(), map()) :: t()

Add a provider to an existing MultiProvider.

New providers are appended to the end of the list (lower priority than existing).

new(providers \\ [])

@spec new([provider_entry()]) :: t()

Create a MultiProvider from a list of {module, config} tuples.

Each module must implement the MPP.Client.PaymentProvider behaviour.

pay(multi_provider, challenge)

@spec pay(t(), MPP.Challenge.t()) :: {:ok, MPP.Credential.t()} | {:error, term()}

Execute payment by dispatching to the first matching provider.

Iterates through providers in order, delegating to the first whose supports?/3 returns true for the challenge's method and intent.

Returns {:error, :unsupported_payment_method} if no provider matches.

supports?(multi_provider, method, intent)

@spec supports?(t(), String.t(), String.t()) :: boolean()

Check if any provider in this MultiProvider supports the method+intent.