Skip to content

Commit 95d9010

Browse files
authored
Merge pull request #117 from RichardCardGate/master
Magento coding standards.
2 parents 5d42f84 + 7de87a7 commit 95d9010

37 files changed

Lines changed: 657 additions & 602 deletions

‎Controller/Payment/Callback.php‎

Lines changed: 94 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
use Cardgate\Payment\Model\GatewayClient;
1010
use Cardgate\Payment\Model\Config\Master;
11+
use Cardgate\Payment\Exception\CallbackException;
1112
use Magento\Framework\App\ActionInterface;
1213
use Magento\Sales\Api\Data\TransactionInterface;
1314
use Magento\CatalogInventory\Model\Spi\StockRegistryProviderInterface;
@@ -18,14 +19,11 @@
1819
class 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
}

‎Controller/Payment/Redirect.php‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Magento\Framework\App\RequestInterface as Request;
1414
use \Magento\Framework\Controller\Result\Redirect as ResultRedirect;
1515
use Cardgate\Payment\Model\Config as CardgateConfig;
16+
use Cardgate\Payment\Exception\RedirectException;
1617

1718
/**
1819
* Client redirect after payment action
@@ -91,12 +92,14 @@ public function execute()
9192
|| empty($code)
9293
|| empty($transactionId)
9394
) {
94-
throw new \Exception(__('Wrong parameters supplied.'));
95+
throw new RedirectException(__('Wrong parameters supplied.'));
9596
}
9697

9798
// If the callback hasn't been received (yet) the most recent status is fetched from the gateway instead
9899
// of relying on the provided status in the url.
99-
$order = ObjectManager::getInstance()->create(\Magento\Sales\Model\Order::class)->loadByIncrementId($orderId);
100+
$order = ObjectManager::getInstance()
101+
->create(\Magento\Sales\Model\Order::class)
102+
->loadByIncrementId($orderId);
100103
if (\Magento\Sales\Model\Order::STATE_NEW == $order->getState()) {
101104
$gatewayClient = ObjectManager::getInstance()->get(\Cardgate\Payment\Model\GatewayClient::class);
102105
$status = $gatewayClient->transactions()->status($transactionId);
@@ -111,12 +114,12 @@ public function execute()
111114
$this->_checkoutSession->restoreQuote();
112115
$this->resultRedirect->setPath('checkout');
113116
} else {
114-
throw new \Exception(__('Transaction canceled.'));
117+
throw new RedirectException(__('Transaction canceled.'));
115118
}
116119
} else {
117-
throw new \Exception(__('Payment not completed.'));
120+
throw new RedirectException(__('Payment not completed.'));
118121
}
119-
} catch (\Exception $e) {
122+
} catch (RedirectException $e) {
120123
$this->messageManager->addErrorMessage(__($e->getMessage()));
121124
if (!!$this->_cardgateConfig->getGlobal('always_show_success_page')) {
122125
$this->_checkoutSession->start();

‎Controller/Payment/Start.php‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Cardgate\Payment\Model\GatewayClient;
1313
use Cardgate\Payment\Model\Config;
1414
use Cardgate\Payment\Model\Config\Master;
15+
use Cardgate\Payment\Exception\StartException;
1516
use Magento\Framework\App\ActionInterface;
1617
use Magento\Framework\App\ObjectManager;
1718
use Magento\Sales\Model\Order\Address;
@@ -87,8 +88,7 @@ class Start implements ActionInterface
8788
private $stockRegistryInterface;
8889

8990
/**
90-
*
91-
* @var Magento\Framework\Controller\Result\Redirect
91+
* @var \Magento\Framework\Controller\Result\Redirect
9292
*/
9393
private $redirect;
9494

@@ -361,8 +361,11 @@ public function execute()
361361
*
362362
* @return void
363363
*/
364-
private function _convertAddress(Address &$oAddress_, \Cardgate\Payment\Model\CardgateClient\Consumer &$oConsumer_, $sMethod_)
365-
{
364+
private function _convertAddress(
365+
Address &$oAddress_,
366+
\Cardgate\Payment\Model\CardgateClient\Consumer &$oConsumer_,
367+
$sMethod_
368+
) {
366369
$oConsumer_->$sMethod_()->setFirstName($oAddress_->getFirstname());
367370
$oConsumer_->$sMethod_()->setLastName($oAddress_->getLastname());
368371
if (!!( $sCompany = $oAddress_->getCompany() )) {

‎Exception/CallbackException.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
/**
3+
* Copyright (c) 2018 CardGate B.V.
4+
* All rights reserved.
5+
* See LICENSE for license details.
6+
*/
7+
namespace Cardgate\Payment\Exception;
8+
9+
/**
10+
* Callback-specific exception
11+
*/
12+
class CallbackException extends CardgateException
13+
{
14+
}

‎Exception/CardgateException.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
/**
3+
* Copyright (c) 2018 CardGate B.V.
4+
* All rights reserved.
5+
* See LICENSE for license details.
6+
*/
7+
namespace Cardgate\Payment\Exception;
8+
9+
/**
10+
* CardGate base exception
11+
*/
12+
class CardgateException extends \Magento\Framework\Exception\LocalizedException
13+
{
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
/**
3+
* Copyright (c) 2018 CardGate B.V.
4+
* All rights reserved.
5+
* See LICENSE for license details.
6+
*/
7+
namespace Cardgate\Payment\Exception;
8+
9+
/**
10+
* GatewayClient-specific exception
11+
*/
12+
class GatewayClientException extends CardgateException
13+
{
14+
}

‎Exception/RedirectException.php‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
/**
3+
* Copyright (c) 2018 CardGate B.V.
4+
* All rights reserved.
5+
* See LICENSE for license details.
6+
*/
7+
namespace Cardgate\Payment\Exception;
8+
9+
/**
10+
* Redirect-specific exception
11+
*/
12+
class RedirectException extends CardgateException
13+
{
14+
}

0 commit comments

Comments
 (0)