Skip to content

Commit 0fbe87a

Browse files
committed
Fix: Created a method to extract public key from mpesa certificates
1 parent f5156bc commit 0fbe87a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Daraja.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,20 @@ public function encryptInitiatorPassword($initiatorPassword, $env="sandbox") {
164164
if (!in_array($env,["live", "sandbox"])) {
165165
throw new \Exception("Environment must be either live or sandbox");
166166
}
167-
$publicKey = config('daraja.mpesa.cert_path')."/".config('daraja.mpesa.'.$env.'_cert_name');
168-
openssl_public_encrypt($initiatorPassword, $encrypted, $publicKey, OPENSSL_PKCS1_PADDING);
167+
$publicKeyFile = config('daraja.mpesa.cert_path')."/".config('daraja.mpesa.'.$env.'_cert_name');
168+
$publicKey = $this->preparePublicKey($publicKeyFile);
169+
openssl_public_encrypt($initiatorPassword, $encrypted,$publicKey, OPENSSL_PKCS1_PADDING);
169170
return base64_encode($encrypted);
170171
}
171172

173+
public function preparePublicKey($publicKeyFile) {
174+
$pkey = file_get_contents(storage_path("mpesa/sandbox.cer"));
175+
$pkey = str_replace(["\r", "\n"],"", $pkey);
176+
$pkey = str_replace(["-----BEGIN CERTIFICATE-----"],"-----BEGIN CERTIFICATE-----\n", $pkey);
177+
$final = str_replace(["-----END CERTIFICATE-----"],"\n-----END CERTIFICATE-----", $pkey);
178+
return $final;
179+
}
180+
172181
/**
173182
* Use this function to initiate a reversal request. This is an abstracted function that takes care of SecurityCredential Generation
174183
* @param $Initiator | The name of Initiator to initiating the request

0 commit comments

Comments
 (0)