ExMpesa.Stk (ExMpesa v0.1.0) View Source

Lipa na M-Pesa Online Payment API is used to initiate a M-Pesa transaction on behalf of a customer using STK Push. This is the same technique mySafaricom App uses whenever the app is used to make payments.

Link to this section Summary

Functions

Initiates the Mpesa Lipa Online STK Push .

STK PUSH Transaction Validation

Link to this section Functions

Specs

request(map()) :: {:error, any()} | {:ok, any()}

Initiates the Mpesa Lipa Online STK Push .

Configuration

Add below config to dev.exs / prod.exs files This asumes you have a clear understanding of how Daraja API works. See docs here https://developer.safaricom.co.ke/docs#lipa-na-m-pesa-online-payment

config.exs

  config :ex_mpesa,
      consumer_key: "",
      consumer_secret: "",
      mpesa_short_code: "",
      mpesa_passkey: "",
      mpesa_callback_url: ""

Parameters

attrs: - a map containing:

  • phone - The MSISDN sending the funds(PhoneNumber).
  • amount - The amount to be transacted.
  • reference - Used with M-Pesa PayBills(AccountReference).
  • description - A description of the transaction(TransactionDesc).

Example

iex> ExMpesa.Stk.request(%{amount: 10, phone: "254724540000", reference: "reference", description: "description"})
{:ok,
  %{
  "CheckoutRequestID" => "ws_CO_010320202011179845",
  "CustomerMessage" => "Success. Request accepted for processing",
  "MerchantRequestID" => "25558-10595705-4",
  "ResponseCode" => "0",
  "ResponseDescription" => "Success. Request accepted for processing"
  }}

Specs

validate(map()) :: {:error, any()} | {:ok, any()}

STK PUSH Transaction Validation

Configuration

Add below config to dev.exs / prod.exs files (at this stage after STK, the config keys should be there) This asumes you have a clear understanding of how Daraja API works. See docs here https://developer.safaricom.co.ke/docs#lipa-na-m-pesa-online-query-request

config.exs

  config :ex_mpesa,
      consumer_key: "",
      consumer_secret: "",
      mpesa_short_code: "",
      mpesa_passkey: "",

Parameters

attrs: - a map containing:

  • checkout_request_id - Checkout RequestID.

Example

iex> ExMpesa.Stk.validate(%{checkout_request_id: "ws_CO_260820202102496165"})
{:ok,
  %{
    "CheckoutRequestID" => "ws_CO_260820202102496165",
    "MerchantRequestID" => "11130-78831728-4",
    "ResponseCode" => "0",
    "ResponseDescription" => "The service request has been accepted successsfully",
    "ResultCode" => "1032",
    "ResultDesc" => "Request cancelled by user"
  }
}