Skip to content

Commit

Permalink
ref: move all Klarna specific code to the compat class and add action…
Browse files Browse the repository at this point in the history
…s + filters to enable this
  • Loading branch information
Marcuzz committed Feb 16, 2022
1 parent 6752ed8 commit fc03094
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 40 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,25 @@ Enables someone with access to Vipps developer keys to test the plugin. This is

Available filters:

`wc_vipps_recurring_supported_currencies($currencies: array)`
`wc_vipps_recurring_supported_currencies(array $currencies)`

- Takes an array of supported currencies in ISO 4217 format (like NOK). Vipps only supports NOK at the moment.

`wc_vipps_recurring_payment_icons($icons: array)`
`wc_vipps_recurring_payment_icons(array $icons)`

- Takes an array of icons that a WooCommerce payment gateway can have. Currently it only contains `vipps`, you can replace the image passed here if you want. It is however not recommended unless it follows Vipps' design specifications.

`wc_vipps_recurring_show_capture_button($show_capture_button: bool, $order: WC_Order)`
`wc_vipps_recurring_show_capture_button(bool $show_capture_button, WC_Order $order)`

- Decides whether the direct capture button shall be displayed on an order or not. Prior to version 1.2.1 this filter was called `woocommerce_vipps_recurring_show_capture_button`. `$show_capture_button` contains the current decision on whether or not it shall be displayed. `$order` contains the current `WC_Order` being viewed.

`wc_vipps_recurring_merchant_agreement_url($url: string)`
`wc_vipps_recurring_merchant_agreement_url(string $url)`

`wc_vipps_recurring_merchant_redirect_url($url: string)`
`wc_vipps_recurring_merchant_redirect_url(string $url)`

`wc_vipps_recurring_transaction_id_for_order(WC_Order $order, string $transaction_id)`

- Determines the return value of `WC_Vipps_Recurring_Helper::get_transaction_id_for_order`

# Frequently Asked Questions

