Skip to content

Commit

Permalink
fix: cancelled order deletion now works as intended
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcuzz committed Dec 23, 2024
1 parent 15c3484 commit 3a956dc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down
50 changes: 47 additions & 3 deletions includes/wc-gateway-vipps-recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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',
Expand All @@ -284,7 +288,7 @@ public function __construct() {
'pay-mt.vipps.no',
'pay-mt.mobilepay.dk',
'pay-mt.mobilepay.fi'
]);
] );
} );
}

Expand Down Expand Up @@ -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 = '[email protected]';
$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',
Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions includes/wc-vipps-recurring-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]';

/**
* Whether we are successfully connected to the Vipps/MobilePay API
*
Expand Down
5 changes: 4 additions & 1 deletion includes/wc-vipps-recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions woo-vipps-recurring.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 3a956dc

Please sign in to comment.