3
3
namespace Moyasar \Mysr \Console \Command ;
4
4
5
5
use DateTime ;
6
+ use Exception ;
6
7
use Magento \Framework \Api \SearchCriteriaBuilder ;
7
8
use Magento \Framework \App \Cache \Frontend \Pool ;
8
9
use Magento \Framework \App \ObjectManager ;
9
10
use Magento \Sales \Model \Order ;
10
11
use Magento \Sales \Model \OrderRepository ;
12
+ use Moyasar \Mysr \Helper \Http \Exceptions \HttpException ;
13
+ use Moyasar \Mysr \Helper \Http \QuickHttp ;
11
14
use Moyasar \Mysr \Helper \MoyasarHelper ;
15
+ use Moyasar \Mysr \Model \Payment \MoyasarPayments ;
12
16
use Psr \Log \LoggerInterface ;
13
17
use Symfony \Component \Console \Command \Command ;
14
18
use Symfony \Component \Console \Input \ArgvInput ;
15
19
use Symfony \Component \Console \Input \InputInterface ;
16
20
use Symfony \Component \Console \Output \ConsoleOutput ;
17
21
use Symfony \Component \Console \Output \OutputInterface ;
18
22
19
- class CheckPendingPaymentsCommand extends Command
23
+ class CheckPending extends Command
20
24
{
21
25
/**
22
26
* @var InputInterface
@@ -48,6 +52,11 @@ class CheckPendingPaymentsCommand extends Command
48
52
*/
49
53
protected $ logger ;
50
54
55
+ /**
56
+ * @var QuickHttp
57
+ */
58
+ protected $ http ;
59
+
51
60
public function __construct ()
52
61
{
53
62
parent ::__construct ('moyasar:payment:process ' );
@@ -75,6 +84,7 @@ protected function initServices()
75
84
$ this ->moyasarHelper = $ objectManager ->get (MoyasarHelper::class);
76
85
$ this ->cachePool = $ objectManager ->get (Pool::class);
77
86
$ this ->logger = $ objectManager ->get (LoggerInterface::class);
87
+ $ this ->http = new QuickHttp ();
78
88
}
79
89
80
90
/**
@@ -88,6 +98,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
88
98
$ orders = $ this ->getPendingOrders ();
89
99
90
100
foreach ($ orders as $ order ) {
101
+ if ($ order ->getPayment ()->getMethod () != MoyasarPayments::CODE ) {
102
+ continue ;
103
+ }
104
+
91
105
$ this ->process ($ order );
92
106
}
93
107
@@ -99,14 +113,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
99
113
*/
100
114
private function process ($ order )
101
115
{
102
- if (
103
- $ order ->getState () != Order::STATE_NEW &&
104
- $ order ->getState () != Order::STATE_PENDING_PAYMENT &&
105
- $ order ->getState () != Order::STATE_PAYMENT_REVIEW
106
- ) {
107
- return ;
108
- }
109
-
110
116
$ cacheKey = 'moyasar-order-checked- ' . $ order ->getId ();
111
117
112
118
// If already checked within the last 5 minutes, skip
@@ -126,21 +132,50 @@ private function process($order)
126
132
*/
127
133
private function processPayment ($ order )
128
134
{
129
- $ payment = $ order ->getPayment ( );
135
+ $ this -> logger -> info ( " Processing pending order " . $ order ->getIncrementId () );
130
136
131
- if (is_null ($ payment )) {
137
+ $ orderPayment = $ order ->getPayment ();
138
+ $ paymentId = $ orderPayment ->getLastTransId ();
139
+ if (! $ paymentId ) {
140
+ $ this ->logger ->warning ("Cannot find payment ID for order " . $ order ->getIncrementId ());
132
141
return ;
133
142
}
134
143
135
- $ additionalInfo = $ payment ->getAdditionalInformation ();
144
+ try {
145
+ $ this ->logger ->info ("Fetching Moyasar payment $ paymentId... " );
136
146
137
- if (! isset ($ additionalInfo ['moyasar_payment_id ' ])) {
138
- return ;
139
- }
147
+ $ payment = $ this ->http
148
+ ->basic_auth ($ this ->moyasarHelper ->secretApiKey ())
149
+ ->get ($ this ->moyasarHelper ->apiBaseUrl ("/v1/payments/ $ paymentId " ))
150
+ ->json ();
151
+
152
+ $ this ->logger ->info ("Fetched payment $ paymentId. " );
153
+
154
+ if ($ payment ['status ' ] != 'paid ' ) {
155
+ $ message = __ ('Payment failed ' );
156
+ if ($ sourceMessage = $ payment ['source ' ]['message ' ]) {
157
+ $ message .= ': ' . $ sourceMessage ;
158
+ }
140
159
141
- $ moyasarPaymentId = $ additionalInfo ['moyasar_payment_id ' ];
160
+ return $ this ->processFailedPayment ($ payment , $ order , [$ message ]);
161
+ }
142
162
143
- $ this ->moyasarHelper ->verifyAndProcess ($ order , $ moyasarPaymentId , null );
163
+ $ errors = $ this ->moyasarHelper ->checkPaymentForErrors ($ order , $ payment );
164
+ if (count ($ errors ) > 0 ) {
165
+ array_unshift ($ errors , 'Un-matching payment details ' . $ payment ['id ' ]);
166
+ return $ this ->processFailedPayment ($ payment , $ order , $ errors );
167
+ }
168
+
169
+ $ order ->addCommentToStatusHistory ('Order was canceled automatically by cron jobs. ' );
170
+ $ this ->moyasarHelper ->processSuccessfulOrder ($ order , $ payment );
171
+
172
+ $ this ->logger ->info ("Processed order " . $ order ->getIncrementId ());
173
+ } catch (HttpException $ e ) {
174
+ $ this ->logger ->error ($ e );
175
+ $ this ->logger ->info ($ e ->response ->body ());
176
+ } catch (Exception $ e ) {
177
+ $ this ->logger ->error ($ e );
178
+ }
144
179
}
145
180
146
181
private function criteriaBuilder ()
@@ -154,35 +189,33 @@ private function getPendingOrders()
154
189
$ dateStart ->modify ('-5 day ' );
155
190
156
191
$ dateEnd = new DateTime ();
157
- $ dateEnd ->modify ('-2 minute ' );
158
-
159
- $ search = $ this ->criteriaBuilder ()
160
- ->addFilter ('state ' , Order::STATE_NEW )
161
- ->addFilter ('created_at ' , $ dateStart ->format ('Y-m-d H:i:s ' ), 'gteq ' )
162
- ->addFilter ('created_at ' , $ dateEnd ->format ('Y-m-d H:i:s ' ), 'lteq ' )
163
- ->create ();
192
+ $ dateEnd ->modify ('-15 minute ' );
164
193
165
194
$ pendingPaymentSearch = $ this ->criteriaBuilder ()
166
195
->addFilter ('state ' , Order::STATE_PENDING_PAYMENT )
167
196
->addFilter ('created_at ' , $ dateStart ->format ('Y-m-d H:i:s ' ), 'gteq ' )
168
197
->addFilter ('created_at ' , $ dateEnd ->format ('Y-m-d H:i:s ' ), 'lteq ' )
169
198
->create ();
170
199
171
- $ reviewPaymentSearch = $ this ->criteriaBuilder ()
172
- ->addFilter ('state ' , Order::STATE_PAYMENT_REVIEW )
173
- ->addFilter ('created_at ' , $ dateStart ->format ('Y-m-d H:i:s ' ), 'gteq ' )
174
- ->addFilter ('created_at ' , $ dateEnd ->format ('Y-m-d H:i:s ' ), 'lteq ' )
175
- ->create ();
176
-
177
- return array_merge (
178
- $ this ->orderRepository ->getList ($ search )->getItems (),
179
- $ this ->orderRepository ->getList ($ pendingPaymentSearch )->getItems (),
180
- $ this ->orderRepository ->getList ($ reviewPaymentSearch )->getItems ()
181
- );
200
+ return $ this ->orderRepository ->getList ($ pendingPaymentSearch )->getItems ();
182
201
}
183
202
184
203
private function cache ()
185
204
{
186
205
return $ this ->cachePool ->current ();
187
206
}
207
+
208
+ /**
209
+ * @param array $payment
210
+ * @param Order $order
211
+ * @param array $errors
212
+ * @return mixed
213
+ */
214
+ private function processFailedPayment ($ payment , $ order , $ errors )
215
+ {
216
+ $ order ->registerCancellation (implode ("\n" , $ errors ));
217
+ $ order ->getPayment ()->setLastTransId ($ payment ['id ' ]);
218
+ $ order ->addCommentToStatusHistory ('Order was canceled automatically by cron jobs. ' );
219
+ $ order ->save ();
220
+ }
188
221
}
0 commit comments