Skip to content

Commit

Permalink
Merge pull request #349 from krokedil/develop
Browse files Browse the repository at this point in the history
Version 3.2.0
  • Loading branch information
mntzrr committed Aug 31, 2023
2 parents a8510c3 + b9f373e commit 1ff93e6
Show file tree
Hide file tree
Showing 21 changed files with 890 additions and 85 deletions.
40 changes: 21 additions & 19 deletions classes/admin/class-kp-form-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ class KP_Form_Fields {
'currency' => 'DKK',
'endpoint' => '',
),
'de' => array(
'name' => 'Germany',
'currency' => 'EUR',
'endpoint' => '',
),

'fi' => array(
'name' => 'Finland',
'currency' => 'EUR',
Expand All @@ -67,6 +63,11 @@ class KP_Form_Fields {
'currency' => 'EUR',
'endpoint' => '',
),
'de' => array(
'name' => 'Germany',
'currency' => 'EUR',
'endpoint' => '',
),
'gr' => array(
'name' => 'Greece',
'currency' => 'EUR',
Expand All @@ -87,26 +88,27 @@ class KP_Form_Fields {
'currency' => 'EUR',
'endpoint' => '',
),
'mx' => array(
'name' => 'Mexico',
'currency' => 'MXN',
'endpoint' => '-na',
),
'nl' => array(
'name' => 'Netherlands',
'currency' => 'EUR',
'endpoint' => '',
),
'no' => array(
'name' => 'Norway',
'currency' => 'NOK',
'endpoint' => '',
),
'nz' => array(
'name' => 'New Zealand',
'currency' => 'NZD',
'endpoint' => '-oc',
),
'mx' => array(
'name' => 'Mexico',
'currency' => 'MXN',
'endpoint' => '-na',
'no' => array(
'name' => 'Norway',
'currency' => 'NOK',
'endpoint' => '',
),

'pl' => array(
'name' => 'Poland',
'currency' => 'PLN',
Expand All @@ -117,16 +119,16 @@ class KP_Form_Fields {
'currency' => 'EUR',
'endpoint' => '',
),
'es' => array(
'name' => 'Spain',
'currency' => 'EUR',
'endpoint' => '',
),
'ro' => array(
'name' => 'Romania',
'currency' => 'RON',
'endpoint' => '',
),
'es' => array(
'name' => 'Spain',
'currency' => 'EUR',
'endpoint' => '',
),
'se' => array(
'name' => 'Sweden',
'currency' => 'SEK',
Expand Down
27 changes: 25 additions & 2 deletions classes/class-kp-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,30 @@ public static function kp_wc_place_order() {
wp_send_json_error( 'missing_data' );
}

$order = wc_get_order( $order_id );
$order = wc_get_order( $order_id );
$recurring_token = false;

if ( KP_Subscription::order_has_subscription( $order ) ) {
$response = KP_WC()->api->create_customer_token( kp_get_klarna_country( $order ), $auth_token, $order_id );
if ( is_wp_error( $response ) ) {
wp_send_json_error( 'customer_token_failed' );
}

$recurring_token = $response['token_id'];

/* translators: Recurring token. */
$order->add_order_note( sprintf( __( 'Recurring token created: %s', 'klarna-payments-for-woocommerce' ), $recurring_token ) );

if ( 0.0 === floatval( $order->get_total() ) ) {
$order->payment_complete();
KP_Subscription::save_recurring_token( $order_id, $recurring_token );
$order->add_order_note( __( 'The order contains a free or trial subscription, and no Klarna order is associated with this purchase. A Klarna order will only be registered once the subscriber is charged.', 'klarna-payments-for-woocommerce' ) );

kp_unset_session_values();
wp_send_json_success( $order->get_checkout_order_received_url() );
}
}

$response = KP_WC()->api->place_order( kp_get_klarna_country( $order ), $auth_token, $order_id );

if ( is_wp_error( $response ) ) {
Expand All @@ -64,7 +87,7 @@ public static function kp_wc_place_order() {
$fraud_status = $response['fraud_status'];
switch ( $fraud_status ) {
case 'ACCEPTED':
kp_process_accepted( $order, $response );
kp_process_accepted( $order, $response, $recurring_token );
kp_unset_session_values();
wp_send_json_success( $response['redirect_url'] );
break;
Expand Down
64 changes: 64 additions & 0 deletions classes/class-kp-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,70 @@ public function place_order( $country, $auth_token, $order_id ) {
return self::check_for_api_error( $response );
}


/**
* Create a customer token (required for creating subscriptions).
*
* @param mixed $country The Klarna country to use.
* @param mixed $auth_token The Klarna auth token for the session.
* @param mixed $order_id The WooCommerce order id.
* @return WP_Error|array
*/
public function create_customer_token( $country, $auth_token, $order_id ) {
$request = new KP_Create_Customer_Token(
array(
'country' => $country,
'auth_token' => $auth_token,
'order_id' => $order_id,
)
);
$response = $request->request();

return self::check_for_api_error( $response );
}

/**
* Create recurring order (subscription).
*
* @param mixed $country The Klarna country to use.
* @param mixed $recurring_token The recurring token for the subscription (referred to as customer token in docs).
* @param mixed $order_id The WooCommerce order id.
* @return WP_Error|array
*/
public function create_recurring_order( $country, $recurring_token, $order_id ) {
$request = new KP_Create_Recurring(
array(
'country' => $country,
'recurring_token' => $recurring_token,
'order_id' => $order_id,
)
);
$response = $request->request();

return self::check_for_api_error( $response );
}


/**
* Cancel recurring order (subscription).
* This is used when a subscription is cancelled in WooCommerce.
*
* @param mixed $country The Klarna country to use.
* @param mixed $recurring_token The recurring token for the subscription (referred to as customer token in docs).
* @return WP_Error|array
*/
public function cancel_recurring_order( $country, $recurring_token ) {
$request = new KP_Cancel_Recurring(
array(
'country' => $country,
'recurring_token' => $recurring_token,
)
);
$response = $request->request();

return self::check_for_api_error( $response );
}

/**
* Checks for WP Errors and returns either the response or a WP Error..
*
Expand Down
3 changes: 2 additions & 1 deletion classes/class-kp-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public function __construct() {
* @hook wp_enqueue_scripts
*/
public function enqueue_checkout_script() {
if ( ! kp_is_checkout_page() ) {
// We do not need to enqueue scripts on the change subscription payment method page since we'll redirect the customer to Klarna's HPP.
if ( ! kp_is_checkout_page() || KP_Subscription::is_change_payment_method() ) {
return;
}

Expand Down
7 changes: 7 additions & 0 deletions classes/class-kp-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ public function kp_wc_authorization( $data ) {
return;
}

// For free or trial subscriptions, authorization can be safely ignored as we do not need to act upon it
// since no Klarna order is associated with the purchase, only a Klarna customer token.
if ( KP_Subscription::order_has_subscription( $order ) && 0.0 === floatval( $order->get_total() ) ) {
$order->payment_complete();
return;
}

$response = KP_WC()->api->place_order( $country, $auth_token, $order->get_id() );
if ( is_wp_error( $response ) ) {
/**
Expand Down
7 changes: 4 additions & 3 deletions classes/class-kp-session.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ public function __construct() {
}

/**
* Gets a Klarna sessios. Creates or updates the Klarna session if needed.
* Gets a Klarna sessions. Creates or updates the Klarna session if needed.
*
* @param int|WC_Order|null $order The WooCommerce order or order id. Null if we are working with a cart.
*/
public function get_session( $order = null ) {
if ( ! kp_is_available() || ! kp_is_checkout_page() ) {
if ( ! kp_is_available() || ! kp_is_checkout_page() && ! KP_Subscription::is_change_payment_method() ) {
return;
}

// Check if we get an order.
$order = $this->maybe_get_order( $order );
$order_id = $order ? $order->get_id() : null;
$order_id = ! ( empty( $order ) || is_wp_error( $order ) ) ? $order->get_id() : null;

// Return WP Error if we get one.
if ( is_wp_error( $order ) ) {
Expand Down Expand Up @@ -193,6 +193,7 @@ private function clear_session_data_in_wc( $order ) {
WC()->session->__unset( 'kp_session_data' );
} else {
$order->delete_meta_data( '_kp_session_data' );
$order->save();
}
}

Expand Down

0 comments on commit 1ff93e6

Please sign in to comment.