GoCardlessClient (GoCardlessClient v2.0.0)

Copy Markdown View Source

Production-ready Elixir client for the GoCardlessClient API.

Quick Start

# 1. Configure in config/config.exs
config :gocardless_client,
  access_token: System.get_env("GOCARDLESS_ACCESS_TOKEN"),
  environment: :sandbox   # or :live

# 2. Build a client
client = GoCardlessClient.client!()

# 3. Call the API
{:ok, customer} = GoCardlessClient.Resources.Customers.create(client, %{
  email: "alice@example.com",
  given_name: "Alice",
  family_name: "Smith",
  country_code: "GB"
})

{:ok, mandate} = GoCardlessClient.Resources.Mandates.create(client, %{
  scheme: "bacs",
  links: %{customer_bank_account: bank_account_id}
})

{:ok, payment} = GoCardlessClient.Resources.Payments.create(client, %{
  amount: 1500,
  currency: "GBP",
  description: "Monthly fee",
  links: %{mandate: mandate_id}
})

Runtime Clients

client = GoCardlessClient.client!(access_token: "tok", environment: :live)

# Per-merchant token (OAuth partner apps)
client = GoCardlessClient.client!(access_token: merchant_token)

Pagination

# Stream lazily — no memory pressure on large datasets
GoCardlessClient.Resources.Payments.stream(client, %{status: "paid_out"})
|> Stream.each(&reconcile/1)
|> Stream.run()

# Or collect all pages eagerly
{:ok, all_customers} = GoCardlessClient.Resources.Customers.collect_all(client)

Webhooks

# In your Phoenix controller:
def handle(conn, _params) do
  events = conn.private[:gocardless_events]
  Enum.each(events, &dispatch_event/1)
  send_resp(conn, 200, "")
end

Modules

ModuleDescription
GoCardlessClient.ClientClient struct and builder
GoCardlessClient.ConfigConfiguration with NimbleOptions validation
GoCardlessClient.Resources.CustomersCustomer management
GoCardlessClient.Resources.CustomerBankAccountsCustomer bank accounts
GoCardlessClient.Resources.MandatesDirect Debit mandate lifecycle
GoCardlessClient.Resources.PaymentsPayment creation and management
GoCardlessClient.Resources.SubscriptionsRecurring payment schedules
GoCardlessClient.Resources.InstalmentSchedulesFixed instalment plans
GoCardlessClient.Resources.BillingRequestsOpen Banking / Pay by Bank
GoCardlessClient.Resources.BillingRequestFlowsHosted billing request UI
GoCardlessClient.Resources.BankAuthorisationsBank authorisation management
GoCardlessClient.Resources.RedirectFlowsGoCardlessClient-hosted mandate setup
GoCardlessClient.Resources.PayoutsPayout management
GoCardlessClient.Resources.PayoutItemsPayout reconciliation
GoCardlessClient.Resources.RefundsPayment refunds
GoCardlessClient.Resources.EventsEvent log
GoCardlessClient.Resources.CreditorsCreditor management
GoCardlessClient.Resources.OutboundPaymentsSend money (requires signing)
GoCardlessClient.Resources.ScenarioSimulatorsSandbox test triggers
GoCardlessClient.WebhooksSignature verification and event parsing
GoCardlessClient.Webhooks.PlugPhoenix/Plug middleware
GoCardlessClient.OAuthOAuth2 partner flow
GoCardlessClient.SigningECDSA request signing
GoCardlessClient.PaginatorCursor-based pagination streams
GoCardlessClient.APIErrorStructured API error
GoCardlessClient.ErrorNetwork/SDK error

Using Resource Modules

All resource operations live in GoCardlessClient.Resources.*. Use module aliases for ergonomic access:

alias GoCardlessClient.Resources.{Customers, Payments, Mandates, Subscriptions}

{:ok, customer} = Customers.create(client, %{email: "alice@example.com"})
{:ok, payment}  = Payments.create(client, %{amount: 1500, currency: "GBP"})

Summary

Functions

Returns the resolved base URL for a client's environment.

Builds a GoCardlessClient.Client from application config merged with opts.

Like client/1 but raises ArgumentError on invalid options.

Returns a new idempotency key (32 random hex chars).

Returns the current rate-limit state from the last observed API response.

Functions

base_url(client)

@spec base_url(GoCardlessClient.Client.t()) :: String.t()

Returns the resolved base URL for a client's environment.

client(opts \\ [])

@spec client(keyword()) ::
  {:ok, GoCardlessClient.Client.t()}
  | {:error, NimbleOptions.ValidationError.t()}

Builds a GoCardlessClient.Client from application config merged with opts.

Returns {:ok, client} or {:error, %NimbleOptions.ValidationError{}}.

client!(opts \\ [])

@spec client!(keyword()) :: GoCardlessClient.Client.t()

Like client/1 but raises ArgumentError on invalid options.

new_idempotency_key()

@spec new_idempotency_key() :: String.t()

Returns a new idempotency key (32 random hex chars).

rate_limit_state(client)

@spec rate_limit_state(GoCardlessClient.Client.t()) :: map()

Returns the current rate-limit state from the last observed API response.