@@ -164,11 +164,20 @@ public function encryptInitiatorPassword($initiatorPassword, $env="sandbox") {
164
164
if (!in_array ($ env ,["live " , "sandbox " ])) {
165
165
throw new \Exception ("Environment must be either live or sandbox " );
166
166
}
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 );
169
170
return base64_encode ($ encrypted );
170
171
}
171
172
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
+
172
181
/**
173
182
* Use this function to initiate a reversal request. This is an abstracted function that takes care of SecurityCredential Generation
174
183
* @param $Initiator | The name of Initiator to initiating the request
0 commit comments