88
99use Cardgate \Payment \Model \GatewayClient ;
1010use Cardgate \Payment \Model \Config \Master ;
11+ use Cardgate \Payment \Exception \CallbackException ;
1112use Magento \Framework \App \ActionInterface ;
1213use Magento \Sales \Api \Data \TransactionInterface ;
1314use Magento \CatalogInventory \Model \Spi \StockRegistryProviderInterface ;
1819class Callback implements ActionInterface
1920{
2021 /**
21- *
22- *
2322 * @var \Magento\Sales\Model\Order\Email\Sender\OrderSender
2423 */
2524 protected $ orderSender ;
2625
2726 /**
28- *
2927 * @var \Magento\Sales\Model\Order\Email\Sender\InvoiceSender
3028 */
3129 protected $ invoiceSender ;
@@ -59,14 +57,12 @@ class Callback implements ActionInterface
5957 private $ _encryptor ;
6058
6159 /**
62- *
63- * @var \Magento\Framework\Encryption\Encryptor
60+ * @var \Magento\Framework\App\Cache\TypeListInterface
6461 */
6562 private $ _listInterface ;
6663
6764 /**
68- *
69- * @var ObjectManager
65+ * @var \Magento\Framework\ObjectManagerInterface
7066 */
7167 private $ _objectManager ;
7268
@@ -116,48 +112,19 @@ public function __construct(
116112 public function execute ()
117113 {
118114 $ result = $ this ->resultFactory ->create (\Magento \Framework \Controller \ResultFactory::TYPE_RAW );
119- $ order = $ payment = null ;
120- $ post = $ this ->_request ->getPostValue ();
121- if (! is_array ($ post )) {
122- $ post = [];
123- }
124115 $ get = $ this ->_request ->getParams ();
125116 if (! is_array ($ get )) {
126117 $ get = [];
127118 }
128119
129120 if (!empty ($ get ['cgp_sitesetup ' ]) && !empty ($ get ['token ' ])) {
121+ return $ this ->processSiteSetup ($ get , $ result );
122+ }
130123
131- try {
132- $ bIsTest = ($ get ['testmode ' ] == 1 ? true : false );
133- $ sMerchantId = (int )$ this ->_cardgateConfig ->getGlobal ('api_username ' );
134- if ($ sMerchantId == 0 ) {
135- $ this ->_cardgateConfig ->setGlobal ('api_username ' , 0 );
136- $ this ->_cardgateConfig ->setGlobal ('api_password ' , $ this ->_encryptor ->encrypt ('initconfig ' ));
137- $ this ->_cardgateConfig ->setGlobal ('testmode ' , $ bIsTest );
138- $ this ->_listInterface ->cleanType ('config ' );
139- }
140- $ this ->_cardgateClient = $ this ->_objectManager ->get (\Cardgate \Payment \Model \GatewayClient::class);
141- $ aResult = $ this ->_cardgateClient ->pullConfig ($ get ['token ' ]);
142- if (isset ($ aResult ['success ' ]) && $ aResult ['success ' ]==1 ) {
143- $ aConfigData = $ aResult ['pullconfig ' ]['content ' ];
144- $ sEncryptedApiKey = $ this ->_encryptor ->encrypt ($ aConfigData ['api_key ' ]);
145- $ this ->_cardgateConfig ->setGlobal ('testmode ' , $ aConfigData ['testmode ' ]);
146- $ this ->_cardgateConfig ->setGlobal ('site_id ' , $ aConfigData ['site_id ' ]);
147- $ this ->_cardgateConfig ->setGlobal ('site_key ' , $ aConfigData ['site_key ' ]);
148- $ this ->_cardgateConfig ->setGlobal ('api_username ' , $ aConfigData ['merchant_id ' ]);
149- $ this ->_cardgateConfig ->setGlobal ('api_password ' , $ sEncryptedApiKey );
150- $ this ->_listInterface ->cleanType ('config ' );
151- $ sApiUserName = $ this ->_cardgateConfig ->getGlobal ('api_username ' );
152- $ sSiteId = $ this ->_cardgateConfig ->getGlobal ('site_id ' );
153- $ sResponse = $ sApiUserName . '. ' . $ sSiteId . '.200 ' ;
154- } else {
155- $ sResponse = 'Data retrieval failed. ' ;
156- }
157- return $ result ->setContents ($ sResponse );
158- } catch (CallbackException $ e ) {
159- return $ result ->setContents ($ e ->getMessage ());
160- }
124+ $ order = $ payment = null ;
125+ $ post = $ this ->_request ->getPostValue ();
126+ if (! is_array ($ post )) {
127+ $ post = [];
161128 }
162129
163130 $ transactionId = empty ($ post ['transaction ' ]) ? $ get ['transaction ' ] : $ post ['transaction ' ];
@@ -170,7 +137,6 @@ public function execute()
170137
171138 $ manualProcessing = !!$ this ->_cardgateConfig ->getGlobal ('manually_process_order ' );
172139 $ updateCardgateData = false ;
173- $ payment = null ;
174140 try {
175141 $ aData = empty ($ post ) ? $ get : $ post ;
176142 $ sSiteKey = $ this ->_cardgateClient ->getSiteKey ();
@@ -194,7 +160,7 @@ public function execute()
194160 // match the one from the gateway.
195161 if ($ payment ->getCardgatePaymentmethod () != $ pmId ) {
196162 $ payment ->setCardgatePaymentmethod ($ pmId );
197- $ sText = "Callback received for transaction %1 with payment method '%2' but
163+ $ sText = "Callback received for transaction %1 with payment method '%2' but
198164 payment method should be '%3'. Processing anyway. " ;
199165 $ sOldPaymentMethod = $ order ->getPayment ()->getCardgatePaymentmethod ();
200166 $ order ->addCommentToStatusHistory (__ ($ sText , $ transactionId , $ pmId , $ sOldPaymentMethod ));
@@ -221,42 +187,19 @@ public function execute()
221187 $ order ->addCommentToStatusHistory (__ ("Transaction success. " ));
222188
223189 if (!$ manualProcessing ) {
224- // Uncancel if needed.
225- if ($ order ->isCanceled ()) {
226- if (($ order ->getState () == \Magento \Sales \Model \Order::STATE_CANCELED )) {
227- $ order ->setState (\Magento \Sales \Model \Order::STATE_PROCESSING );
228- }
229-
230- foreach ($ order ->getItems () as $ item ) {
231- foreach ($ item ->getChildrenItems () as $ child ) {
232- $ sProductId = $ child ->getProductId ();
233- $ sWebsiteId = $ order ->getStore ()->getWebsiteId ();
234- $ stockItem = $ this ->_stockRegistry ->getStockItem ($ sProductId , $ sWebsiteId );
235- $ stockItem ->setQty ($ stockItem ->getQty () - $ item ->getQtyCanceled ());
236- $ stockItem ->save ();
237- }
238- $ sItemProductId = $ item ->getProductId ();
239- $ sWebsiteId = $ order ->getStore ()->getWebsiteId ();
240- $ stockItem = $ this ->_stockRegistry ->getStockItem ($ sItemProductId , $ sWebsiteId );
241- $ stockItem ->setQty ($ stockItem ->getQty () - $ item ->getQtyCanceled ());
242- $ stockItem ->save ();
243- $ item ->setQtyCanceled (0 );
244- $ item ->setTaxCanceled (0 );
245- $ item ->setDiscountTaxCompensationCanceled (0 );
246- $ item ->save ();
247- }
248- $ sRebookedText = 'Transaction rebooked. Product stock reclaimed from inventory. ' ;
249- $ order ->addCommentToStatusHistory (__ ($ sRebookedText ));
250- }
190+ $ this ->rebookOrder ($ order );
251191
252192 // Test if transaction has been processed already.
253- $ paymentRepository = $ this ->_objectManager ->get (\Magento \Sales \Model \Order \Payment \Transaction \Repository::class);
193+ $ paymentRepository = $ this ->_objectManager
194+ ->get (\Magento \Sales \Model \Order \Payment \Transaction \Repository::class);
254195 $ currentTransaction = $ paymentRepository ->getByTransactionId (
255196 $ transactionId ,
256197 $ payment ->getId (),
257198 $ order ->getId ()
258199 );
259- if (! empty ($ currentTransaction ) && $ currentTransaction ->getTxnType () == TransactionInterface::TYPE_CAPTURE ) {
200+ if (! empty ($ currentTransaction )
201+ && $ currentTransaction ->getTxnType () == TransactionInterface::TYPE_CAPTURE
202+ ) {
260203 $ order ->addCommentToStatusHistory (__ ('Transaction already processed. ' ));
261204 $ updateCardgateData = false ;
262205 throw new CallbackException ('transaction already processed. ' );
@@ -323,12 +266,6 @@ public function execute()
323266 $ currency . ' ' . round ($ amount / 100 , 2 )
324267 ));
325268 }
326- } elseif ($ code >= 600
327- && $ code < 700
328- ) {
329- // 6xx notification from bank
330- } elseif ($ code < 800 ) {
331- // 7xx waiting for confirmation
332269 }
333270
334271 // Set the output to a string that the gateway expects.
@@ -354,4 +291,82 @@ public function execute()
354291
355292 return $ result ;
356293 }
294+
295+ /**
296+ * Process site setup.
297+ *
298+ * @param array $get
299+ * @param \Magento\Framework\Controller\Result\Raw $result
300+ * @return \Magento\Framework\Controller\Result\Raw
301+ */
302+ private function processSiteSetup (array $ get , $ result )
303+ {
304+ try {
305+ $ bIsTest = ($ get ['testmode ' ] == 1 ? true : false );
306+ $ sMerchantId = (int )$ this ->_cardgateConfig ->getGlobal ('api_username ' );
307+ if ($ sMerchantId == 0 ) {
308+ $ this ->_cardgateConfig ->setGlobal ('api_username ' , 0 );
309+ $ this ->_cardgateConfig ->setGlobal ('api_password ' , $ this ->_encryptor ->encrypt ('initconfig ' ));
310+ $ this ->_cardgateConfig ->setGlobal ('testmode ' , $ bIsTest );
311+ $ this ->_listInterface ->cleanType ('config ' );
312+ }
313+ $ this ->_cardgateClient = $ this ->_objectManager ->get (\Cardgate \Payment \Model \GatewayClient::class);
314+ $ aResult = $ this ->_cardgateClient ->pullConfig ($ get ['token ' ]);
315+ if (isset ($ aResult ['success ' ]) && $ aResult ['success ' ] == 1 ) {
316+ $ aConfigData = $ aResult ['pullconfig ' ]['content ' ];
317+ $ sEncryptedApiKey = $ this ->_encryptor ->encrypt ($ aConfigData ['api_key ' ]);
318+ $ this ->_cardgateConfig ->setGlobal ('testmode ' , $ aConfigData ['testmode ' ]);
319+ $ this ->_cardgateConfig ->setGlobal ('site_id ' , $ aConfigData ['site_id ' ]);
320+ $ this ->_cardgateConfig ->setGlobal ('site_key ' , $ aConfigData ['site_key ' ]);
321+ $ this ->_cardgateConfig ->setGlobal ('api_username ' , $ aConfigData ['merchant_id ' ]);
322+ $ this ->_cardgateConfig ->setGlobal ('api_password ' , $ sEncryptedApiKey );
323+ $ this ->_listInterface ->cleanType ('config ' );
324+ $ sApiUserName = $ this ->_cardgateConfig ->getGlobal ('api_username ' );
325+ $ sSiteId = $ this ->_cardgateConfig ->getGlobal ('site_id ' );
326+ $ sResponse = $ sApiUserName . '. ' . $ sSiteId . '.200 ' ;
327+ } else {
328+ $ sResponse = 'Data retrieval failed. ' ;
329+ }
330+ return $ result ->setContents ($ sResponse );
331+ } catch (CallbackException $ e ) {
332+ return $ result ->setContents ($ e ->getMessage ());
333+ }
334+ }
335+
336+ /**
337+ * Rebook order and reclaim product stock from inventory.
338+ *
339+ * @param \Magento\Sales\Model\Order $order
340+ * @return void
341+ */
342+ private function rebookOrder ($ order )
343+ {
344+ // Uncancel if needed.
345+ if ($ order ->isCanceled ()) {
346+ if (($ order ->getState () == \Magento \Sales \Model \Order::STATE_CANCELED )) {
347+ $ order ->setState (\Magento \Sales \Model \Order::STATE_PROCESSING );
348+ }
349+
350+ foreach ($ order ->getItems () as $ item ) {
351+ foreach ($ item ->getChildrenItems () as $ child ) {
352+ $ sProductId = $ child ->getProductId ();
353+ $ sWebsiteId = $ order ->getStore ()->getWebsiteId ();
354+ $ stockItem = $ this ->_stockRegistry ->getStockItem ($ sProductId , $ sWebsiteId );
355+ $ stockItem ->setQty ($ stockItem ->getQty () - $ item ->getQtyCanceled ());
356+ $ stockItem ->save ();
357+ }
358+ $ sItemProductId = $ item ->getProductId ();
359+ $ sWebsiteId = $ order ->getStore ()->getWebsiteId ();
360+ $ stockItem = $ this ->_stockRegistry ->getStockItem ($ sItemProductId , $ sWebsiteId );
361+ $ stockItem ->setQty ($ stockItem ->getQty () - $ item ->getQtyCanceled ());
362+ $ stockItem ->save ();
363+ $ item ->setQtyCanceled (0 );
364+ $ item ->setTaxCanceled (0 );
365+ $ item ->setDiscountTaxCompensationCanceled (0 );
366+ $ item ->save ();
367+ }
368+ $ sRebookedText = 'Transaction rebooked. Product stock reclaimed from inventory. ' ;
369+ $ order ->addCommentToStatusHistory (__ ($ sRebookedText ));
370+ }
371+ }
357372}
0 commit comments