Skip to content

Commit

Permalink
Merge pull request #54 from vippsas/feat/klarna-checkout-compat
Browse files Browse the repository at this point in the history
feat: Klarna Checkout compatibility
  • Loading branch information
Marcuzz authored Nov 26, 2021
2 parents 751a0d5 + 70bba68 commit bc94bb1
Show file tree
Hide file tree
Showing 42 changed files with 426 additions and 133 deletions.
Empty file modified .editorconfig
100644 → 100755
Empty file.
Empty file modified .gitignore
100644 → 100755
Empty file.
Empty file modified .wordpress-org/banner-1544x500.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified .wordpress-org/banner-772x250.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified .wordpress-org/icon-128x128.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified .wordpress-org/icon-256x256.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified .wordpress-org/screenshot-1.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified .wordpress-org/screenshot-2.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
6 changes: 6 additions & 0 deletions README.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ Alternatively you could look into using WooCommerce "Early renewals": [https://d

== Changelog ==

= 1.12.0 =
* Added: Support for Klarna Checkout.
* Fixed: No longer attempt to load Vipps Recurring Payments if WooCommerce is disabled/not installed.
* Fixed: If you have a variable subscription product the Vipps settings from the "parent" will now be respected.
* Fixed: This gateway no longer shows up for single purchase products, only if there is at least one subscription product in the cart.

= 1.11.0 =
* Added: You can now pay for single payment products in the same shopping cart as a subscription.

Expand Down
Empty file modified assets/css/vipps-recurring-admin.css
100644 → 100755
Empty file.
Empty file modified assets/css/vipps-recurring.css
100644 → 100755
Empty file.
Empty file modified assets/images/pay-with-vipps.svg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/images/vipps-icon-smile.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/images/vipps-logg-inn-neg.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/images/vipps-logg-inn.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/images/vipps-logo-negative-rgb-transparent.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/images/vipps-logo.svg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/vipps-rgb-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/vipps_logo_negative_rgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified assets/js/vipps-recurring-admin.js
100644 → 100755
Empty file.
Empty file modified composer.json
100644 → 100755
Empty file.
Empty file modified composer.lock
100644 → 100755
Empty file.
Empty file.
Empty file.
Empty file modified includes/admin/vipps-recurring-settings.php
100644 → 100755
Empty file.
184 changes: 184 additions & 0 deletions includes/compat/wc-vipps-recurring-kc-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
<?php

defined( 'ABSPATH' ) || exit;

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_api_request_args', [
'WC_Vipps_Recurring_Kc_Support',
'create_vipps_recurring_order'
], 90 );

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

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

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

add_action( 'woocommerce_checkout_order_processed', [
'WC_Vipps_Recurring_Kc_Support',
'reset_default_payment_method'
], 10, 3 );
}

