MpesaElixir.StkPush.Request (MpesaElixir v0.2.1)

Represents a request body for the Lipa na M-PESA online API (STK Push).

This struct contains all the required parameters for initiating a C2B payment through M-PESA's STK Push service, which sends a payment prompt to the customer's phone.

Summary

Types

t()

STK Push request parameters

Functions

Creates a new STK Push request with the provided parameters.

Converts the struct to a map with keys formatted as required by the M-PESA API. This function transforms snake_case field names to PascalCase as expected by the API.

Types

t()

@type t() :: %MpesaElixir.StkPush.Request{
  account_reference: String.t(),
  amount: number(),
  business_short_code: integer(),
  call_back_url: String.t(),
  party_a: integer(),
  party_b: integer(),
  password: String.t(),
  phone_number: String.t(),
  timestamp: String.t(),
  transaction_desc: String.t(),
  transaction_type: String.t()
}

STK Push request parameters

Functions

new(attrs)

Creates a new STK Push request with the provided parameters.

Examples

iex> MpesaElixir.StkPush.Request.new(%{
...>   business_short_code: "174379",
...>   password: "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMTYwMjE2MTY1NjI3",
...>   timestamp: "20160216165627",
...>   transaction_type: "CustomerPayBillOnline",
...>   amount: "1",
...>   party_a: "254708374149",
...>   party_b: "174379",
...>   phone_number: "254708374149",
...>   call_back_url: "https://mydomain.com/path",
...>   account_reference: "Test",
...>   transaction_desc: "Test"
...> })

to_api_map(request)

Converts the struct to a map with keys formatted as required by the M-PESA API. This function transforms snake_case field names to PascalCase as expected by the API.

will set the password and timestamp if they are not provided.

Examples

iex> request = %MpesaElixir.StkPush.Request{
...>   business_short_code: "174379",
...>   password: "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMTYwMjE2MTY1NjI3",
...>   timestamp: "20160216165627",
...>   transaction_type: "CustomerPayBillOnline",
...>   amount: "1",
...>   party_a: "254708374149",
...>   party_b: "174379",
...>   phone_number: "254708374149",
...>   call_back_url: "https://mydomain.com/path",
...>   account_reference: "Test",
...>   transaction_desc: "Test"
...> }
iex> MpesaElixir.StkPush.Request.to_api_map(request)
%{
  "BusinessShortCode" => "174379",
  "Password" => "MTc0Mzc5YmZiMjc5ZjlhYTliZGJjZjE1OGU5N2RkNzFhNDY3Y2QyZTBjODkzMDU5YjEwZjc4ZTZiNzJhZGExZWQyYzkxOTIwMTYwMjE2MTY1NjI3",
  "Timestamp" => "20160216165627",
  "TransactionType" => "CustomerPayBillOnline",
  "Amount" => "1",
  "PartyA" => "254708374149",
  "PartyB" => "174379",
  "PhoneNumber" => "254708374149",
  "CallBackURL" => "https://mydomain.com/path",
  "AccountReference" => "Test",
  "TransactionDesc" => "Test"
}