Skip to content

Commit

Permalink
Refactore Mpesa class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephencoduor committed Sep 22, 2022
1 parent cae782c commit 1210c51
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 70 deletions.
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ $ composer require stephencoduor/mpesa
use Stephencoduor\Mpesa\Mpesa;


$mpesastk = new stk(
$mpesa = new Mpesa(

[

"consumer_key" => "Your Consumer Key",
"consumer_secret" => "Your Consumer Secret",
"shortcode" => "174379",
"shortcode1" => "174379",
"store" => "174379",
"passkey" => "Your Online Passkey",
'initiator_name' => 'Your initiator_name',
'initiator_pass' => 'Your initiator_pass',
Expand All @@ -49,7 +49,7 @@ $ composer require stephencoduor/mpesa
"consumer_key" => "Your Consumer Key",
"consumer_secret" => "Your Consumer Secret",
"shortcode" => "174379",
"shortcode1" => "174379",
"strore" => "174379",
"passkey" => "Your Online Passkey",
'initiator_name' => 'Your initiator_name',
'initiator_pass' => 'Your initiator_pass',
Expand All @@ -62,29 +62,29 @@ $ composer require stephencoduor/mpesa
]


$mpesac2b = new C2B(array $config);
$mpesa = new Mpesa(array $config);




---------------Now you can call functions on Mpesa as Below ----------------

echo " Token : " . $mpesa->oauth_token();
$stk = new STK($config);
$stk->stkPushQuery('ws_CO_DMZ_297481201_09042019174418021');
$mpesa->stk('1','254708374149','pay now','test');
$mpesa = new Mpesa($config);
$mpesa->stk()->stkPushQuery('ws_CO_DMZ_297481201_09042019174418021');
$mpesa->stk()->stkPush('1','254708374149','pay now','test');

$mpesac2b = new C2B($config);
$mpesac2b->register_url();
$mpesac2b->c2b('1000', '254708374149', 'account');

$mpesa->c2b()->register_url();
$mpesa->c2b()->c2bPay('1000', '254708374149', 'account');

$mpesab2c = new B2C($config)
mpesab2c->b2c('200', 'BusinessPayment', '254708374149', 'payment','b2c_timeout','b2c_result'); // last two parameter define callback https://example.com/result_url.php/b2c_timeout/ or https://example.com/result_url/b2c_result/

$mpesab2b = new B2B($config)
$mpesab2b->b2b('10000','BusinessPayBill','60000','4','4','paytest','cool','b2b_timeout','b2b_result');
$mpesa->b2c()->b2cSend('200', 'BusinessPayment', '254708374149', 'payment','b2c_timeout','b2c_result'); // last two parameter define callback https://example.com/result_url.php/b2c_timeout/ or https://example.com/result_url/b2c_result/


$mpesa->b2c()->b2bSend('10000','BusinessPayBill','60000','4','4','paytest','cool','b2b_timeout','b2b_result');

$mpesa = new Service($config)
$mpesa = new Mpesa($config)
$mpesa->accountbalance('600443','4','remarks','acc_timeout','acc_result');
$mpesa->reversal('2','254708374149','1','NCR7S1UXBT','PAY NOW');
$mpesa->transaction_status('NCR7S1UXBT','254708374149','4','apitest');
Expand Down
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@
}
},

"require": {
"php": ">=7.4",
"guzzlehttp/guzzle": ">=7.2.0",
"ext-curl": "*",
"ext-dom": "*",
"ext-hash": "*",
"ext-xmlwriter": "*"
},


