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 9 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
10 changes: 5 additions & 5 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ 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() ];
$payment_methods = WC_Payments::get_gateway()->get_payment_method_ids_enabled_at_checkout( null, true );
FangedParakeet marked this conversation as resolved.
Show resolved Hide resolved

// Make sure the payment method being charged was created in the platform.
if (
Expand Down Expand Up @@ -1256,7 +1256,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 @@ -2679,7 +2679,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 @@ -2690,15 +2690,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
22 changes: 16 additions & 6 deletions includes/class-wc-payments-token-service.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,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( 'woocommerce_payments_sepa_debit' );
FangedParakeet marked this conversation as resolved.
Show resolved Hide resolved
$token->set_last4( $payment_method[ Payment_Method::SEPA ]['last4'] );
} else {
$token = new WC_Payment_Token_CC();
Expand Down Expand Up @@ -100,7 +100,10 @@ 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() ) {

$gateway_ids = [ WC_Payment_Gateway_WCPay::GATEWAY_ID, WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA ];
tommyshellberg marked this conversation as resolved.
Show resolved Hide resolved

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

Expand All @@ -120,7 +123,7 @@ 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(), $gateway_ids, true ) ) {
$stored_tokens[ $token->get_token() ] = $token;
}
}
Expand All @@ -137,12 +140,13 @@ public function woocommerce_get_customer_payment_tokens( $tokens, $user_id, $gat

// 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'] ] ) && ( 'card' === $payment_method['type'] && WC_Payment_Gateway_WCPay::GATEWAY_ID === $gateway_id || 'sepa_debit' === $payment_method['type'] && WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA === $gateway_id ) || empty( $gateway_id ) ) {
FangedParakeet marked this conversation as resolved.
Show resolved Hide resolved
$token = $this->add_token_to_user( $payment_method, get_user_by( 'id', $user_id ) );
$tokens[ $token->get_id() ] = $token;
} else {
Expand All @@ -169,7 +173,10 @@ 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() ) {

$gateway_ids = [ WC_Payment_Gateway_WCPay::GATEWAY_ID, WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA ];

if ( in_array( $token->get_gateway_id(), $gateway_ids, true ) ) {
try {
$this->payments_api_client->detach_payment_method( $token->get_token() );
// Clear cached payment methods.
Expand All @@ -187,7 +194,10 @@ 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() ) {

$gateway_ids = [ WC_Payment_Gateway_WCPay::GATEWAY_ID, WC_Payment_Gateway_WCPay::GATEWAY_ID . '_' . Payment_Method::SEPA ];
FangedParakeet marked this conversation as resolved.
Show resolved Hide resolved

if ( in_array( $token->get_gateway_id(), $gateway_ids, 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
11 changes: 7 additions & 4 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,9 @@ 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;
$all_upe_gateways = [];
$reusable_methods = [];

if ( WC_Payments_Features::is_upe_enabled() ) {
foreach ( self::$card_gateway->get_payment_method_ids_enabled_at_checkout() as $payment_method_id ) {
FangedParakeet marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -507,13 +508,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 @@ -100,6 +100,7 @@ public function __construct(
add_action( "wc_ajax_wcpay_create_payment_intent_$this->stripe_id", [ $this, 'create_payment_intent_ajax' ] );
add_action( "wc_ajax_wcpay_update_payment_inten_ $this->stripe_id", [ $this, 'update_payment_intent_ajax' ] );
add_action( "wc_ajax_wcpay_init_setup_intent_$this->stripe_id", [ $this, 'init_setup_intent_ajax' ] );
add_action( 'woocommerce_after_account_payment_methods', [ $this, 'remove_upe_setup_intent_from_session' ], 10, 0 );

if ( 'card' !== $this->stripe_id ) {
$this->id = self::GATEWAY_ID . '_' . $this->stripe_id;
Expand All @@ -115,7 +116,6 @@ public function __construct(
add_action( 'wp', [ $this, 'maybe_process_upe_redirect' ] );

add_action( 'woocommerce_order_payment_status_changed', [ __CLASS__, 'remove_upe_payment_intent_from_session' ], 10, 0 );
add_action( 'woocommerce_after_account_payment_methods', [ $this, 'remove_upe_setup_intent_from_session' ], 10, 0 );
add_action( 'woocommerce_subscription_payment_method_updated', [ $this, 'remove_upe_setup_intent_from_session' ], 10, 0 );
}
}
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