Daraja.B2C.Callback (daraja v0.1.0)

Copy Markdown View Source

Helpers for parsing asynchronous B2C callback payloads posted to ResultURL.

B2C transactions are asynchronous; this module parses successful and unsuccessful callback payloads into typed structs and provides helpers to extract ResultParameters values by key.

Verify inbound requests with Daraja.Callback.Security, deduplicate on originator_conversation_id with Daraja.Callback.Guard, and use parse/1 on untrusted input.

Summary

Functions

Builds the JSON response body used to acknowledge a B2C result callback.

Parses a B2C callback payload map into a %Result{} struct.

Parses a B2C callback map from an untrusted HTTP request.

Flattens ResultParameters.ResultParameter into %{"Key" => value}.

Functions

accept()

@spec accept() :: %{required(String.t()) => String.t() | non_neg_integer()}

Builds the JSON response body used to acknowledge a B2C result callback.

B2C callbacks are one-way notifications; M-PESA only needs the merchant to acknowledge receipt with ResultCode: 0.

Daraja.B2C.Callback.accept()
#=> %{"ResultCode" => 0, "ResultDesc" => "Success"}

from_map(map)

@spec from_map(map()) :: Daraja.B2C.Callback.Result.t()

Parses a B2C callback payload map into a %Result{} struct.

parse(map)

@spec parse(map()) ::
  {:ok, Daraja.B2C.Callback.Result.t()}
  | {:error, :invalid_callback, String.t()}

Parses a B2C callback map from an untrusted HTTP request.

result_parameters_map(parameters)

@spec result_parameters_map([map()] | map() | nil) :: %{
  optional(String.t()) => term()
}

Flattens ResultParameters.ResultParameter into %{"Key" => value}.