Download OpenAPI specification:
This API is part of the our ecosystem. It allows you to make payments, find out the status of transactions and much more. Here you will find the latest documentation on setting up your solution.
| Provider ID | Provider Name | Country | Notes |
|---|---|---|---|
| 14 | Simulator | ANY | For testing purposes |
| 42 | Safaricom | KE | 254000000000 - This is the format of the phone number sent in the request |
During tests runs, using 14 provider ID (simulator) the callback is not returned and the transaction remains in the "in progress" status and if successful you will see in the response
{
"order_id": "54321",
"transaction_id": "12345",
"transaction_ref": "",
"status": 1,
"result": {
"code": 0,
"message": "OK"
},
"provider_result": {
"code": -8888,
"message": "Good"
},
"service_id": 1,
"service_version": "1.03/1.14|1.0/1.26|1.0/1.0|1.01/1.01|1.01/1.01||1.01/1.27",
"service_date_time": "2023-05-15 10:00:00.000000",
"confirm_type": 0
}
Merchant’s request and callback have to be signed to verify sent data. To generate the signature all sent parameters from the payload are included in the order they were sent. The parameter signature should be excluded, of course, and added to the payload after generating.
Note: to generate a correct signature you need a secretKey received with other credentials.
function calculateSignature(array $data, string $secretKey, string $currentParamPrefix = '', int $depth = 16, int $currentRecursionLevel = 0 ): string
{
if ($currentRecursionLevel >= $depth) {
throw new Exception('Recursion level exceeded');
}
$stringForSignature = '';
foreach ($data as $key => $value) {
if (is_array($value)) {
$stringForSignature .= calculateSignature(
$value,
$secretKey,
"$currentParamPrefix$key.",
$depth,
$currentRecursionLevel + 1
);
} else if ($key !== 'signature') {
$stringForSignature .= "$currentParamPrefix$key" . $value;
}
}
if ($currentRecursionLevel == 0) {
return strtolower(hash_hmac('sha512', $stringForSignature, $secretKey));
} else {
return $StringForSignature;
}
}
$postData = [
'merchant_id' => 'fffed61be9780b97c5e4c65e4e07bb6b',
'provider_id' => 10,
'client_id' => '080000000',
'country' => 'KE',
'order_id' => 'order_3444298767545',
'amount' => 1000,
'currency' => 'CDF',
'callback_url' => 'https://my.callback.url'
];
$secretKey = "cf11635572c1e8d77297207152dc0791ad91f22b32d23c758ce3ba2637202ad8f7290ba41f2243cccf32edde1dfb8bf0f5dea62525309e293b3adb2c76eed6a5";
$signature = calculateSignature($postData, $secretKey);
$postData['signature'] = $signature;
Examples in other languages are available on request
What is Safaricom Paybill?
The Pay Bill service is a mobile payments service that allows your organization to collect money on a regular basis from your customers through M-PESA.
The merchant should provide their callback URL to the our tech team to receive information about such transactions.
| Name of field | Type | Description | Example |
|---|---|---|---|
| extra | object | A list of values | |
| billRefNumber | integer | This field contains the parameter from Account number field in the payment form on Lipa Na M-PESA, which is indicated by the Customer.The Merchant has to inform the Customer to indicate the identification number of their account with the Merchant system. | 555555555 |
| firstName | string | Client’s first name (received from Safaricom) | ALEX |
| amount | number | Amount to pay | 100 |
| result | object | Operation result | |
| code | integer | Result code of the operation (0 - success) | 0 |
| message | string | Short description of the result code | OK |
| status | integer | Status of the performed operation | 2 |
| currency | string | Currency code in ISO 4217 format | KES |
| order_id | string | For paybills transactions, this unique value is generated by us for each Operation. Max length is 128 symbols. Allowed symbols: [a-z], [A-Z], [0-9], “_” (underscore character), “-” (hyphen), “:” (colon), “.” (dot). For example, GUID or TIMESTAMP can be used as an order_id. This parameter provides API idempotency. It means that requests with identical nonce from the same transaction initiator will have identical responses, and the corresponding operation won’t be repeated. In the backoffice, it may be found in the Merchant Trnx ID field | 2023-03-15-15-25-12-255432 |
| signature | string | Merchant’s request and callback have to be signed to verify sent data. To generate the signature all sent parameters are included in the order they were sent. The parameter signature should be excluded, of course | |
| service_id | integer | Unique ID of the service in the Payment gateway | 1 |
| customer_id | string | Customer’s phone number of 12 symbols. Client's phone should start from the country code: 254ХХХХХХХХХ (part of the number is returned as asterisk) | 2547 ***** 000 |
| merchant_id | string | Unique Merchant ID received during the merchant registration | 555222hhh555fff999rrr444qqq222 |
| provider_id | integer | Provider ID: 40: Safaricom | 40 |
| destination_id | integer | Transaction destination ID (merchant’s shortcode) | 7000000 |
| operation_type | integer | For paybills - 32 | 32 |
| transaction_id | string | Usually generated by Safaricom. Empty for paybills | |
| provider_result | object | ||
| code | integer | Result code of the operation from the provider | 0 |
| message | string | Result description from the provider | OK |
| service_version | string | Payment gateway service version used for the operation | |
| transaction_ref | string | Transaction number registered in Safaricom (RRN). This field could be empty | RBQ0000000 |
| service_date_time | string | Payment gateway timestamp of the operation | 2023-03-03 13:33:33.333333 |
{
"merchant_id": "555222hhh555fff999rrr444qqq222",
"operation_type": 32,
"customer_id": "2547 ***** 000",
"amount": 100,
"currency": "KES",
"order_id": "2023-03-15-15-25-12-255432",
"transaction_id": "",
"transaction_ref": "RBQ0000000",
"status": 2,
"provider_id": 40,
"destination_id": "7000000",
"result": {
"code": 0,
"message": "OK"
},
"provider_result": {
"code": 0,
"message": ""
},
"service_id": 1,
"service_version": "1.03/1.0|1.0/1.26|1.0/1.0|1.01/1.0|1.01/1.0||1.01/1.27",
"service_date_time": "2023-03-03 13:33:33.333333",
"extra": {
"BillRefNumber": "555555555",
"FirstName": "ALEX",
"MiddleName": "",
"LastName": ""
},
"signature": "sdfkdfpogu9550603etgkdopftege34t0i5rggdftoe0tgskguo09w6t"
}
The parameter that is directly involved in the validation is PayBillRefNumber
To be able to pre-verify payments via paybill, the Merchant needs to add another URL in his system to which requests will be sent (the same as on paybill callback. This URL must be reported to us. The Merchant will be able to verify the validity of this request (for example, the existence of the customer's account number in the Merchant's system) and send a response to it. A response with the code=0 means that the validation has been completed and the payment can be credited; any other response means that the transaction should not be completed, and a request to cancel the payment will be sent to Safaricom. In order for the response to be counted as confirmation, it has to contain a JSON with "code":0. Any other data will be ignored.
Example:
{"code":0,"status":"ok"}
| Code | Name | Description |
|---|---|---|
| -1 | undefined | Operation status is undefined (for example in an error situation) |
| 0 | initiated | Operation is initiated |
| 1 | in progress | Operation is in progress |
| 2 | success | Operation is successful |
| 3 | failed | Operation is failed |
Depending on the type of request you may see the following code
| Code | Operation |
|---|---|
| 16 | payment_b2c |
| 17 | payment_c2b |
| Code | Note |
|---|---|
| USD | Non-betting merchants only |
| KES | Kenya, betting and non-betting merchants |
Responses for confirmation requests have the same format as original operation responses.
C2b transaction status is sent via callback because it needs a confirmation by client done asynchronously. Usually the callback should be sent in 2-3 minutes maximum. In case of missing callback there is a way to get the transaction status using API method status. It needs the order ID as an parameter and returns a status of the performed transaction.
Payment gateway considers the Merchant system response as successful if HTTP 200 was received.
| public_id required | string Example: f54ec96649be11ebb3780242ac130002 Merchant public ID |
Parameters to initiate a customer to the merchant payment
| merchant_id required | string (merchantIdDef) Unique Merchant ID received during the merchant registration |
| customer_id required | string (customerIdDef) Customer ID (usually mobile phone number of the customer) |
| order_id required | string (orderIdDef) The unique value is generated by the transaction initiator for each Operation. Max length is 128 symbols. Allowed symbols: [a-z], [A-Z], [0-9], “_” (underscore character), “-” (hyphen), “:” (colon), “.” (dot). For example, GUID or TIMESTAMP can be used as an order_id. This parameter provides API idempotency. It means that requests with identical nonce from the same transaction initiator will have identical responses and The corresponding operation won’t be repeated. |
| amount required | string Amount to pay, should be in format with two digits after point |
| currency required | string (currencyDef) Currency code in ISO 4217 format from the list of availabe currencies |
| country | |
| callback_url | string URL to notify the merchant via callback. Recommended |
| provider_id required | |
| signature required | string (signatureDef) Merchant’s request and callback have to be signed to verify sent data. To generate the signature all sent parameters are included in the order they were sent. The parameter signature should be excluded, of course. Example can be found here |
{- "merchant_id": "e0fecd91fcb24f348048193b3fb34875ba3722b4",
- "customer_id": "0900000001",
- "order_id": "16280954971628095497",
- "amount": "100.00",
- "currency": "CDF",
- "country": "KE",
- "provider_id": 14,
- "signature": "d7d6d76b0e22c6f9d369fa6c24f107053d12bfd24d3b154f2deb6676bf179c123134e1f20879c803be455d81cfe792f00cd8892c26ce7cf5a05beebb9c80843e"
}{- "order_id": "16280954971628095497",
- "transaction_id": "732007046722",
- "transaction_ref": "MP.33234.342.CP33",
- "status": 2,
- "result": {
- "code": 0,
- "message": "OK"
}, - "provider_result": {
- "code": 0,
- "message": "OK"
}, - "service_id": 1,
- "service_version": 11.1,
- "service_date_time": "2020-11-25 10:08:32.832969"
}{- "transaction_id": "1234567",
- "order_id": "16280954971628095497",
- "service_id": 12345,
- "service_version": "5.2.0",
- "service_date_time": "2020-11-25 10:08:32.832969",
- "result": {
- "code": 0,
- "message": "OK"
}, - "signature": "d7d6d76b0e22c6f9d369fa6c24f107053d12bfd24d3b154f2deb6676bf179c123134e1f20879c803be455d81cfe792f00cd8892c26ce7cf5a05beebb9c80843e"
}Cashless payment from the merchant to the customer. If the confirm_type response parameter is a non-zero merchant, send the second payment_b2c request with confirmation data according to the section Confirmation Types.
| public_id required | string Example: f54ec96649be11ebb3780242ac130002 Merchant public ID |
Parameters to initiate the merchant to the customer payment
| merchant_id required | string (merchantIdDef) Unique Merchant ID received during the merchant registration |
| customer_id required | string (customerIdDef) Customer ID (usually mobile phone number of the customer) |
| order_id required | string (orderIdDef) The unique value is generated by the transaction initiator for each Operation. Max length is 128 symbols. Allowed symbols: [a-z], [A-Z], [0-9], “_” (underscore character), “-” (hyphen), “:” (colon), “.” (dot). For example, GUID or TIMESTAMP can be used as an order_id. This parameter provides API idempotency. It means that requests with identical nonce from the same transaction initiator will have identical responses and The corresponding operation won’t be repeated. |
| amount required | string Amount to pay, with two digits after point |
| currency required | string (currencyDef) Currency code in ISO 4217 format from the list of availabe currencies |
| country | |
| callback_url | string URL to notify the merchant via callback |
| provider_id required | |
| signature required | string (signatureDef) Merchant’s request and callback have to be signed to verify sent data. To generate the signature all sent parameters are included in the order they were sent. The parameter signature should be excluded, of course. Example can be found here |
{- "merchant_id": "e0fecd91fcb24f348048193b3fb34875ba3722b4",
- "customer_id": "0900000001",
- "order_id": "16280954971628095497",
- "amount": "100.00",
- "currency": "CDF",
- "country": "KE",
- "provider_id": 14,
- "signature": "d7d6d76b0e22c6f9d369fa6c24f107053d12bfd24d3b154f2deb6676bf179c123134e1f20879c803be455d81cfe792f00cd8892c26ce7cf5a05beebb9c80843e"
}{- "order_id": "16280954971628095497",
- "transaction_id": "532007056722",
- "transaction_ref": "",
- "status": 2,
- "result": {
- "code": 0,
- "message": "OK"
}, - "provider_result": {
- "code": 0,
- "message": "OK"
}, - "service_id": 11,
- "service_version": 11.1,
- "service_date_time": "2020-11-25 10:08:32.832969",
- "confirm_type": 0
}| public_id required | string Example: f54ec96649be11ebb3780242ac130002 Merchant public ID |
Get the status of the performed transaction
| merchant_id required | string (merchantIdDef) Unique Merchant ID received during the merchant registration |
| order_id required | string (orderIdDef) The unique value is generated by the transaction initiator for each Operation. Max length is 128 symbols. Allowed symbols: [a-z], [A-Z], [0-9], “_” (underscore character), “-” (hyphen), “:” (colon), “.” (dot). For example, GUID or TIMESTAMP can be used as an order_id. This parameter provides API idempotency. It means that requests with identical nonce from the same transaction initiator will have identical responses and The corresponding operation won’t be repeated. |
| signature required | string (signatureDef) Merchant’s request and callback have to be signed to verify sent data. To generate the signature all sent parameters are included in the order they were sent. The parameter signature should be excluded, of course. Example can be found here |
{- "merchant_id": "e0fecd91fcb24f348048193b3fb34875ba3722b4",
- "order_id": "16280954971628095497",
- "signature": "d7d6d76b0e22c6f9d369fa6c24f107053d12bfd24d3b154f2deb6676bf179c123134e1f20879c803be455d81cfe792f00cd8892c26ce7cf5a05beebb9c80843e"
}{- "order_id": "16280954971628095497",
- "transaction_id": "732007046722",
- "transaction_ref": "MP.33234.342.CP33",
- "status": 2,
- "result": {
- "code": 0,
- "message": "OK"
}, - "provider_result": {
- "code": 0,
- "message": "OK"
}, - "service_id": 1,
- "service_version": 11.1,
- "service_date_time": "2020-11-25 10:08:32.832969"
}