diff --git a/README.txt b/README.txt index f6eac5b..baad43f 100755 --- a/README.txt +++ b/README.txt @@ -3,7 +3,7 @@ Contributors: EverydayAS Tags: vipps, mobilepay, recurring payments, subscriptions, woocommerce subscriptions Requires at least: 5.0 Tested up to: 6.7 -Stable tag: 2.1.1 +Stable tag: 2.1.2 Requires PHP: 7.4 License: AGPLv3.0 or later License URI: https://www.gnu.org/licenses/agpl-3.0.html @@ -162,6 +162,9 @@ Alternatively you could look into using WooCommerce "Early renewals": [https://d == Changelog == += 2.1.2 = +* Fixed: Cleaning up old cancelled orders that were initiated via Checkout now works as intended. + = 2.1.1 = * Fixed: Added two new hosts to `allowed_redirect_hosts`: `pay.mobilepay.dk` and `pay.mobilepay.fi`. This fixes a bug where customers were not able to switch to MobilePay. diff --git a/includes/wc-gateway-vipps-recurring.php b/includes/wc-gateway-vipps-recurring.php index a2f338f..541fd10 100755 --- a/includes/wc-gateway-vipps-recurring.php +++ b/includes/wc-gateway-vipps-recurring.php @@ -185,6 +185,10 @@ public function __construct() { 'append_valid_statuses_for_payment_complete' ] ); + add_action( 'woocommerce_order_status_pending_to_cancelled', [ $this, 'maybe_delete_order' ], 99999 ); + add_action( 'woocommerce_new_order', [$this, 'maybe_delete_order_later'] ); + add_action( 'woocommerce_vipps_recurring_delete_pending_order', [$this, 'maybe_delete_order'] ); + add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' @@ -273,7 +277,7 @@ public function __construct() { // Woo Subscriptions uses `wp_safe_redirect()` during a gateway change, which will not allow us to redirect to the Vipps MobilePay API // Unless we whitelist the domains specifically add_filter( 'allowed_redirect_hosts', function ( $hosts ) { - return array_merge($hosts, [ + return array_merge( $hosts, [ // Production servers 'api.vipps.no', 'pay.vipps.no', @@ -284,7 +288,7 @@ public function __construct() { 'pay-mt.vipps.no', 'pay-mt.mobilepay.dk', 'pay-mt.mobilepay.fi' - ]); + ] ); } ); } @@ -2685,7 +2689,7 @@ public function create_or_get_anonymous_system_customer(): WC_Customer { // Create a user if it does not exist if ( ! get_user_by( 'ID', $customer_id ) ) { - $email = 'anonymous@vippsmobilepay.local'; + $email = WC_Vipps_Recurring_Helper::FAKE_USER_EMAIL; $username = wc_create_new_customer_username( $email ); $customer_id = wc_create_new_customer( $email, $username, null, [ 'first_name' => 'Anonymous Vipps MobilePay Customer', @@ -2862,4 +2866,44 @@ public function create_partial_subscriptions_from_order( WC_Order $order ) { return $subscriptions; } + + public function maybe_delete_order_later( $order_id ) { + if ( $this->get_option( 'checkout_cleanup_abandoned_orders' ) !== 'yes' ) { + return; + } + + if ( ! wp_next_scheduled( 'woocommerce_vipps_recurring_delete_pending_order', [ $order_id ] ) ) { + wp_schedule_single_event( time() + 3600, 'woocommerce_vipps_recurring_delete_pending_order', [ $order_id ] ); + } + } + + public function maybe_delete_order( $order_id ): bool { + $order = wc_get_order( $order_id ); + if ( ! $order ) { + return false; + } + + if ( $this->id !== $order->get_payment_method() ) { + return false; + } + + $express = WC_Vipps_Recurring_Helper::get_meta( $order, WC_Vipps_Recurring_Helper::META_ORDER_IS_EXPRESS ); + if ( ! $express ) { + return false; + } + + $empty_email = $order->get_billing_email() === WC_Vipps_Recurring_Helper::FAKE_USER_EMAIL || ! $order->get_billing_email(); + if ( ! $empty_email ) { + return false; + } + + if ( $this->get_option( 'checkout_cleanup_abandoned_orders' ) !== 'yes' ) { + return false; + } + + WC_Vipps_Recurring_Helper::update_meta_data( $order, WC_Vipps_Recurring_Helper::META_ORDER_MARKED_FOR_DELETION, 1 ); + $order->save(); + + return true; + } } diff --git a/includes/wc-vipps-recurring-helper.php b/includes/wc-vipps-recurring-helper.php index b0db433..3377077 100755 --- a/includes/wc-vipps-recurring-helper.php +++ b/includes/wc-vipps-recurring-helper.php @@ -78,6 +78,8 @@ class WC_Vipps_Recurring_Helper { public const SESSION_ADDRESS_HASH = '_vipps_recurring_address_hash'; public const SESSION_ORDER_EXPRESS_AUTH_TOKEN = '_vipps_recurring_order_express_auth_token'; + public const FAKE_USER_EMAIL = 'anonymous@vippsmobilepay.local'; + /** * Whether we are successfully connected to the Vipps/MobilePay API * diff --git a/includes/wc-vipps-recurring.php b/includes/wc-vipps-recurring.php index e5232c4..5a372e0 100644 --- a/includes/wc-vipps-recurring.php +++ b/includes/wc-vipps-recurring.php @@ -792,7 +792,10 @@ public function check_orders_marked_for_deletion() { $order = wc_get_order( $order_id ); // If this order has been manually updated in the mean-time, we no longer want to delete it. - if ( ! in_array( $order->get_status( 'edit' ), [ 'pending', 'cancelled' ] ) ) { + // Similarly, if it has a billing email we don't want to delete it. + $empty_email = $order->get_billing_email() === WC_Vipps_Recurring_Helper::FAKE_USER_EMAIL || !$order->get_billing_email(); + + if ( ! in_array( $order->get_status( 'edit' ), [ 'pending', 'cancelled' ] ) || $empty_email ) { WC_Vipps_Recurring_Helper::delete_meta_data( $order, WC_Vipps_Recurring_Helper::META_ORDER_MARKED_FOR_DELETION ); continue; diff --git a/woo-vipps-recurring.php b/woo-vipps-recurring.php index a87d6c3..457b85c 100755 --- a/woo-vipps-recurring.php +++ b/woo-vipps-recurring.php @@ -5,7 +5,7 @@ * Description: Offer recurring payments with Vipps MobilePay for WooCommerce Subscriptions * Author: Everyday AS * Author URI: https://everyday.no - * Version: 2.1.1 + * Version: 2.1.2 * Requires Plugins: woocommerce * Requires at least: 6.1 * Tested up to: 6.7 @@ -18,7 +18,7 @@ // phpcs:disable WordPress.Files.FileName -define( 'WC_VIPPS_RECURRING_VERSION', '2.1.1' ); +define( 'WC_VIPPS_RECURRING_VERSION', '2.1.2' ); /** * Polyfills