Skip to content

Commit

Permalink
Merge pull request #141 from krokedil/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MichaelBengtsson authored Jul 30, 2019
2 parents ca07649 + b8a807a commit ca04455
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 12 deletions.
6 changes: 4 additions & 2 deletions assets/js/klarna-payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,13 @@ jQuery( function($) {
},

getSelectorContainerID: function() {
return $('input[name="payment_method"]:checked').parent().find('.klarna_payments_container').attr('id');
var containerID = $('input[name="payment_method"]:checked').attr('id').replace('payment_method_', '');

return containerID + '_container';
},

getSelectedPaymentCategory: function() {
var selected_category = $('input[name="payment_method"]:checked').parent().find('.klarna_payments_container').data('payment_method_category');
var selected_category = $('input[name="payment_method"]:checked').attr('id').replace('payment_method_', '');
console.log( selected_category );
return selected_category.replace('klarna_payments_', '');
},
Expand Down
4 changes: 4 additions & 0 deletions includes/class-wc-gateway-klarna-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@ public function place_order() {
wp_send_json_error( $order->get_cancel_order_url_raw() );
wp_die();
break;
default:
wp_send_json_error( $order->get_cancel_order_url_raw() );
wp_die();
break;
}
}

Expand Down
93 changes: 85 additions & 8 deletions includes/class-wc-klarna-payments-order-lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ private function process_shipping() {
}

