Skip to content

Commit 96c382d

Browse files
2021-05-14: v2.4.1 :
Fix PayVault Card Delete. Transaction ID added for Pending orders after initiate. Cron schedule amended. Payment Types radio block fix for some themes. Remove object manager. Fix undefined index error $orderData['additional_information']. Order status to pending if checksum fails to pick up by cron. Don't update order status by IPN if already complete or processing.
1 parent fa3b903 commit 96c382d

File tree

15 files changed

+282
-188
lines changed

15 files changed

+282
-188
lines changed

Paygate/PayWeb/Controller/Cron/Index.php

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
*
77
* Released under the GNU General Public License
88
*/
9+
910
namespace PayGate\PayWeb\Controller\Cron;
1011

12+
use PayGate\PayWeb\Model\Config as PayGateConfig;
13+
use PayGate\PayWeb\Model\PayGate;
14+
1115
/**
1216
* Responsible for loading page content.
1317
*
@@ -16,50 +20,77 @@
1620
*/
1721
class Index extends \PayGate\PayWeb\Controller\AbstractPaygate
1822
{
23+
/**
24+
* @var \Magento\Framework\App\Area
25+
*/
26+
private $state;
27+
/**
28+
* @var \Magento\Framework\DB\Transaction
29+
*/
30+
private $transactionModel;
31+
/**
32+
* @var PayGateConfig
33+
*/
34+
private $paygateConfig;
35+
/**
36+
* @var \Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory
37+
*/
38+
protected $transactionSearchResultInterfaceFactory;
39+
40+
public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\Session\Generic $paygateSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl, \Psr\Log\LoggerInterface $logger, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, PayGate $paymentMethod, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Sales\Model\Order\Email\Sender\OrderSender $OrderSender, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Sales\Model\Order\Payment\Transaction\Builder $_transactionBuilder, \Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory $transactionSearchResultInterfaceFactory, PayGateConfig $paygateConfig, \Magento\Framework\DB\Transaction $transactionModel, \Magento\Framework\App\State $state)
41+
{
42+
$this->state = $state;
43+
$this->transactionModel = $transactionModel;
44+
$this->paygateConfig = $paygateConfig;
45+
$this->transactionSearchResultInterfaceFactory = $transactionSearchResultInterfaceFactory;
46+
parent::__construct($context, $pageFactory, $customerSession, $checkoutSession, $orderFactory, $paygateSession, $urlHelper, $customerUrl, $logger, $transactionFactory, $invoiceService, $invoiceSender, $paymentMethod, $urlBuilder, $orderRepository, $storeManager, $OrderSender, $date, $orderCollectionFactory, $_transactionBuilder);
47+
}
1948

