Skip to content

Commit

Permalink
(minor): Reversals Implementation
Browse files Browse the repository at this point in the history
- Added a function to encrypt initiator password using the safaricom public certs.
- Added safaricom cert vendor publishes
- Added default config to vendor publishes
- Abstracted the reverseTransaction method that will be used to reverse transactions (WIP)
  • Loading branch information
coolsam726 committed Jun 24, 2020
1 parent 7f28fdf commit f5156bc
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
38 changes: 38 additions & 0 deletions certs/live.cer
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-----BEGIN CERTIFICATE-----
MIIGkzCCBXugAwIBAgIKXfBp5gAAAD+hNjANBgkqhkiG9w0BAQsFADBbMRMwEQYK
CZImiZPyLGQBGRYDbmV0MRkwFwYKCZImiZPyLGQBGRYJc2FmYXJpY29tMSkwJwYD
VQQDEyBTYWZhcmljb20gSW50ZXJuYWwgSXNzdWluZyBDQSAwMjAeFw0xNzA0MjUx
NjA3MjRaFw0xODAzMjExMzIwMTNaMIGNMQswCQYDVQQGEwJLRTEQMA4GA1UECBMH
TmFpcm9iaTEQMA4GA1UEBxMHTmFpcm9iaTEaMBgGA1UEChMRU2FmYXJpY29tIExp
bWl0ZWQxEzARBgNVBAsTClRlY2hub2xvZ3kxKTAnBgNVBAMTIGFwaWdlZS5hcGlj
YWxsZXIuc2FmYXJpY29tLmNvLmtlMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIB
CgKCAQEAoknIb5Tm1hxOVdFsOejAs6veAai32Zv442BLuOGkFKUeCUM2s0K8XEsU
t6BP25rQGNlTCTEqfdtRrym6bt5k0fTDscf0yMCoYzaxTh1mejg8rPO6bD8MJB0c
FWRUeLEyWjMeEPsYVSJFv7T58IdAn7/RhkrpBl1dT7SmIZfNVkIlD35+Cxgab+u7
+c7dHh6mWguEEoE3NbV7Xjl60zbD/Buvmu6i9EYz+27jNVPI6pRXHvp+ajIzTSsi
eD8Ztz1eoC9mphErasAGpMbR1sba9bM6hjw4tyTWnJDz7RdQQmnsW1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AgYIKwYBBQUHAwEwJwYJKwYBBAGCNxUKBBowGDAKBggr
BgEFBQcDAjAKBggrBgEFBQcDATANBgkqhkiG9w0BAQsFAAOCAQEAC/hWx7KTwSYr
x2SOyyHNLTRmCnCJmqxA/Q+IzpW1mGtw4Sb/8jdsoWrDiYLxoKGkgkvmQmB2J3zU
ngzJIM2EeU921vbjLqX9sLWStZbNC2Udk5HEecdpe1AN/ltIoE09ntglUNINyCmf
zChs2maF0Rd/y5hGnMM9bX9ub0sqrkzL3ihfmv4vkXNxYR8k246ZZ8tjQEVsKehE
dqAmj8WYkYdWIHQlkKFP9ba0RJv7aBKb8/KP+qZ5hJip0I5Ey6JJ3wlEWRWUYUKh
gYoPHrJ92ToadnFCCpOlLKWc0xVxANofy6fqreOVboPO0qTAYpoXakmgeRNLUiar
0ah6M/q/KA==
-----END CERTIFICATE-----
35 changes: 35 additions & 0 deletions certs/sandbox.cer
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
-----BEGIN CERTIFICATE-----
MIIGKzCCBROgAwIBAgIQDL7NH8cxSdUpl0ihH0A1wTANBgkqhkiG9w0BAQsFADBN
MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMScwJQYDVQQDEx5E
aWdpQ2VydCBTSEEyIFNlY3VyZSBTZXJ2ZXIgQ0EwHhcNMTgwODI3MDAwMDAwWhcN
MTkwNDA0MTIwMDAwWjBuMQswCQYDVQQGEwJLRTEQMA4GA1UEBxMHTmFpcm9iaTEW
MBQGA1UEChMNU2FmYXJpY29tIFBMQzETMBEGA1UECxMKRGlnaXRhbCBJVDEgMB4G
A1UEAxMXc2FuZGJveC5zYWZhcmljb20uY28ua2UwggEiMA0GCSqGSIb3DQEBAQUA
A4IBDwAwggEKAoIBAQC78yeC/wLoZY6TJeqc4g/9eAKIpeCwEsjX09pD8ZxAGXqT
Oi7ssdIGJBPmJZNeEVyf8ocFhisCuLngJ9Z5e/AvH52PhrEFmVu2D03zSf4C+rhZ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K6HGwB4jDMIecmKaOcMS18R2jxwB3AId1v+dZfPiMQ5lf
vfNu/1aNR1Y2/0q1YMG06v9eoIMPAAABZXs1F8IAAAQDAEgwRgIhAIRq2XFiC+RS
uDCYq8ICJg0QafSV+e9BLpJnElEdaSjiAiEAyiiW4vxwv4cWcAXE6FAipctyUBs6
bE5QyaCnmNpoDiQwDQYJKoZIhvcNAQELBQADggEBAB0YoWve9Sxhb0PBS3Hc46Rf
a7H1jhHuwE+UyscSQsdJdk8uPAgDuKRZMvJPGEaCkNHm36NfcaXXFjPOl7LI1d1a
9zqSP0xeZBI6cF0x96WuQGrI9/WR2tfxjmaUSp8a/aJ6n+tZA28eJZNPrIaMm+6j
gh7AkKnqcf+g8F/MvCCVdNAiVMdz6UpCscf6BRPHNZ5ifvChGh7aUKjrVLLuF4Ls
HE05qm6HNyV5eTa6wvcbc4ewguN1UDZvPWetSyfBk10Wbpor4znQ4TJ3Y9uCvsJH
41ldblDvZZ2z4kB2UYQ7iBkPlJSxSOaFgW/GGDXq49sz/995xzhVITHxh2SdLkI=
-----END CERTIFICATE-----
8 changes: 6 additions & 2 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
* You can place your custom package configuration in here.
*/
return [

];
"mpesa" => [
"cert_path" => storage_path("mpesa"),
"live_cert_name" => "live.cer",
"sandbox_cert_name" => "sandbox.cer"
]
];
36 changes: 34 additions & 2 deletions src/Daraja.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,40 @@ public function registerCallbacks($shortCode,$confirmationURL, $validationURL=nu
}

