ExMpesa.B2C (ExMpesa v0.1.0) View Source

Business to Customer (B2C) API enables the Business or organization to pay its customers who are the end-users of its products or services. Currently, the B2C API allows the org to perform around 3 types of transactions: SalaryPayments, BusinessPayments or Promotion payments. Salary payments are used by organizations paying their employees via M-Pesa, Business Payments are normal business transactions to customers e.g. bank transfers to mobile, Promotion payments are payments made by organization carrying out promotional services e.g. betting companies paying out winnings to clients

Link to this section Summary

Functions

Initiates the Mpesa B2C .

Link to this section Functions

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."
  }}