Skip to content

Commit

Permalink
fix: cancelled subscriptions sync (#3466)
Browse files Browse the repository at this point in the history
* fix: cancelled subscriptions sync

* fix: sync dates with date + time

* test: update tests with new date format

---------

Co-authored-by: dkoo <derrick.koo@automattic.com>
  • Loading branch information
leogermani and dkoo authored Oct 8, 2024
1 parent 2148248 commit 6cad50a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion includes/reader-activation/sync/class-metadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
25 changes: 11 additions & 14 deletions includes/reader-activation/sync/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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 );
}
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 7 additions & 5 deletions tests/unit-tests/reader-activation-sync-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

use Newspack\Reader_Activation\Sync;
use Newspack\Reader_Activation\Sync\Metadata;

require_once __DIR__ . '/../mocks/wc-mocks.php';

Expand Down Expand Up @@ -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'],
Expand Down Expand Up @@ -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() );
}

/**
Expand All @@ -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'] );
}

/**
Expand All @@ -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 );

Expand Down

0 comments on commit 6cad50a

Please sign in to comment.