Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split UPE: Correct token behavior #4670

Merged
merged 19 commits into from
Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1252,7 +1252,15 @@ public function process_payment_for_order( $cart, $payment_information, $additio
throw new Exception( WC_Payments_Utils::get_filtered_error_message( $e ) );
}

$selected_payment_method_type = [ WC_Payments::get_gateway()->get_selected_stripe_payment_type_id() ];
$upe_payment_method = sanitize_text_field( wp_unslash( $_POST['payment_method'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification

if ( 'woocommerce_payments' !== $upe_payment_method ) {
$upe_payment_method = str_replace( 'woocommerce_payments_', '', $upe_payment_method );
} else {
$upe_payment_method = 'card';
}

$payment_methods = [ $upe_payment_method ];

$additional_api_parameters['is_platform_payment_method'] = $this->is_platform_payment_method( $payment_information->is_using_saved_payment_method() );

Expand Down Expand Up @@ -1298,7 +1306,7 @@ public function process_payment_for_order( $cart, $payment_information, $additio
$this->get_level3_data_from_order( $order ),
$payment_information->is_merchant_initiated(),
$additional_api_parameters,
$selected_payment_method_type,
$payment_methods,
$payment_information->get_cvc_confirmation()
);
}
Expand Down Expand Up @@ -2867,7 +2875,7 @@ public function schedule_order_tracking( $order_id, $order = null ) {
* Create a payment intent without confirming the intent.
*
* @param WC_Order $order - Order based on which to create intent.
* @param array $selected_payment_method_type - A list of allowed payment methods. Eg. card, card_present.
* @param array $payment_methods - A list of allowed payment methods. Eg. card, card_present.
* @param string $capture_method - Controls when the funds will be captured from the customer's account ("automatic" or "manual").
* It must be "manual" for in-person (terminal) payments.
*
Expand All @@ -2878,15 +2886,15 @@ public function schedule_order_tracking( $order_id, $order = null ) {
*
* @throws Exception - When an error occurs in intent creation.
*/
public function create_intent( WC_Order $order, array $selected_payment_method_type, string $capture_method = 'automatic', array $metadata = [], string $customer_id = null ) {
public function create_intent( WC_Order $order, array $payment_methods, string $capture_method = 'automatic', array $metadata = [], string $customer_id = null ) {
$currency = strtolower( $order->get_currency() );
$converted_amount = WC_Payments_Utils::prepare_amount( $order->get_total(), $currency );

try {
$intent = $this->payments_api_client->create_intention(
$converted_amount,
$currency,
$selected_payment_method_type,
$payment_methods,
$order->get_order_number(),
$capture_method,
$metadata,
Expand Down
7 changes: 5 additions & 2 deletions includes/class-wc-payments-customer-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use WCPay\Database_Cache;
use WCPay\Exceptions\API_Exception;
use WCPay\Logger;
use WCPay\Constants\Payment_Method;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -266,8 +267,10 @@ public function clear_cached_payment_methods_for_user( $user_id ) {
if ( WC_Payments::is_network_saved_cards_enabled() ) {
return; // No need to do anything, payment methods will never be cached in this case.
}
$customer_id = $this->get_customer_id_by_user_id( $user_id );
foreach ( WC_Payments::get_gateway()->get_upe_enabled_payment_method_ids() as $type ) {

$retrievable_payment_method_types = [ Payment_Method::CARD, Payment_Method::SEPA ];
$customer_id = $this->get_customer_id_by_user_id( $user_id );
foreach ( $retrievable_payment_method_types as $type ) {
$this->database_cache->delete( Database_Cache::PAYMENT_METHODS_KEY_PREFIX . $customer_id . '_' . $type );
}
}
Expand Down
35 changes: 27 additions & 8 deletions includes/class-wc-payments-token-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class WC_Payments_Token_Service {

const REUSABLE_GATEWAYS = [ WC_Payment_Gateway_WCPay::GATEWAY_ID, WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA ];

/**
* Client for making requests to the WooCommerce Payments API
*
Expand Down Expand Up @@ -62,7 +64,7 @@ public function add_token_to_user( $payment_method, $user ) {

if ( Payment_Method::SEPA === $payment_method['type'] ) {
$token = new WC_Payment_Token_WCPay_SEPA();
$token->set_gateway_id( CC_Payment_Gateway::GATEWAY_ID );
$token->set_gateway_id( WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA );
$token->set_last4( $payment_method[ Payment_Method::SEPA ]['last4'] );
} else {
$token = new WC_Payment_Token_CC();
Expand Down Expand Up @@ -100,7 +102,8 @@ public function add_payment_method_to_user( $payment_method_id, $user ) {
* @return array
*/
public function woocommerce_get_customer_payment_tokens( $tokens, $user_id, $gateway_id ) {
if ( ( ! empty( $gateway_id ) && WC_Payment_Gateway_WCPay::GATEWAY_ID !== $gateway_id ) || ! is_user_logged_in() ) {

if ( ( ! empty( $gateway_id ) && ! in_array( $gateway_id, self::REUSABLE_GATEWAYS, true ) ) || ! is_user_logged_in() ) {
return $tokens;
}

Expand All @@ -120,29 +123,43 @@ public function woocommerce_get_customer_payment_tokens( $tokens, $user_id, $gat
$stored_tokens = [];

foreach ( $tokens as $token ) {
if ( WC_Payment_Gateway_WCPay::GATEWAY_ID === $token->get_gateway_id() ) {
if ( in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS, true ) ) {
$stored_tokens[ $token->get_token() ] = $token;
}
}

$payment_methods = [ [] ];
foreach ( WC_Payments::get_gateway()->get_upe_enabled_payment_method_ids() as $type ) {
$retrievable_payment_method_types = [ Payment_Method::CARD ];

if ( in_array( Payment_Method::SEPA, WC_Payments::get_gateway()->get_upe_enabled_payment_method_ids(), true ) ) {
$retrievable_payment_method_types[] = Payment_Method::SEPA;
}

$payment_methods = [];

foreach ( $retrievable_payment_method_types as $type ) {
$payment_methods[] = $this->customer_service->get_payment_methods_for_customer( $customer_id, $type );
}

$payment_methods = array_merge( ...$payment_methods );

} catch ( Exception $e ) {
Logger::error( 'Failed to fetch payment methods for customer.' . $e );
return $tokens;
}

// Prevent unnecessary recursion, WC_Payment_Token::save() ends up calling 'woocommerce_get_customer_payment_tokens' in some cases.
remove_action( 'woocommerce_get_customer_payment_tokens', [ $this, 'woocommerce_get_customer_payment_tokens' ], 10, 3 );

foreach ( $payment_methods as $payment_method ) {
if ( ! isset( $payment_method['type'] ) ) {
continue;
}

if ( ! isset( $stored_tokens[ $payment_method['id'] ] ) ) {
if ( ! isset( $stored_tokens[ $payment_method['id'] ] ) && (
( Payment_Method::CARD === $payment_method['type'] && WC_Payment_Gateway_WCPay::GATEWAY_ID === $gateway_id ) ||
( Payment_Method::SEPA === $payment_method['type'] && WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA === $gateway_id ) ) ||
empty( $gateway_id )
) {
$token = $this->add_token_to_user( $payment_method, get_user_by( 'id', $user_id ) );
$tokens[ $token->get_id() ] = $token;
} else {
Expand All @@ -169,7 +186,8 @@ public function woocommerce_get_customer_payment_tokens( $tokens, $user_id, $gat
* @param WC_Payment_Token $token Token object.
*/
public function woocommerce_payment_token_deleted( $token_id, $token ) {
if ( WC_Payment_Gateway_WCPay::GATEWAY_ID === $token->get_gateway_id() ) {

if ( in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS, true ) ) {
try {
$this->payments_api_client->detach_payment_method( $token->get_token() );
// Clear cached payment methods.
Expand All @@ -187,7 +205,8 @@ public function woocommerce_payment_token_deleted( $token_id, $token ) {
* @param WC_Payment_Token $token Token object.
*/
public function woocommerce_payment_token_set_default( $token_id, $token ) {
if ( WC_Payment_Gateway_WCPay::GATEWAY_ID === $token->get_gateway_id() ) {

if ( in_array( $token->get_gateway_id(), self::REUSABLE_GATEWAYS, true ) ) {
$customer_id = $this->customer_service->get_customer_id_by_user_id( $token->get_user_id() );
if ( $customer_id ) {
$this->customer_service->set_default_payment_method_for_customer( $customer_id, $token->get_token() );
Expand Down
12 changes: 8 additions & 4 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,10 +495,12 @@ public static function add_plugin_links( $links ) {
* @return array The list of payment gateways that will be available, including WooCommerce Payments' Gateway class.
*/
public static function register_gateway( $gateways ) {
$gateways[] = self::$legacy_card_gateway;
$reusable_methods[] = self::$legacy_card_gateway;
$gateways[] = self::$legacy_card_gateway;

if ( WC_Payments_Features::is_upe_enabled() ) {
$all_upe_gateways = [];
$reusable_methods = [];

foreach ( self::$card_gateway->get_payment_method_ids_enabled_at_checkout() as $payment_method_id ) {
if ( 'card' === $payment_method_id ) {
continue;
Expand All @@ -510,13 +512,15 @@ public static function register_gateway( $gateways ) {
$reusable_methods[] = $upe_gateway;
}

$gateways[] = $upe_gateway;
$all_upe_gateways[] = $upe_gateway;

}

if ( is_add_payment_method_page() ) {
return $reusable_methods;
return array_merge( $gateways, $reusable_methods );
}

return array_merge( $gateways, $all_upe_gateways );
}

return $gateways;
Expand Down
2 changes: 1 addition & 1 deletion includes/payment-methods/class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public function process_payment( $order_id ) {
$payment_needed = 0 < $converted_amount;
$selected_upe_payment_type = $this->stripe_id;
$payment_type = $this->is_payment_recurring( $order_id ) ? Payment_Type::RECURRING() : Payment_Type::SINGLE();
$save_payment_method = $payment_type->equals( Payment_Type::RECURRING() ) || ! empty( $_POST[ 'wc-' . static::GATEWAY_ID . '-new-payment-method' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$save_payment_method = $payment_type->equals( Payment_Type::RECURRING() ) || ! empty( $_POST[ 'wc-' . self::GATEWAY_ID . '_' . $this->stripe_id . '-new-payment-method' ] ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
$payment_country = ! empty( $_POST['wcpay_payment_country'] ) ? wc_clean( wp_unslash( $_POST['wcpay_payment_country'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification.Missing

if ( $payment_intent_id ) {
Expand Down
12 changes: 2 additions & 10 deletions tests/unit/payment-methods/test-class-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,34 +701,26 @@ public function test_process_payment_returns_correct_redirect_url() {
}

public function test_process_payment_passes_save_payment_method_to_store() {
$mock_card_payment_gateway = $this->mock_payment_gateways[ Payment_Method::CARD ];
$mock_sepa_payment_gateway = $this->mock_payment_gateways[ Payment_Method::SEPA ];

$order = WC_Helper_Order::create_order();
$order_id = $order->get_id();
$gateway_id = UPE_Payment_Gateway::GATEWAY_ID;
$gateway_id = UPE_Payment_Gateway::GATEWAY_ID . '_' . Payment_Method::SEPA;
$save_payment_param = "wc-$gateway_id-new-payment-method";
$_POST[ $save_payment_param ] = 'yes';
$_POST['wc_payment_intent_id'] = 'pi_mock';

$payment_intent = WC_Helper_Intention::create_intention( [ 'status' => 'processing' ] );

$this->mock_api_client
->expects( $this->exactly( 2 ) )
->expects( $this->exactly( 1 ) )
->method( 'update_intention' )
->willReturn(
$payment_intent
);

$this->set_cart_contains_subscription_items( false );

// Test saving with cards.
$result = $mock_card_payment_gateway->process_payment( $order->get_id() );
$this->assertEquals( 'success', $result['result'] );
$this->assertMatchesRegularExpression( "/order_id=$order_id/", $result['redirect_url'] );
$this->assertMatchesRegularExpression( '/wc_payment_method=woocommerce_payments/', $result['redirect_url'] );
$this->assertMatchesRegularExpression( '/save_payment_method=yes/', $result['redirect_url'] );

// Test saving with SEPA.
$result = $mock_sepa_payment_gateway->process_payment( $order->get_id() );
$this->assertEquals( 'success', $result['result'] );
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test-class-wc-payments-token-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public function test_add_token_to_user_for_sepa() {

$token = $this->token_service->add_token_to_user( $mock_payment_method, wp_get_current_user() );

$this->assertEquals( 'woocommerce_payments', $token->get_gateway_id() );
$this->assertEquals( 'woocommerce_payments_sepa_debit', $token->get_gateway_id() );
$this->assertEquals( 1, $token->get_user_id() );
$this->assertEquals( 'pm_mock', $token->get_token() );
$this->assertEquals( '3000', $token->get_last4() );
Expand Down