24
24
use Magento \Checkout \Model \Session ;
25
25
use Buckaroo \Magento2 \Model \Service \Order ;
26
26
use Buckaroo \Magento2 \Model \Method \Payconiq ;
27
- use Buckaroo \Magento2 \Model \Method \Giftcards ;
28
27
use Magento \Quote \Api \CartRepositoryInterface ;
29
28
use Buckaroo \Magento2 \Model \ConfigProvider \Account ;
30
29
use Buckaroo \Magento2 \Helper \PaymentGroupTransaction ;
31
30
use Buckaroo \Magento2 \Model \Giftcard \Remove as GiftcardRemove ;
32
- use Buckaroo \Magento2 \Model \Method \AbstractMethod ;
33
- use Buckaroo \Magento2 \Service \Sales \Quote \Recreate as QuoteRecreate ;
31
+ use Magento \Framework \Event \Observer ;
32
+ use Magento \Framework \Event \ObserverInterface ;
33
+ use Magento \Quote \Model \Quote ;
34
34
35
- class RestoreQuote implements \ Magento \ Framework \ Event \ ObserverInterface
35
+ class RestoreQuote implements ObserverInterface
36
36
{
37
- /**
38
- * @var \Magento\Checkout\Model\Session
39
- */
40
37
private $ checkoutSession ;
41
-
42
- /**
43
- * @var \Buckaroo\Magento2\Model\ConfigProvider\Account
44
- */
45
38
protected $ accountConfig ;
46
-
47
- /**
48
- * @var \Buckaroo\Magento2\Helper\Data
49
- */
50
- private \Buckaroo \Magento2 \Helper \Data $ helper ;
51
-
52
-
53
-
54
- /**
55
- * @var \Magento\Quote\Api\CartRepositoryInterface
56
- */
39
+ private $ helper ;
57
40
protected $ quoteRepository ;
58
-
59
- /**
60
- * @var \Buckaroo\Magento2\Model\Service\Order
61
- */
62
41
protected $ orderService ;
63
-
64
- /**
65
- * @var \Buckaroo\Magento2\Model\Giftcard\Remove
66
- */
67
42
protected $ giftcardRemoveService ;
68
-
69
- /**
70
- * @var \Buckaroo\Magento2\Helper\PaymentGroupTransaction
71
- */
72
43
protected $ groupTransaction ;
73
44
74
-
75
- /**
76
- * @param Session $checkoutSession
77
- * @param Account $accountConfig
78
- * @param Data $helper
79
- * @param QuoteRecreate $quoteRecreate
80
- * @param CartRepositoryInterface $quoteRepository
81
- * @param Order $orderService
82
- */
83
45
public function __construct (
84
- \ Magento \ Checkout \ Model \ Session $ checkoutSession ,
85
- \ Buckaroo \ Magento2 \ Model \ ConfigProvider \ Account $ accountConfig ,
86
- \ Buckaroo \ Magento2 \ Helper \ Data $ helper ,
87
- \ Magento \ Quote \ Api \ CartRepositoryInterface $ quoteRepository ,
88
- \ Buckaroo \ Magento2 \ Model \ Service \ Order $ orderService ,
46
+ Session $ checkoutSession ,
47
+ Account $ accountConfig ,
48
+ Data $ helper ,
49
+ CartRepositoryInterface $ quoteRepository ,
50
+ Order $ orderService ,
89
51
GiftcardRemove $ giftcardRemoveService ,
90
52
PaymentGroupTransaction $ groupTransaction
91
53
) {
@@ -101,57 +63,38 @@ public function __construct(
101
63
/**
102
64
* Restore Quote and Cancel LastRealOrder
103
65
*
104
- * @param \Magento\Framework\Event\ Observer $observer
66
+ * @param Observer $observer
105
67
* @return void
106
68
*/
107
- public function execute (\ Magento \ Framework \ Event \ Observer $ observer )
69
+ public function execute (Observer $ observer )
108
70
{
109
71
$ this ->helper ->addDebug (__METHOD__ . '|1| ' . var_export ($ this ->checkoutSession ->getData (), true ));
110
72
111
73
$ lastRealOrder = $ this ->checkoutSession ->getLastRealOrder ();
112
74
$ previousOrderId = $ lastRealOrder ->getId ();
113
75
114
76
if ($ payment = $ lastRealOrder ->getPayment ()) {
115
- if ($ this ->shouldSkipFurtherEventHandling ()
116
- || strpos ($ payment ->getMethod (), 'buckaroo_magento2 ' ) === false
117
- || in_array ($ payment ->getMethod (), [Payconiq::PAYMENT_METHOD_CODE ])) {
77
+ if ($ this ->shouldSkipFurtherEventHandling () || $ this ->isPayconiqPaymentMethod ($ payment )) {
118
78
$ this ->helper ->addDebug (__METHOD__ . '|10| ' );
119
79
return ;
120
80
}
121
81
122
- if ($ payment ->getMethod () === 'buckaroo_magento2_ideal ' &&
123
- isset ($ payment ->getAdditionalInformation ()['issuer ' ]) &&
124
- $ payment ->getAdditionalInformation ()['issuer ' ] === 'fastcheckout ' ) {
125
- $ this ->helper ->addDebug (__METHOD__ . '|Detected buckaroo_magento2_ideal with issuer fastcheckout, handling accordingly. ' );
126
- return ;
127
- }
128
-
129
82
if ($ this ->accountConfig ->getCartKeepAlive ($ lastRealOrder ->getStore ())) {
130
83
$ this ->helper ->addDebug (__METHOD__ . '|20| ' );
131
84
132
- if ($ this ->checkoutSession ->getQuote ()
133
- && $ this ->checkoutSession ->getQuote ()->getId ()
134
- && ($ quote = $ this ->quoteRepository ->getActive ($ this ->checkoutSession ->getQuote ()->getId ()))
135
- ) {
136
- $ this ->helper ->addDebug (__METHOD__ . '|25| ' );
137
- $ shippingAddress = $ quote ->getShippingAddress ();
138
- if (!$ shippingAddress ->getShippingMethod ()) {
139
- $ this ->helper ->addDebug (__METHOD__ . '|35| ' );
140
- $ shippingAddress ->load ($ shippingAddress ->getAddressId ());
141
- }
85
+ $ quote = $ this ->getActiveQuote ();
86
+ if ($ quote ) {
87
+ $ this ->processShippingAddress ($ quote );
142
88
}
143
89
144
-
145
- if (
146
- (
147
- $ this ->helper ->getRestoreQuoteLastOrder () &&
148
- ($ lastRealOrder ->getData ('state ' ) === 'new ' ) &&
149
- ($ lastRealOrder ->getData ('status ' ) === 'pending ' ) &&
150
- $ payment ->getMethodInstance ()->usesRedirect
151
- ) || $ this ->canRestoreFailedFromSpam ()
152
- ) {
90
+ if ($ this ->shouldRestoreQuote ($ lastRealOrder , $ payment )) {
153
91
$ this ->helper ->addDebug (__METHOD__ . '|40| ' );
154
92
$ this ->checkoutSession ->restoreQuote ();
93
+
94
+ if ($ this ->isFastCheckout ($ payment )) {
95
+ $ this ->clearRestoredQuoteAddresses ($ this ->checkoutSession ->getQuote ());
96
+ }
97
+
155
98
$ this ->rollbackPartialPayment ($ lastRealOrder ->getIncrementId ());
156
99
$ this ->setOrderToCancel ($ previousOrderId );
157
100
}
@@ -166,10 +109,126 @@ public function execute(\Magento\Framework\Event\Observer $observer)
166
109
}
167
110
168
111
/**
169
- * Check if order has failed from max spam payment attempts
112
+ * Get the active quote from the checkout session.
113
+ *
114
+ * @return Quote|null
115
+ */
116
+ private function getActiveQuote ()
117
+ {
118
+ $ quote = null ;
119
+ if ($ this ->checkoutSession ->getQuote () && $ this ->checkoutSession ->getQuote ()->getId ()) {
120
+ try {
121
+ $ quote = $ this ->quoteRepository ->getActive ($ this ->checkoutSession ->getQuote ()->getId ());
122
+ } catch (\Exception $ e ) {
123
+ $ this ->helper ->addError (__METHOD__ . '|Error fetching active quote: ' . $ e ->getMessage ());
124
+ }
125
+ }
126
+ return $ quote ;
127
+ }
128
+
129
+ /**
130
+ * Process the shipping address of the quote.
131
+ *
132
+ * @param Quote $quote
133
+ */
134
+ private function processShippingAddress ($ quote )
135
+ {
136
+ $ this ->helper ->addDebug (__METHOD__ . '|25| ' );
137
+ $ shippingAddress = $ quote ->getShippingAddress ();
138
+ if ($ shippingAddress && !$ shippingAddress ->getShippingMethod ()) {
139
+ $ this ->helper ->addDebug (__METHOD__ . '|35| ' );
140
+ try {
141
+ $ shippingAddress ->load ($ shippingAddress ->getAddressId ());
142
+ } catch (\Exception $ e ) {
143
+ $ this ->helper ->addError (__METHOD__ . '|Error loading shipping address: ' . $ e ->getMessage ());
144
+ }
145
+ }
146
+ }
147
+
148
+ /**
149
+ * Check if the quote should be restored.
150
+ *
151
+ * @param $lastRealOrder
152
+ * @param $payment
153
+ * @return bool
154
+ */
155
+ private function shouldRestoreQuote ($ lastRealOrder , $ payment )
156
+ {
157
+ return (
158
+ ($ this ->helper ->getRestoreQuoteLastOrder () &&
159
+ ($ lastRealOrder ->getData ('state ' ) === 'new ' ) &&
160
+ ($ lastRealOrder ->getData ('status ' ) === 'pending ' ) &&
161
+ $ payment ->getMethodInstance ()->usesRedirect ) || $ this ->canRestoreFailedFromSpam ()
162
+ );
163
+ }
164
+
165
+ /**
166
+ * Clear addresses after quote restoration to ensure they are not unintentionally restored.
167
+ *
168
+ * @param Quote $quote
169
+ */
170
+ private function clearRestoredQuoteAddresses ($ quote )
171
+ {
172
+ if ($ quote && $ quote ->getId ()) {
173
+ $ quote ->setCustomerEmail (null );
174
+
175
+ // Remove existing addresses if they exist
176
+ $ this ->clearAddress ($ quote , $ quote ->getBillingAddress ());
177
+ $ this ->clearAddress ($ quote , $ quote ->getShippingAddress ());
178
+
179
+ // Save the modified quote to ensure addresses are cleared
180
+ try {
181
+ $ this ->quoteRepository ->save ($ quote );
182
+ $ this ->helper ->addDebug (__METHOD__ . '|Addresses cleared after restoreQuote() ' );
183
+ } catch (\Exception $ e ) {
184
+ $ this ->helper ->addDebug (__METHOD__ . '|Error clearing addresses: ' . $ e ->getMessage ());
185
+ }
186
+ }
187
+ }
188
+
189
+
190
+ /**
191
+ * Clear address data and remove the address object from the quote.
192
+ *
193
+ * @param Quote $quote
194
+ * @param $address
195
+ */
196
+ private function clearAddress ($ quote , $ address )
197
+ {
198
+ if ($ address ) {
199
+ // Remove the address from the quote
200
+ $ quote ->removeAddress ($ address ->getId ());
201
+
202
+ // Optionally clear address data if needed to reset but keep structure intact
203
+ $ address ->addData ([]);
204
+ }
205
+ }
206
+
207
+ /**
208
+ * Check if the payment method is fastcheckout.
209
+ *
210
+ * @param $payment
211
+ * @return bool
212
+ */
213
+ private function isFastCheckout ($ payment )
214
+ {
215
+ return $ payment ->getMethod () === 'buckaroo_magento2_ideal ' &&
216
+ isset ($ payment ->getAdditionalInformation ()['issuer ' ]) &&
217
+ $ payment ->getAdditionalInformation ()['issuer ' ] === 'fastcheckout ' ;
218
+ }
219
+
220
+ /**
221
+ * Check if the payment method should be skipped.
170
222
*
171
- * @return boolean
223
+ * @param $payment
224
+ * @return bool
172
225
*/
226
+ private function isPayconiqPaymentMethod ($ payment )
227
+ {
228
+ return strpos ($ payment ->getMethod (), 'buckaroo_magento2 ' ) === false ||
229
+ in_array ($ payment ->getMethod (), [Payconiq::PAYMENT_METHOD_CODE ]);
230
+ }
231
+
173
232
public function canRestoreFailedFromSpam ()
174
233
{
175
234
return $ this ->helper ->getRestoreQuoteLastOrder () &&
@@ -181,18 +240,11 @@ public function shouldSkipFurtherEventHandling()
181
240
return false ;
182
241
}
183
242
184
- /**
185
- * Set previous order id on the payment object for the next payment
186
- *
187
- * @param int $previousOrderId
188
- *
189
- * @return void
190
- */
191
243
private function setOrderToCancel ($ previousOrderId )
192
244
{
193
245
$ this ->checkoutSession ->getQuote ()
194
- ->getPayment ()
195
- ->setAdditionalInformation ('buckaroo_cancel_order_id ' , $ previousOrderId );
246
+ ->getPayment ()
247
+ ->setAdditionalInformation ('buckaroo_cancel_order_id ' , $ previousOrderId );
196
248
$ this ->quoteRepository ->save ($ this ->checkoutSession ->getQuote ());
197
249
}
198
250
@@ -206,6 +258,5 @@ public function rollbackPartialPayment($incrementId)
206
258
} catch (\Throwable $ th ) {
207
259
$ this ->helper ->addDebug (__METHOD__ . (string )$ th );
208
260
}
209
-
210
261
}
211
262
}
0 commit comments