Skip to content

Commit

Permalink
Merge branch 'master' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
iverok committed Sep 9, 2024
2 parents 900eb8e + 446f181 commit 1fb618e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
\#.*
*~
ignored.txt

node_modules
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ with a small amount, like 2 NOK. Just refund or cancel the purchase as needed.

For issues with your WooCommerce installation you should use the
[support forum on WordPress.org](https://wordpress.org/support/plugin/woo-vipps).
For other issues, you should contact [Vipps MobilePay](https://developer.vippsmobilepay.com/docs/contact).
For other issues, you should contact [Vipps MobilePay](https://developer.vippsmobilepay.com/docs/contact/#contact-details).

### General questions

Expand Down
26 changes: 25 additions & 1 deletion VippsCheckout.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ public function init () {
add_action('wp_ajax_vipps_checkout_start_session', array($this, 'vipps_ajax_checkout_start_session'));
add_action('wp_ajax_nopriv_vipps_checkout_start_session', array($this, 'vipps_ajax_checkout_start_session'));

// Check cart total before initiating Vipps Checkout NT-2024-09-07
// This allows for real-time validation of the cart before proceeding with the checkout process
add_action('wp_ajax_vipps_checkout_validate_cart', array($this, 'ajax_vipps_checkout_validate_cart'));
add_action('wp_ajax_nopriv_vipps_checkout_validate_cart', array($this, 'ajax_vipps_checkout_validate_cart'));

// Prevent previews and prefetches of the Vipps Checkout page starting and creating orders
add_action('wp_head', array($this, 'wp_head'));

Expand Down Expand Up @@ -276,9 +281,12 @@ public function init () {
do_action('vipps_cart_changed');
return $updated;
});
// Trigger cart_changed when a coupon is applied
add_action('woocommerce_applied_coupon', array($this, 'cart_changed'));
// Trigger cart_changed when a coupon is removed
add_action('woocommerce_removed_coupon', array($this, 'cart_changed'));
// Then handle the actual cart change
add_action('vipps_cart_changed', array($this, 'cart_changed'));

}

public function admin_init () {
Expand Down Expand Up @@ -586,6 +594,22 @@ public function vipps_ajax_checkout_poll_session () {
wp_send_json_success(array('msg'=>'unknown', 'url'=>''));
}

// Check cart total before initiating Vipps Checkout NT-2024-09-07
// Also any other checks we might want to do in the future. This will validate the cart each time the
// checkout page loads, even if a session is already in progress. IOK 2024-09-09
public function ajax_vipps_checkout_validate_cart() {
$cart_total = WC()->cart->get_total('edit');
$minimum_amount = 1; // 1 in the store currency

if ($cart_total < $minimum_amount) {
wp_send_json_error(array(
'message' => sprintf(__('Vipps Checkout cannot be used for orders less than %1$s %2$s', 'woo-vipps'), $minimum_amount, get_woocommerce_currency() )
));
} else {
wp_send_json_success(array('message', __("OK", 'woo-vipps')));
}
}

// Retrieve the current pending Vipps Checkout session, if it exists, and do some cleanup
// if it isn't correct IOK 2021-09-03
protected function vipps_checkout_current_pending_session () {
Expand Down
47 changes: 46 additions & 1 deletion js/vipps-checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,51 @@ jQuery( document ).ready( function() {
}


// Initialize the Vipps Checkout process
function initVippsCheckout () {
// Prevent multiple initializations
if (initiating) return;
initiating=true;
initiating = true;

// Set visual indicators for processing state
jQuery("body").css("cursor", "progress");
jQuery("body").addClass('processing');

// Disable all Vipps checkout buttons
jQuery('.vipps_checkout_button.button').each(function () {
jQuery(this).addClass('disabled');
jQuery(this).css("cursor", "progress");
});

// Check cart total before proceeding with checkout NT-2024-09-07
// handle any errors in handleCheckoutError IOK 2024-09-09
return validateCart(proceedWithCheckout, handleCheckoutError);
}

// Check if the cart total meets the minimum required amount NT-2024-09-07
function validateCart(success, failure) {
jQuery.ajax(VippsConfig['vippsajaxurl'], {
cache: false,
dataType: 'json',
data: { 'action': 'vipps_checkout_validate_cart' },
method: 'POST',
success: function(result) {
// If cart total is valid, proceed
if (result.success) {
success();
} else {
failure(result.data.message)
}
},
error: function(xhr, statustext, error) {
// Ignore any validation errors if ajax somehow breaks, but log the thing
console.log("Error validating cart: " + statustext);
success();
}
});
}

function proceedWithCheckout() {
// Try to start Vipps Checkout with any session provided.
function doVippsCheckout() {
if (!VippsSessionState) return false;
Expand Down Expand Up @@ -149,6 +183,17 @@ jQuery( document ).ready( function() {
}
}

// Function to handle errors during the Vipps checkout process NT-2024-09-07
function handleCheckoutError(errorMessage) {
console.error(errorMessage);
jQuery("body").css("cursor", "default");
jQuery('.vipps_checkout_button.button').css("cursor", "default");
jQuery('.vipps_checkout_startdiv').hide();
jQuery("body").removeClass('processing');
jQuery('#vippscheckouterror').hide();
jQuery('#vippscheckoutframe').html('<div class="woocommerce-error">' + errorMessage + '</div>');
initiating = false;
}

function listenToFrame() {
if (listening) return;
Expand Down
10 changes: 7 additions & 3 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: wphostingdev, iverok, perwilhelmsen, nikolaidev, lassepladsen
Tags: woocommerce, vipps, mobilepay
Tags: woocommerce, vipps
Version: 2.1.8
Stable tag: 2.1.8
Version: 2.1.9
Stable tag: 2.1.9
Requires at least: 4.7
Tested up to: 6.6.1
Requires PHP: 7.0
Expand Down Expand Up @@ -94,6 +94,7 @@ Shareable links and QR codes can be generated from the Vipps tab on the product
This project is hosted on Github at: https://github.com/vippsas/vipps-woocommerce

== Upgrade Notice ==
Ensure coupons added to the cart works with Vipps Checkout
Version 2.1.8 supports the upcoming Finnish VAT change
Cancelling _completed_ orders will normally also refund these orders if the refund has been captured; but we now implement a cutoff for this so that this does not happen for orders older than 30 days (tunable by the 'woo_vipps_cancel_refund_days_threshold' filter). This is a safety measure to avoid accidental refunds of archived orders. You can still use the 'refund' status or refund the orders manually.
Allow Klarna as an external payment method for norway
Expand Down Expand Up @@ -239,7 +240,10 @@ From version 1.1.13 you can also modify the javascript using the new WP hooks li

== Changelog ==

= 2024-08-26 version 2.1.7 =
= 2024-09-09 version 2.1.9 =
Handle coupons for Vipps Checkout

= 2024-08-26 version 2.1.8 =
Change API to use taxRate instead of taxPercent to allow for VAT change in Finland 1. sep 2024.

= 2024-08-16 version 2.1.7 =
Expand Down
6 changes: 3 additions & 3 deletions woo-vipps.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
Author URI: https://www.wp-hosting.no/
Text-domain: woo-vipps
Domain Path: /languages
Version: 2.1.8
Stable tag: 2.1.8
Version: 2.1.9
Stable tag: 2.1.9
Requires at least: 4.7
Tested up to: 6.6.1
Requires PHP: 7.0
Expand Down Expand Up @@ -49,7 +49,7 @@


// Report version externally
define('WOO_VIPPS_VERSION', '2.1.8');
define('WOO_VIPPS_VERSION', '2.1.9');

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
Expand Down

0 comments on commit 1fb618e

Please sign in to comment.