Skip to content

Commit

Permalink
Fix: Created a method to extract public key from mpesa certificates
Browse files Browse the repository at this point in the history
  • Loading branch information
coolsam726 committed Jun 24, 2020
1 parent f5156bc commit 0fbe87a
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/Daraja.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,20 @@ 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);
$publicKeyFile = config('daraja.mpesa.cert_path')."/".config('daraja.mpesa.'.$env.'_cert_name');
$publicKey = $this->preparePublicKey($publicKeyFile);
openssl_public_encrypt($initiatorPassword, $encrypted,$publicKey, OPENSSL_PKCS1_PADDING);
return base64_encode($encrypted);
}

public function preparePublicKey($publicKeyFile) {
$pkey = file_get_contents(storage_path("mpesa/sandbox.cer"));
$pkey = str_replace(["\r", "\n"],"", $pkey);
$pkey = str_replace(["-----BEGIN CERTIFICATE-----"],"-----BEGIN CERTIFICATE-----\n", $pkey);
$final = str_replace(["-----END CERTIFICATE-----"],"\n-----END CERTIFICATE-----", $pkey);
return $final;
}

/**
* 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
Expand Down

0 comments on commit 0fbe87a

Please sign in to comment.