Expand Down
70 changes: 51 additions & 19 deletions includes/compat/wc-vipps-recurring-kc-support.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,71 @@ class WC_Vipps_Recurring_Kc_Support {
* Initialize Vipps Recurring KC Support class.
*/
public static function init() {
add_filter( 'kco_wc_gateway_settings', [ 'WC_Vipps_Recurring_Kc_Support', 'form_fields' ] );
add_filter( 'kco_wc_gateway_settings', [ WC_Vipps_Recurring_Kc_Support::class, 'form_fields' ] );

add_filter( 'kco_wc_api_request_args', [
'WC_Vipps_Recurring_Kc_Support',
WC_Vipps_Recurring_Kc_Support::class,
'create_vipps_recurring_order'
], 90 );

add_filter( 'kco_wc_klarna_order_pre_submit', [
'WC_Vipps_Recurring_Kc_Support',
WC_Vipps_Recurring_Kc_Support::class,
'canonicalize_phone_number'
], 11 );

add_action( 'init', [ 'WC_Vipps_Recurring_Kc_Support', 'maybe_remove_other_gateway_button' ] );
add_action( 'init', [ WC_Vipps_Recurring_Kc_Support::class, 'maybe_remove_other_gateway_button' ] );

add_action( 'kco_wc_before_submit', [ 'WC_Vipps_Recurring_Kc_Support', 'add_vipps_recurring_payment_method' ] );
add_action( 'kco_wc_before_submit', [
WC_Vipps_Recurring_Kc_Support::class,
'add_vipps_recurring_payment_method'
] );

add_action( 'woocommerce_payment_complete', [
'WC_Vipps_Recurring_Kc_Support',
WC_Vipps_Recurring_Kc_Support::class,
'reset_default_payment_method'
], 10 );

add_filter( 'wc_vipps_recurring_transaction_id_for_order', [
WC_Vipps_Recurring_Kc_Support::class,
'fix_transaction_id'
], 10, 2 );

add_action( 'wc_vipps_recurring_before_process_order_charge', [
WC_Vipps_Recurring_Kc_Support::class,
'fix_payment_method_on_subscription'
] );
}

/**
* Klarna messes up our transaction id by inserting their own. We don't want theirs!
*/
public static function fix_transaction_id( $order, $transaction_id ) {
$_wc_klarna_order_id = WC_Vipps_Recurring_Helper::get_meta( $order, '_wc_klarna_order_id' );
if ( $_wc_klarna_order_id === $transaction_id ) {
return false;
}

return $transaction_id;
}

/**
* Fix 16.02.2022 - KCO did not set the correct external payment method on a subscription after completed payment
* KCO 2.6.4, WooCommerce 6.2.0
*/
public static function fix_payment_method_on_subscription( $order ) {
$gateway = WC_Vipps_Recurring::get_instance()->gateway;

$subscriptions = $gateway->get_subscriptions_for_order( $order );
foreach ( $subscriptions as $subscription ) {
if ( $subscription->get_payment_method() === 'kco' && ! empty( WC_Vipps_Recurring_Helper::get_agreement_id_from_order( $order ) ) ) {
$subscription->set_payment_method( $gateway->id );
$subscription->save();
}
}
}

/**
* Add custom setting fields to Klarna's Vipps Recurring settings.
*
* @param $settings
*
* @return mixed
*/
public static function form_fields( $settings ) {
$settings['epm_vipps_recurring_settings_title'] = [
Expand Down Expand Up @@ -82,10 +119,6 @@ public static function form_fields( $settings ) {

/**
* Add Vipps Recurring to Klarna Checkout.
*
* @param $create
*
* @return mixed
*/
public static function create_vipps_recurring_order( $create ) {
$merchant_urls = KCO_WC()->merchant_urls->get_urls();
Expand All @@ -111,7 +144,7 @@ public static function create_vipps_recurring_order( $create ) {
$gateway = [
'name' => $name,
'redirect_url' => add_query_arg( [
'kco-external-payment' => 'vipps_recurring',
'kco-external-payment' => WC_Vipps_Recurring::get_instance()->gateway->id,
'order_id' => $create['agreement_id'] ?? '{checkout.order.id}'
], $confirmation_url ),
'image_url' => $image_url,
Expand All @@ -138,6 +171,9 @@ public static function add_vipps_recurring_payment_method() {
}
}

/**
* Reset the default payment method back to KCO after a completed Vipps payment
*/
public static function reset_default_payment_method() {
if ( WC()->session && WC()->session->get( 'vipps_via_klarna' ) ) {
WC()->session->set( 'chosen_payment_method', 'kco' );
Expand All @@ -159,11 +195,7 @@ public static function maybe_remove_other_gateway_button() {
}

/**
* @param $klarna_order
*
* We need to remove +47 and all spaces from the phone number before handing it off to the Vipps API.
*
* @return mixed
*/
public static function canonicalize_phone_number( $klarna_order ) {
if ( isset( $_GET['kco-external-payment'] ) && 'vipps_recurring' === $_GET['kco-external-payment'] ) {
Expand Down
10 changes: 1 addition & 9 deletions includes/wc-gateway-vipps-recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,15 +685,7 @@ public function process_order_charge( $order, $charge ) {
return;
}

// Fix 16.02.2022 - KCO did not set the correct external payment method on a subscription after completed payment
// KCO 2.6.4, WooCommerce 6.2.0
$subscriptions = $this->get_subscriptions_for_order( $order );
foreach ( $subscriptions as $subscription ) {
if ( $subscription->get_payment_method() === 'kco' && ! empty( WC_Vipps_Recurring_Helper::get_agreement_id_from_order( $order ) ) ) {
$subscription->set_payment_method( $this->id );
$subscription->save();
}
}
do_action( 'wc_vipps_recurring_before_process_order_charge', $order );

WC_Vipps_Recurring_Helper::update_meta_data( $order, WC_Vipps_Recurring_Helper::META_CHARGE_ID, $charge['id'] );
$transaction_id = WC_Vipps_Recurring_Helper::get_transaction_id_for_order( $order );
Expand Down
9 changes: 2 additions & 7 deletions includes/wc-vipps-recurring-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,15 +269,10 @@ public static function get_transaction_id_for_order( $order ) {
: $order->get_transaction_id();

if ( is_integer( $transaction_id ) && ! $transaction_id ) {
return false;
}

$_wc_klarna_order_id = self::get_meta( $order, '_wc_klarna_order_id' );
if ( $_wc_klarna_order_id === $transaction_id ) {
return false;
$transaction_id = false;
}

return $transaction_id;
return apply_filters( 'wc_vipps_recurring_transaction_id_for_order', $order, $transaction_id );
}

/**
Expand Down

0 comments on commit fc03094

Please sign in to comment.