Mpesa

class Mpesa(consumerKey: String, consumerSecret: String, environment: Environment = Environment.SANDBOX, shouldEnableLogging: Boolean = environment == Environment.SANDBOX, httpClientEngine: HttpClientEngine? = null, val cache: Cache<Long, AccessToken> = Cache.Builder<Long, AccessToken>().build(), httpClientConfig: HttpClientConfig<*>.() -> Unit = { commonKtorConfiguration( consumerKey, consumerSecret, environment, shouldEnableLogging, cache ).invoke(this) }, httpClient: HttpClient = createHttpClient(httpClientEngine, httpClientConfig))

Provides various methods to interact with the Daraja v2.0 M-PESA API. For the official documentation, see Safaricom developer website.

Parameters

consumerKey

Daraja API consumer key.

consumerSecret

Daraja API consumer secret.

environment

The Environment of the API. Defaults to Environment.SANDBOX.

shouldEnableLogging

Boolean value indicating whether to enable logging. Defaults to true if the environment is Environment.SANDBOX, false otherwise.

httpClientEngine

The HTTP client engine to use. Defaults to null, which uses the default engine for the current platform.

cache

A cache to store access tokens. Defaults to a new Cache instance.

httpClientConfig

A lambda expression that configures the HTTP client. Defaults to a configuration that uses the provided consumer key, consumer secret, environment, and logging settings.

httpClient

The HTTP client to use. Defaults to a new client configured with the provided configuration.

Constructors

Link copied to clipboard
constructor(consumerKey: String, consumerSecret: String, environment: Environment = Environment.SANDBOX, shouldEnableLogging: Boolean = environment == Environment.SANDBOX, httpClientEngine: HttpClientEngine? = null, cache: Cache<Long, AccessToken> = Cache.Builder<Long, AccessToken>().build(), httpClientConfig: HttpClientConfig<*>.() -> Unit = { commonKtorConfiguration( consumerKey, consumerSecret, environment, shouldEnableLogging, cache ).invoke(this) }, httpClient: HttpClient = createHttpClient(httpClientEngine, httpClientConfig))

Properties

Link copied to clipboard
val cache: Cache<Long, AccessToken>

Functions

Link copied to clipboard
suspend fun generateDynamicQr(dynamicQrRequest: DynamicQrRequest): DynamicQrResponse

Dynamic QR - Generates a dynamic M-PESA QR Code.

Link copied to clipboard

Account balance - Enquire the balance on an M-Pesa BuyGoods (Till Number).

Link copied to clipboard

Transaction status - Check the status of a transaction.

Link copied to clipboard

B2C - Transact between an M-Pesa short code to a phone number registered on M-Pesa.

Link copied to clipboard

Initiates a C2B transaction between a phone number and an M-Pesa short code registered on M-Pesa.

Link copied to clipboard
suspend fun initiateStkPush(stkPushRequest: StkPushRequest): StkPushResponse

Initiates an STK Push (Lipa na M-Pesa Online) transaction.

Link copied to clipboard

STK Push query - Check the status of a Lipa Na M-Pesa Online Payment.

Link copied to clipboard
suspend fun registerC2bUrls(c2bRegisterRequest: C2bRegisterRequest): C2bRegisterResponse

C2B register - Register validation and confirmation URLs on M-Pesa

Link copied to clipboard

Reversal - Reverses a C2B M-Pesa transaction.