ExMpesa.Reversal (ExMpesa v0.1.0) View Source

Reversal API enables reversal of transactions done You will be able to reverse a transaction where you are the credit party. This means it will be done via the Web portal, and may require manual authorization from the Service Provider side. But if you are allowed to reverse a transaction via API, it may also need to be authorized.

An initiator requires the Org Reversals Initiator role to be able to perform reversals via API

Link to this section Summary

Functions

Makes a request to the mpesa reversal endpoint with the given params.

Link to this section Functions

Link to this function

reverse(params, option \\ :reversal)

View Source

Makes a request to the mpesa reversal endpoint with the given params.

Params

The function requires two keys to be present for a successful request, :transation_id and :amount The params can be any of the accepted params by the api endpoint with the keys converted to snake case. For example QueueTimeOutURL is expected to be in the format queue_time_out_url.

Additionally, the keys :security_credential, :initiator, :receiver_party, :result_url, :queue_time_out_url are loaded from the respective config and used if they're not provided as part of the params in the arguments.

Options

Because reversal can be done for B2B, B2C or C2B, this function allows for an option to load the configs from. It defaults to :reversal which means it will use config under the :mpesa config. In order to reuse the configs for the other apis, use the parent key under the mpesa config. For example, in order to use the b2b configs, pass in :b2b as the option

Configuration

Add below config to dev.exs / prod.exs files

config.exs

  config :ex_mpesa,
      cert: "",
      reversal: [
        short_code: "",
        initiator_name: "",
        password: "",
        timeout_url: "",
        result_url: "",
        security_credential: ""
      ]

To generate security_credential, head over to https://developer.safaricom.co.ke/test_credentials, then Initiator Security Password for your environment. Alternatively, generate security credential using certificate cert - This is the M-Pesa public key certificate used to encrypt your plain password. There are 2 types of certificates.

- sandox - https://developer.safaricom.co.ke/sites/default/files/cert/cert_sandbox/cert.cer .
- production - https://developer.safaricom.co.ke/sites/default/files/cert/cert_prod/cert.cer .

password - This is a plain unencrypted password. Environment

- production - set password from the organization portal.
- sandbox - use your own custom password

Example

iex> ExMpesa.Reversal.reverse(%{amount: 30, transaction_id: "LGR013H3J2"}, :reversal)
{:ok,
  %{
     "ConversationID" => "AG_20201011_00006511c0024c170286",
    "OriginatorConversationID" => "8094-41340768-1",
    "ResponseCode" => "0",
    "ResponseDescription" => "Accept the service request successfully."
  }}