6
6
*
7
7
* Released under the GNU General Public License
8
8
*/
9
+
9
10
namespace PayGate \PayWeb \Controller \Cron ;
10
11
12
+ use PayGate \PayWeb \Model \Config as PayGateConfig ;
13
+ use PayGate \PayWeb \Model \PayGate ;
14
+
11
15
/**
12
16
* Responsible for loading page content.
13
17
*
16
20
*/
17
21
class Index extends \PayGate \PayWeb \Controller \AbstractPaygate
18
22
{
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
+ }
19
48
20
49
public function execute ()
21
50
{
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
+ }
55
86
56
- }
87
+ }
88
+ });
57
89
}
58
90
59
91
public function getQueryResult ( $ orderquery )
60
92
{
61
- $ objectManager = \Magento \Framework \App \ObjectManager::getInstance ();
62
- $ config = $ objectManager ->get ( "PayGate\PayWeb\Model\Config " )->getApiCredentials ();
93
+ $ config = $ this ->paygateConfig ->getApiCredentials ();
63
94
$ encryption_key = $ config ['encryption_key ' ];
64
95
$ paygate_id = $ config ['paygate_id ' ];
65
96
@@ -86,7 +117,6 @@ public function getQueryResult( $orderquery )
86
117
curl_setopt ( $ ch , CURLOPT_URL , 'https://secure.paygate.co.za/payweb3/query.trans ' );
87
118
curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , true );
88
119
curl_setopt ( $ ch , CURLOPT_NOBODY , false );
89
- curl_setopt ( $ ch , CURLOPT_REFERER , $ _SERVER ['HTTP_HOST ' ] );
90
120
curl_setopt ( $ ch , CURLOPT_POST , true );
91
121
curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ fieldsString );
92
122
@@ -101,14 +131,21 @@ public function getQueryResult( $orderquery )
101
131
102
132
public function updatePaymentStatus ( $ order , $ resp )
103
133
{
104
-
105
134
if ( is_array ( $ resp ) && count ( $ resp ) > 0 ) {
106
135
107
136
$ paymentData = array ();
108
137
foreach ( $ resp as $ param ) {
109
138
$ pr = explode ( "= " , $ param );
110
139
$ paymentData [$ pr [0 ]] = $ pr [1 ];
111
140
}
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
+
112
149
if ( $ paymentData ['TRANSACTION_STATUS ' ] == 1 ) {
113
150
$ status = \Magento \Sales \Model \Order::STATE_PROCESSING ;
114
151
$ order ->setStatus ( $ status );
@@ -117,7 +154,6 @@ public function updatePaymentStatus( $order, $resp )
117
154
try {
118
155
$ this ->generateInvoice ( $ order );
119
156
} catch ( \Exception $ ex ) {
120
-
121
157
$ this ->_logger ->error ( $ ex ->getMessage () );
122
158
}
123
159
} else {
@@ -145,7 +181,7 @@ public function generateInvoice( $order )
145
181
$ invoice ->register ();
146
182
147
183
// Save the invoice to the order
148
- $ transaction = $ this ->_objectManager -> create ( ' Magento\Framework\DB\Transaction ' )
184
+ $ transaction = $ this ->transactionModel
149
185
->addObject ( $ invoice )
150
186
->addObject ( $ invoice ->getOrder () );
151
187
0 commit comments