Skip to content

Commit

Permalink
Code review fixes - Added support for non wcpay subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
zmaglica committed Aug 30, 2023
1 parent 8f1b3dd commit d1996ca
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
21 changes: 12 additions & 9 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1509,15 +1509,18 @@ protected function get_metadata_from_order( $order, $payment_type ) {
'subscription_payment' => 'no',
];

// If the order belongs to a WCPay Subscription, set the payment context to 'wcpay_subscription' (this helps with associating which fees belong to orders).
if ( 'recurring' === (string) $payment_type && ! $this->is_subscriptions_plugin_active() ) {
$subscriptions = wcs_get_subscriptions_for_order( $order, [ 'order_type' => 'any' ] );

foreach ( $subscriptions as $subscription ) {
if ( WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) {
$metadata['payment_context'] = 'wcpay_subscription';
$metadata['subscription_payment'] = count( $subscriptions ) > 1 ? 'renewal' : 'initial';
break;
if ( 'recurring' === (string) $payment_type ) {
$subscriptions = wcs_get_subscriptions_for_order( $order, [ 'order_type' => 'any' ] );
$metadata['subscription_payment'] = count( $subscriptions ) > 1 ? 'renewal' : 'initial';
$metadata['payment_context'] = 'regular_subscription';

// If the order belongs to a WCPay Subscription, set the payment context to 'wcpay_subscription' (this helps with associating which fees belong to orders).
if ( $this->is_subscriptions_plugin_active() ) {
foreach ( $subscriptions as $subscription ) {
if ( WC_Payments_Subscription_Service::is_wcpay_subscription( $subscription ) ) {
$metadata['payment_context'] = 'wcpay_subscription';
break;
}
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/payment-methods/test-class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
]
Expand Down Expand Up @@ -484,7 +484,7 @@ public function test_update_payment_intent_with_selected_upe_payment_method() {
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
]
Expand Down Expand Up @@ -582,7 +582,7 @@ public function test_update_payment_intent_with_payment_country() {
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ public function test_update_payment_intent_adds_customer_save_payment_and_level3
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];
Expand Down Expand Up @@ -491,8 +491,8 @@ public function test_update_payment_intent_with_selected_upe_payment_method() {
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'client_version' => '6.3.2',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',

];
Expand Down Expand Up @@ -570,7 +570,7 @@ public function test_update_payment_intent_with_payment_country() {
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic', // This won't show split UPE as gateway since the mocked class is used.
'checkout_type' => 'unknown',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,15 +445,18 @@ function( $metadata ) {
}

public function test_saved_card_zero_dollar_subscription() {
$order = WC_Helper_Order::create_order( self::USER_ID, 0 );
$order = WC_Helper_Order::create_order( self::USER_ID, 0 );
$subscriptions = [ new WC_Subscription() ];
$subscriptions[0]->set_parent( $order );

$this->mock_wcs_order_contains_subscription( true );
$this->mock_wcs_get_subscriptions_for_order( $subscriptions );

$_POST = [
'payment_method' => WC_Payment_Gateway_WCPay::GATEWAY_ID,
self::TOKEN_REQUEST_KEY => $this->token->get_id(),
];

$this->mock_wcs_order_contains_subscription( true );

// The card is already saved and there's no payment needed, so no Setup Intent needs to be created.
$request = $this->mock_wcpay_request( Create_And_Confirm_Setup_Intention::class, 0 );

Expand All @@ -463,9 +466,6 @@ public function test_saved_card_zero_dollar_subscription() {
->expects( $this->never() )
->method( 'add_payment_method_to_user' );

$subscriptions = [ WC_Helper_Order::create_order( self::USER_ID ) ];
$this->mock_wcs_get_subscriptions_for_order( $subscriptions );

$result = $this->mock_wcpay_gateway->process_payment( $order->get_id() );
$result_order = wc_get_order( $order->get_id() );

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test-class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ public function test_capture_charge_metadata() {
'order_key' => $order->get_order_key(),
'payment_type' => Payment_Type::SINGLE(),
'gateway_type' => 'classic',
'checkout_type' => 'unknown',
'checkout_type' => '',
'client_version' => WCPAY_VERSION_NUMBER,
'subscription_payment' => 'no',
];
Expand Down

0 comments on commit d1996ca

Please sign in to comment.