@@ -198,6 +198,7 @@ protected function _construct()
198
198
public function setOrder (Order $ order )
199
199
{
200
200
$ this ->_order = $ order ;
201
+
201
202
return $ this ;
202
203
}
203
204
@@ -225,6 +226,7 @@ public function getOrder()
225
226
public function setTransactionId ($ transactionId )
226
227
{
227
228
$ this ->setData ('transaction_id ' , $ transactionId );
229
+
228
230
return $ this ;
229
231
}
230
232
@@ -247,6 +249,7 @@ public function getTransactionId()
247
249
public function setIsTransactionClosed ($ isClosed )
248
250
{
249
251
$ this ->setData ('is_transaction_closed ' , (bool )$ isClosed );
252
+
250
253
return $ this ;
251
254
}
252
255
@@ -293,6 +296,7 @@ public function canCapture()
293
296
return false ;
294
297
}
295
298
}
299
+
296
300
return true ;
297
301
}
298
302
@@ -499,15 +503,16 @@ public function registerAuthorizationNotification($amount)
499
503
*/
500
504
public function pay ($ invoice )
501
505
{
502
- $ this ->_updateTotals (
503
- [
504
- 'amount_paid ' => $ invoice ->getGrandTotal (),
505
- 'base_amount_paid ' => $ invoice ->getBaseGrandTotal (),
506
- 'shipping_captured ' => $ invoice ->getShippingAmount (),
507
- 'base_shipping_captured ' => $ invoice ->getBaseShippingAmount (),
508
- ]
506
+ $ totals = $ this ->collectTotalAmounts (
507
+ $ this ->getOrder (),
508
+ ['grand_total ' , 'base_grand_total ' , 'shipping_amount ' , 'base_shipping_amount ' ]
509
509
);
510
+ $ this ->setAmountPaid ($ totals ['grand_total ' ]);
511
+ $ this ->setBaseAmountPaid ($ totals ['base_grand_total ' ]);
512
+ $ this ->setShippingCaptured ($ totals ['shipping_amount ' ]);
513
+ $ this ->setBaseShippingCaptured ($ totals ['base_shipping_amount ' ]);
510
514
$ this ->_eventManager ->dispatch ('sales_order_payment_pay ' , ['payment ' => $ this , 'invoice ' => $ invoice ]);
515
+
511
516
return $ this ;
512
517
}
513
518
@@ -550,6 +555,7 @@ protected function _invoice()
550
555
}
551
556
552
557
$ this ->getOrder ()->addRelatedObject ($ invoice );
558
+
553
559
return $ invoice ;
554
560
}
555
561
@@ -567,6 +573,7 @@ public function canVoid()
567
573
$ this ->_canVoidLookup = (bool )$ authTransaction && !(int )$ authTransaction ->getIsClosed ();
568
574
}
569
575
}
576
+
570
577
return $ this ->_canVoidLookup ;
571
578
}
572
579
@@ -581,6 +588,7 @@ public function void(\Magento\Framework\DataObject $document)
581
588
{
582
589
$ this ->_void (true );
583
590
$ this ->_eventManager ->dispatch ('sales_order_payment_void ' , ['payment ' => $ this , 'invoice ' => $ document ]);
591
+
584
592
return $ this ;
585
593
}
586
594
@@ -596,6 +604,7 @@ public function registerVoidNotification($amount = null)
596
604
if (!$ this ->hasMessage ()) {
597
605
$ this ->setMessage (__ ('Registered a Void notification. ' ));
598
606
}
607
+
599
608
return $ this ->_void (false , $ amount );
600
609
}
601
610
@@ -608,6 +617,7 @@ public function registerVoidNotification($amount = null)
608
617
public function setCreditmemo (Creditmemo $ creditmemo )
609
618
{
610
619
$ this ->setData ('creditmemo ' , $ creditmemo );
620
+
611
621
return $ this ;
612
622
}
613
623
@@ -724,6 +734,7 @@ public function refund($creditmemo)
724
734
'sales_order_payment_refund ' ,
725
735
['payment ' => $ this , 'creditmemo ' => $ creditmemo ]
726
736
);
737
+
727
738
return $ this ;
728
739
}
729
740
@@ -778,6 +789,7 @@ public function registerRefundNotification($amount)
778
789
),
779
790
false
780
791
);
792
+
781
793
return $ this ;
782
794
}
783
795
@@ -807,6 +819,7 @@ public function registerRefundNotification($amount)
807
819
);
808
820
$ message = $ this ->_appendTransactionToMessage ($ transaction , $ message );
809
821
$ this ->setOrderStateProcessing ($ message );
822
+
810
823
return $ this ;
811
824
}
812
825
@@ -830,6 +843,7 @@ public function cancelCreditmemo($creditmemo)
830
843
'sales_order_payment_cancel_creditmemo ' ,
831
844
['payment ' => $ this , 'creditmemo ' => $ creditmemo ]
832
845
);
846
+
833
847
return $ this ;
834
848
}
835
849
@@ -904,6 +918,7 @@ public function accept()
904
918
);
905
919
$ this ->setOrderStatePaymentReview ($ message , $ transactionId );
906
920
}
921
+
907
922
return $ this ;
908
923
}
909
924
@@ -944,6 +959,7 @@ public function deny($isOnline = true)
944
959
);
945
960
$ this ->setOrderStatePaymentReview ($ message , $ transactionId );
946
961
}
962
+
947
963
return $ this ;
948
964
}
949
965
@@ -998,7 +1014,8 @@ protected function updateBaseAmountPaidOnlineTotal($invoice)
998
1014
{
999
1015
if ($ invoice instanceof Invoice) {
1000
1016
$ invoice ->pay ();
1001
- $ this ->_updateTotals (['base_amount_paid_online ' => $ invoice ->getBaseGrandTotal ()]);
1017
+ $ totals = $ this ->collectTotalAmounts ($ this ->getOrder (), ['base_grand_total ' ]);
1018
+ $ this ->setBaseAmountPaidOnline ($ totals ['base_grand_total ' ]);
1002
1019
$ this ->getOrder ()->addRelatedObject ($ invoice );
1003
1020
}
1004
1021
}
@@ -1147,6 +1164,7 @@ protected function _void($isOnline, $amount = null, $gatewayCallback = 'void')
1147
1164
$ message = $ this ->_appendTransactionToMessage ($ transaction , $ message );
1148
1165
$ this ->setOrderStateProcessing ($ message );
1149
1166
$ order ->setDataChanges (true );
1167
+
1150
1168
return $ this ;
1151
1169
}
1152
1170
@@ -1168,6 +1186,7 @@ public function addTransaction($type, $salesDocument = null, $failSafe = false)
1168
1186
if ($ salesDocument ) {
1169
1187
$ builder ->setSalesDocument ($ salesDocument );
1170
1188
}
1189
+
1171
1190
return $ builder ->build ($ type );
1172
1191
}
1173
1192
@@ -1202,6 +1221,7 @@ public function importTransactionInfo(Transaction $transactionTo)
1202
1221
$ data
1203
1222
);
1204
1223
}
1224
+
1205
1225
return $ this ;
1206
1226
}
1207
1227
@@ -1235,6 +1255,7 @@ protected function _appendTransactionToMessage($transaction, $message)
1235
1255
$ txnId = is_object ($ transaction ) ? $ transaction ->getHtmlTxnId () : $ transaction ;
1236
1256
$ message .= ' ' . __ ('Transaction ID: "%1" ' , $ txnId );
1237
1257
}
1258
+
1238
1259
return $ message ;
1239
1260
}
1240
1261
@@ -1260,9 +1281,11 @@ public function prependMessage($messagePrependTo)
1260
1281
) {
1261
1282
$ comment = $ preparedMessage ->getComment () . ' ' . $ messagePrependTo ;
1262
1283
$ preparedMessage ->setComment ($ comment );
1284
+
1263
1285
return $ comment ;
1264
1286
}
1265
1287
}
1288
+
1266
1289
return $ messagePrependTo ;
1267
1290
}
1268
1291
@@ -1276,6 +1299,7 @@ public function prependMessage($messagePrependTo)
1276
1299
public function formatAmount ($ amount , $ asFloat = false )
1277
1300
{
1278
1301
$ amount = $ this ->priceCurrency ->round ($ amount );
1302
+
1279
1303
return !$ asFloat ? (string )$ amount : $ amount ;
1280
1304
}
1281
1305
@@ -1311,6 +1335,7 @@ public function getAuthorizationTransaction()
1311
1335
public function isCaptureFinal ($ amountToCapture )
1312
1336
{
1313
1337
$ total = $ this ->getOrder ()->getBaseTotalDue ();
1338
+
1314
1339
return $ this ->formatAmount ($ total , true ) == $ this ->formatAmount ($ amountToCapture , true );
1315
1340
}
1316
1341
@@ -1354,6 +1379,7 @@ public function getTransactionAdditionalInfo()
1354
1379
public function resetTransactionAdditionalInfo ()
1355
1380
{
1356
1381
$ this ->transactionAdditionalInfo = [];
1382
+
1357
1383
return $ this ;
1358
1384
}
1359
1385
@@ -1410,6 +1436,7 @@ protected function _getInvoiceForTransactionId($transactionId)
1410
1436
foreach ($ this ->getOrder ()->getInvoiceCollection () as $ invoice ) {
1411
1437
if ($ invoice ->getTransactionId () == $ transactionId ) {
1412
1438
$ invoice ->load ($ invoice ->getId ());
1439
+
1413
1440
// to make sure all data will properly load (maybe not required)
1414
1441
return $ invoice ;
1415
1442
}
@@ -1420,9 +1447,11 @@ protected function _getInvoiceForTransactionId($transactionId)
1420
1447
)
1421
1448
) {
1422
1449
$ invoice ->setTransactionId ($ transactionId );
1450
+
1423
1451
return $ invoice ;
1424
1452
}
1425
1453
}
1454
+
1426
1455
return false ;
1427
1456
}
1428
1457
@@ -1436,7 +1465,7 @@ private function getOrderStateResolver()
1436
1465
$ this ->orderStateResolver = ObjectManager::getInstance ()->get (OrderStateResolverInterface::class);
1437
1466
}
1438
1467
1439
- return $ this ->orderStateResolver ;
1468
+ return $ this ->orderStateResolver ;
1440
1469
}
1441
1470
1442
1471
//@codeCoverageIgnoreStart
@@ -2413,6 +2442,7 @@ public function setExtensionAttributes(\Magento\Sales\Api\Data\OrderPaymentExten
2413
2442
public function setIsTransactionPending ($ flag )
2414
2443
{
2415
2444
$ this ->setData ('is_transaction_pending ' , (bool )$ flag );
2445
+
2416
2446
return $ this ;
2417
2447
}
2418
2448
@@ -2436,6 +2466,7 @@ public function getIsTransactionPending()
2436
2466
public function setIsFraudDetected ($ flag )
2437
2467
{
2438
2468
$ this ->setData ('is_fraud_detected ' , (bool )$ flag );
2469
+
2439
2470
return $ this ;
2440
2471
}
2441
2472
@@ -2459,6 +2490,7 @@ public function getIsFraudDetected()
2459
2490
public function setShouldCloseParentTransaction ($ flag )
2460
2491
{
2461
2492
$ this ->setData ('should_close_parent_transaction ' , (bool )$ flag );
2493
+
2462
2494
return $ this ;
2463
2495
}
2464
2496
@@ -2493,4 +2525,26 @@ private function setTransactionIdsForRefund(Transaction $transaction)
2493
2525
}
2494
2526
2495
2527
//@codeCoverageIgnoreEnd
2528
+
2529
+ /**
2530
+ * Collects order invoices totals by provided keys.
2531
+ * Returns result as {key: amount}.
2532
+ *
2533
+ * @param Order $order
2534
+ * @param array $keys
2535
+ * @return array
2536
+ */
2537
+ private function collectTotalAmounts (Order $ order , array $ keys )
2538
+ {
2539
+ $ result = array_fill_keys ($ keys , 0.00 );
2540
+ $ invoiceCollection = $ order ->getInvoiceCollection ();
2541
+ /** @var Invoice $invoice */
2542
+ foreach ($ invoiceCollection as $ invoice ) {
2543
+ foreach ($ keys as $ key ) {
2544
+ $ result [$ key ] += $ invoice ->getData ($ key );
2545
+ }
2546
+ }
2547
+
2548
+ return $ result ;
2549
+ }
2496
2550
}
0 commit comments