Conditions | 12 |
Paths | 3024 |
Total Lines | 147 |
Code Lines | 94 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
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'] : $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 | $emd_attachment = json_encode( array( |
||
195 | 'customer_account_info' => $emd_info |
||
196 | ) ); |
||
197 | |||
198 | $create['attachment'] = array(); |
||
199 | $create['attachment']['content_type'] = 'application/vnd.klarna.internal.emd-v2+json'; |
||
200 | $create['attachment']['body'] = $emd_attachment; |
||
201 | |||
202 | $create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) ); |
||
203 | $create['purchase_country'] = $purchase_country; |
||
204 | $create['purchase_currency'] = get_option( 'eduadmin-currency', 'SEK' ); |
||
205 | |||
206 | $merchant = array(); |
||
207 | $merchant['id'] = $this->get_option( 'eid', '' ); |
||
208 | $merchant['terms_uri'] = $this->get_option( 'termsurl', '' ); |
||
209 | |||
210 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
211 | |||
212 | $booking_id = 0; |
||
213 | $programme_booking_id = 0; |
||
214 | |||
215 | $reference_id = 0; |
||
216 | |||
217 | $_event = null; |
||
218 | |||
219 | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
||
220 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
221 | $reference_id = $booking_id; |
||
222 | |||
223 | $_event = EDUAPI()->OData->Events->GetItem( $ebi->EventBooking['EventId'] ); |
||
224 | } |
||
225 | |||
226 | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
||
227 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
228 | $reference_id = $programme_booking_id; |
||
229 | |||
230 | $_event = EDUAPI()->OData->ProgrammeStarts->GetItem( $ebi->EventBooking['ProgrammeStartId'] ); |
||
231 | } |
||
232 | |||
233 | $rowExtraInfo = ""; |
||
234 | |||
235 | if ( null != $_event ) { |
||
236 | if ( ! empty( $_event['City'] ) ) { |
||
237 | $rowExtraInfo .= ';' . $_event['City']; |
||
238 | } |
||
239 | |||
240 | if ( ! empty( $_event['StartDate'] ) ) { |
||
241 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['StartDate'] ) ); |
||
242 | } |
||
243 | |||
244 | if ( ! empty( $_event['EndDate'] ) ) { |
||
245 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['EndDate'] ) ); |
||
246 | } |
||
247 | } |
||
248 | |||
249 | $confirmation_url = add_query_arg( |
||
250 | array( |
||
251 | 'klarna_order_id' => '{checkout.order.id}', |
||
252 | 'booking_id' => $booking_id, |
||
253 | 'programme_booking_id' => $programme_booking_id, |
||
254 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
255 | 'act' => 'paymentCompleted', |
||
256 | 'edu-thankyou' => $reference_id |
||
257 | ), |
||
258 | $current_url |
||
259 | ); |
||
260 | |||
261 | $push_url = add_query_arg( |
||
262 | array( |
||
263 | 'klarna_order_id' => '{checkout.order.id}', |
||
264 | 'booking_id' => $booking_id, |
||
265 | 'programme_booking_id' => $programme_booking_id, |
||
266 | 'status' => 'push', |
||
267 | ), |
||
268 | $current_url |
||
269 | ); |
||
270 | |||
271 | $merchant['checkout_uri'] = $current_url; |
||
272 | $merchant['confirmation_uri'] = $confirmation_url; |
||
273 | $merchant['push_uri'] = $push_url; |
||
274 | |||
275 | $create['merchant'] = $merchant; |
||
276 | |||
277 | $create['merchant_reference'] = array(); |
||
278 | $create['merchant_reference']['orderid1'] = $reference_id; |
||
279 | $create['merchant_reference']['orderid2'] = $reference_id; |
||
280 | |||
281 | $create['cart'] = array(); |
||
282 | $create['cart']['items'] = array(); |
||
283 | |||
284 | foreach ( $ebi->EventBooking['OrderRows'] as $order_row ) { |
||
285 | $cart_item = array(); |
||
286 | |||
287 | $cart_item['reference'] = $order_row['ItemNumber']; |
||
288 | $cart_item['name'] = $order_row['Description'] . $rowExtraInfo; |
||
289 | $cart_item['quantity'] = intval( $order_row['Quantity'] ); |
||
290 | |||
291 | if ( ! $order_row['PriceIncVat'] ) { |
||
292 | $price_per_unit = $order_row['PricePerUnit'] * ( 1 + ( $order_row['VatPercent'] / 100 ) ) * 100; |
||
293 | } else { |
||
294 | $price_per_unit = $order_row['PricePerUnit'] * 100; |
||
295 | } |
||
296 | |||
297 | $cart_item['unit_price'] = $price_per_unit; |
||
298 | $cart_item['tax_rate'] = intval( $order_row['VatPercent'] * 100 ); |
||
299 | $cart_item['discount_rate'] = intval( $order_row['DiscountPercent'] * 100 ); |
||
300 | |||
301 | $create['cart']['items'][] = $cart_item; |
||
302 | } |
||
303 | |||
304 | try { |
||
305 | $connector = Klarna_Checkout_Connector::create( |
||
306 | $shared_secret, |
||
307 | $checkout_url |
||
308 | ); |
||
309 | |||
310 | $order = new Klarna_Checkout_Order( $connector ); |
||
311 | $order->create( $create ); |
||
312 | |||
313 | $order->fetch(); |
||
314 | |||
315 | $order_id = $order['id']; |
||
316 | EDU()->session['klarna-order-id'] = $order_id; |
||
317 | |||
318 | return $order; |
||
319 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
320 | EDU()->write_debug( $ex->getMessage() ); |
||
321 | EDU()->write_debug( $ex->getPayload() ); |
||
322 | |||
323 | return null; |
||
324 | } |
||
384 |