Mpesa Client¶
Mpesa ¶
M-Pesa API client.
Usage
from safcom import Mpesa
mpesa = Mpesa( consumer_key="your_key", consumer_secret="your_secret", passkey="your_passkey", shortcode="174379", env="sandbox" )
response = mpesa.stk_push( phone="254712345678", amount=100, account_ref="INV-001", )
Source code in safcom/mpesa.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | |
Attributes¶
Methods:¶
__init__ ¶
__init__(
consumer_key: str,
consumer_secret: str,
passkey: str,
shortcode: str,
env: str = "sandbox",
)
Source code in safcom/mpesa.py
set_callback_url ¶
Set the default callback URL for M-Pesa transaction notifications.
All subsequent STK Push requests will use this URL unless
overridden per-request with the callback_url parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
url
|
str
|
Public HTTPS URL where M-Pesa will POST transaction results. Must be reachable from the internet. |
required |
Source code in safcom/mpesa.py
stk_push ¶
stk_push(
phone: str,
amount: float,
account_ref: str,
transaction_desc: str | None = None,
callback_url: str | None = None,
) -> STKPushResponse
Send a payment request to a customer's phone.
The customer receives an M-Pesa PIN prompt on their phone. They enter their PIN and the money moves instantly. This is known as Lipa Na M-Pesa Online.
A successful response only means M-Pesa received the request.
Use stk_push_query() or set up a callback URL to learn
whether the customer actually paid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone
|
str
|
Customer's phone number. Accepts any format —
|
required |
amount
|
float
|
Amount to charge in KES. Must be greater than 0. |
required |
account_ref
|
str
|
Short reference visible to the customer on their M-Pesa screen. Maximum 12 characters. |
required |
transaction_desc
|
str | None
|
Optional description. Defaults to the
value of |
None
|
callback_url
|
str | None
|
Override the default callback URL for this
request only. If not provided, the URL set via
|
None
|
Returns:
| Type | Description |
|---|---|
STKPushResponse
|
STKPushResponse with the |
STKPushResponse
|
Save this ID — you'll need it to query the payment |
STKPushResponse
|
status later. |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the phone number is invalid, amount is not positive, or account_ref is too long. |
APIError
|
If the M-Pesa API returns an error. |
Example
resp = mpesa.stk_push( ... phone="254712345678", ... amount=500, ... account_ref="INV-001", ... ) resp.checkout_request_id 'ws_CO_202507202359590000' resp.customer_message 'Please enter your M-Pesa PIN'
Source code in safcom/mpesa.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 | |
stk_push_query ¶
Check whether an STK push payment went through.
Use this to poll for the final status after sending an STK push. The customer needs time to enter their PIN — wait at least 5 seconds before the first query.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
checkout_request_id
|
str
|
The |
required |
Returns:
| Type | Description |
|---|---|
STKPushStatus
|
STKPushStatus with the transaction result. Check |
STKPushStatus
|
|
STKPushStatus
|
|
STKPushStatus
|
timeout, |
Raises:
| Type | Description |
|---|---|
APIError
|
If the M-Pesa API returns an error. |
Example
status = mpesa.stk_push_query("ws_CO_202507202359590000") status.success True status.receipt 'NLJ91HA6ES'
Source code in safcom/mpesa.py
b2c ¶
b2c(
phone: str,
amount: float,
remarks: str = "payment",
initiator_name: str = "testapi",
security_credential: str = "",
occasion: str = "",
) -> B2CResponse
Send money from your business to a customer.
Used for refunds, payouts, and rewards. Requires B2C API enabled and initiator credentials configured in the M-Pesa org portal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
phone
|
str
|
Recipient phone number. Accepts any format
( |
required |
amount
|
float
|
Amount to send in KES. |
required |
remarks
|
str
|
Reason for the payment. Shows in the customer's M-Pesa statement. |
'payment'
|
initiator_name
|
str
|
The API initiator username configured in your M-Pesa org portal. |
'testapi'
|
security_credential
|
str
|
Base64-encrypted initiator password. Generate this using Safaricom's provided tool. |
''
|
occasion
|
str
|
Optional field (e.g. for promotions). |
''
|
Returns:
| Type | Description |
|---|---|
B2CResponse
|
B2CResponse with the |
B2CResponse
|
The actual result arrives via callback to your |
B2CResponse
|
|
Raises:
| Type | Description |
|---|---|
ValidationError
|
If the phone number or amount is invalid. |
APIError
|
If the M-Pesa API returns an error. |
Source code in safcom/mpesa.py
account_balance ¶
account_balance(
initiator_name: str = "testapi", security_credential: str = ""
) -> AccountBalanceResponse
Query your M-Pesa account balance.
Sends a balance inquiry request. The actual balance information
arrives via callback to your ResultURL.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
initiator_name
|
str
|
The API initiator username configured in your M-Pesa org portal. |
'testapi'
|
security_credential
|
str
|
Base64-encrypted initiator password. Generate this using Safaricom's provided tool. |
''
|
Returns:
| Type | Description |
|---|---|
AccountBalanceResponse
|
AccountBalanceResponse confirming the request was received. |
AccountBalanceResponse
|
Balance details arrive via callback to your ResultURL. |
Raises:
| Type | Description |
|---|---|
APIError
|
If the M-Pesa API returns an error. |