5
5
*/
6
6
namespace Magento \Authorizenet \Model ;
7
7
8
+ use Magento \Payment \Model \Method \Logger ;
9
+
8
10
/**
9
11
* @SuppressWarnings(PHPMD.TooManyFields)
10
12
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
@@ -54,6 +56,8 @@ abstract class Authorizenet extends \Magento\Payment\Model\Method\Cc
54
56
55
57
const RESPONSE_REASON_CODE_PENDING_REVIEW_DECLINED = 254 ;
56
58
59
+ const PAYMENT_UPDATE_STATUS_CODE_SUCCESS = 'Ok ' ;
60
+
57
61
/**
58
62
* Transaction fraud state key
59
63
*/
@@ -95,6 +99,11 @@ abstract class Authorizenet extends \Magento\Payment\Model\Method\Cc
95
99
*/
96
100
protected $ transactionDetails = [];
97
101
102
+ /**
103
+ * {@inheritdoc}
104
+ */
105
+ protected $ _debugReplacePrivateDataKeys = ['merchantAuthentication ' , 'x_login ' ];
106
+
98
107
/**
99
108
* @param \Magento\Framework\Model\Context $context
100
109
* @param \Magento\Framework\Registry $registry
@@ -364,11 +373,11 @@ protected function buildRequest(\Magento\Framework\DataObject $payment)
364
373
*/
365
374
protected function postRequest (\Magento \Authorizenet \Model \Request $ request )
366
375
{
367
- $ debugData = ['request ' => $ request ->getData ()];
368
376
$ result = $ this ->responseFactory ->create ();
369
377
$ client = new \Magento \Framework \HTTP \ZendClient ();
370
- $ uri = $ this ->getConfigData ('cgi_url ' );
371
- $ client ->setUri ($ uri ? $ uri : self ::CGI_URL );
378
+ $ url = $ this ->getConfigData ('cgi_url ' ) ?: self ::CGI_URL ;
379
+ $ debugData = ['url ' => $ url , 'request ' => $ request ->getData ()];
380
+ $ client ->setUri ($ url );
372
381
$ client ->setConfig (['maxredirects ' => 0 , 'timeout ' => 30 ]);
373
382
374
383
foreach ($ request ->getData () as $ key => $ value ) {
@@ -381,21 +390,21 @@ protected function postRequest(\Magento\Authorizenet\Model\Request $request)
381
390
382
391
try {
383
392
$ response = $ client ->request ();
393
+ $ responseBody = $ response ->getBody ();
394
+ $ debugData ['response ' ] = $ responseBody ;
384
395
} catch (\Exception $ e ) {
385
396
$ result ->setXResponseCode (-1 )
386
397
->setXResponseReasonCode ($ e ->getCode ())
387
398
->setXResponseReasonText ($ e ->getMessage ());
388
399
389
- $ debugData ['result ' ] = $ result ->getData ();
390
- $ this ->_debug ($ debugData );
391
400
throw new \Magento \Framework \Exception \LocalizedException (
392
401
$ this ->dataHelper ->wrapGatewayError ($ e ->getMessage ())
393
402
);
403
+ } finally {
404
+ $ this ->_debug ($ debugData );
394
405
}
395
406
396
- $ responseBody = $ response ->getBody ();
397
407
$ r = explode (self ::RESPONSE_DELIM_CHAR , $ responseBody );
398
-
399
408
if ($ r ) {
400
409
$ result ->setXResponseCode ((int )str_replace ('" ' , '' , $ r [0 ]))
401
410
->setXResponseReasonCode ((int )str_replace ('" ' , '' , $ r [2 ]))
@@ -413,10 +422,6 @@ protected function postRequest(\Magento\Authorizenet\Model\Request $request)
413
422
__ ('Something went wrong in the payment gateway. ' )
414
423
);
415
424
}
416
-
417
- $ debugData ['result ' ] = $ result ->getData ();
418
- $ this ->_debug ($ debugData );
419
-
420
425
return $ result ;
421
426
}
422
427
@@ -473,24 +478,35 @@ protected function loadTransactionDetails($transactionId)
473
478
);
474
479
475
480
$ client = new \Magento \Framework \HTTP \ZendClient ();
476
- $ uri = $ this ->getConfigData ('cgi_url_td ' );
477
- $ client ->setUri ($ uri ? $ uri : self :: CGI_URL_TD );
481
+ $ url = $ this ->getConfigData ('cgi_url_td ' ) ?: self :: CGI_URL_TD ;
482
+ $ client ->setUri ($ url );
478
483
$ client ->setConfig (['timeout ' => 45 ]);
479
484
$ client ->setHeaders (['Content-Type: text/xml ' ]);
480
485
$ client ->setMethod (\Zend_Http_Client::POST );
481
486
$ client ->setRawData ($ requestBody );
482
487
483
- $ debugData = ['request ' => $ requestBody ];
488
+ $ debugData = ['url ' => $ url , ' request ' => $ this -> removePrivateDataFromXml ( $ requestBody) ];
484
489
485
490
try {
486
491
$ responseBody = $ client ->request ()->getBody ();
487
- $ debugData ['result ' ] = $ responseBody ;
488
- $ this ->_debug ($ debugData );
492
+ $ debugData ['response ' ] = $ responseBody ;
489
493
libxml_use_internal_errors (true );
490
494
$ responseXmlDocument = new \Magento \Framework \Simplexml \Element ($ responseBody );
491
495
libxml_use_internal_errors (false );
492
496
} catch (\Exception $ e ) {
493
- throw new \Magento \Framework \Exception \LocalizedException (__ ('Payment updating error. ' ));
497
+ throw new \Magento \Framework \Exception \LocalizedException (
498
+ __ ('Unable to get transaction details. Try again later. ' )
499
+ );
500
+ } finally {
501
+ $ this ->_debug ($ debugData );
502
+ }
503
+
504
+ if (!isset ($ responseXmlDocument ->messages ->resultCode )
505
+ || $ responseXmlDocument ->messages ->resultCode != static ::PAYMENT_UPDATE_STATUS_CODE_SUCCESS
506
+ ) {
507
+ throw new \Magento \Framework \Exception \LocalizedException (
508
+ __ ('Unable to get transaction details. Try again later. ' )
509
+ );
494
510
}
495
511
496
512
$ this ->transactionDetails [$ transactionId ] = $ responseXmlDocument ;
@@ -509,4 +525,20 @@ protected function getTransactionDetails($transactionId)
509
525
? $ this ->transactionDetails [$ transactionId ]
510
526
: $ this ->loadTransactionDetails ($ transactionId );
511
527
}
528
+
529
+ /**
530
+ * Remove nodes with private data from XML string
531
+ *
532
+ * Uses values from $_debugReplacePrivateDataKeys property
533
+ *
534
+ * @param string $xml
535
+ * @return string
536
+ */
537
+ protected function removePrivateDataFromXml ($ xml )
538
+ {
539
+ foreach ($ this ->getDebugReplacePrivateDataKeys () as $ key ) {
540
+ $ xml = preg_replace (sprintf ('~(?<=<%s>).*?(?=</%s>)~ ' , $ key , $ key ), Logger::DEBUG_KEYS_MASK , $ xml );
541
+ }
542
+ return $ xml ;
543
+ }
512
544
}
0 commit comments