Skip to content

Commit

Permalink
Merge pull request #9 from Iankumu/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Iankumu authored Jun 28, 2023
2 parents 8124500 + 5cad514 commit 34d740b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
name: run-tests

on: [push, pull_request]
on:
pull_request:
branches: ["main"]

jobs:
test:
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,11 @@ return [
*/
'reversal_timeout_url' => env('MPESA_REVERSAL_TIMEOUT_URL'),
/*-----------------------------------------
|Mpesa B2C urls
|Mpesa B2B urls
|------------------------------------------
*/
'b2b_result_url' => env('MPESA_B2B_RESULT_URL'),

'b2b_timeout_url' => env('MPESA_B2B_TIMEOUT_URL'),
];

Expand Down
23 changes: 23 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,26 @@ $response = Mpesa::reversal('600981','OEI2AK4Q16', 500, 'Wrong Payment');
$result = json_decode((string)$response);
return $result;
```

## B2B API

This API is useful when you want to make `business to business payments`. It is, however, not publicly available in the documentation and you have to contact Safaricom and give them a valid usecase/reason on how you plan to use the API for them to give you access to the API.

Before using this method, ensure you have added the `initiator_name`,`initiator_password`,`b2c_shortcode`,`b2b_result_url` and `b2b_timeout_url` configurations to the `config/mpesa.php` file.

To use this API you need to call `b2b()` method on the `Mpesa` facade. This function accept the following parameters

1. `receiver_shortcode` - The shortcode of the recipient
2. `command_id` -The type of transaction being made. Can be `BusinessPayBill`, `MerchantToMerchantTransfer`, `MerchantTransferFromMerchantToWorking`, `MerchantServicesMMFAccountTransfer`, `AgencyFloatAdvance`
3. `amount` - The amount to send to the recipient
4. `remarks` - small decription of the payment being made.
5. `account_number` - Required for `BusinessPaybill` CommandID

```php
use Iankumu\Mpesa\Facades\Mpesa;

$response = Mpesa::b2b($receiver_shortcode, $command_id, $amount, $remarks, $account_number = null);

