Skip to content

Commit

Permalink
Merge pull request #580 from krokedil/develop-2.12.3-fix
Browse files Browse the repository at this point in the history
2.12.3 Bugfixes
  • Loading branch information
NiklasHogefjord authored Jun 5, 2024
2 parents ec2dcad + d7a800c commit 4c30d4a
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
9 changes: 9 additions & 0 deletions classes/class-kco-api-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ public function push_cb() {
KCO_WC()->api->get_klarna_om_order( $klarna_order_id )
);

if( is_wp_error( $klarna_order ) ) {
KCO_WC()->logger->log( 'ERROR Push callback failed to get Klarna order data for Klarna order ID ' . stripslashes_deep( wp_json_encode( $klarna_order_id ) ) );
return;
}

if ( ! kco_validate_order_total( $klarna_order, $order ) ) {
return;
}

// The Woo order was already created. Check if order status was set (in process_payment_handler).
if ( empty( $order->get_date_paid() ) ) {
if ( 'ACCEPTED' === $klarna_order['fraud_status'] ) {
Expand Down
3 changes: 2 additions & 1 deletion classes/class-kco-confirmation.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class KCO_Confirmation {
* @var $instance
*/
protected static $instance;

/**
* Returns the *Singleton* instance of this class.
*
Expand Down Expand Up @@ -53,7 +54,7 @@ public function confirm_order() {
$order_id = filter_input( INPUT_GET, 'order_id', FILTER_SANITIZE_NUMBER_INT );
$order_key = filter_input( INPUT_GET, 'key', FILTER_SANITIZE_FULL_SPECIAL_CHARS );

// Return if we dont have our parameters set.
// Return if we don't have our parameters set.
if ( empty( $kco_confirm ) || empty( $klarna_order_id ) || empty( $order_key ) ) {
return;
}
Expand Down
53 changes: 51 additions & 2 deletions includes/kco-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ function kco_wc_prefill_consent() {
} else {
$button_text = 'Meine Adressdaten vorausfüllen';
$link_text = 'Es gelten die Nutzungsbedingungen zur Datenübertragung';
$popup_text = 'We use Klarna Checkout as our checkout, which offers a simplified purchase experience. When you choose to go to the checkout, your email address, first name, last name, date of birth, address and phone number may be automatically transferred to Klarna AB, enabling the provision of Klarna Checkout. These User Terms apply for the use of Klarna Checkout is available here:
$popup_text = 'We use Klarna Checkout as our checkout, which offers a simplified purchase experience. When you choose to go to the checkout, your email address, first name, last name, date of birth, address and phone number may be automatically transferred to Klarna AB, enabling the provision of Klarna Checkout. These User Terms apply for the use of Klarna Checkout is available here:
<a target="_blank" href="https://cdn.klarna.com/1.0/shared/content/legal/terms/' . $merchant_id . '/en_us/checkout">https://cdn.klarna.com/1.0/shared/content/legal/terms/' . $merchant_id . '/en_us/checkout</a>';
}
?>
Expand Down Expand Up @@ -589,6 +589,10 @@ function kco_confirm_klarna_order( $order_id = null, $klarna_order_id = null ) {
$klarna_order = KCO_WC()->api->get_klarna_om_order( $klarna_order_id );

if ( ! is_wp_error( $klarna_order ) ) {
if ( ! kco_validate_order_total( $klarna_order, $order ) ) {
return;
}

kco_maybe_save_surcharge( $order_id, $klarna_order );
kco_maybe_save_org_nr( $order_id, $klarna_order );
kco_maybe_save_reference( $order_id, $klarna_order );
Expand Down Expand Up @@ -632,6 +636,50 @@ function kco_confirm_klarna_order( $order_id = null, $klarna_order_id = null ) {
}
}

/**
* Validate the Klarna Checkout order total against the WooCommerce order.
*
* @param array $klarna_order The Klarna order.
* @param WC_Order $order The WooCommerce order.
*
* @return bool
*/
function kco_validate_order_total( $klarna_order, $order ) {
// Get the Klarna order total.
$klarna_order_total = $klarna_order['order_amount'];

// Get the WooCommerce order total.
$order_total = $order->get_total();

// Convert the WC Order total to be in minor units with zero decimal places.
$order_total = wc_format_decimal( $order_total * 100, array( 'decimals' => 0 ) );

// Get the difference between the two.
$diff = abs( $klarna_order_total - $order_total );

// If the difference is greater than 1, then log the error and return false.
if ( $diff > 1 ) {
KCO_Logger::log( 'Order total mismatch. Klarna Order total: ' . $klarna_order_total . ' WC Order total: ' . $order_total . ' Klarna order ID: ' . $klarna_order['order_id'] . ' WC Order ID: ' . $order->get_id() );

$klarna_order_total = wc_format_decimal( $klarna_order_total / 100, array( 'decimals' => 2 ) );
$order_total = wc_format_decimal( $order_total / 100, array( 'decimals' => 2 ) );

// translators: 1: Klarna order total, 2: WooCommerce order total.
$order->set_status(
'on-hold',
sprintf(
__( 'Klarna order total (%1$s) does not match WooCommerce order total (%2$s). Please verify the order with Klarna before processing.', 'klarna-checkout-for-woocommerce' ),
$klarna_order_total,
$order_total
)
);
$order->save();
return false;
}

return true;
}

/**
* Converts a region string to the expected country code format for WooCommerce.
*
Expand Down Expand Up @@ -770,7 +818,8 @@ function kco_update_wc_shipping( $data, $klarna_order = false ) {

/**
* Returns the WooCommerce order that has a matching Klarna order id saved as a meta field. If no order is found, returns false, and if many orders are found the newest one is returned.
* @param string $klarna_order_id
*
* @param string $klarna_order_id
* @param string|null $date_after
* @return WC_Order|false
*/
Expand Down
4 changes: 2 additions & 2 deletions klarna-checkout-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Klarna Checkout payment gateway for WooCommerce.
* Author: Klarna
* Author URI: https://klarna.com/
* Version: 2.12.2
* Version: 2.12.3
* Text Domain: klarna-checkout-for-woocommerce
* Domain Path: /languages
*
Expand Down Expand Up @@ -35,7 +35,7 @@
/**
* Required minimums and constants
*/
define( 'KCO_WC_VERSION', '2.12.2' );
define( 'KCO_WC_VERSION', '2.12.3' );
define( 'KCO_WC_MIN_PHP_VER', '5.6.0' );
define( 'KCO_WC_MIN_WC_VER', '3.9.0' );
define( 'KCO_WC_MAIN_FILE', __FILE__ );
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ Klarna Checkout works for merchants in Sweden, Finland, Norway, Germany, Austria
For help setting up and configuring Klarna Checkout for WooCommerce please refer to our [documentation](https://docs.krokedil.com/klarna-checkout-for-woocommerce/).

== Changelog ==
= 2024.06.05 - version 2.12.3 =
* Fix - Bugfixes.

= 2024.05.15 - version 2.12.2 =
* Tweak - If the store serves exactly one country, and the default location is set to "No location by default", that country will be used to determine payment gateway availability. This should prevent issues with the billing country not being set.
* Tweak - Added the billing_address_change event which should ensure that the billing address is consistent between Woo, and what is seen in the Klarna form.
Expand Down Expand Up @@ -86,7 +89,7 @@ For help setting up and configuring Klarna Checkout for WooCommerce please refer
= 2023.07.18 - version 2.11.3 =
* Fix - Fixed an issue where the recurring token was no longer being displayed in the billing fields.
* Fix - Fixed an undefined index warning that ocurred when the shipping was shown outside of the iframe.
* Fix - When processing a payment, and the order is not found, we'll now return the response in the expected format.
* Fix - When processing a payment, and the order is not found, we'll now return the response in the expected format.

= 2023.06.28 - version 2.11.2 =
* Fix - Fixed an issue with how we made our meta queries when trying to find orders based on a Klarna order ID.
Expand Down

0 comments on commit 4c30d4a

Please sign in to comment.