ElixirMpesa.GenerateSessionKey (ElixirMpesa v0.1.0)

View Source

Handles the session key generation and management for M-Pesa OpenAPI.

Before integrating with the M-Pesa OpenAPI solution, you must exchange your Application Key for a Session Key. The API Key is created with the creation of a new application. The Session Key acts as an access token that authorizes the rest of your REST API calls to the system. A valid Session Key is needed to transact on M-Pesa using OpenAPI.

Process Flow

  1. Encrypt API key using the public key provided by M-Pesa
  2. Send the encrypted API key to the Session API endpoint
  3. Receive a session key in response
  4. Encrypt the session key for use in subsequent API calls

Example

# Encrypt the API key
{:ok, encrypted_api_key} = ElixirMpesa.GenerateSessionKey.encrypt_api_key()

# Generate a session ID using the encrypted API key
{:ok, session_data} = ElixirMpesa.GenerateSessionKey.generate_output_session_id(encrypted_api_key)
session_key = session_data["output_SessionID"]

# Encrypt the session key for use in transactions
{:ok, encrypted_session_key} = ElixirMpesa.GenerateSessionKey.encrypt_session_id(session_key)

Summary

Functions

Encrypts the API key using the configured public key.

Encrypts a session ID using the configured public key.

Generates a session ID by sending the encrypted API key to the M-Pesa API.

Functions

encrypt_api_key(options \\ [])

Encrypts the API key using the configured public key.

This function takes the API key and encrypts it using the RSA public key provided by M-Pesa. The encrypted API key is then used to obtain a session key.

Options

  • :public_key - Override the configured public key
  • :api_key - Override the configured API key
  • :api_type - Override the configured API type ("sandbox" or "openapi")
  • :url_context - Override the configured URL context

Returns

  • {:ok, encrypted_api_key} - The successfully encrypted API key
  • {:error, reason} - If encryption fails

Examples

iex> ElixirMpesa.GenerateSessionKey.encrypt_api_key()
{:ok, "encrypted_api_key_string"}

iex> ElixirMpesa.GenerateSessionKey.encrypt_api_key([public_key: "custom_public_key"])
{:ok, "encrypted_api_key_string"}

encrypt_session_id(session_id, options \\ [])

Encrypts a session ID using the configured public key.

After obtaining a session ID from the M-Pesa API, it needs to be encrypted before it can be used in subsequent API calls.

Parameters

  • session_id - The session ID to encrypt
  • options - A keyword list of options (see below)

Options

  • :public_key - Override the configured public key

Returns

  • {:ok, encrypted_session_id} - The successfully encrypted session ID
  • {:error, reason} - If encryption fails

Examples

iex> ElixirMpesa.GenerateSessionKey.encrypt_session_id("session_id_string")
{:ok, "encrypted_session_id_string"}

generate_output_session_id(encrypted_api_key, options \\ [])

Generates a session ID by sending the encrypted API key to the M-Pesa API.

Parameters

  • encrypted_api_key - The encrypted API key (optional, will use the configured key if nil)
  • options - A keyword list of options (see below)

Options

  • :api_type - Override the configured API type ("sandbox" or "openapi")
  • :url_context - Override the configured URL context

Returns

  • {:ok, response} - The API response containing the session ID
  • {:error, reason} - If the API call fails

Examples

iex> {:ok, encrypted_key} = ElixirMpesa.GenerateSessionKey.encrypt_api_key()
iex> ElixirMpesa.GenerateSessionKey.generate_output_session_id(encrypted_key)
{:ok, %{"output_ResponseCode" => "INS-0", "output_ResponseDesc" => "Request processed successfully", "output_SessionID" => "session_id_string"}}