9
9
10
10
use Exception ;
11
11
use Magento \Customer \Api \AddressRepositoryInterface ;
12
- use Magento \Framework \Api \SearchCriteriaInterface ;
13
12
use Magento \Framework \App \Action \Action ;
14
13
use Magento \Framework \App \Action \Context ;
15
14
use Magento \Framework \App \ResponseInterface ;
@@ -33,11 +32,6 @@ class Confirm extends Action
33
32
*/
34
33
private $ addressRepository ;
35
34
36
- /**
37
- * @var SearchCriteriaInterface
38
- */
39
- private $ searchCriteria ;
40
-
41
35
/**
42
36
* @var OrderService
43
37
*/
@@ -51,13 +45,11 @@ class Confirm extends Action
51
45
public function __construct (
52
46
Context $ context ,
53
47
AddressRepositoryInterface $ addressRepository ,
54
- SearchCriteriaInterface $ searchCriteria ,
55
48
OrderService $ orderService ,
56
49
OrderSender $ orderSender ,
57
50
ConfigRepository $ configRepository
58
51
) {
59
52
$ this ->addressRepository = $ addressRepository ;
60
- $ this ->searchCriteria = $ searchCriteria ;
61
53
$ this ->orderService = $ orderService ;
62
54
$ this ->orderSender = $ orderSender ;
63
55
$ this ->configRepository = $ configRepository ;
@@ -70,6 +62,7 @@ public function __construct(
70
62
*/
71
63
public function execute ()
72
64
{
65
+ $ order = null ;
73
66
try {
74
67
$ order = $ this ->orderService ->getOrderByReference ();
75
68
$ twoOrder = $ this ->orderService ->getTwoOrderFromApi ($ order );
@@ -79,9 +72,21 @@ public function execute()
79
72
}
80
73
$ this ->orderSender ->send ($ order );
81
74
try {
82
- $ this ->saveAddress ($ order , $ twoOrder );
75
+ $ this ->updateCustomerAddress ($ order , $ twoOrder );
76
+ } catch (LocalizedException $ exception ) {
77
+ $ message = __ (
78
+ "Failed to update %1 customer address: %2 " ,
79
+ $ this ->configRepository ::PROVIDER ,
80
+ $ exception ->getMessage ()
81
+ );
82
+ $ this ->orderService ->addOrderComment ($ order , $ message );
83
83
} catch (Exception $ exception ) {
84
- $ this ->orderService ->addOrderComment ($ order , $ exception ->getMessage ());
84
+ $ message = __ (
85
+ "Failed to update %1 customer address: %2 " ,
86
+ $ this ->configRepository ::PROVIDER ,
87
+ $ exception ->getMessage ()
88
+ );
89
+ $ this ->orderService ->addOrderComment ($ order , $ message );
85
90
}
86
91
$ this ->orderService ->processOrder ($ order , $ twoOrder ['id ' ]);
87
92
return $ this ->getResponse ()->setRedirect ($ this ->_url ->getUrl ('checkout/onepage/success ' ));
@@ -99,87 +104,45 @@ public function execute()
99
104
}
100
105
} catch (Exception $ exception ) {
101
106
$ this ->orderService ->restoreQuote ();
102
- if (isset ( $ order) ) {
107
+ if ($ order !== null ) {
103
108
$ this ->orderService ->failOrder ($ order , $ exception ->getMessage ());
104
109
}
105
-
106
110
$ this ->messageManager ->addErrorMessage ($ exception ->getMessage ());
107
111
return $ this ->getResponse ()->setRedirect ($ this ->_url ->getUrl ('checkout/cart ' ));
108
112
}
109
113
}
110
114
111
115
/**
112
- * Save address
116
+ * Update customer address
113
117
*
114
118
* @param $order
115
119
* @param array $twoOrder
116
120
*
121
+ * @return void
117
122
* @throws Exception
118
123
*/
119
- private function saveAddress ($ order , $ twoOrder )
124
+ private function updateCustomerAddress ($ order , $ twoOrder )
120
125
{
121
- if ($ order ->getCustomerId ()) {
122
- if ($ order ->getBillingAddress ()->getCustomerAddressId ()) {
123
- $ customerAddress = $ this ->addressRepository ->getById (
124
- $ order ->getBillingAddress ()->getCustomerAddressId ()
125
- );
126
- } else {
127
- $ this ->searchCriteria
128
- ->setField ('parent_id ' )
129
- ->setValue ($ order ->getCustomerId ())
130
- ->setConditionType ('eq ' );
131
- $ customerAddressCollection = $ this ->addressRepository
132
- ->getList ($ this ->searchCriteria )
133
- ->getItems ();
134
- $ customerAddress = $ customerAddressCollection [0 ] ?? null ;
135
- }
126
+ $ customerAddress = null ;
127
+ if ($ order ->getBillingAddress ()->getCustomerAddressId ()) {
128
+ $ customerAddress = $ this ->addressRepository ->getById (
129
+ $ order ->getBillingAddress ()->getCustomerAddressId ()
130
+ );
136
131
if ($ customerAddress && $ customerAddress ->getId ()) {
137
- $ this ->copyMetadataToAddress (
138
- $ twoOrder ,
139
- $ customerAddress
140
- );
132
+ if (isset ($ twoOrder ['buyer ' ]['company ' ]['organization_number ' ])) {
133
+ $ customerAddress ->setData ('company_id ' , $ twoOrder ['buyer ' ]['company ' ]['organization_number ' ]);
134
+ }
135
+ if (isset ($ twoOrder ['buyer ' ]['company ' ]['company_name ' ])) {
136
+ $ customerAddress ->setData ('company_name ' , $ twoOrder ['buyer ' ]['company ' ]['company_name ' ]);
137
+ }
138
+ if (isset ($ twoOrder ['buyer_department ' ])) {
139
+ $ customerAddress ->setData ('department ' , $ twoOrder ['buyer_department ' ]);
140
+ }
141
+ if (isset ($ twoOrder ['buyer_project ' ])) {
142
+ $ customerAddress ->setData ('project ' , $ twoOrder ['buyer_project ' ]);
143
+ }
141
144
$ this ->addressRepository ->save ($ customerAddress );
142
145
}
143
- } else {
144
- // Save metadata to shipping address
145
- $ shippingAddress = $ order ->getShippingAddress ();
146
- $ this ->copyMetadataToAddress (
147
- $ twoOrder ,
148
- $ shippingAddress
149
- );
150
- $ shippingAddress ->save ();
151
- // Save metadata to billing address
152
- $ billingAddress = $ order ->getBillingAddress ();
153
- $ this ->copyMetadataToAddress (
154
- $ twoOrder ,
155
- $ billingAddress
156
- );
157
- $ billingAddress ->save ();
158
- }
159
- }
160
-
161
- /**
162
- * Set metadata to customer address
163
- *
164
- * @param array $twoOrder
165
- * @param $address
166
- *
167
- * @return void
168
- * @throws Exception
169
- */
170
- private function copyMetadataToAddress (array $ twoOrder , $ address )
171
- {
172
- if (isset ($ twoOrder ['buyer ' ]['company ' ]['organization_number ' ])) {
173
- $ address ->setData ('company_id ' , $ twoOrder ['buyer ' ]['company ' ]['organization_number ' ]);
174
- }
175
- if (isset ($ twoOrder ['buyer ' ]['company ' ]['company_name ' ])) {
176
- $ address ->setData ('company_name ' , $ twoOrder ['buyer ' ]['company ' ]['company_name ' ]);
177
- }
178
- if (isset ($ twoOrder ['buyer_department ' ])) {
179
- $ address ->setData ('department ' , $ twoOrder ['buyer_department ' ]);
180
- }
181
- if (isset ($ twoOrder ['buyer_project ' ])) {
182
- $ address ->setData ('project ' , $ twoOrder ['buyer_project ' ]);
183
146
}
184
147
}
185
148
}
0 commit comments