Total Complexity | 50 |
Total Lines | 410 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Complex classes like EDU_KlarnaCheckout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EDU_KlarnaCheckout, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
5 | class EDU_KlarnaCheckout extends EDU_Integration { |
||
6 | public function __construct() { |
||
7 | $this->id = 'eduadmin-klarnacheckout'; |
||
8 | $this->displayName = __( 'Klarna Checkout', 'eduadmin-wp-klarna-checkout' ); |
||
9 | $this->description = ''; |
||
10 | $this->type = 'payment'; |
||
11 | |||
12 | $this->init_form_fields(); |
||
13 | $this->init_settings(); |
||
14 | |||
15 | add_action( 'eduadmin-checkpaymentplugins', array( $this, 'intercept_booking' ) ); |
||
16 | add_action( 'eduadmin-processbooking', array( $this, 'process_booking' ) ); |
||
17 | add_action( 'eduadmin-bookingcompleted', array( $this, 'process_klarnaresponse' ) ); |
||
18 | add_action( 'wp_loaded', array( $this, 'process_paymentstatus' ) ); |
||
19 | |||
20 | add_shortcode( 'eduadmin-klarna-testpage', array( $this, 'test_page' ) ); |
||
21 | } |
||
22 | |||
23 | /** |
||
24 | * @param $attributes |
||
25 | */ |
||
26 | public function test_page( $attributes ) { |
||
27 | $attributes = shortcode_atts( |
||
28 | array( |
||
29 | 'bookingid' => 0, |
||
30 | 'programmebookingid' => 0, |
||
31 | ), |
||
32 | normalize_empty_atts( $attributes ), |
||
33 | 'test_page' |
||
34 | ); |
||
35 | |||
36 | if ( $attributes['bookingid'] > 0 ) { |
||
37 | $event_booking = EDUAPI()->OData->Bookings->GetItem( |
||
|
|||
38 | $attributes['bookingid'], |
||
39 | null, |
||
40 | 'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows', |
||
41 | false |
||
42 | ); |
||
43 | } elseif ( $attributes['programmebookingid'] > 0 ) { |
||
44 | $event_booking = EDUAPI()->OData->ProgrammeBookings->GetItem( |
||
45 | $attributes['programmebookingid'], |
||
46 | null, |
||
47 | 'Customer($select=CustomerId;),ContactPerson($select=PersonId;),OrderRows', |
||
48 | false |
||
49 | ); |
||
50 | } |
||
51 | |||
52 | $_customer = EDUAPI()->OData->Customers->GetItem( |
||
53 | $event_booking['Customer']['CustomerId'], |
||
54 | null, |
||
55 | "BillingInfo", |
||
56 | false |
||
57 | ); |
||
58 | |||
59 | $_contact = EDUAPI()->OData->Persons->GetItem( |
||
60 | $event_booking['ContactPerson']['PersonId'], |
||
61 | null, |
||
62 | null, |
||
63 | false |
||
64 | ); |
||
65 | |||
66 | $ebi = new EduAdmin_BookingInfo( $event_booking, $_customer, $_contact ); |
||
67 | |||
68 | if ( ! empty( EDU()->session['klarna-order-id'] ) && ! empty( $_GET['klarna_order_id'] ) && EDU()->session['klarna-order-id'] === $_GET['klarna_order_id'] ) { |
||
69 | do_action( 'eduadmin-bookingcompleted', $ebi ); |
||
70 | } else { |
||
71 | do_action( 'eduadmin-processbooking', $ebi ); |
||
72 | } |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * @param EduAdmin_BookingInfo|null $ebi |
||
77 | */ |
||
78 | public function intercept_booking( $ebi = null ) { |
||
85 | } |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @param EduAdmin_BookingInfo|null $ebi |
||
90 | */ |
||
91 | public function process_booking( $ebi = null ) { |
||
103 | } |
||
104 | } |
||
105 | |||
106 | public function process_klarnaresponse() { |
||
133 | } |
||
134 | } |
||
135 | } |
||
136 | |||
137 | public function init_form_fields() { |
||
168 | ), |
||
169 | ); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param EduAdmin_BookingInfo|null $ebi |
||
174 | * |
||
175 | * @return Klarna_Checkout_Order|null |
||
176 | */ |
||
177 | public function create_checkout( $ebi = null ) { |
||
178 | |||
179 | $checkout_url = ! checked( $this->get_option( 'test_mode', 'no' ), '1', false ) ? Klarna_Checkout_Connector::BASE_URL : Klarna_Checkout_Connector::BASE_TEST_URL; |
||
180 | $shared_secret = $this->get_option( 'shared_secret', '' ); |
||
181 | |||
182 | $create = array(); |
||
183 | |||
184 | $organization = EDUAPIHelper()->GetOrganization(); |
||
185 | $purchase_country = $organization["CountryCode"]; |
||
186 | |||
187 | $emd_info = array( |
||
188 | 'unique_account_identifier' => ( ! empty( $ebi->Contact['Email'] ) ? $ebi->Contact['Email'] : "uid:" . $ebi->Contact['PersonId'] ), |
||
189 | 'account_registration_date' => date( 'Y-m-d\TH:i', strtotime( $ebi->Contact['Created'] ) ), |
||
190 | 'account_last_modified' => date( 'Y-m-d\TH:i' ) |
||
191 | ); |
||
192 | |||
193 | $emd_info = array( $emd_info ); |
||
194 | |||
195 | $booking_id = 0; |
||
196 | $programme_booking_id = 0; |
||
197 | |||
198 | $reference_id = 0; |
||
199 | |||
200 | $_event = null; |
||
201 | |||
202 | $eventName = ''; |
||
203 | |||
204 | $locationAddress = ''; |
||
205 | $locationCountry = ''; |
||
206 | $locationPostalCode = ''; |
||
207 | |||
208 | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
||
209 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
210 | $reference_id = $booking_id; |
||
211 | |||
212 | $_event = EDUAPI()->OData->Events->GetItem( $ebi->EventBooking['EventId'], null, "LocationAddress" ); |
||
213 | |||
214 | $eventName = $_event['EventName']; |
||
215 | |||
216 | if ( ! empty( $_event['LocationAddress'] ) && $_event['LocationAdress'] != null ) { |
||
217 | $locationAddress = $_event['LocationAddress']['Address']; |
||
218 | $locationCountry = $_event['LocationAddress']['Country']; |
||
219 | $locationPostalCode = $_event['LocationAddress']['AddressZip']; |
||
220 | } |
||
221 | } |
||
222 | |||
223 | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
||
224 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
225 | $reference_id = $programme_booking_id; |
||
226 | |||
227 | $_event = EDUAPI()->OData->ProgrammeStarts->GetItem( $ebi->EventBooking['ProgrammeStartId'] ); |
||
228 | |||
229 | $eventName = $_event['ProgrammeStartName']; |
||
230 | } |
||
231 | |||
232 | $emd_event_info = array( |
||
233 | 'event_name' => ( ! empty( $eventName ) ? $eventName : "" ), |
||
234 | 'event_company' => $organization['OrganisationName'], |
||
235 | 'genre_of_event' => ( ! empty( $_event['CategoryName'] ) ? $_event['CategoryName'] : "" ), |
||
236 | 'arena_name' => ( ! empty( $_event['AddressName'] ) ? $_event['AddressName'] : "" ), |
||
237 | 'arena_location' => array( |
||
238 | 'street_address' => $locationAddress, |
||
239 | 'postal_code' => $locationPostalCode, |
||
240 | 'city' => ( ! empty( $_event['City'] ) ? $_event['City'] : "" ), |
||
241 | 'country' => $locationCountry |
||
242 | ), |
||
243 | 'start_time' => date( 'Y-m-d\TH:i', strtotime( $_event['StartDate'] ) ), |
||
244 | 'end_time' => date( 'Y-m-d\TH:i', strtotime( $_event['EndDate'] ) ), |
||
245 | 'access_controlled_venue' => false |
||
246 | ); |
||
247 | |||
248 | $emd_event_info = array( $emd_event_info ); |
||
249 | |||
250 | $emd_attachment = array( |
||
251 | 'customer_account_info' => $emd_info, |
||
252 | 'event' => $emd_event_info |
||
253 | ); |
||
254 | |||
255 | $create['attachment'] = array(); |
||
256 | $create['attachment']['content_type'] = 'application/vnd.klarna.internal.emd-v2+json'; |
||
257 | $create['attachment']['body'] = json_encode( $emd_attachment ); |
||
258 | |||
259 | $create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) ); |
||
260 | $create['purchase_country'] = $purchase_country; |
||
261 | $create['purchase_currency'] = get_option( 'eduadmin-currency', 'SEK' ); |
||
262 | |||
263 | $merchant = array(); |
||
264 | $merchant['id'] = $this->get_option( 'eid', '' ); |
||
265 | $merchant['terms_uri'] = $this->get_option( 'termsurl', '' ); |
||
266 | |||
267 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
268 | |||
269 | $rowExtraInfo = ""; |
||
270 | |||
271 | if ( null != $_event ) { |
||
272 | if ( ! empty( $_event['City'] ) ) { |
||
273 | $rowExtraInfo .= ';' . $_event['City']; |
||
274 | } |
||
275 | |||
276 | if ( ! empty( $_event['StartDate'] ) ) { |
||
277 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['StartDate'] ) ); |
||
278 | } |
||
279 | |||
280 | if ( ! empty( $_event['EndDate'] ) ) { |
||
281 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['EndDate'] ) ); |
||
282 | } |
||
283 | } |
||
284 | |||
285 | $confirmation_url = add_query_arg( |
||
286 | array( |
||
287 | 'klarna_order_id' => '{checkout.order.id}', |
||
288 | 'booking_id' => $booking_id, |
||
289 | 'programme_booking_id' => $programme_booking_id, |
||
290 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
291 | 'act' => 'paymentCompleted', |
||
292 | 'edu-thankyou' => $reference_id |
||
293 | ), |
||
294 | $current_url |
||
295 | ); |
||
296 | |||
297 | $push_url = add_query_arg( |
||
298 | array( |
||
299 | 'klarna_order_id' => '{checkout.order.id}', |
||
300 | 'booking_id' => $booking_id, |
||
301 | 'programme_booking_id' => $programme_booking_id, |
||
302 | 'status' => 'push', |
||
303 | ), |
||
304 | $current_url |
||
305 | ); |
||
306 | |||
307 | $merchant['checkout_uri'] = $current_url; |
||
308 | $merchant['confirmation_uri'] = $confirmation_url; |
||
309 | $merchant['push_uri'] = $push_url; |
||
310 | |||
311 | $create['merchant'] = $merchant; |
||
312 | |||
313 | $create['merchant_reference'] = array(); |
||
314 | $create['merchant_reference']['orderid1'] = $reference_id; |
||
315 | $create['merchant_reference']['orderid2'] = $reference_id; |
||
316 | |||
317 | $create['cart'] = array(); |
||
318 | $create['cart']['items'] = array(); |
||
319 | |||
320 | foreach ( $ebi->EventBooking['OrderRows'] as $order_row ) { |
||
321 | $cart_item = array(); |
||
322 | |||
323 | $cart_item['reference'] = $order_row['ItemNumber']; |
||
324 | $cart_item['name'] = $order_row['Description'] . $rowExtraInfo; |
||
325 | $cart_item['quantity'] = intval( $order_row['Quantity'] ); |
||
326 | |||
327 | if ( ! $order_row['PriceIncVat'] ) { |
||
328 | $price_per_unit = $order_row['PricePerUnit'] * ( 1 + ( $order_row['VatPercent'] / 100 ) ) * 100; |
||
329 | } else { |
||
330 | $price_per_unit = $order_row['PricePerUnit'] * 100; |
||
331 | } |
||
332 | |||
333 | $cart_item['unit_price'] = $price_per_unit; |
||
334 | $cart_item['tax_rate'] = intval( $order_row['VatPercent'] * 100 ); |
||
335 | $cart_item['discount_rate'] = intval( $order_row['DiscountPercent'] * 100 ); |
||
336 | |||
337 | $create['cart']['items'][] = $cart_item; |
||
338 | } |
||
339 | |||
340 | try { |
||
341 | $connector = Klarna_Checkout_Connector::create( |
||
342 | $shared_secret, |
||
343 | $checkout_url |
||
344 | ); |
||
345 | |||
346 | $order = new Klarna_Checkout_Order( $connector ); |
||
347 | $order->create( $create ); |
||
348 | |||
349 | $order->fetch(); |
||
350 | |||
351 | $order_id = $order['id']; |
||
352 | EDU()->session['klarna-order-id'] = $order_id; |
||
353 | |||
354 | return $order; |
||
355 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
356 | EDU()->write_debug( $ex->getMessage() ); |
||
357 | EDU()->write_debug( $ex->getPayload() ); |
||
358 | |||
359 | return null; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | public function process_paymentstatus() { |
||
415 | } |
||
416 | } |
||
420 |