Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate order content #590

Merged
merged 8 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions classes/class-kco-api-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ public function push_cb() {
KCO_WC()->api->get_klarna_om_order( $klarna_order_id )
);

if( is_wp_error( $klarna_order ) ) {
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 ) ) {
if ( ! kco_validate_order_total( $klarna_order, $order ) || ! kco_validate_order_content( $klarna_order, $order ) ) {
return;
}

Expand Down
75 changes: 74 additions & 1 deletion includes/kco-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ 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 ) ) {
if ( ! kco_validate_order_total( $klarna_order, $order ) || ! kco_validate_order_content( $klarna_order, $order ) ) {
return;
}

Expand Down Expand Up @@ -680,6 +680,79 @@ function kco_validate_order_total( $klarna_order, $order ) {
return true;
}

/**
* Validate that the Woo order matches the corresponding Klarna order.
*
* @param array $klarna_order The Klarna order.
* @param WC_Order $order The Woo order.
*
* @return bool
*/
function kco_validate_order_content( $klarna_order, $order ) {
$order_data = new KCO_Request_Order();

$mismatch = false;
foreach ( $order->get_items() as $order_item ) {
$order_line = $order_data->get_order_line_items( $order_item );
if ( $mismatch ) {
break;
}

$match = false;
$reference = $order_line['reference'];
foreach ( $klarna_order['order_lines'] as $klarna_order_item ) {
$match = false;
if ( $reference === $klarna_order_item['reference'] ) {
$match = true;
if ( $klarna_order_item['quantity'] !== $order_line['quantity'] ) {
$mismatch = true;
}

break;
}
}

// Check if the Woo item was not found in the Klarna order.
if ( ! $match ) {
$mismatch = true;
}
}

$shipping = ! empty( $order->get_shipping_method() ) ? $order_data->get_order_line_shipping( $order ) : false;
$klarna_shipping = array_filter(
$klarna_order['order_lines'],
function ( $order_line ) {
return 'shipping_fee' === $order_line['type'];
}
);
$klarna_shipping = reset( $klarna_shipping );

if ( empty( $klarna_shipping ) !== empty( $shipping ) ) {
$mismatch = true;
} elseif ( ! empty( $klarna_shipping ) && ! empty( $shipping ) ) {
// If KSA is enabled, we'll skip the control.
$is_ksa = strpos( $shipping['reference'], 'klarna_kss' ) !== false;
if ( ! $is_ksa && $shipping['reference'] !== $klarna_shipping['reference'] ) {
$mismatch = true;
}
MichaelBengtsson marked this conversation as resolved.
Show resolved Hide resolved
}

if ( $mismatch ) {
KCO_Logger::log( 'The Klarna and Woo orders do not match. Klarna order ID: ' . $klarna_order['order_id'] . ' WC Order ID: ' . $order->get_id() );

$order->set_status(
'on-hold',
sprintf(
__( 'A mismatch between the WooCommerce and Klarna orders was identified. Please verify the order in the Klarna merchant portal before processing.', 'klarna-checkout-for-woocommerce' )
)
);
$order->save();
return false;
}

return true;
}

/**
* Converts a region string to the expected country code format for WooCommerce.
*
Expand Down
Loading