Skip to content

Commit

Permalink
Merge pull request #31 from ngarawakimani/master
Browse files Browse the repository at this point in the history
added symphony Dotenv component package support for non-symphony and …
  • Loading branch information
mossey authored Jan 6, 2019
2 parents d598292 + af7ea25 commit c66895d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 22 deletions.
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"psr-4": {
"Safaricom\\Mpesa\\": "src/"
}
},
"require-dev": {
"symfony/dotenv": "^4.3@dev"
}
}

115 changes: 94 additions & 21 deletions src/Mpesa.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,47 @@
*/
namespace Safaricom\Mpesa;

include_once("../vendor/autoload.php");

use Symfony\Component\Dotenv\Dotenv;

/**
* Class Mpesa
* @package Safaricom\Mpesa
*/
class Mpesa
{

/**
* Define env method similar to laravel's
*
* @param String $env_param | Environment Param Name
*
* @return String | Actual Param
*/
public static function env($env_param){

$dotenv = new Dotenv();

$dotenv->load('../.env');

$env = getenv($env_param);

return $env;
}
/**
* This is used to generate tokens for the live environment
* @return mixed
*/
public static function generateLiveToken(){
$consumer_key=env("MPESA_CONSUMER_KEY");
$consumer_secret=env("MPESA_CONSUMER_SECRET");

try {
$consumer_key = env("MPESA_CONSUMER_KEY");
$consumer_secret = env("MPESA_CONSUMER_SECRET");
} catch (\Throwable $th) {
$consumer_key = self::env("MPESA_CONSUMER_KEY");
$consumer_secret = self::env("MPESA_CONSUMER_SECRET");
}

if(!isset($consumer_key)||!isset($consumer_secret)){
die("please declare the consumer key and consumer secret as defined in the documentation");
Expand Down Expand Up @@ -48,8 +75,15 @@ public static function generateLiveToken(){
* @return mixed
*/
public static function generateSandBoxToken(){
$consumer_key= env("MPESA_CONSUMER_KEY");
$consumer_secret= env("MPESA_CONSUMER_SECRET");

try {
$consumer_key = env("MPESA_CONSUMER_KEY");
$consumer_secret = env("MPESA_CONSUMER_SECRET");
} catch (\Throwable $th) {
$consumer_key = self::env("MPESA_CONSUMER_KEY");
$consumer_secret = self::env("MPESA_CONSUMER_SECRET");
}

if(!isset($consumer_key)||!isset($consumer_secret)){
die("please declare the consumer key and consumer secret as defined in the documentation");
}
Expand Down Expand Up @@ -84,7 +118,12 @@ public static function generateSandBoxToken(){
* @return mixed|string
*/
public static function reversal($CommandID, $Initiator, $SecurityCredential, $TransactionID, $Amount, $ReceiverParty, $RecieverIdentifierType, $ResultURL, $QueueTimeOutURL, $Remarks, $Occasion){
$environment=env("MPESA_ENV");

try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/reversal/v1/request';
Expand Down Expand Up @@ -142,7 +181,12 @@ public static function reversal($CommandID, $Initiator, $SecurityCredential, $Tr
* @return string
*/
public static function b2c($InitiatorName, $SecurityCredential, $CommandID, $Amount, $PartyA, $PartyB, $Remarks, $QueueTimeOutURL, $ResultURL, $Occasion){
$environment=env("MPESA_ENV");

try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/b2c/v1/paymentrequest';
Expand Down Expand Up @@ -194,8 +238,13 @@ public static function b2c($InitiatorName, $SecurityCredential, $CommandID, $Amo
* @return mixed|string
*/
public static function c2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRefNumber ){
$environment=env("MPESA_ENV");


try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/c2b/v1/simulate';
$token=self::generateLiveToken();
Expand Down Expand Up @@ -245,8 +294,12 @@ public static function c2b($ShortCode, $CommandID, $Amount, $Msisdn, $BillRef
* @return mixed|string
*/
public static function accountBalance($CommandID, $Initiator, $SecurityCredential, $PartyA, $IdentifierType, $Remarks, $QueueTimeOutURL, $ResultURL){
$environment=env("MPESA_ENV");


try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/accountbalance/v1/query';
Expand Down Expand Up @@ -300,8 +353,13 @@ public static function accountBalance($CommandID, $Initiator, $SecurityCredentia
* @return mixed|string
*/
public function transactionStatus($Initiator, $SecurityCredential, $CommandID, $TransactionID, $PartyA, $IdentifierType, $ResultURL, $QueueTimeOutURL, $Remarks, $Occasion){
$environment=env("MPESA_ENV");


try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/transactionstatus/v1/query';
$token=self::generateLiveToken();
Expand Down Expand Up @@ -361,8 +419,13 @@ public function transactionStatus($Initiator, $SecurityCredential, $CommandID, $
* @return mixed|string
*/
public function b2b($Initiator, $SecurityCredential, $Amount, $PartyA, $PartyB, $Remarks, $QueueTimeOutURL, $ResultURL, $AccountReference, $commandID, $SenderIdentifierType, $RecieverIdentifierType){
$environment=env("MPESA_ENV");


try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/b2b/v1/paymentrequest';
$token=self::generateLiveToken();
Expand Down Expand Up @@ -414,13 +477,19 @@ public function b2b($Initiator, $SecurityCredential, $Amount, $PartyA, $PartyB,
* @return mixed|string
*/
public function STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $TransactionType, $Amount, $PartyA, $PartyB, $PhoneNumber, $CallBackURL, $AccountReference, $TransactionDesc, $Remark){
$environment=env("MPESA_ENV");

try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
$token=self::generateLiveToken();
$token = self::generateLiveToken();
}elseif ($environment=="sandbox"){
$url = 'https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest';
$token=self::generateSandBoxToken();
$token = self::generateSandBoxToken();
}else{
return json_encode(["Message"=>"invalid application status"]);
}
Expand All @@ -446,8 +515,7 @@ public function STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $Tran
'PhoneNumber' => $PhoneNumber,
'CallBackURL' => $CallBackURL,
'AccountReference' => $AccountReference,
'TransactionDesc' => $TransactionType,
'Remark'=> $Remark
'TransactionDesc' => $TransactionType
);

$data_string = json_encode($curl_post_data);
Expand All @@ -472,8 +540,13 @@ public function STKPushSimulation($BusinessShortCode, $LipaNaMpesaPasskey, $Tran
* @return mixed|string
*/
public static function STKPushQuery($environment, $checkoutRequestID, $businessShortCode, $password, $timestamp){
$environment=env("MPESA_ENV");


try {
$environment = env("MPESA_ENV");
} catch (\Throwable $th) {
$environment = self::env("MPESA_ENV");
}

if( $environment =="live"){
$url = 'https://api.safaricom.co.ke/mpesa/stkpushquery/v1/query';
$token=self::generateLiveToken();
Expand Down

0 comments on commit c66895d

Please sign in to comment.