44
55use Cybersource \GlobalParameter ;
66use CyberSource \Authentication \Util \Cache as Cache ;
7- use CyberSource \Logging \LogFactory as LogFactory ;
7+ use \ CyberSource \Logging \LogFactory as LogFactory ;
88use CyberSource \Logging \LogConfiguration ;
99use Jose \Component \Core \JWK ;
1010use Jose \Component \Core \AlgorithmManager ;
2323
2424class MLEUtility
2525{
26- private $ logger = null ;
26+ private static $ logger = null ;
2727
2828 private static $ cache = null ;
2929
@@ -50,30 +50,30 @@ public static function checkIsMLEForAPI($merchantConfig, $isMLESupportedByCybsFo
5050
5151 public static function encryptRequestPayload ($ merchantConfig , $ requestBody )
5252 {
53- $ logger = (new LogFactory ())->getLogger (\CyberSource \Utilities \Helpers \ClassHelper::getClassName (get_class ()), $ merchantConfig ->getLogConfiguration ());
54-
55- $ mleCert = self ::getMLECert ($ merchantConfig , $ logger );
53+ if (self ::$ logger === null ) {
54+ self ::$ logger = (new LogFactory ())->getLogger (\CyberSource \Utilities \Helpers \ClassHelper::getClassName (get_class ()), $ merchantConfig ->getLogConfiguration ());
55+ }
56+ $ mleCert = self ::getMLECert ($ merchantConfig );
5657
5758 if ($ merchantConfig ->getLogConfiguration ()->isMaskingEnabled ()) {
5859 $ printRequestBody = \CyberSource \Utilities \Helpers \DataMasker::maskData ($ requestBody );
5960 } else {
6061 $ printRequestBody = $ requestBody ;
6162 }
6263
63- $ logger ->debug ("Request before MLE: \n" . print_r ($ printRequestBody , true ));
64+ self :: $ logger ->debug ("Request before MLE: \n" . print_r ($ printRequestBody , true ));
6465
65- $ jweToken = self ::generateToken ($ mleCert , $ requestBody, $ logger );
66+ $ jweToken = self ::generateToken ($ mleCert , $ requestBody );
6667 $ mleRequest = json_encode (['encryptedRequest ' => $ jweToken ]);
6768
68- $ logger ->debug ("Request after MLE: \n" . print_r ($ mleRequest , true ));
69- // self::$logger->close();
69+ self ::$ logger ->debug ("Request after MLE: \n" . print_r ($ mleRequest , true ));
7070 return $ mleRequest ;
7171 }
7272
73- private static function generateToken ($ cert , $ requestBody, $ logger )
73+ private static function generateToken ($ cert , $ requestBody )
7474 {
7575 try {
76- $ serialNumber = self ::extractSerialNumber ($ cert, $ logger );
76+ $ serialNumber = self ::extractSerialNumber ($ cert );
7777
7878 $ publicKey = openssl_pkey_get_details (openssl_pkey_get_public ($ cert ))['key ' ];
7979
@@ -114,12 +114,12 @@ private static function generateToken($cert, $requestBody, $logger)
114114 $ serializer = new CompactSerializer ();
115115 return $ serializer ->serialize ($ jwe );
116116 } catch (\Exception $ e ) {
117- $ logger ->error ("Error encrypting request payload: " . $ e ->getMessage ());
117+ self :: $ logger ->error ("Error encrypting request payload: " . $ e ->getMessage ());
118118 throw new MLEException ("Error encrypting request payload: " . $ e ->getMessage ());
119119 }
120120 }
121121
122- public static function getMLECert ($ merchantConfig, $ logger )
122+ public static function getMLECert ($ merchantConfig )
123123 {
124124 try {
125125 if (!isset (self ::$ cache )) {
@@ -131,18 +131,19 @@ public static function getMLECert($merchantConfig, $logger)
131131 $ x509Cert = $ fileCache ['mle_cert ' ];
132132
133133 if ($ x509Cert ) {
134- self ::validateCertificateExpiry ($ x509Cert , $ merchantConfig ->getMleKeyAlias (), $ logger );
134+ self ::validateCertificateExpiry ($ x509Cert , $ merchantConfig ->getMleKeyAlias ());
135+ // throw new MLEException("Certificate with MLE alias $keyAlias is expired.");
135136 return $ x509Cert ;
136137 } else {
137- throw new MLEException ("Certificate with alias $ mleKeyAlias not found. " );
138+ throw new MLEException ("Certificate with alias " . $ merchantConfig -> getMleKeyAlias () . " not found " );
138139 }
139140 } catch (\Exception $ e ) {
140- $ logger ->error ("Error fetching MLE certificate: " . $ e ->getMessage ());
141+ self :: $ logger ->error ("Error fetching MLE certificate: " . $ e ->getMessage ());
141142 throw new MLEException ("Error fetching MLE certificate: " . $ e ->getMessage ());
142143 }
143144 }
144145
145- public static function extractSerialNumber ($ cert, $ logger )
146+ public static function extractSerialNumber ($ cert )
146147 {
147148 try {
148149 $ certDetails = openssl_x509_parse ($ cert );
@@ -153,42 +154,41 @@ public static function extractSerialNumber($cert, $logger)
153154 }
154155
155156 if ($ serialNumber === null ) {
156- $ logger ->warning ("Serial number not found in MLE certificate for alias. " );
157- // this will be in hexdec is it fine?
157+ self ::$ logger ->warning ("Serial number not found in MLE certificate for alias. " );
158158 $ serialNumber = $ certDetails ['serialNumber ' ];
159159 }
160160 return $ serialNumber ;
161161 } catch (\Exception $ e ) {
162- $ logger ->error ("Error extracting serial number from certificate: " . $ e ->getMessage ());
162+ self :: $ logger ->error ("Error extracting serial number from certificate: " . $ e ->getMessage ());
163163 throw new MLEException ("Error extracting serial number from certificate: " . $ e ->getMessage ());
164164 }
165165 }
166166
167- public static function validateCertificateExpiry ($ certificate , $ keyAlias, $ logger )
167+ public static function validateCertificateExpiry ($ certificate , $ keyAlias )
168168 {
169169 try {
170170 $ certDetails = openssl_x509_parse ($ certificate );
171171 $ notValidAfter = isset ($ certDetails ['validTo_time_t ' ]) ? $ certDetails ['validTo_time_t ' ] : null ;
172172
173173 if ($ notValidAfter === null ) {
174- $ logger ->warning ("Certificate with MLE alias $ keyAlias does not have a valid expiry date. " );
175- throw new MLEException ("Certificate with MLE alias $ keyAlias does not have a valid expiry date. " );
174+ self ::$ logger ->warning ("Certificate with MLE alias $ keyAlias does not have a valid expiry date. " );
176175 }
177176
178177 if ($ notValidAfter < time ()) {
179- $ logger ->warning ("Certificate with MLE alias $ keyAlias is expired as of " . date ('Y-m-d H:i:s ' , $ notValidAfter ) . ". Please update p12 file. " );
180- // throw new MLEException("Certificate with MLE alias $keyAlias is expired.");
178+ self :: $ logger ->warning ("Certificate with MLE alias $ keyAlias is expired as of " . date ('Y-m-d H:i:s ' , $ notValidAfter ) . ". Please update p12 file. " );
179+ // throw new MLEException("Certificate with MLE alias $keyAlias is expired.");
181180 } else {
182181 $ timeToExpire = $ notValidAfter - time ();
183182 $ warningPeriod = GlobalLabelParameters::CERTIFICATE_EXPIRY_DATE_WARNING_DAYS * 24 * 60 * 60 ;
184183
185184 if ($ timeToExpire < $ warningPeriod ) {
186- $ logger ->warning ("Certificate for MLE with alias $ keyAlias is going to expire on " . date ('Y-m-d H:i:s ' , $ notValidAfter ) . ". Please update p12 file before that. " );
185+ self :: $ logger ->warning ("Certificate for MLE with alias $ keyAlias is going to expire on " . date ('Y-m-d H:i:s ' , $ notValidAfter ) . ". Please update p12 file before that. " );
187186 }
188187 }
189188 } catch (\Exception $ e ) {
190- $ logger ->error ("Error while checking certificate expiry: " . $ e ->getMessage ());
191- }
189+ self ::$ logger ->error ("Error validating certificate expiry: " . $ e ->getMessage ());
190+ // throw new MLEException("Error validating certificate expiry: " . $e->getMessage());
191+ }
192192 }
193193}
194194?>
0 commit comments