/**
* Use this function to initiate a reversal request
* Generate an encrypted security credential from the initiator password on either live or sandbox env.
* @param string $initiatorPassword
* @param string $env live|sandbox
*/
public function encryptInitiatorPassword($initiatorPassword, $env="sandbox") {
if (!in_array($env,["live", "sandbox"])) {
throw new \Exception("Environment must be either live or sandbox");
}
$publicKey = config('daraja.mpesa.cert_path')."/".config('daraja.mpesa.'.$env.'_cert_name');
openssl_public_encrypt($initiatorPassword, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING);
return base64_encode($encrypted);
}

/**
* Use this function to initiate a reversal request. This is an abstracted function that takes care of SecurityCredential Generation
* @param $Initiator | The name of Initiator to initiating the request
* @param $InitiatorPassword | Plain Text Initiator Password for generating the security credential
* @param $TransactionID | Unique Id received with every transaction response.
* @param $Amount | Amount
* @param $ReceiverParty | Organization /MSISDN sending the transaction
* @param $ResultURL | The path that stores information of transaction
* @param $QueueTimeOutURL | The path that stores information of time out transaction
* @param $Remarks | Comments that are sent along with the transaction.
* @param $Occasion | Optional Parameter
* @return mixed|string
*/
public function reverseTransaction($Initiator, $InitiatorPassword, $TransactionID, $Amount, $ReceiverParty, $ResultURL, $QueueTimeOutURL, $Remarks, $Occasion) {
$SecurityCredential = $this->encryptInitiatorPassword($InitiatorPassword,$this->environment);
$CommandID = 'TransactionReversal';
$RecieverIdentifierType = 11;
return $this->reversal($CommandID,$Initiator,$SecurityCredential,$TransactionID,$Amount,$ReceiverParty,$RecieverIdentifierType,$ResultURL,$QueueTimeOutURL,$Remarks,$Occasion);
}
/**
* Use this function to initiate a reversal request. This is the lowest level function that can change even the Organization Id Type.
* @param $CommandID | Takes only 'TransactionReversal' Command id
* @param $Initiator | The name of Initiator to initiating the request
* @param $SecurityCredential | Encrypted Credential of user getting transaction amount
Expand Down Expand Up @@ -211,7 +244,6 @@ public function reversal($CommandID, $Initiator, $SecurityCredential, $Transacti
curl_setopt($curl, CURLOPT_HEADER, false);
$curl_response = curl_exec($curl);
return json_decode($curl_response);

}

/**
Expand Down
5 changes: 5 additions & 0 deletions src/DarajaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function boot()
__DIR__.'/../config/config.php' => config_path('daraja.php'),
], 'config');

// Publishing mpesa certs.
$this->publishes([
__DIR__.'/../certs' => storage_path('mpesa'),
], 'certs');

// Publishing the views.
/*$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/daraja'),
Expand Down

0 comments on commit f5156bc

Please sign in to comment.