| Conditions | 13 |
| Paths | 4536 |
| Total Lines | 140 |
| Code Lines | 88 |
| 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 | if ( ! empty( $ebi->Customer["CountryCode"] ) ) { |
||
| 188 | $purchase_country = $ebi->Customer["CountryCode"]; |
||
| 189 | |||
| 190 | if ( ! empty( $ebi->Customer["BillingInfo"]["CountryCode"] ) ) { |
||
| 191 | $purchase_country = $ebi->Customer["BillingInfo"]["CountryCode"]; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | $create['locale'] = strtolower( str_replace( '_', '-', get_locale() ) ); |
||
| 196 | $create['purchase_country'] = $purchase_country; |
||
| 197 | $create['purchase_currency'] = get_option( 'eduadmin-currency', 'SEK' ); |
||
| 198 | |||
| 199 | $merchant = array(); |
||
| 200 | $merchant['id'] = $this->get_option( 'eid', '' ); |
||
| 201 | $merchant['terms_uri'] = $this->get_option( 'termsurl', '' ); |
||
| 202 | |||
| 203 | $current_url = esc_url( "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}" ); |
||
| 204 | |||
| 205 | $booking_id = 0; |
||
| 206 | $programme_booking_id = 0; |
||
| 207 | |||
| 208 | $reference_id = 0; |
||
| 209 | |||
| 210 | $_event = null; |
||
| 211 | |||
| 212 | if ( ! empty( $ebi->EventBooking['BookingId'] ) ) { |
||
| 213 | $booking_id = intval( $ebi->EventBooking['BookingId'] ); |
||
| 214 | $reference_id = $booking_id; |
||
| 215 | |||
| 216 | $_event = EDUAPI()->OData->Events->GetItem( $ebi->EventBooking['EventId'] ); |
||
| 217 | } |
||
| 218 | |||
| 219 | if ( ! empty( $ebi->EventBooking['ProgrammeBookingId'] ) ) { |
||
| 220 | $programme_booking_id = intval( $ebi->EventBooking['ProgrammeBookingId'] ); |
||
| 221 | $reference_id = $programme_booking_id; |
||
| 222 | |||
| 223 | $_event = EDUAPI()->OData->ProgrammeStarts->GetItem( $ebi->EventBooking['ProgrammeStartId'] ); |
||
| 224 | } |
||
| 225 | |||
| 226 | $rowExtraInfo = ""; |
||
| 227 | |||
| 228 | if ( null != $_event ) { |
||
| 229 | if ( ! empty( $_event['City'] ) ) { |
||
| 230 | $rowExtraInfo .= ';' . $_event['City']; |
||
| 231 | } |
||
| 232 | |||
| 233 | if ( ! empty( $_event['StartDate'] ) ) { |
||
| 234 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['StartDate'] ) ); |
||
| 235 | } |
||
| 236 | |||
| 237 | if ( ! empty( $_event['EndDate'] ) ) { |
||
| 238 | $rowExtraInfo .= ';' . date( "Y-m-d", strtotime( $_event['EndDate'] ) ); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | $confirmation_url = add_query_arg( |
||
| 243 | array( |
||
| 244 | 'klarna_order_id' => '{checkout.order.id}', |
||
| 245 | 'booking_id' => $booking_id, |
||
| 246 | 'programme_booking_id' => $programme_booking_id, |
||
| 247 | 'edu-valid-form' => wp_create_nonce( 'edu-booking-confirm' ), |
||
| 248 | 'act' => 'paymentCompleted', |
||
| 249 | 'edu-thankyou' => $reference_id |
||
| 250 | ), |
||
| 251 | $current_url |
||
| 252 | ); |
||
| 253 | |||
| 254 | $push_url = add_query_arg( |
||
| 255 | array( |
||
| 256 | 'klarna_order_id' => '{checkout.order.id}', |
||
| 257 | 'booking_id' => $booking_id, |
||
| 258 | 'programme_booking_id' => $programme_booking_id, |
||
| 259 | 'status' => 'push', |
||
| 260 | ), |
||
| 261 | $current_url |
||
| 262 | ); |
||
| 263 | |||
| 264 | $merchant['checkout_uri'] = $current_url; |
||
| 265 | $merchant['confirmation_uri'] = $confirmation_url; |
||
| 266 | $merchant['push_uri'] = $push_url; |
||
| 267 | |||
| 268 | $create['merchant'] = $merchant; |
||
| 269 | |||
| 270 | $create['merchant_reference'] = array(); |
||
| 271 | $create['merchant_reference']['orderid1'] = $reference_id; |
||
| 272 | $create['merchant_reference']['orderid2'] = $reference_id; |
||
| 273 | |||
| 274 | $create['cart'] = array(); |
||
| 275 | $create['cart']['items'] = array(); |
||
| 276 | |||
| 277 | foreach ( $ebi->EventBooking['OrderRows'] as $order_row ) { |
||
| 278 | $cart_item = array(); |
||
| 279 | |||
| 280 | $cart_item['reference'] = $order_row['ItemNumber']; |
||
| 281 | $cart_item['name'] = $order_row['Description'] . $rowExtraInfo; |
||
| 282 | $cart_item['quantity'] = intval( $order_row['Quantity'] ); |
||
| 283 | |||
| 284 | if ( ! $order_row['PriceIncVat'] ) { |
||
| 285 | $price_per_unit = $order_row['PricePerUnit'] * ( 1 + ( $order_row['VatPercent'] / 100 ) ) * 100; |
||
| 286 | } else { |
||
| 287 | $price_per_unit = $order_row['PricePerUnit'] * 100; |
||
| 288 | } |
||
| 289 | |||
| 290 | $cart_item['unit_price'] = $price_per_unit; |
||
| 291 | $cart_item['tax_rate'] = intval( $order_row['VatPercent'] * 100 ); |
||
| 292 | $cart_item['discount_rate'] = intval( $order_row['DiscountPercent'] * 100 ); |
||
| 293 | |||
| 294 | $create['cart']['items'][] = $cart_item; |
||
| 295 | } |
||
| 296 | |||
| 297 | try { |
||
| 298 | $connector = Klarna_Checkout_Connector::create( |
||
| 299 | $shared_secret, |
||
| 300 | $checkout_url |
||
| 301 | ); |
||
| 302 | |||
| 303 | $order = new Klarna_Checkout_Order( $connector ); |
||
| 304 | $order->create( $create ); |
||
| 305 | |||
| 306 | $order->fetch(); |
||
| 307 | |||
| 308 | $order_id = $order['id']; |
||
| 309 | EDU()->session['klarna-order-id'] = $order_id; |
||
| 310 | |||
| 311 | return $order; |
||
| 312 | } catch ( Klarna_Checkout_ApiErrorException $ex ) { |
||
| 313 | EDU()->write_debug( $ex->getMessage() ); |
||
| 314 | EDU()->write_debug( $ex->getPayload() ); |
||
| 315 | |||
| 316 | return null; |
||
| 317 | } |
||
| 377 |