Skip to content

Commit

Permalink
Merge pull request #125 from krokedil/develop
Browse files Browse the repository at this point in the history
1.7.4
  • Loading branch information
NiklasHogefjord authored Nov 12, 2018
2 parents 6532f24 + 62dcd62 commit a782bb1
Show file tree
Hide file tree
Showing 13 changed files with 450 additions and 278 deletions.
18 changes: 16 additions & 2 deletions includes/class-klarna-checkout-for-woocommerce-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,11 @@ public static function kco_wc_checkout_error() {
} else {
$klarna_order_id = KCO_WC()->api->get_order_id_from_session();
}

// Check if we have items in cart. If not return redirect URL
if ( WC()->cart->is_empty() && ! is_customize_preview() && apply_filters( 'woocommerce_checkout_redirect_empty_cart', true ) ) {
wp_send_json_success( array( 'redirect' => wc_get_page_permalink( 'cart' ) ) );
wp_die();
}
// Create order via fallback sequence
$order = Klarna_Checkout_For_WooCommerce_Create_Local_Order_Fallback::create( $klarna_order_id, $error_message );

Expand All @@ -296,6 +300,7 @@ public static function kco_wc_checkout_error() {
krokedil_log_events( null, 'Fallback order creation done. Redirecting customer to thank you page.', '' );
$note = sprintf( __( 'This order was made as a fallback due to an error in the checkout (%s). Please verify the order with Klarna.', 'klarna-checkout-for-woocommerce' ), $error_message );
$order->add_order_note( $note );
$order->update_status( 'on-hold' );
$redirect_url = $order->get_checkout_order_received_url();
} else {
KCO_WC()->logger->log( 'Fallback order creation ERROR. Redirecting customer to simplified thank you page.' . json_decode( $order ) );
Expand All @@ -318,7 +323,16 @@ public static function kco_wc_save_form_data() {
}
if ( ! empty( $_POST['form'] ) ) {
$form = $_POST['form'];
set_transient( WC()->session->get( 'kco_wc_order_id' ), $form, 60 * 60 * 24 );
if ( false === get_transient( 'kco_wc_order_id_' . WC()->session->get( 'kco_wc_order_id' ) ) ) {
set_transient( 'kco_wc_order_id_' . WC()->session->get( 'kco_wc_order_id' ), array( 'form' => $form ), 60 * 60 * 24 );
} else {
$old_transient = get_transient( 'kco_wc_order_id_' . WC()->session->get( 'kco_wc_order_id' ) );
$updated_transient = array( 'form' => $form );
if ( isset( $old_transient['cart_hash'] ) ) {
$updated_transient['cart_hash'] = $old_transient['cart_hash'];
}
set_transient( 'kco_wc_order_id_' . WC()->session->get( 'kco_wc_order_id' ), $updated_transient, 60 * 60 * 24 );
}
}
wp_send_json_success();
wp_die();
Expand Down
26 changes: 21 additions & 5 deletions includes/class-klarna-checkout-for-woocommerce-api-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,13 @@ public function validation_cb() {
$has_subscription = false;
$needs_login = false;
$email_exists = false;
$cart_hash_valid = true;

$form_data = get_transient( $data['order_id'] );
$kco_transient = get_transient( 'kco_wc_order_id_' . $data['order_id'] );
$form_data = false;
if ( isset( $kco_transient['form'] ) ) {
$form_data = $kco_transient['form'];
}
$has_required_data = true;
$failed_required_check = array();
if ( false !== $form_data ) {
Expand Down Expand Up @@ -240,7 +245,7 @@ public function validation_cb() {
}
}
// Validate any potential coupons.
if ( ! empty( $data['merchant_data'] ) ) {
if ( ! empty( json_decode( $data['merchant_data'] )->coupons ) ) {
$coupons = json_decode( $data['merchant_data'] )->coupons;
$emails[] = $data['billing_address']['email'];
foreach ( $coupons as $coupon ) {
Expand Down Expand Up @@ -282,10 +287,10 @@ public function validation_cb() {
}
}

if ( ! empty( $data['merchant_data'] ) ) {
if ( ! empty( json_decode( $data['merchant_data'] )->is_user_logged_in ) ) {
$is_user_logged_in = json_decode( $data['merchant_data'] )->is_user_logged_in;
}
// Check if any product is subscription product
// Check if any product is subscription product.
if ( class_exists( 'WC_Subscriptions_Cart' ) && $has_subscription ) {
$checkout = WC()->checkout();
if ( ! $checkout->is_registration_enabled() && ! $is_user_logged_in ) {
Expand All @@ -297,8 +302,17 @@ public function validation_cb() {
}
}

// Check cart hash.
if ( ! empty( json_decode( $data['merchant_data'] )->cart_hash ) && ! empty( $kco_transient['cart_hash'] ) ) {
$sent_cart_hash = json_decode( $data['merchant_data'] )->cart_hash;
$saved_cart_hash = $kco_transient['cart_hash'];
if ( $sent_cart_hash !== $saved_cart_hash ) {
$cart_hash_valid = false;
}
}

do_action( 'kco_validate_checkout', $data, $all_in_stock, $shipping_chosen );
if ( $all_in_stock && $shipping_chosen && $has_required_data && $coupon_valid && ! $needs_login && ! $email_exists ) {
if ( $all_in_stock && $shipping_chosen && $has_required_data && $coupon_valid && $cart_hash_valid && ! $needs_login && ! $email_exists ) {
header( 'HTTP/1.0 200 OK' );
} else {
header( 'HTTP/1.0 303 See Other' );
Expand All @@ -317,6 +331,8 @@ public function validation_cb() {
header( 'Location: ' . wc_get_checkout_url() . '?needs_login' );
} elseif ( $email_exists ) {
header( 'Location: ' . wc_get_checkout_url() . '?email_exists' );
} elseif ( ! $cart_hash_valid ) {
header( 'Location: ' . wc_get_checkout_url() . '?invalid_cart_hash' );
}
}
}
Expand Down
121 changes: 73 additions & 48 deletions includes/class-klarna-checkout-for-woocommerce-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ class Klarna_Checkout_For_WooCommerce_API {
*/
private $settings = array();

/**
* Merchant data JSON string.
*
* @var string
*/
public static $merchant_data = '';

/**
* Klarna_Checkout_For_WooCommerce_API constructor.
*/
Expand Down Expand Up @@ -54,7 +47,7 @@ public function request_pre_create_order() {

if ( is_wp_error( $response ) ) {
$error = $this->extract_error_messages( $response );
KCO_WC()->logger->log( 'Create Klarna order ERROR (' . $error . ') ' . stripslashes_deep( json_encode( $response ) ) );
KCO_WC()->logger->log( 'Create Klarna order ERROR (' . stripslashes_deep( json_encode( $error ) ) . ') ' . stripslashes_deep( json_encode( $response ) ) );
return $error;
}

Expand Down Expand Up @@ -96,7 +89,7 @@ public function request_pre_retrieve_order( $klarna_order_id ) {

$response = wp_safe_remote_get( $request_url, $request_args );

if ( $response['response']['code'] >= 200 && $response['response']['code'] <= 299 ) {
if ( ! is_wp_error( $response ) && ( $response['response']['code'] >= 200 && $response['response']['code'] <= 299 ) ) {
$klarna_order = json_decode( $response['body'] );
$log_order = clone $klarna_order;
$log_order->html_snippet = '';
Expand Down Expand Up @@ -136,7 +129,7 @@ public function request_pre_update_order() {

$response = wp_safe_remote_post( $request_url, $request_args );

if ( $response['response']['code'] >= 200 && $response['response']['code'] <= 299 ) {
if ( ! is_wp_error( $response ) && ( $response['response']['code'] >= 200 && $response['response']['code'] <= 299 ) ) {
WC()->session->set( 'kco_wc_update_md5', md5( serialize( $request_args ) ) );

$klarna_order = json_decode( $response['body'] );
Expand Down Expand Up @@ -190,8 +183,8 @@ public function request_pre_get_order( $klarna_order_id ) {
);
$response = wp_safe_remote_get( $request_url, $request_args );

$log_order = $response['body'];
$log_order = (array) json_decode( $log_order );
$log_order = $response['body'];
$log_order = (array) json_decode( $log_order );
$log_order['html_snippet'] = '';
krokedil_log_events( null, 'Pre Get Order response', stripslashes_deep( $log_order ) );
KCO_WC()->logger->log( 'Pre Get Order response (' . $request_url . ') ' . stripslashes_deep( json_encode( $log_order ) ) );
Expand Down Expand Up @@ -353,6 +346,9 @@ public function maybe_clear_session_values( $order ) {
WC()->session->__unset( 'kco_wc_order_api' );
WC()->session->__unset( 'kco_wc_extra_fields_values' );
WC()->session->__unset( 'kco_wc_prefill_consent' );
if ( $order ) {
delete_transient( 'kco_wc_order_id_' . $order->order_id );
}
}
}

Expand Down Expand Up @@ -487,6 +483,26 @@ public function get_merchant_urls() {
return KCO_WC()->merchant_urls->get_urls();
}

/**
* Gets merchant data for Klarna purchase.
*
* @return array
*/
public function get_merchant_data() {
$merchant_data = array();

// Coupon info.
foreach ( WC()->cart->get_applied_coupons() as $coupon ) {
$merchant_data['coupons'][] = $coupon;
}

// Cart hash.
$cart_hash = md5( wp_json_encode( wc_clean( WC()->cart->get_cart_for_session() ) ) . WC()->cart->total );
$merchant_data['cart_hash'] = $cart_hash;

return json_encode( $merchant_data );
}

/**
* Gets Klarna API request headers.
*
Expand Down Expand Up @@ -533,7 +549,7 @@ public function get_request_body( $request_type = null ) {
'order_tax_amount' => KCO_WC()->order_lines->get_order_tax_amount(),
'order_lines' => KCO_WC()->order_lines->get_order_lines(),
'shipping_countries' => $this->get_shipping_countries(),
'merchant_data' => self::$merchant_data,
'merchant_data' => $this->get_merchant_data(),
);

if ( kco_wc_prefill_allowed() ) {
Expand Down Expand Up @@ -568,34 +584,35 @@ public function get_request_body( $request_type = null ) {

// Allow external payment method plugin to do its thing.
// @TODO: Extract this into a hooked function.
if ( 'create' === $request_type ) {
if ( in_array( $this->get_purchase_country(), array( 'SE', 'NO', 'FI' ), true ) ) {
if ( isset( $this->settings['allowed_customer_types'] ) ) {
$customer_types_setting = $this->settings['allowed_customer_types'];

switch ( $customer_types_setting ) {
case 'B2B':
$allowed_customer_types = array( 'organization' );
$customer_type = 'organization';
break;
case 'B2BC':
$allowed_customer_types = array( 'person', 'organization' );
$customer_type = 'organization';
break;
case 'B2CB':
$allowed_customer_types = array( 'person', 'organization' );
$customer_type = 'person';
break;
default:
$allowed_customer_types = array( 'person' );
$customer_type = 'person';
}

$request_args['options']['allowed_customer_types'] = $allowed_customer_types;
$request_args['customer']['type'] = $customer_type;
if ( in_array( $this->get_purchase_country(), array( 'SE', 'NO', 'FI' ), true ) ) {
if ( isset( $this->settings['allowed_customer_types'] ) ) {
$customer_types_setting = $this->settings['allowed_customer_types'];

switch ( $customer_types_setting ) {
case 'B2B':
$allowed_customer_types = array( 'organization' );
$customer_type = 'organization';
break;
case 'B2BC':
$allowed_customer_types = array( 'person', 'organization' );
$customer_type = 'organization';
break;
case 'B2CB':
$allowed_customer_types = array( 'person', 'organization' );
$customer_type = 'person';
break;
default:
$allowed_customer_types = array( 'person' );
$customer_type = 'person';
}
}

$request_args['options']['allowed_customer_types'] = $allowed_customer_types;
if ( 'create' === $request_type ) {
$request_args['customer']['type'] = $customer_type;
}
}
}
if ( 'create' === $request_type ) {
$request_args = apply_filters( 'kco_wc_create_order', $request_args );
}

Expand Down Expand Up @@ -669,31 +686,31 @@ private function get_iframe_colors() {
$color_settings = array();

if ( $this->check_option_field( 'color_button' ) ) {
$color_settings['color_button'] = $this->check_option_field( 'color_button' );
$color_settings['color_button'] = self::add_hash_to_color( $this->check_option_field( 'color_button' ) );
}

if ( $this->check_option_field( 'color_button_text' ) ) {
$color_settings['color_button_text'] = $this->check_option_field( 'color_button_text' );
$color_settings['color_button_text'] = self::add_hash_to_color( $this->check_option_field( 'color_button_text' ) );
}

if ( $this->check_option_field( 'color_checkbox' ) ) {
$color_settings['color_checkbox'] = $this->check_option_field( 'color_checkbox' );
$color_settings['color_checkbox'] = self::add_hash_to_color( $this->check_option_field( 'color_checkbox' ) );
}

if ( $this->check_option_field( 'color_checkbox_checkmark' ) ) {
$color_settings['color_checkbox_checkmark'] = $this->check_option_field( 'color_checkbox_checkmark' );
$color_settings['color_checkbox_checkmark'] = self::add_hash_to_color( $this->check_option_field( 'color_checkbox_checkmark' ) );
}

if ( $this->check_option_field( 'color_header' ) ) {
$color_settings['color_header'] = $this->check_option_field( 'color_header' );
$color_settings['color_header'] = self::add_hash_to_color( $this->check_option_field( 'color_header' ) );
}

if ( $this->check_option_field( 'color_link' ) ) {
$color_settings['color_link'] = $this->check_option_field( 'color_link' );
$color_settings['color_link'] = self::add_hash_to_color( $this->check_option_field( 'color_link' ) );
}

if ( $this->check_option_field( 'radius_border' ) ) {
$color_settings['radius_border'] = $this->check_option_field( 'radius_border' );
$color_settings['radius_border'] = self::add_hash_to_color( $this->check_option_field( 'radius_border' ) );
}

if ( count( $color_settings ) > 0 ) {
Expand All @@ -703,6 +720,14 @@ private function get_iframe_colors() {
return false;
}

private static function add_hash_to_color( $hex ) {
if ( '' != $hex ) {
$hex = str_replace( '#', '', $hex );
$hex = '#' . $hex;
}
return $hex;
}

private function check_option_field( $field ) {
if ( array_key_exists( $field, $this->settings ) && '' !== $this->settings[ $field ] ) {
return $this->settings[ $field ];
Expand Down Expand Up @@ -751,10 +776,10 @@ public function request_create_recurring_order( $order, $recurring_token ) {
'user-agent' => $this->get_user_agent(),
'body' => $this->get_recurring_body( $order ),
);

KCO_WC()->logger->log( 'Create recurring order request (' . $request_url . ') ' . stripslashes_deep( json_encode( $request_args ) ) );
krokedil_log_events( $order->get_id(), 'Create recurring order request', $request_args );
$response = wp_safe_remote_post( $request_url, $request_args );
$response = wp_safe_remote_post( $request_url, $request_args );

KCO_WC()->logger->log( 'Create recurring order response' . stripslashes_deep( json_encode( $response ) ) );
krokedil_log_events( $order->get_id(), 'Create recurring order response', $response );
Expand Down
Loading

0 comments on commit a782bb1

Please sign in to comment.