6
6
namespace Magento \Authorizenet \Model ;
7
7
8
8
use Magento \Authorizenet \Model \TransactionService ;
9
+ use Magento \Framework \App \ObjectManager ;
9
10
use Magento \Framework \HTTP \ZendClientFactory ;
10
11
use Magento \Payment \Model \Method \ConfigInterface ;
11
12
use Magento \Payment \Model \Method \TransparentInterface ;
12
13
use Magento \Sales \Model \Order \Email \Sender \OrderSender ;
14
+ use Magento \Sales \Api \PaymentFailuresInterface ;
13
15
14
16
/**
15
17
* Authorize.net DirectPost payment method model.
@@ -124,6 +126,16 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
124
126
*/
125
127
private $ psrLogger ;
126
128
129
+ /**
130
+ * @var PaymentFailuresInterface
131
+ */
132
+ private $ paymentFailures ;
133
+
134
+ /**
135
+ * @var \Magento\Sales\Model\Order
136
+ */
137
+ private $ order ;
138
+
127
139
/**
128
140
* @param \Magento\Framework\Model\Context $context
129
141
* @param \Magento\Framework\Registry $registry
@@ -147,6 +159,7 @@ class Directpost extends \Magento\Authorizenet\Model\Authorizenet implements Tra
147
159
* @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
148
160
* @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
149
161
* @param array $data
162
+ * @param PaymentFailuresInterface|null $paymentFailures
150
163
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
151
164
*/
152
165
public function __construct (
@@ -171,7 +184,8 @@ public function __construct(
171
184
\Magento \Sales \Api \TransactionRepositoryInterface $ transactionRepository ,
172
185
\Magento \Framework \Model \ResourceModel \AbstractResource $ resource = null ,
173
186
\Magento \Framework \Data \Collection \AbstractDb $ resourceCollection = null ,
174
- array $ data = []
187
+ array $ data = [],
188
+ PaymentFailuresInterface $ paymentFailures = null
175
189
) {
176
190
$ this ->orderFactory = $ orderFactory ;
177
191
$ this ->storeManager = $ storeManager ;
@@ -180,6 +194,8 @@ public function __construct(
180
194
$ this ->orderSender = $ orderSender ;
181
195
$ this ->transactionRepository = $ transactionRepository ;
182
196
$ this ->_code = static ::METHOD_CODE ;
197
+ $ this ->paymentFailures = $ paymentFailures ? : ObjectManager::getInstance ()
198
+ ->get (PaymentFailuresInterface::class);
183
199
184
200
parent ::__construct (
185
201
$ context ,
@@ -564,13 +580,10 @@ public function process(array $responseData)
564
580
$ this ->validateResponse ();
565
581
566
582
$ response = $ this ->getResponse ();
567
- //operate with order
568
- $ orderIncrementId = $ response ->getXInvoiceNum ();
569
583
$ responseText = $ this ->dataHelper ->wrapGatewayError ($ response ->getXResponseReasonText ());
570
584
$ isError = false ;
571
- if ($ orderIncrementId ) {
572
- /* @var $order \Magento\Sales\Model\Order */
573
- $ order = $ this ->orderFactory ->create ()->loadByIncrementId ($ orderIncrementId );
585
+ if ($ this ->getOrderIncrementId ()) {
586
+ $ order = $ this ->getOrderFromResponse ();
574
587
//check payment method
575
588
$ payment = $ order ->getPayment ();
576
589
if (!$ payment || $ payment ->getMethod () != $ this ->getCode ()) {
@@ -635,9 +648,10 @@ public function checkResponseCode()
635
648
return true ;
636
649
case self ::RESPONSE_CODE_DECLINED :
637
650
case self ::RESPONSE_CODE_ERROR :
638
- throw new \Magento \Framework \Exception \LocalizedException (
639
- $ this ->dataHelper ->wrapGatewayError ($ this ->getResponse ()->getXResponseReasonText ())
640
- );
651
+ $ errorMessage = $ this ->dataHelper ->wrapGatewayError ($ this ->getResponse ()->getXResponseReasonText ());
652
+ $ order = $ this ->getOrderFromResponse ();
653
+ $ this ->paymentFailures ->handle ($ order ->getQuoteId (), $ errorMessage );
654
+ throw new \Magento \Framework \Exception \LocalizedException ($ errorMessage );
641
655
default :
642
656
throw new \Magento \Framework \Exception \LocalizedException (
643
657
__ ('There was a payment authorization error. ' )
@@ -992,12 +1006,40 @@ protected function getTransactionResponse($transactionId)
992
1006
private function getPsrLogger ()
993
1007
{
994
1008
if (null === $ this ->psrLogger ) {
995
- $ this ->psrLogger = \ Magento \ Framework \ App \ ObjectManager::getInstance ()
1009
+ $ this ->psrLogger = ObjectManager::getInstance ()
996
1010
->get (\Psr \Log \LoggerInterface::class);
997
1011
}
998
1012
return $ this ->psrLogger ;
999
1013
}
1000
1014
1015
+ /**
1016
+ * Fetch order by increment id from response.
1017
+ *
1018
+ * @return \Magento\Sales\Model\Order
1019
+ */
1020
+ private function getOrderFromResponse ()
1021
+ {
1022
+ if (!$ this ->order ) {
1023
+ $ this ->order = $ this ->orderFactory ->create ();
1024
+
1025
+ if ($ incrementId = $ this ->getOrderIncrementId ()) {
1026
+ $ this ->order = $ this ->order ->loadByIncrementId ($ incrementId );
1027
+ }
1028
+ }
1029
+
1030
+ return $ this ->order ;
1031
+ }
1032
+
1033
+ /**
1034
+ * Fetch order increment id from response.
1035
+ *
1036
+ * @return string
1037
+ */
1038
+ private function getOrderIncrementId ()
1039
+ {
1040
+ return $ this ->getResponse ()->getXInvoiceNum ();
1041
+ }
1042
+
1001
1043
/**
1002
1044
* Checks if filter action is Report Only. Transactions that trigger this filter are processed as normal,
1003
1045
* but are also reported in the Merchant Interface as triggering this filter.
0 commit comments