"extra": {
"branch-alias": {
"dev-master": "v1.1-dev"
Expand Down
6 changes: 3 additions & 3 deletions src/B2B.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stephencoduor\Mpesa;

class B2B extends Service
class B2B extends Mpesa
{
public function __construct(array $config)
{
Expand All @@ -13,7 +13,7 @@ public function __construct(array $config)
}

/** B2B Payment Request transactions between a business and another business
* Api requires a valid and verifiedB2B Service shortcode for the business initiating the transaction
* Api requires a valid and verifiedB2B Mpesa shortcode for the business initiating the transaction
* andthe bothbusiness involved in the transaction
* Command ID : BussinessPayBill ,MerchantToMerchantTransfer,MerchantTransferFromMerchantToWorking,MerchantServucesMMFAccountTransfer,AgencyFloatAdvance
*
Expand All @@ -26,7 +26,7 @@ public function __construct(array $config)
* @param string $remarks
* @return array object
*/
public function b2b($Amount, $commandId, $PartyB, $RecieverIdentifierType, $SenderIdentifierType, $AccountReference, $Remarks, $timeout_url, $result_url)
public function b2bSend($Amount, $commandId, $PartyB, $RecieverIdentifierType, $SenderIdentifierType, $AccountReference, $Remarks, $timeout_url, $result_url)
{
$url = $this->env('/mpesa/b2b/v1/paymentrequest');

Expand Down
4 changes: 2 additions & 2 deletions src/B2C.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stephencoduor\Mpesa;

class B2C extends Service
class B2C extends Mpesa
{

public function __construct(array $config)
Expand All @@ -25,7 +25,7 @@ public function __construct(array $config)
* @param string $ocassion | optional
* @return array object
*/
public function b2c($amount, $commandId, $receiver, $remark, $occassion = null, $timeout_url, $result_url)
public function b2cSend($amount, $commandId, $receiver, $remark, $occassion = null, $timeout_url, $result_url)
{
$url = $this->env('mpesa/b2c/v1/paymentrequest');

Expand Down
6 changes: 3 additions & 3 deletions src/C2B.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stephencoduor\Mpesa;

class C2B extends Service
class C2B extends Mpesa
{

public function __construct(array $config)
Expand All @@ -17,7 +17,7 @@ public function __construct(array $config)
/** C2B enable Paybill and buy goods merchants to integrate to mpesa and receive real time payment notification
* C2B register URL API register the 3rd party's confirmation and validation url to mpesa
* which then maps these URLs to the 3rd party shortcode whenever mpesa receives atransaction on the shortcode
* Service triggers avalidation request against the validation URL and the 3rd party system responds to mpesa
* Mpesa triggers avalidation request against the validation URL and the 3rd party system responds to mpesa
* with a validation response (eithera success or an error code)
*
* @return json
Expand Down Expand Up @@ -45,7 +45,7 @@ public function register_url()
* @param int $BillRefNumber | Bill Reference Number (Optional).
* @return array object
*/
public function c2b($Amount, $Msisdn, $BillRefNumber = NULL)
public function c2bPay($Amount, $Msisdn, $BillRefNumber = NULL)
{
$url = $this->env('mpesa/c2b/v1/simulate');

Expand Down
43 changes: 28 additions & 15 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Stephencoduor\Mpesa;


use Exception;

class Config
{
Expand All @@ -11,6 +11,7 @@ class Config
protected string $consumer_key;
protected string $consumer_secret;
protected string $shortcode;
protected string $store;
protected string $pass_key;
protected string $initiator_name;
protected string $initiator_pass;
Expand All @@ -28,26 +29,38 @@ class Config
protected function __construct(array $config) {


if(isset($config['consumer_key'])) {
$this->consumer_key = $config['consumer_key'];
} else {
throw new Exception('Missing Parameter consumer_key"', 400);
}
if(isset($config['consumer_secret'])) {
$this->consumer_secret = $config['consumer_secret'];
} else {
throw new Exception('Missing parameter "consumer_secret"', 400);
}

$this->consumer_key = $config['consumer_key'];
$this->consumer_secret = $config['consumer_secret'];
$this->shortcode = $config['shortcode'];
$this->shortcode1 = $config['shortcode1'];
$this->pass_key = $config['pass_key'];
$this->initiator_name = $config['initiator_name'];
$this->initiator_pass = $config['initiator_pass'];
$this->callback_url = $config['callback_url'];
$this->confirmation_url = $config['confirmation_url'];
$this->timeout_url = $config['timeout_url'];
$this->validation_url = $config['validation_url'];
$this->result_url = $config['result_url'];
$this->env = $config['env '];
if(isset($config['pass_key'])) {
$this->pass_key = $config['pass_key'];
} else {
throw new InvalidRequestException('Missing parameter "pass_key"', 400);
}

$this->shortcode = (isset($config['shortcode'])) ? $config['shortcode'] :null;
$this->initiator_name = (isset($config['initiator_name'])) ? $config['initiator_name'] :null;
$this->initiator_pass = (isset($config['initiator_pass'])) ? $config['initiator_pass'] :null;
$this->store = (isset($config['store'])) ? $config['store'] :null;
$this->callback_url = (isset($config['callback_url'])) ? $config['callback_url'] :null;
$this->confirmation_url = (isset($config['confirmation_url'])) ? $config['confirmation_url'] :null;
$this->timeout_url = (isset($config['timeout_url'])) ? $config['timeout_url'] :null;
$this->validation_url = (isset($config['validation_url'])) ? $config['validation_url'] :null;
$this->result_url = (isset($config['result_url'])) ? $config['result_url'] :null;
$this->env = (isset($config['env'])) ? $config['env'] :true;

$this->live_endpoint = 'https://api.safaricom.co.ke/';
$this->sandbox_endpoint = 'https://sandbox.safaricom.co.ke/';



}


Expand Down
Loading

0 comments on commit 1210c51

Please sign in to comment.