$result = json_decode((string)$response);
return $result;
```
5 changes: 3 additions & 2 deletions config/mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@
'reversal_timeout_url' => env('MPESA_REVERSAL_TIMEOUT_URL'),

/*-----------------------------------------
|Mpesa B2C urls
|Mpesa B2B urls
|------------------------------------------
*/
*/
'b2b_result_url' => env('MPESA_B2B_RESULT_URL'),

'b2b_timeout_url' => env('MPESA_B2B_TIMEOUT_URL'),
];
47 changes: 27 additions & 20 deletions src/Mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Mpesa

public $stkcallback;

public $b2b_result_url;

public $b2b_timeout_url;

/**
* The Base URL
*
Expand Down Expand Up @@ -117,7 +121,7 @@ public function __construct()
*/
public function stkpush($phonenumber, $amount, $account_number, $callbackurl = null)
{
$url = $this->url.'/mpesa/stkpush/v1/processrequest';
$url = $this->url . '/mpesa/stkpush/v1/processrequest';
$data = [
//Fill in the request parameters with valid values
'BusinessShortCode' => $this->shortcode, //Has to be a paybill and not a till number since it is not supported
Expand All @@ -133,15 +137,15 @@ public function stkpush($phonenumber, $amount, $account_number, $callbackurl = n
];

//url should be https and should not contain keywords such as mpesa,safaricom etc
if (! is_null($callbackurl) && is_null($this->stkcallback)) {
if (!is_null($callbackurl) && is_null($this->stkcallback)) {
$data += [
'CallBackURL' => $callbackurl,
];
} elseif (is_null($callbackurl) && ! is_null($this->stkcallback)) {
} elseif (is_null($callbackurl) && !is_null($this->stkcallback)) {
$data += [
'CallBackURL' => $this->stkcallback,
];
} elseif (! is_null($callbackurl) && ! is_null($this->stkcallback)) {
} elseif (!is_null($callbackurl) && !is_null($this->stkcallback)) {
$data += [
'CallBackURL' => $callbackurl,
];
Expand Down Expand Up @@ -172,7 +176,7 @@ public function stkquery($checkoutRequestId)
'CheckoutRequestID' => $checkoutRequestId,
];

$url = $this->url.'/mpesa/stkpushquery/v1/query';
$url = $this->url . '/mpesa/stkpushquery/v1/query';

return $this->MpesaRequest($url, $post_data);
}
Expand All @@ -190,7 +194,7 @@ public function stkquery($checkoutRequestId)
*/
public function b2c($phonenumber, $command_id, $amount, $remarks)
{
$url = $this->url.'/mpesa/b2c/v1/paymentrequest';
$url = $this->url . '/mpesa/b2c/v1/paymentrequest';

$body = [
'InitiatorName' => $this->initiator_name,
Expand Down Expand Up @@ -248,7 +252,7 @@ public function b2c($phonenumber, $command_id, $amount, $remarks)
*/
public function validated_b2c($phonenumber, $command_id, $amount, $remarks, $id_number)
{
$url = $this->url.'/mpesa/b2cvalidate/v2/paymentrequest';
$url = $this->url . '/mpesa/b2cvalidate/v2/paymentrequest';
$body = [
'InitiatorName' => $this->initiator_name,
'SecurityCredential' => $this->security_credential,
Expand Down Expand Up @@ -292,9 +296,9 @@ public function validated_b2c($phonenumber, $command_id, $amount, $remarks, $id_
* @param string $command_id The type of transaction being made. Can be BusinessPayBill, MerchantToMerchantTransfer, MerchantTransferFromMerchantToWorking, MerchantServicesMMFAccountTransfer, AgencyFloatAdvance
* @param string $remarks Any additional information. Must be present.
* @param string $account_number Required for “BusinessPaybill” CommandID.
* @return object Curl Response from Mpesa
* @return \Illuminate\Http\Client\Response
*/
public function b2b($receiver_shortcode, $command_id, $amount, $remarks, $account_number=null)
public function b2b($receiver_shortcode, $command_id, $amount, $remarks, $account_number = null)
{
$url = $this->url . "/mpesa/b2b/v1/paymentrequest";

Expand All @@ -305,7 +309,7 @@ public function b2b($receiver_shortcode, $command_id, $amount, $remarks, $accoun
"SenderIdentifierType" => '4', //4 for shortcode
"RecieverIdentifierType" => '4', //4 for shortcode
"Amount" => $amount,
"PartyA" => $this->b2c_shortcode,//uses same shortcode as b2c
"PartyA" => $this->b2c_shortcode, //uses same shortcode as b2c
"PartyB" => $receiver_shortcode,
"AccountReference" => $account_number,
"Remarks" => $remarks
Expand All @@ -314,17 +318,20 @@ public function b2b($receiver_shortcode, $command_id, $amount, $remarks, $accoun
if ($account_number == null)
throw new \Exception("Account Number is required for BusinessPayBill CommandID");

$body['AccountReference'] = $account_number;
$body['AccountReference'] = $account_number;
}
//check urls
if (!filter_var($this->b2b_result_url, FILTER_VALIDATE_URL))
if (!filter_var($this->b2b_result_url, FILTER_VALIDATE_URL)) {
throw new CallbackException("Result URL is not valid");
if (!filter_var($this->b2b_timeout_url, FILTER_VALIDATE_URL))
}
if (!filter_var($this->b2b_timeout_url, FILTER_VALIDATE_URL)) {
throw new CallbackException("Timeout URL is not valid");
}

$body['QueueTimeOutURL'] = $this->b2b_timeout_url;
$body['ResultURL'] = $this->b2b_result_url;
$response = $this->MpesaRequest($url, $body);
return $response;

return $this->MpesaRequest($url, $body);
}

/**
Expand All @@ -338,7 +345,7 @@ public function b2b($receiver_shortcode, $command_id, $amount, $remarks, $accoun
public function c2bregisterURLS($shortcode)
{

$url = $this->url.'/mpesa/c2b/v2/registerurl';
$url = $this->url . '/mpesa/c2b/v2/registerurl';

$body = [
'ShortCode' => $shortcode,
Expand Down Expand Up @@ -398,7 +405,7 @@ public function c2bsimulate($phonenumber, $amount, $shortcode, $command_id, $acc
];
}

$url = $this->url.'/mpesa/c2b/v2/simulate';
$url = $this->url . '/mpesa/c2b/v2/simulate';

return $this->MpesaRequest($url, $data);
}
Expand All @@ -416,7 +423,7 @@ public function c2bsimulate($phonenumber, $amount, $shortcode, $command_id, $acc
*/
public function transactionStatus($shortcode, $transactionid, $identiertype, $remarks)
{
$url = $this->url.'/mpesa/transactionstatus/v1/query';
$url = $this->url . '/mpesa/transactionstatus/v1/query';

$body = [

Expand Down Expand Up @@ -461,7 +468,7 @@ public function transactionStatus($shortcode, $transactionid, $identiertype, $re
*/
public function accountBalance($shortcode, $identiertype, $remarks)
{
$url = $this->url.'/mpesa/accountbalance/v1/query';
$url = $this->url . '/mpesa/accountbalance/v1/query';

$body = [
'Initiator' => $this->initiator_name,
Expand Down Expand Up @@ -504,7 +511,7 @@ public function accountBalance($shortcode, $identiertype, $remarks)
*/
public function reversal($shortcode, $transactionid, $amount, $remarks)
{
$url = $this->url.'/mpesa/reversal/v1/request';
$url = $this->url . '/mpesa/reversal/v1/request';

$body = [

Expand Down

0 comments on commit 34d740b

Please sign in to comment.