Daraja.Client (daraja v0.1.0)

Copy Markdown View Source

Connection-level handle holding Daraja credentials, merchant defaults, and the target environment.

Use new/1 to build a client. Per-call options override values pulled from the :daraja application environment.

Examples

Build a client from application configuration:

client = Daraja.Client.new()

Override individual fields (handy for multi-tenant scenarios):

client =
  Daraja.Client.new(
    consumer_key: merchant.consumer_key,
    consumer_secret: merchant.consumer_secret
  )

Pass the client to an endpoint module:

Daraja.Express.request(client, %{
  amount: 100,
  phone_number: "254712345678",
  account_reference: "Order-001"
})

Required vs. optional fields

consumer_key and consumer_secret are always required. new/1 raises if they are absent from both options and the application environment.

business_short_code, passkey, and callback_url are STK Push-only and default to nil. Daraja.Express.request/2 validates that they are set before sending a request.

Summary

Functions

Returns the Safaricom base URL for the client's environment.

Builds a Daraja.Client from options, falling back to the :daraja application environment for any missing value.

Types

env()

@type env() :: :sandbox | :production

t()

@type t() :: %Daraja.Client{
  business_short_code: String.t() | nil,
  callback_url: String.t() | nil,
  consumer_key: String.t(),
  consumer_secret: String.t(),
  environment: env(),
  passkey: String.t() | nil
}

Functions

base_url(client)

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

Returns the Safaricom base URL for the client's environment.

new(opts \\ [])

@spec new(keyword()) :: t()

Builds a Daraja.Client from options, falling back to the :daraja application environment for any missing value.

Raises if consumer_key or consumer_secret cannot be resolved.