2049
public function execute()
2150
{
22-
$cutoffTime = ( new \DateTime() )->sub( new \DateInterval( 'PT10M' ) )->format( 'Y-m-d H:i:s' );
23-
$this->_logger->info( 'Cutoff: ' . $cutoffTime );
24-
$ocf = $this->_orderCollectionFactory->create();
25-
$ocf->addAttributeToSelect( 'entity_id' );
26-
$ocf->addAttributeToFilter( 'status', ['eq' => 'pending_payment'] );
27-
$ocf->addAttributeToFilter( 'created_at', ['lt' => $cutoffTime] );
28-
$ocf->addAttributeToFilter( 'updated_at', ['lt' => $cutoffTime] );
29-
$orderIds = $ocf->getData();
30-
31-
$this->_logger->info( 'Orders for cron: ' . json_encode( $orderIds ) );
32-
33-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
34-
35-
foreach ( $orderIds as $orderId ) {
36-
$order_id = $orderId['entity_id'];
37-
$transactionSearchResult = $objectManager->get( '\Magento\Sales\Api\Data\TransactionSearchResultInterfaceFactory' );
38-
$transaction = $transactionSearchResult->create()->addOrderIdFilter( $order_id )->getFirstItem();
39-
40-
$transactionId = $transaction->getData( 'txn_id' );
41-
$order = $this->orderRepository->get( $orderId['entity_id'] );
42-
$PaymentTitle = $order->getPayment()->getMethodInstance()->getTitle();
43-
44-
if ( !empty( $transactionId ) & $PaymentTitle == "PayGate" ) {
45-
$orderquery['orderId'] = $order->getRealOrderId();
46-
$orderquery['country'] = $order->getBillingAddress()->getCountryId();
47-
$orderquery['currency'] = $order->getOrderCurrencyCode();
48-
$orderquery['amount'] = $order->getGrandTotal();
49-
$orderquery['reference'] = $order->getRealOrderId();
50-
$orderquery['transaction_id'] = $transactionId;
51-
52-
$result = explode( "&", $this->getQueryResult( $orderquery ) );
53-
$this->updatePaymentStatus( $order, $result );
54-
}
51+
$this->state->emulateAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND, function () {
52+
53+
$cutoffTime = ( new \DateTime() )->sub( new \DateInterval( 'PT10M' ) )->format( 'Y-m-d H:i:s' );
54+
$this->_logger->info( 'Cutoff: ' . $cutoffTime );
55+
$ocf = $this->_orderCollectionFactory->create();
56+
$ocf->addAttributeToSelect( 'entity_id' );
57+
$ocf->addAttributeToFilter( 'status', ['eq' => 'pending_payment'] );
58+
$ocf->addAttributeToFilter( 'created_at', ['lt' => $cutoffTime] );
59+
$ocf->addAttributeToFilter( 'updated_at', ['lt' => $cutoffTime] );
60+
$orderIds = $ocf->getData();
61+
62+
$this->_logger->info( 'Orders for cron: ' . json_encode( $orderIds ) );
63+
64+
foreach ( $orderIds as $orderId ) {
65+
$order_id = $orderId['entity_id'];
66+
$transactionSearchResult = $this->transactionSearchResultInterfaceFactory;
67+
$transaction = $transactionSearchResult->create()->addOrderIdFilter( $order_id )->getFirstItem();
68+
69+
$transactionId = $transaction->getData( 'txn_id' );
70+
$order = $this->orderRepository->get( $orderId['entity_id'] );
71+
$PaymentTitle = $order->getPayment()->getMethodInstance()->getTitle();
72+
73+
if ( !empty( $transactionId ) & $PaymentTitle == "PayGate PayWeb" ) {
74+
$orderquery['orderId'] = $order->getRealOrderId();
75+
$orderquery['country'] = $order->getBillingAddress()->getCountryId();
76+
$orderquery['currency'] = $order->getOrderCurrencyCode();
77+
$orderquery['amount'] = $order->getGrandTotal();
78+
$orderquery['reference'] = $order->getRealOrderId();
79+
$orderquery['transaction_id'] = $transactionId;
80+
81+
$result = explode( "&", $this->getQueryResult( $orderquery ) );
82+
83+
$this->updatePaymentStatus( $order, $result );
84+
85+
}
5586

56-
}
87+
}
88+
});
5789
}
5890