public function get_order_line_tax_rate( $order, $order_item = false ) {
if ( $this->separate_sales_tax ) {
return 0;
}
$tax_items = $order->get_items( 'tax' );
foreach ( $tax_items as $tax_item ) {
$rate_id = $tax_item->get_rate_id();
Expand Down Expand Up @@ -351,11 +354,11 @@ private function get_order_items( $order_id = false ) {
'reference' => $this->get_item_reference( $product ),
'name' => $order_item->get_name(),
'quantity' => $order_item->get_quantity(),
'unit_price' => round( ( ( $order_item->get_subtotal() + $order_item->get_subtotal_tax() ) / $order_item->get_quantity() ) * 100 ),
'unit_price' => $this->get_order_item_unit_price( $order_item ),
'tax_rate' => ( '0' !== $order_item->get_total_tax() ) ? $this->get_order_line_tax_rate( $order, $order_item ) : 0,
'total_amount' => round( ( $order_item->get_total() + $order_item->get_total_tax() ) * 100 ),
'total_tax_amount' => round( $order_item->get_total_tax() * 100 ),
'total_discount_amount' => round( ( $order_item->get_subtotal() + $order_item->get_subtotal_tax() - $order_item->get_total() - $order_item->get_total_tax() ) * 100 ),
'total_amount' => $this->get_order_item_total_amount( $order_item ),
'total_tax_amount' => $this->get_order_item_total_tax( $order_item ),
'total_discount_amount' => $this->get_order_item_discount_amount( $order_item ),
);

// Add images.
Expand Down Expand Up @@ -385,10 +388,10 @@ private function get_order_shipping( $order_id = false ) {
'reference' => 1,
'name' => ( '' !== $order->get_shipping_method() ) ? $order->get_shipping_method() : $shipping_name = __( 'Shipping', 'klarna-payments-for-woocommerce' ),
'quantity' => 1,
'unit_price' => round( ( $order->get_shipping_total() + $order->get_shipping_tax() ) * 100 ),
'unit_price' => $this->get_order_shipping_unit_price( $order ),
'tax_rate' => ( '0' !== $order->get_shipping_tax() ) ? $this->get_order_line_tax_rate( $order, current( $order->get_items( 'shipping' ) ) ) : 0,
'total_amount' => round( ( $order->get_shipping_total() + $order->get_shipping_tax() ) * 100 ),
'total_tax_amount' => $order->get_shipping_tax() * 100,
'total_amount' => $this->get_order_shipping_unit_price( $order ),
'total_tax_amount' => $this->get_order_shipping_tax_amount( $order ),
);

$this->order_lines[] = $shipping;
Expand All @@ -403,7 +406,7 @@ private function get_order_shipping( $order_id = false ) {
private function get_order_sales_tax( $order_id = false ) {
$order = wc_get_order( $order_id );
if ( $this->separate_sales_tax ) {
$sales_tax_amount = round( ( $order->get_total_tax() + $order->get_shipping_tax() ) * 100 );
$sales_tax_amount = round( ( $order->get_total_tax() ) * 100 );

// Add sales tax line item.
$sales_tax = array(
Expand Down Expand Up @@ -816,4 +819,78 @@ private function get_shipping_tax_amount() {
return round( $shipping_tax_amount );
}

/**
* Returns the order items unit price.
*
* @param object $order_item
* @return int
*/
private function get_order_item_unit_price( $order_item ) {
if ( $this->separate_sales_tax ) {
return round( ( ( $order_item->get_subtotal() ) / $order_item->get_quantity() ) * 100 );
}
return round( ( ( $order_item->get_subtotal() + $order_item->get_subtotal_tax() ) / $order_item->get_quantity() ) * 100 );
}

/**
* Returns the order item total amount.
*
* @param object $order_item
* @return int
*/
private function get_order_item_total_amount( $order_item ) {
if ( $this->separate_sales_tax ) {
return round( $order_item->get_total() * 100 );
}
return round( ( $order_item->get_total() + $order_item->get_total_tax() ) * 100 );
}

/**
* Returns the order item total tax amount.
*
* @param object $order_item
* @return int
*/
private function get_order_item_total_tax( $order_item ) {
if ( $this->separate_sales_tax ) {
return 0;
}
return round( $order_item->get_total_tax() * 100 );
}

private function get_order_item_discount_amount( $order_item ) {
if ( $order_item === null ) {
return 0;
}
if ( $this->separate_sales_tax ) {
return round( ( $order_item->get_subtotal() - $order_item->get_total() ) * 100 );
}
return round( ( $order_item->get_subtotal() + $order_item->get_subtotal_tax() - $order_item->get_total() - $order_item->get_total_tax() ) * 100 );
}

/**
* Get the order shipping unit price
*
* @param object $order
* @return int
*/
private function get_order_shipping_unit_price( $order ) {
if ( $this->separate_sales_tax ) {
return round( $order->get_shipping_total() * 100 );
}
return round( ( $order->get_shipping_total() + $order->get_shipping_tax() ) * 100 );
}

/**
* Get the order shipping tax amount
*
* @param object $order
* @return int
*/
private function get_order_shipping_tax_amount( $order ) {
if ( $this->separate_sales_tax ) {
return 0;
}
return $order->get_shipping_tax() * 100;
}
}
4 changes: 2 additions & 2 deletions klarna-payments-for-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Provides Klarna Payments as payment method to WooCommerce.
* Author: krokedil, klarna, automattic
* Author URI: https://krokedil.com/
* Version: 1.8.0
* Version: 1.8.1
* Text Domain: klarna-payments-for-woocommerce
* Domain Path: /languages
*
Expand Down Expand Up @@ -35,7 +35,7 @@
/**
* Required minimums and constants
*/
define( 'WC_KLARNA_PAYMENTS_VERSION', '1.8.0' );
define( 'WC_KLARNA_PAYMENTS_VERSION', '1.8.1' );
define( 'WC_KLARNA_PAYMENTS_MIN_PHP_VER', '5.6.0' );
define( 'WC_KLARNA_PAYMENTS_MIN_WC_VER', '3.3.0' );
define( 'WC_KLARNA_PAYMENTS_MAIN_FILE', __FILE__ );
Expand Down
5 changes: 5 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ For help setting up and configuring Klarna Payments for WooCommerce please refer
* A SSL Certificate is required.

== Changelog ==
= 2019.07.30 - version 1.8.1 =
* Enhancement - Improved JavaScript selectors for some elements. Should increase compatibility with custom themes.
* Enhancement - Added a failsafe for orders not properly being placed with Klarna.
* Fix - Fixed issue when it comes to separate sales tax for American merchants.

= 2019.07.23 - version 1.8.0 =
* Feature - Full rewrite of the order flow. Should now be more compatible with other plugin and themes.
* Feature - Improved debug logging.
Expand Down

0 comments on commit ca04455

Please sign in to comment.