ExMpesa.B2C.request

You're seeing just the function request, go back to ExMpesa.B2C module for more information.

Initiates the Mpesa B2C .

Configuration

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

config.exs

  config :ex_mpesa,
      cert: "",
      b2c: [
          initiator_name: "Safaricom1",
          password: "Safaricom133",
          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

Parameters

attrs: - a map containing:

  • command_id - Unique command for each transaction type, possible values are: BusinessPayBill, MerchantToMerchantTransfer, MerchantTransferFromMerchantToWorking, MerchantServicesMMFAccountTransfer, AgencyFloatAdvance.
  • amount - The amount being transacted.
  • phone_number - Phone number receiving the transaction.
  • remarks - Comments that are sent along with the transaction.
  • occassion - Optional.
  • result_url - The end-point that receives the response of the transaction. You can customize the result url.
                Defaults to the value passed in the config if not added to params

Example

iex> ExMpesa.B2C.request(%{command_id: "BusinessPayment", amount: 10500, phone_number: "254722000000", remarks: "B2C Request", result_url: "https://58cb49b30213.ngrok.io/b2c/result_url"})
{:ok,
  %{
    "ConversationID" => "AG_20201010_00006bd489ffcaf79e91",
    "OriginatorConversationID" => "27293-71728391-3",
    "ResponseCode" => "0",
    "ResponseDescription" => "Accept the service request successfully."
  }}