From 6cad50a5254b55933bfe2ef17ee0d50782b9f5f7 Mon Sep 17 00:00:00 2001 From: leogermani Date: Tue, 8 Oct 2024 15:05:28 -0300 Subject: [PATCH] fix: cancelled subscriptions sync (#3466) * fix: cancelled subscriptions sync * fix: sync dates with date + time * test: update tests with new date format --------- Co-authored-by: dkoo --- .../reader-activation/sync/class-metadata.php | 2 +- .../sync/class-woocommerce.php | 25 ++++++++----------- .../reader-activation-sync-woocommerce.php | 12 +++++---- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/includes/reader-activation/sync/class-metadata.php b/includes/reader-activation/sync/class-metadata.php index 67f35eed17..7747d6f574 100644 --- a/includes/reader-activation/sync/class-metadata.php +++ b/includes/reader-activation/sync/class-metadata.php @@ -17,7 +17,7 @@ */ class Metadata { - const DATE_FORMAT = 'Y-m-d'; + const DATE_FORMAT = 'Y-m-d H:i:s'; const PREFIX = 'NP_'; const PREFIX_OPTION = '_newspack_metadata_prefix'; diff --git a/includes/reader-activation/sync/class-woocommerce.php b/includes/reader-activation/sync/class-woocommerce.php index b2e96cc84c..87cba2ba80 100644 --- a/includes/reader-activation/sync/class-woocommerce.php +++ b/includes/reader-activation/sync/class-woocommerce.php @@ -98,20 +98,24 @@ private static function get_current_product_order_for_sync( $customer ) { * @return ?WCS_Subscription A Subscription object or null. */ private static function get_most_recent_cancelled_or_expired_subscription( $user_id ) { - $subcriptions = array_reduce( + $subscriptions = array_reduce( array_keys( \wcs_get_users_subscriptions( $user_id ) ), function( $acc, $subscription_id ) { $subscription = \wcs_get_subscription( $subscription_id ); if ( $subscription->has_status( WooCommerce_Connection::FORMER_SUBSCRIBER_STATUSES ) ) { - $acc[] = $subscription_id; + + // Only subscriptions that had a completed order are considered. + if ( ! empty( $subscription->get_date( 'last_order_date_completed' ) ) ) { + $acc[] = $subscription_id; + } } return $acc; }, [] ); - if ( ! empty( $subcriptions ) ) { - return reset( $subcriptions ); + if ( ! empty( $subscriptions ) ) { + return reset( $subscriptions ); } } @@ -174,10 +178,6 @@ private static function get_order_metadata( $order, $payment_page_url = false ) $is_subscription = \wcs_is_subscription( $order ); } - // Only update last payment data if new payment has been received. - $active_statuses = $is_subscription ? WooCommerce_Connection::ACTIVE_SUBSCRIPTION_STATUSES : WooCommerce_Connection::ACTIVE_ORDER_STATUSES; - $payment_received = $order->has_status( $active_statuses ); - $metadata = []; if ( empty( $payment_page_url ) ) { @@ -233,7 +233,7 @@ private static function get_order_metadata( $order, $payment_page_url = false ) $metadata['product_name'] = reset( $order_items )->get_name(); } $order_date_paid = $order->get_date_paid(); - if ( $payment_received && ! empty( $order_date_paid ) ) { + if ( ! empty( $order_date_paid ) ) { $metadata['last_payment_amount'] = $order->get_total(); $metadata['last_payment_date'] = $order_date_paid->date( Metadata::DATE_FORMAT ); } @@ -268,11 +268,8 @@ private static function get_order_metadata( $order, $payment_page_url = false ) $metadata['sub_end_date'] = $current_subscription->get_date( 'end' ) ? $current_subscription->get_date( 'end' ) : ''; $metadata['billing_cycle'] = $current_subscription->get_billing_period(); $metadata['recurring_payment'] = $current_subscription->get_total(); - - if ( $payment_received ) { - $metadata['last_payment_amount'] = $current_subscription->get_total(); - $metadata['last_payment_date'] = $current_subscription->get_date( 'last_order_date_paid' ) ? $current_subscription->get_date( 'last_order_date_paid' ) : gmdate( Metadata::DATE_FORMAT ); - } + $metadata['last_payment_amount'] = $current_subscription->get_total(); + $metadata['last_payment_date'] = $current_subscription->get_date( 'last_order_date_paid' ) ? $current_subscription->get_date( 'last_order_date_paid' ) : gmdate( Metadata::DATE_FORMAT ); // When a WC Subscription is terminated, the next payment date is set to 0. We don't want to sync that – the next payment date should remain as it was // in the event of cancellation. diff --git a/tests/unit-tests/reader-activation-sync-woocommerce.php b/tests/unit-tests/reader-activation-sync-woocommerce.php index 7ca7a58258..786ed8aaf2 100644 --- a/tests/unit-tests/reader-activation-sync-woocommerce.php +++ b/tests/unit-tests/reader-activation-sync-woocommerce.php @@ -6,6 +6,7 @@ */ use Newspack\Reader_Activation\Sync; +use Newspack\Reader_Activation\Sync\Metadata; require_once __DIR__ . '/../mocks/wc-mocks.php'; @@ -95,7 +96,7 @@ public function test_payment_metadata_basic() { $payment_page_url = 'https://example.com/donate'; $contact_data = Sync\WooCommerce::get_contact_from_order( $order, $payment_page_url ); - $today = gmdate( 'Y-m-d' ); + $today = gmdate( Metadata::DATE_FORMAT ); $this->assertEquals( [ 'email' => self::USER_DATA['user_email'], @@ -161,11 +162,12 @@ public function test_payment_metadata_with_failed_order() { ] ); + $previous_order = self::$current_order; self::$current_order = $order; $contact_data = Sync\WooCommerce::get_contact_from_order( $order ); - $this->assertEmpty( $contact_data['metadata']['last_payment_date'] ); - $this->assertEmpty( $contact_data['metadata']['last_payment_amount'] ); + $this->assertEquals( $contact_data['metadata']['last_payment_date'], $previous_order->get_date_paid()->date( Metadata::DATE_FORMAT ) ); + $this->assertEquals( $contact_data['metadata']['last_payment_amount'], self::$current_order->get_total() ); } /** @@ -183,7 +185,7 @@ public function test_payment_metadata_from_customer() { $contact_data = Sync\WooCommerce::get_contact_from_customer( self::$user_id ); $this->assertEquals( $order_data['total'], $contact_data['metadata']['last_payment_amount'] ); - $this->assertEquals( gmdate( 'Y-m-d' ), $contact_data['metadata']['last_payment_date'] ); + $this->assertEquals( gmdate( Metadata::DATE_FORMAT ), $contact_data['metadata']['last_payment_date'] ); } /** @@ -194,7 +196,7 @@ public function test_payment_metadata_from_customer_with_last_order_failed() { 'customer_id' => self::$user_id, 'status' => 'completed', 'total' => 70, - 'date_paid' => gmdate( 'Y-m-d', strtotime( '-1 week' ) ), + 'date_paid' => gmdate( Metadata::DATE_FORMAT, strtotime( '-1 week' ) ), ]; $order = \wc_create_order( $completed_order_data );