Skip to content

Commit

Permalink
feat: started working on support for Klarna Checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcuzz committed Nov 15, 2021
1 parent 751a0d5 commit adc8291
Show file tree
Hide file tree
Showing 44 changed files with 2,259 additions and 129 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.
4 changes: 4 additions & 0 deletions README.txt
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ 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.

= 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.
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.
189 changes: 189 additions & 0 deletions includes/compat/wc-vipps-recurring-kc-support.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?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' => plugins_url( 'assets/images/vipps-rgb-black.png', __FILE__ )
];

$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;
}

/**
* Compatibility code to create a Vipps Recurring order in Klarna.
*
* @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'];

// // todo: REMOVE ME
// $confirmation_url = str_replace( 'http://wordpress.test', 'https://98e1-141-0-97-106.ngrok.io', $confirmation_url );
// // todo: END REMOVE ME

$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 do Vipps as 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, 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 an other gateway" is enabled we have to remove this 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'] ?? 'no';
$remove = ( 'yes' === $disable_button );

// Let the user decide whether to use the 'use external payment method' button
// This is present for legacy reasons only, and is probably not the one you want. See the 'woo_vipps_activate_kco_external_payment' filter instead.
$remove = apply_filters( 'wc_vipps_recurring_remove_klarna_another_payment_button', $remove );

if ( $remove ) {
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.
2 changes: 1 addition & 1 deletion includes/wc-gateway-vipps-recurring.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -1725,7 +1725,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.
Loading

0 comments on commit adc8291

Please sign in to comment.