M-Pesa B2C
How to initiate and approve M-Pesa B2C transactions
M-Pesa B2C are ideal for payment disbursement to M-Pesa accounts. M-Pesa B2C is normally used for salary and promotional payments, remittance termination, and even business payouts if the beneficiary prefer to be paid direct to their M-Pesa phone number accounts rather than Till/PayBill numbers.
Like other send money transactions, the M-Pesa B2C is a two step process. The first step is to initiate the transaction and the second one is to approve the transactions. Both the initiate and the approval are automated with the API.
The payload contains transactions field which is of type array. This means you can send money to many beneficiaries in a single request.
M-Pesa B2C Transaction Fields
The following fields must be specified in your transaction item
Field | Description |
---|---|
name | Name of the beneficiary |
account | Beneficiary phone number. Must be prefixed with the country code e.g 254.... |
amount | Amount to send in KES |
narrative | Purpose of payment |
Step 1: Initiate M-Pesa B2C
To begin, please go through the Introduction to obtain the API Keys, Device ID and everything else you need to proceed.
Once setup we recommend using the SDKs provided by IntaSend to make it easy for you to sign and approve with less efforts. We have done the heavy lifting for you, so do not reinvent the wheel if the SDK of your stack is already available.
NB: Refer to how to authenticate documentation for more details on client libraries setup.
from intasend import APIService
service = APIService(token=token)
transactions = [{'name': 'Awesome Customer 1', 'account': 25472.., 'amount': 10},
{'name': 'Awesome Customer 2', 'account': 25472.., 'amount': 10000}]
requires_approval = 'YES' # Set to 'NO' if you want the transaction to go through
# without calling the approve method
response = service.transfer.mpesa(currency='KES', transactions=transactions,
requires_approval=requires_approval)
print(response)
use IntaSend\IntaSendPHP\Transfer;
$transactions = [
['account'=>'254723890353','amount'=>'20'],
['account'=>'254723890260','amount'=>'15']
];
$requires_approval = 'YES'; // Set to 'NO' if you want the transaction to go through
// without calling the approve method
$transfer = new Transfer();
$transfer->init($credentials);
$response=$transfer->mpesa("KES", $transactions, $requires_approval=$requires_approval);
const IntaSend = require('intasend-node');
const intasend = new IntaSend(/*...Authenticate*/)
let payouts = intasend.payouts();
var req_approval = "YES" // Set to 'NO' if you want the transaction
// to go through without calling the approve method
payouts
.mpesa({
currency: 'KES',
requires_approval: req_approval,
transactions: [{
name: 'Joe Doe',
account: '254708374149',
amount: '10',
narrative: 'Reason for payment'
}]
})
.then((resp) => {
console.log(`Payouts response:`, resp);
})
.catch((err) => {
console.error(`Payouts error:`, err);
});
Step 2: Approve and release payment
If requires_approval
is set to YES
, the response from Step 1 is passed to the approve function to approve the transaction.
approved_response = service.transfer.approve(response)
print(approved_response)
$response = $transfer->approve($response);
print_r($response);
payouts
.approve(resp, false)
.then((resp) => {
console.log(`Payouts approve:`, resp);
})
.catch((err) => {
console.error(`Payouts approve error:`,err);
});
Updated 7 months ago