5991
public function getQueryResult( $orderquery )
6092
{
61-
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
62-
$config = $objectManager->get( "PayGate\PayWeb\Model\Config" )->getApiCredentials();
93+
$config = $this->paygateConfig->getApiCredentials();
6394
$encryption_key = $config['encryption_key'];
6495
$paygate_id = $config['paygate_id'];
6596

@@ -86,7 +117,6 @@ public function getQueryResult( $orderquery )
86117
curl_setopt( $ch, CURLOPT_URL, 'https://secure.paygate.co.za/payweb3/query.trans' );
87118
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
88119
curl_setopt( $ch, CURLOPT_NOBODY, false );
89-
curl_setopt( $ch, CURLOPT_REFERER, $_SERVER['HTTP_HOST'] );
90120
curl_setopt( $ch, CURLOPT_POST, true );
91121
curl_setopt( $ch, CURLOPT_POSTFIELDS, $fieldsString );
92122

@@ -101,14 +131,21 @@ public function getQueryResult( $orderquery )
101131

102132
public function updatePaymentStatus( $order, $resp )
103133
{
104-
105134
if ( is_array( $resp ) && count( $resp ) > 0 ) {
106135

107136
$paymentData = array();
108137
foreach ( $resp as $param ) {
109138
$pr = explode( "=", $param );
110139
$paymentData[$pr[0]] = $pr[1];
111140
}
141+
if(isset($paymentData['ERROR'])){
142+
$status = \Magento\Sales\Model\Order::STATE_CANCELED;
143+
$order->setStatus( $status );
144+
$order->setState( $status );
145+
$order->save();
146+
return false;
147+
}
148+
112149
if ( $paymentData['TRANSACTION_STATUS'] == 1 ) {
113150
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
114151
$order->setStatus( $status );
@@ -117,7 +154,6 @@ public function updatePaymentStatus( $order, $resp )
117154
try {
118155
$this->generateInvoice( $order );
119156
} catch ( \Exception $ex ) {
120-
121157
$this->_logger->error( $ex->getMessage() );
122158
}
123159
} else {
@@ -145,7 +181,7 @@ public function generateInvoice( $order )
145181
$invoice->register();
146182

147183
// Save the invoice to the order
148-
$transaction = $this->_objectManager->create( 'Magento\Framework\DB\Transaction' )
184+
$transaction = $this->transactionModel
149185
->addObject( $invoice )
150186
->addObject( $invoice->getOrder() );
151187

Paygate/PayWeb/Controller/Notify/Indexm220.php

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@
1010
namespace PayGate\PayWeb\Controller\Notify;
1111

1212
use PayGate\PayWeb\Controller\AbstractPaygate;
13+
use PayGate\PayWeb\Model\PayGate;
1314

1415
class Indexm220 extends AbstractPaygate
1516
{
17+
/**
18+
* @var \Magento\Framework\DB\Transaction
19+
*/
20+
private $transactionModel;
1621

1722
/**
1823
* indexAction
1924
*
2025
*/
26+
27+
public function __construct(\Magento\Framework\App\Action\Context $context, \Magento\Framework\View\Result\PageFactory $pageFactory, \Magento\Customer\Model\Session $customerSession, \Magento\Checkout\Model\Session $checkoutSession, \Magento\Sales\Model\OrderFactory $orderFactory, \Magento\Framework\Session\Generic $paygateSession, \Magento\Framework\Url\Helper\Data $urlHelper, \Magento\Customer\Model\Url $customerUrl, \Psr\Log\LoggerInterface $logger, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Sales\Model\Service\InvoiceService $invoiceService, \Magento\Sales\Model\Order\Email\Sender\InvoiceSender $invoiceSender, PayGate $paymentMethod, \Magento\Framework\UrlInterface $urlBuilder, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Store\Model\StoreManagerInterface $storeManager, \Magento\Sales\Model\Order\Email\Sender\OrderSender $OrderSender, \Magento\Framework\Stdlib\DateTime\DateTime $date, \Magento\Sales\Model\ResourceModel\Order\CollectionFactory $orderCollectionFactory, \Magento\Sales\Model\Order\Payment\Transaction\Builder $_transactionBuilder,\Magento\Framework\DB\Transaction $transactionModel)
28+
{
29+
$this->transactionModel = $transactionModel;
30+
parent::__construct($context, $pageFactory, $customerSession, $checkoutSession, $orderFactory, $paygateSession, $urlHelper, $customerUrl, $logger, $transactionFactory, $invoiceService, $invoiceSender, $paymentMethod, $urlBuilder, $orderRepository, $storeManager, $OrderSender, $date, $orderCollectionFactory, $_transactionBuilder);
31+
}
32+
2133
public function execute()
2234
{
2335
echo "OK";
@@ -91,42 +103,45 @@ public function execute()
91103
$order = $this->orderRepository->get( $reference );
92104
switch ( $status ) {
93105
case 1:
94-
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
95-
if ( $this->getConfigData( 'Successful_Order_status' ) != "" ) {
96-
$status = $this->getConfigData( 'Successful_Order_status' );
97-
}
98-
99-
$model = $this->_paymentMethod;
100-
$order_successful_email = $model->getConfigData( 'order_email' );
101-
102-
if ( $order_successful_email != '0' ) {
103-
$this->OrderSender->send( $order );
104-
$order->addStatusHistoryComment( __( 'Notified customer about order #%1.', $order->getId() ) )->setIsCustomerNotified( true )->save();
106+
$orderState = $order->getState();
107+
if ($orderState != \Magento\Sales\Model\Order::STATE_COMPLETE && $orderState != \Magento\Sales\Model\Order::STATE_PROCESSING) {
108+
$status = \Magento\Sales\Model\Order::STATE_PROCESSING;
109+
if ( $this->getConfigData( 'Successful_Order_status' ) != "" ) {
110+
$status = $this->getConfigData( 'Successful_Order_status' );
111+
}
112+
113+
$model = $this->_paymentMethod;
114+
$order_successful_email = $model->getConfigData( 'order_email' );
115+
116+
if ( $order_successful_email != '0' ) {
117+
$this->OrderSender->send( $order );
118+
$order->addStatusHistoryComment( __( 'Notified customer about order #%1.', $order->getId() ) )->setIsCustomerNotified( true )->save();
119+
}
120+
121+
// Capture invoice when payment is successfull
122+
$invoice = $this->_invoiceService->prepareInvoice( $order );
123+
$invoice->setRequestedCaptureCase( \Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE );
124+
$invoice->register();
125+
126+
// Save the invoice to the order
127+
$transaction = $this->transactionModel
128+
->addObject( $invoice )
129+
->addObject( $invoice->getOrder() );
130+
131+
$transaction->save();
132+
133+
// Magento\Sales\Model\Order\Email\Sender\InvoiceSender
134+
$send_invoice_email = $model->getConfigData( 'invoice_email' );
135+
if ( $send_invoice_email != '0' ) {
136+
$this->invoiceSender->send( $invoice );
137+
$order->addStatusHistoryComment( __( 'Notified customer about invoice #%1.', $invoice->getId() ) )->setIsCustomerNotified( true )->save();
138+
}
139+
140+
// Save Transaction Response
141+
$this->createTransaction( $order, $paygate_data );
142+
$order->setState( $status )->setStatus( $status )->save();
105143
}
106144

107-
// Capture invoice when payment is successfull
108-
$invoice = $this->_invoiceService->prepareInvoice( $order );
109-
$invoice->setRequestedCaptureCase( \Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE );
110-
$invoice->register();
111-
112-
// Save the invoice to the order
113-
$transaction = $this->_objectManager->create( 'Magento\Framework\DB\Transaction' )
114-
->addObject( $invoice )
115-
->addObject( $invoice->getOrder() );
116-
117-
$transaction->save();
118-
119-
// Magento\Sales\Model\Order\Email\Sender\InvoiceSender
120-
$send_invoice_email = $model->getConfigData( 'invoice_email' );
121-
if ( $send_invoice_email != '0' ) {
122-
$this->invoiceSender->send( $invoice );
123-
$order->addStatusHistoryComment( __( 'Notified customer about invoice #%1.', $invoice->getId() ) )->setIsCustomerNotified( true )->save();
124-
}
125-
126-
// Save Transaction Response
127-
$this->createTransaction( $order, $paygate_data );
128-
$order->setState( $status )->setStatus( $status )->save();
129-
130145
exit;
131146
break;
132147
case 2:

0 commit comments

Comments
 (0)