Skip to content

Commit

Permalink
Merge pull request #145 from krokedil/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
MichaelBengtsson authored Aug 10, 2019
2 parents d4107d3 + cf64d74 commit ee72f7e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 62 deletions.
45 changes: 23 additions & 22 deletions assets/js/klarna-payments.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ jQuery( function($) {
show_form: false,
klarna_container_selector: '#klarna_container_2',
checkout_values: {},
addresses: {},

check_changes: function() {
$('.woocommerce-billing-fields input, .woocommerce-billing-fields select, .woocommerce-shipping-fields input, .woocommerce-shipping-fields select').each(function() {
Expand Down Expand Up @@ -103,7 +104,7 @@ jQuery( function($) {
*/
$('form.checkout').on('keyup', '#billing_company', klarna_payments.debounce_changes(function() {
if (klarna_payments.isKlarnaPaymentsSelected()) {
$('#place_order').attr('disabled', true);
//$('#place_order').attr('disabled', true);
klarna_payments.load().then(klarna_payments.loadHandler);
}
}, 750));
Expand Down Expand Up @@ -232,7 +233,7 @@ jQuery( function($) {
try {
Klarna.Payments.authorize(
address,
{payment_method_category: klarna_payments.getSelectedPaymentCategory(), auto_finalize: false},
{payment_method_category: klarna_payments.getSelectedPaymentCategory()},
function (response) {
klarna_payments.authorization_response = response;
$defer.resolve(response);
Expand All @@ -248,32 +249,32 @@ jQuery( function($) {
get_address: function() {
var address = {
billing_address: {
given_name : $(klarna_payments_params.default_checkout_fields.billing_given_name).val(),
family_name : $(klarna_payments_params.default_checkout_fields.billing_family_name).val(),
email : $(klarna_payments_params.default_checkout_fields.billing_email).val(),
phone : $(klarna_payments_params.default_checkout_fields.billing_phone).val(),
country : $(klarna_payments_params.default_checkout_fields.billing_country).val(),
region : $(klarna_payments_params.default_checkout_fields.billing_region).val(),
postal_code : ( klarna_payments_params.remove_postcode_spaces === 'yes' ) ? $(klarna_payments_params.default_checkout_fields.billing_postal_code).val().replace(/\s/g, '') : $(klarna_payments_params.default_checkout_fields.billing_postal_code).val(),
city : $(klarna_payments_params.default_checkout_fields.billing_city).val(),
street_address : $(klarna_payments_params.default_checkout_fields.billing_street_address).val(),
street_address2 : $(klarna_payments_params.default_checkout_fields.billing_street_address2).val(),
organization_name : ( 'b2b' === klarna_payments_params.customer_type ) ? $(klarna_payments_params.default_checkout_fields.billing_company).val() : '',
given_name : klarna_payments.addresses.billing.given_name,
family_name : klarna_payments.addresses.billing.family_name,
email : klarna_payments.addresses.billing.email,
phone : klarna_payments.addresses.billing.phone,
country : klarna_payments.addresses.billing.country,
region : klarna_payments.addresses.billing.region,
postal_code : klarna_payments.addresses.billing.postal_code,
city : klarna_payments.addresses.billing.city,
street_address : klarna_payments.addresses.billing.street_address,
street_address2 : klarna_payments.addresses.billing.street_address2,
organization_name : ( 'b2b' === klarna_payments_params.customer_type ) ? klarna_payments.addresses.billing.organization_name : '',
},
shipping_address: {}
};

address.shipping_address = $.extend({}, address.billing_address);

if ( $( '#ship-to-different-address' ).find( 'input' ).is( ':checked' ) ) {
address.shipping_address.given_name = $(klarna_payments_params.default_checkout_fields.shipping_given_name).val();
address.shipping_address.family_name = $(klarna_payments_params.default_checkout_fields.shipping_family_name).val();
address.shipping_address.country = $(klarna_payments_params.default_checkout_fields.shipping_country).val();
address.shipping_address.region = $(klarna_payments_params.default_checkout_fields.shipping_region).val();
address.shipping_address.postal_code = ( klarna_payments_params.remove_postcode_spaces === 'yes' ) ? $(klarna_payments_params.default_checkout_fields.shipping_postal_code).val().replace(/\s/g, '') : $(klarna_payments_params.default_checkout_fields.shipping_postal_code).val();
address.shipping_address.city = $(klarna_payments_params.default_checkout_fields.shipping_city).val();
address.shipping_address.street_address = $(klarna_payments_params.default_checkout_fields.shipping_street_address).val();
address.shipping_address.street_address2 = $(klarna_payments_params.default_checkout_fields.shipping_street_address2).val();
address.shipping_address.given_name = klarna_payments.addresses.shipping.given_name;
address.shipping_address.family_name = klarna_payments.addresses.shipping.family_name;
address.shipping_address.country = klarna_payments.addresses.shipping.country;
address.shipping_address.region = klarna_payments.addresses.shipping.region;
address.shipping_address.postal_code = klarna_payments.addresses.shipping.postal_code;
address.shipping_address.city = klarna_payments.addresses.shipping.city;
address.shipping_address.street_address = klarna_payments.addresses.shipping.street_address;
address.shipping_address.street_address2 = klarna_payments.addresses.shipping.street_address2;
}

return address;
Expand Down Expand Up @@ -305,7 +306,7 @@ jQuery( function($) {

if( splittedHash[0] === "#kp" ){
var json = JSON.parse( atob( splittedHash[1] ) );

klarna_payments.addresses = json.addresses
klarna_payments.authorize().done( function( response ) {
if ('authorization_token' in response) {
$.ajax(
Expand Down
84 changes: 47 additions & 37 deletions includes/class-wc-gateway-klarna-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,9 @@ public function check_authorization_token( $posted ) {
public function process_payment( $order_id ) {

$response = array(
'order_id' => $order_id,
'time' => time(),
'order_id' => $order_id,
'addresses' => $this->get_address_from_order( $order_id ),
'time' => time(),
);

update_post_meta( $order_id, '_wc_klarna_environment', $this->environment );
Expand All @@ -855,7 +856,6 @@ public function process_payment( $order_id ) {
'result' => 'success',
'redirect' => wc_get_checkout_url() . '#kp=' . base64_encode( wp_json_encode( $response ) ),
);
// return $this->process_klarna_response( $response, $order );
}

/**
Expand Down Expand Up @@ -990,37 +990,7 @@ public function place_order() {
$order_lines_processor = new WC_Klarna_Payments_Order_Lines( $this->shop_country );
$order_lines = $order_lines_processor->order_lines( $order_id );

$billing_address = array(
'given_name' => stripslashes( $order->get_billing_first_name() ),
'family_name' => stripslashes( $order->get_billing_last_name() ),
'email' => stripslashes( $order->get_billing_email() ),
'phone' => stripslashes( $order->get_billing_phone() ),
'street_address' => stripslashes( $order->get_billing_address_1() ),
'street_address2' => stripslashes( $order->get_billing_address_2() ),
'postal_code' => stripslashes( ( apply_filters( 'wc_kp_remove_postcode_spaces', false ) ) ? str_replace( ' ', '', $order->get_billing_postcode() ) : $order->get_billing_postcode() ),
'city' => stripslashes( $order->get_billing_city() ),
'region' => stripslashes( $order->get_billing_state() ),
'country' => stripslashes( $order->get_billing_country() ),
);
if ( 'b2b' === $this->get_option( 'customer_type' ) ) {
$billing_address['organization_name'] = stripslashes( $order->get_billing_company() );
}
if ( '' !== $order->get_shipping_first_name() && 'b2c' === $this->get_option( 'customer_type' ) && ! wc_ship_to_billing_address_only() ) {
$shipping_address = array(
'given_name' => stripslashes( $order->get_shipping_first_name() ),
'family_name' => stripslashes( $order->get_shipping_last_name() ),
'email' => stripslashes( $order->get_billing_email() ),
'phone' => stripslashes( $order->get_billing_phone() ),
'street_address' => stripslashes( $order->get_shipping_address_1() ),
'street_address2' => stripslashes( $order->get_shipping_address_2() ),
'postal_code' => stripslashes( ( apply_filters( 'wc_kp_remove_postcode_spaces', false ) ) ? str_replace( ' ', '', $order->get_shipping_postcode() ) : $order->get_shipping_postcode() ),
'city' => stripslashes( $order->get_shipping_city() ),
'region' => stripslashes( $order->get_shipping_state() ),
'country' => stripslashes( $order->get_shipping_country() ),
);
} else {
$shipping_address = $billing_address;
}
$addresses = $this->get_address_from_order( $order_id );

$request_url = $this->server_base . 'payments/v1/authorizations/' . $auth_token . '/order';
$request_args = array(
Expand All @@ -1034,10 +1004,10 @@ public function place_order() {
'kp_wc_api_request_args',
array(
'purchase_country' => $this->klarna_country,
'purchase_currency' => get_woocommerce_currency(),
'purchase_currency' => $order->get_currency(),
'locale' => $this->get_locale_for_klarna_country(),
'billing_address' => $billing_address,
'shipping_address' => $shipping_address,
'billing_address' => $addresses['billing'],
'shipping_address' => $addresses['shipping'],
'order_amount' => $order_lines['order_amount'],
'order_tax_amount' => $order_lines['order_tax_amount'],
'order_lines' => $order_lines['order_lines'],
Expand Down Expand Up @@ -1388,4 +1358,44 @@ public function set_payment_method_title( $order, $klarna_place_order_response )
$order->set_payment_method_title( $new_title );
}
}

public function get_address_from_order( $order_id ) {
$order = wc_get_order( $order_id );
$billing_address = array(
'given_name' => stripslashes( $order->get_billing_first_name() ),
'family_name' => stripslashes( $order->get_billing_last_name() ),
'email' => stripslashes( $order->get_billing_email() ),
'phone' => stripslashes( $order->get_billing_phone() ),
'street_address' => stripslashes( $order->get_billing_address_1() ),
'street_address2' => stripslashes( $order->get_billing_address_2() ),
'postal_code' => stripslashes( ( apply_filters( 'wc_kp_remove_postcode_spaces', false ) ) ? str_replace( ' ', '', $order->get_billing_postcode() ) : $order->get_billing_postcode() ),
'city' => stripslashes( $order->get_billing_city() ),
'region' => stripslashes( $order->get_billing_state() ),
'country' => stripslashes( $order->get_billing_country() ),
);
if ( 'b2b' === $this->get_option( 'customer_type' ) ) {
$billing_address['organization_name'] = stripslashes( $order->get_billing_company() );
}
if ( '' !== $order->get_shipping_first_name() && 'b2c' === $this->get_option( 'customer_type' ) && ! wc_ship_to_billing_address_only() ) {
$shipping_address = array(
'given_name' => stripslashes( $order->get_shipping_first_name() ),
'family_name' => stripslashes( $order->get_shipping_last_name() ),
'email' => stripslashes( $order->get_billing_email() ),
'phone' => stripslashes( $order->get_billing_phone() ),
'street_address' => stripslashes( $order->get_shipping_address_1() ),
'street_address2' => stripslashes( $order->get_shipping_address_2() ),
'postal_code' => stripslashes( ( apply_filters( 'wc_kp_remove_postcode_spaces', false ) ) ? str_replace( ' ', '', $order->get_shipping_postcode() ) : $order->get_shipping_postcode() ),
'city' => stripslashes( $order->get_shipping_city() ),
'region' => stripslashes( $order->get_shipping_state() ),
'country' => stripslashes( $order->get_shipping_country() ),
);
} else {
$shipping_address = $billing_address;
}

return array(
'billing' => $billing_address,
'shipping' => $shipping_address,
);
}
}
6 changes: 3 additions & 3 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.2
* Version: 1.8.3
* 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.2' );
define( 'WC_KLARNA_PAYMENTS_VERSION', '1.8.3' );
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 Expand Up @@ -362,7 +362,7 @@ public function auth_failed() {

$order = wc_get_order( $order_id );

$order->add_order_note( __( 'Klarna Payments Authorization failed.', 'klarna-payments-for-woocommerce' ) );
$order->add_order_note( __( 'Payment rejected by klarna.', 'klarna-payments-for-woocommerce' ) );

wp_send_json_success();
wp_die();
Expand Down
7 changes: 7 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ For help setting up and configuring Klarna Payments for WooCommerce please refer
* A SSL Certificate is required.

== Changelog ==
= 2019.08.10 - version 1.8.3 =
* Enhancement - We now use order data for authorization calls. This prevents issues with difference in formating of adress details between create order and authorization.
* Enhancement - Changed the text added to the order note to "Payment rejected by Klarna" on a failed authorization calls.
* Fix - Fixed issue with Sofort, removed a flag that was not needed to be sent with the authorization call.
* Fix - Fixed an issue where billing_company field could softblock the checkout.
* Fix - Get currency from the order instead of the WooCommerce default.

= 2019.07.31 - version 1.8.2 =
* Fix - Added handling for failed authorization calls.

Expand Down

0 comments on commit ee72f7e

Please sign in to comment.