krokedil / klarna-checkout-for-woocommerce

Klarna Checkout for WooCommerce plugin
15 stars 23 forks source link

Improve logic when to fetch recurring token from Klarna #554

Closed NiklasHogefjord closed 1 year ago

NiklasHogefjord commented 1 year ago

Currently we do the following check if we should try to fetch a recurring token from Klarna:

if ( class_exists( 'WC_Subscription' ) && ( wcs_order_contains_subscription( $wc_order, array( 'parent', 'renewal', 'resubscribe', 'switch' ) ) || wcs_is_subscription( $wc_order ) ) ) {
}

In some cases it seems as this check is not true even though the customer is placing a subscription in Woo.

To improve this, we now add a meta data field during process_payment() where we save _kco_recurring_order as yes or no. We can then use this as an improved check if we should try to fetch the recurring token, like this:

$recurring_order = $wc_order->get_meta( '_kco_recurring_order', true );

if ( 'yes' === $recurring_order || class_exists( 'WC_Subscription' ) && ( wcs_order_contains_subscription( $wc_order, array( 'parent', 'renewal', 'resubscribe', 'switch' ) ) || wcs_is_subscription( $wc_order ) ) ) {
}

This PR also saves the received recurring token to subscription order notes.