/**
* 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'] = [
'title' => __( 'External Payment Method - Vipps Recurring Payments', 'woo-vipps-recurring' ),
'type' => 'title',
];

$settings['epm_vipps_recurring_activate'] = [
'title' => __( 'Activate', 'woo-vipps-recurring' ),
'type' => 'checkbox',
'description' => __( 'Activate Vipps Recurring Payments as an external payment method for Klarna Checkout', 'woo-vipps-recurring' ),
'default' => 'yes',
];

$settings['epm_vipps_recurring_name'] = [
'title' => __( 'Name', 'woo-vipps-recurring' ),
'type' => 'text',
'description' => __( 'Title for Vipps Recurring Payments method. This controls the title which the user sees in the checkout form.', 'woo-vipps-recurring' ),
'default' => __( 'Vipps', 'woo-vipps-recurring' ),
];

$settings['epm_vipps_recurring_description'] = [
'title' => __( 'Description', 'woo-vipps-recurring' ),
'type' => 'textarea',
'description' => __( 'Description for Vipps Recurring Payments method. This controls the description which the user sees in the checkout form.', 'woo-vipps-recurring' ),
'default' => '',
];

$settings['epm_vipps_recurring_img_url'] = [
'title' => __( 'Image url', 'woo-vipps-recurring' ),
'type' => 'text',
'description' => __( 'URL to the Vipps logo', 'woo-vipps-recurring' ),
'default' => WC_VIPPS_RECURRING_PLUGIN_URL . '/assets/images/vipps-rgb-black.png'
];

$settings['epm_vipps_recurring_disable_button'] = [
'title' => __( 'Disable other gateway button', 'woo-vipps-recurring' ),
'type' => 'checkbox',
'description' => __( 'Disables the "Select another Payment method" button in Klarna Checkout.', 'woo-vipps-recurring' ),
'default' => 'no',
];

return $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();
$confirmation_url = $merchant_urls['confirmation'];

$kco_settings = get_option( 'woocommerce_kco_settings' );
$activate = ! isset( $kco_settings['epm_vipps_recurring_activate'] ) || $kco_settings['epm_vipps_recurring_activate'] === 'yes';

global $vipps_recurring;
$activate = apply_filters( 'wc_vipps_recurring_activate_kco_external_payment', ( $activate && $vipps_recurring->gateway->is_available() ) );

if ( ! isset( $create['external_payment_methods'] ) || ! is_array( $create['external_payment_methods'] ) ) {
$create['external_payment_methods'] = [];
}

if ( ! $activate ) {
return $create;
}

$name = $kco_settings['epm_vipps_recurring_name'] ?? '';
$image_url = $kco_settings['epm_vipps_recurring_img_url'] ?? '';
$description = $kco_settings['epm_vipps_recurring_description'] ?? '';

$klarna_external_payment = [
'name' => $name,
'redirect_url' => add_query_arg( 'kco-external-payment', 'vipps_recurring', $confirmation_url ),
'image_url' => $image_url,
'description' => $description,
];

if ( ! isset( $create['external_payment_methods'] ) || ! is_array( $create['external_payment_methods'] ) ) {
$create['external_payment_methods'] = [];
}
$create['external_payment_methods'][] = $klarna_external_payment;

// Ensure we don't make Vipps the default payment method. This is checked in "woocommerce_checkout_order_processed" hook.
WC()->session->set( 'vipps_recurring_via_klarna', 1 );

return $create;
}

/**
* Add the Vipps Recurring Payments method to Klarna
*/
public static function add_vipps_recurring_payment_method() {
if ( isset( $_GET['kco-external-payment'] ) && 'vipps_recurring' === $_GET['kco-external-payment'] ) { ?>
$('input#payment_method_vipps_recurring').prop('checked', true);
$('input#legal').prop('checked', true);
<?php
// In case other actions are needed we can add more Javascript to this hook
do_action( 'wc_vipps_recurring_klarna_checkout_support_on_submit_javascript' );
}
}

/**
* Reset default payment method.
*
* @param $order_id
* @param $post_data
* @param $order
*/
public static function reset_default_payment_method( $order_id, $post_data, $order ) {
if ( WC()->session->get( 'vipps_recurring_via_klarna' ) ) {
WC()->session->set( 'chosen_payment_method', 'kco' );
WC()->session->set( 'vipps_recurring_via_klarna', 0 );
}
}

/**
* If the setting to remove "select another gateway" is enabled we have to remove that button.
*/
public static function maybe_remove_other_gateway_button() {
$kco_settings = get_option( 'woocommerce_kco_settings' );
$disable_button = $kco_settings['epm_vipps_recurring_disable_button'] === 'yes';

if ( $disable_button ) {
remove_action( 'kco_wc_after_order_review', 'kco_wc_show_another_gateway_button', 20 );
}
}

