4
4
5
5
use Magento \Checkout \Model \Session ;
6
6
use Magento \Quote \Api \CartRepositoryInterface ;
7
+ use Magento \Sales \Api \OrderRepositoryInterface ;
7
8
use Magento \Sales \Model \Order ;
8
9
use Magento \Sales \Model \OrderFactory ;
9
10
@@ -28,20 +29,28 @@ class Checkout
28
29
*/
29
30
private $ quoteRepository ;
30
31
32
+ /**
33
+ * @var OrderRepositoryInterface
34
+ */
35
+ private $ orderRepository ;
36
+
31
37
/**
32
38
* Checkout constructor.
33
39
* @param Session $session
34
40
* @param OrderFactory $order
35
41
* @param CartRepositoryInterface $quoteRepository
42
+ * @param OrderRepositoryInterface $orderRepository
36
43
*/
37
44
public function __construct (
38
45
Session $ session ,
39
46
OrderFactory $ order ,
40
- CartRepositoryInterface $ quoteRepository
47
+ CartRepositoryInterface $ quoteRepository ,
48
+ OrderRepositoryInterface $ orderRepository
41
49
) {
42
50
$ this ->session = $ session ;
43
51
$ this ->orderFactory = $ order ;
44
52
$ this ->quoteRepository = $ quoteRepository ;
53
+ $ this ->orderRepository = $ orderRepository ;
45
54
}
46
55
47
56
/**
@@ -56,8 +65,8 @@ public function cancelCurrentOrder($comment)
56
65
$ order = $ this ->session ->getLastRealOrder ();
57
66
58
67
if ($ order ->getId () && $ order ->getState () != Order::STATE_CANCELED ) {
59
- $ order ->registerCancellation ($ comment )-> save () ;
60
- return true ;
68
+ $ order ->registerCancellation ($ comment );
69
+ return $ this -> orderRepository -> save ( $ order ) ;
61
70
}
62
71
return false ;
63
72
}
@@ -88,8 +97,23 @@ public function cancelOrderById($orderId, $comment)
88
97
$ order = $ this ->orderFactory ->create ()->loadByIncrementId ($ orderId );
89
98
90
99
if ($ order ->getId () && $ order ->getState () != Order::STATE_CANCELED ) {
91
- $ order ->registerCancellation ($ comment )->save ();
92
- return true ;
100
+ $ order ->registerCancellation ($ comment );
101
+ return $ this ->orderRepository ->save ($ order );
102
+ }
103
+ return false ;
104
+ }
105
+
106
+ /**
107
+ * @param Order $order
108
+ * @param string $comment
109
+ * @return bool
110
+ * @throws \Magento\Framework\Exception\LocalizedException
111
+ */
112
+ public function cancelOrder (Order $ order , string $ comment = "" )
113
+ {
114
+ if ($ order ->getState () != Order::STATE_CANCELED ) {
115
+ $ order ->registerCancellation ($ comment );
116
+ return $ this ->orderRepository ->save ($ order );
93
117
}
94
118
return false ;
95
119
}
@@ -104,15 +128,13 @@ public function cancelOrderById($orderId, $comment)
104
128
public function processOrdersFailedPayment ($ orderIds , $ failureReason = 'Unexpected Error with empty charge ' )
105
129
{
106
130
foreach ($ orderIds as $ key => $ value ) {
107
- $ order = $ this ->orderFactory ->create ();
108
- $ order ->load ($ value );
109
-
131
+ $ order = $ this ->orderRepository ->get ($ value );
110
132
$ orderState = Order::STATE_CANCELED ;
111
- $ order ->setState ($ orderState )
133
+ $ order ->setState ($ orderState )
112
134
->setStatus ($ orderState )
113
135
->addStatusHistoryComment ("Order # " . $ value . " was rejected by Xendit because " . $ failureReason );
114
- $ order ->save ();
115
136
137
+ $ order = $ this ->orderRepository ->save ($ order );
116
138
$ quoteId = $ order ->getQuoteId ();
117
139
$ quote = $ this ->quoteRepository ->get ($ quoteId );
118
140
$ this ->restoreQuote ($ quote );
0 commit comments