/**
* @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'] ) {
$phone_number = preg_replace( "/^\+47|[\s]/", '', $klarna_order->billing_address->phone );
$klarna_order->billing_address->phone = $phone_number;
}

return $klarna_order;
}
}
Empty file modified includes/pages/admin/vipps-recurring-admin-menu-page.php
100644 → 100755
Empty file.
12 changes: 7 additions & 5 deletions includes/wc-gateway-vipps-recurring.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1260,7 +1260,8 @@ public function maybe_update_subscription_details_in_app( $subscription_id ) {
}

$item_name = $item->get_name();
$product_description = WC_Vipps_Recurring_Helper::get_product_description( $item->get_product() );
$parent_product = wc_get_product( $item->get_product_id() );
$product_description = WC_Vipps_Recurring_Helper::get_product_description( $parent_product );

if ( $prefix = WC_Vipps_Recurring_Helper::get_meta( $subscription, WC_Vipps_Recurring_Helper::META_SUBSCRIPTION_UPDATE_IN_APP_DESCRIPTION_PREFIX ) ) {
$product_description = "[$prefix] $product_description";
Expand Down Expand Up @@ -1394,6 +1395,7 @@ public function process_payment( $order_id, bool $retry = true, bool $previous_e
} );
$item = array_pop( $subscription_items );
$product = $item->get_product();
$parent_product = wc_get_product( $item->get_product_id() );

$extra_initial_charge_description = '';

Expand All @@ -1411,7 +1413,7 @@ public function process_payment( $order_id, bool $retry = true, bool $previous_e
}

$is_virtual = $product->is_virtual();
$direct_capture = $product->get_meta( WC_Vipps_Recurring_Helper::META_PRODUCT_DIRECT_CAPTURE ) === 'yes';
$direct_capture = $parent_product->get_meta( WC_Vipps_Recurring_Helper::META_PRODUCT_DIRECT_CAPTURE ) === 'yes';

$agreement_url = filter_var( get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ), FILTER_VALIDATE_URL )
? get_permalink( get_option( 'woocommerce_myaccount_page_id' ) )
Expand All @@ -1430,7 +1432,7 @@ public function process_payment( $order_id, bool $retry = true, bool $previous_e
$is_subscription_switch = wcs_order_contains_switch( $order );

if ( $is_subscription_switch ) {
$subscription_switch_data = WC_Vipps_Recurring_Helper::get_meta( $order, '_subscription_switch_data' );;
$subscription_switch_data = WC_Vipps_Recurring_Helper::get_meta( $order, '_subscription_switch_data' );

if ( isset( $subscription_switch_data[ array_key_first( $subscription_switch_data ) ]['switches'] ) ) {
$switches = $subscription_switch_data[ array_key_first( $subscription_switch_data ) ]['switches'];
Expand Down Expand Up @@ -1468,7 +1470,7 @@ public function process_payment( $order_id, bool $retry = true, bool $previous_e
'initialCharge' => [
'amount' => WC_Vipps_Recurring_Helper::get_vipps_amount( $order->get_total() ),
'currency' => $order->get_currency(),
'description' => WC_Vipps_Recurring_Helper::get_product_description( $product ) . $extra_initial_charge_description,
'description' => WC_Vipps_Recurring_Helper::get_product_description( $parent_product ) . $extra_initial_charge_description,
'transactionType' => $capture_immediately ? 'DIRECT_CAPTURE' : 'RESERVE_CAPTURE',
],
] );
Expand Down Expand Up @@ -1725,7 +1727,7 @@ public function process_admin_options() {
public function append_valid_statuses_for_payment_complete( $statuses ): array {
$statuses = array_merge( $statuses, $this->statuses_to_attempt_capture );

if ( ! in_array( 'completed', $statuses ) && $this->transition_renewals_to_completed ) {
if ( ! in_array( 'completed', $statuses, true ) && $this->transition_renewals_to_completed ) {
$statuses[] = 'completed';
}

Expand Down
Empty file modified includes/wc-vipps-recurring-admin-notices.php
100644 → 100755
Empty file.
Empty file modified includes/wc-vipps-recurring-api.php
100644 → 100755
Empty file.
10 changes: 10 additions & 0 deletions includes/wc-vipps-recurring-compatibility.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

global $vipps_recurring;

if ( defined( 'KCO_WC_VERSION' ) && $vipps_recurring->gateway->enabled === 'yes' && class_exists( 'KCO' )
&& version_compare( KCO_WC_VERSION, '2.0.0', '>=' )
&& ! has_filter( 'kco_wc_api_request_args', 'kcoepm_create_vipps_recurring_order' ) ) {
require_once __DIR__ . '/compat/wc-vipps-recurring-kc-support.php';
WC_Vipps_Recurring_Kc_Support::init();
}
Empty file modified includes/wc-vipps-recurring-exceptions.php
100644 → 100755
Empty file.
Empty file modified includes/wc-vipps-recurring-helper.php
100644 → 100755
Empty file.
Empty file modified includes/wc-vipps-recurring-logger.php
100644 → 100755
Empty file.
Binary file modified languages/woo-vipps-recurring-nb_NO.mo
Binary file not shown.
Loading

0 comments on commit bc94bb1

Please sign in to comment.