From 5265b45b4cddaa08c5d9722a141b6150d0463d8c Mon Sep 17 00:00:00 2001 From: Alefe Souza Date: Wed, 1 Nov 2023 20:13:11 -0300 Subject: [PATCH] Add WooCommerce Multi-Currency support on WooPay (#7246) Co-authored-by: Hsing-yu Flowers --- ...x-woopay-woocommerce-multicurrency-support | 4 ++ .../class-woopay-adapted-extensions.php | 19 ++++++++ includes/woopay/class-woopay-session.php | 44 ++++++++++--------- .../test-class-woopay-adapted-extensions.php | 10 +++++ 4 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 changelog/fix-woopay-woocommerce-multicurrency-support diff --git a/changelog/fix-woopay-woocommerce-multicurrency-support b/changelog/fix-woopay-woocommerce-multicurrency-support new file mode 100644 index 00000000000..d0caf0158b2 --- /dev/null +++ b/changelog/fix-woopay-woocommerce-multicurrency-support @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Add WooCommerce Multi-Currency support on WooPay. diff --git a/includes/woopay/class-woopay-adapted-extensions.php b/includes/woopay/class-woopay-adapted-extensions.php index b0d943d87bd..340af75bfab 100644 --- a/includes/woopay/class-woopay-adapted-extensions.php +++ b/includes/woopay/class-woopay-adapted-extensions.php @@ -155,6 +155,25 @@ public function get_gift_cards_data( $user ) { return $gift_cards_script_data; } + /** + * The custom data from plugins to be used on WooPay, + * it's not an adapted extension because it doesn't + * use the email verification integration. + * + * @return array The custom data. + */ + public function get_extension_data() { + $extension_data = []; + + if ( defined( 'WOOCOMMERCE_MULTICURRENCY_VERSION' ) ) { + $extension_data[ 'woocommerce-multicurrency' ] = [ + 'currency' => get_woocommerce_currency(), + ]; + } + + return $extension_data; + } + /** * Get WC Blocks registered integrations. * diff --git a/includes/woopay/class-woopay-session.php b/includes/woopay/class-woopay-session.php index 6d374689f5b..f4ec84b3d15 100644 --- a/includes/woopay/class-woopay-session.php +++ b/includes/woopay/class-woopay-session.php @@ -384,13 +384,15 @@ private static function get_init_session_request( $order_id = null, $key = null, remove_filter( 'woocommerce_store_api_disable_nonce_check', '__return_true' ); $checkout_data = isset( $preloaded_checkout_data['/wc/store/v1/checkout'] ) ? $preloaded_checkout_data['/wc/store/v1/checkout']['body'] : ''; + $email = ! empty( $_POST['email'] ) ? wc_clean( wp_unslash( $_POST['email'] ) ) : ''; + $request = [ 'wcpay_version' => WCPAY_VERSION_NUMBER, 'user_id' => $user->ID, 'customer_id' => $customer_id, 'session_nonce' => self::create_woopay_nonce( $user->ID ), 'store_api_token' => self::init_store_api_token(), - 'email' => '', + 'email' => $email, 'store_data' => [ 'store_name' => get_bloginfo( 'name' ), 'store_logo' => $store_logo, @@ -423,6 +425,26 @@ private static function get_init_session_request( $order_id = null, $key = null, 'tracks_user_identity' => WC_Payments::woopay_tracker()->tracks_get_identity( $user->ID ), ]; + $woopay_adapted_extensions = new WooPay_Adapted_Extensions(); + $request['extension_data'] = $woopay_adapted_extensions->get_extension_data(); + + if ( ! empty( $email ) ) { + // Save email in session to skip TYP verify email and check if + // WooPay verified email matches. + WC()->customer->set_billing_email( $email ); + WC()->customer->save(); + + $request['adapted_extensions'] = $woopay_adapted_extensions->get_adapted_extensions_data( $email ); + + if ( ! is_user_logged_in() && count( $request['adapted_extensions'] ) > 0 ) { + $store_user_email_registered = get_user_by( 'email', $email ); + + if ( $store_user_email_registered ) { + $request['email_verified_session_nonce'] = self::create_woopay_nonce( $store_user_email_registered->ID ); + } + } + } + return $request; } @@ -441,33 +463,13 @@ public static function ajax_init_woopay() { ); } - $email = ! empty( $_POST['email'] ) ? wc_clean( wp_unslash( $_POST['email'] ) ) : ''; $order_id = ! empty( $_POST['order_id'] ) ? absint( wp_unslash( $_POST['order_id'] ) ) : null; $key = ! empty( $_POST['key'] ) ? sanitize_text_field( wp_unslash( $_POST['key'] ) ) : null; $billing_email = ! empty( $_POST['billing_email'] ) ? sanitize_text_field( wp_unslash( $_POST['billing_email'] ) ) : null; $body = self::get_init_session_request( $order_id, $key, $billing_email ); - $body['email'] = $email; $body['user_session'] = isset( $_REQUEST['user_session'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['user_session'] ) ) : null; - if ( ! empty( $email ) ) { - // Save email in session to skip TYP verify email and check if - // WooPay verified email matches. - WC()->customer->set_billing_email( $email ); - WC()->customer->save(); - - $woopay_adapted_extensions = new WooPay_Adapted_Extensions(); - $body['adapted_extensions'] = $woopay_adapted_extensions->get_adapted_extensions_data( $email ); - - if ( ! is_user_logged_in() && count( $body['adapted_extensions'] ) > 0 ) { - $store_user_email_registered = get_user_by( 'email', $email ); - - if ( $store_user_email_registered ) { - $body['email_verified_session_nonce'] = self::create_woopay_nonce( $store_user_email_registered->ID ); - } - } - } - $args = [ 'url' => WooPay_Utilities::get_woopay_rest_url( 'init' ), 'method' => 'POST', diff --git a/tests/unit/woopay/test-class-woopay-adapted-extensions.php b/tests/unit/woopay/test-class-woopay-adapted-extensions.php index 02ad41b2fec..9144ea7ed31 100644 --- a/tests/unit/woopay/test-class-woopay-adapted-extensions.php +++ b/tests/unit/woopay/test-class-woopay-adapted-extensions.php @@ -139,4 +139,14 @@ public function test_get_gift_cards_data_while_logged_out_with_zero_balance() { $this->assertEquals( $this->woopay_adapted_extensions->get_gift_cards_data( $this->test_user ), $expected ); } + + public function test_get_extension_data_with_no_data() { + $this->assertEquals( $this->woopay_adapted_extensions->get_extension_data(), [] ); + } + + public function test_get_extension_data() { + define( 'WOOCOMMERCE_MULTICURRENCY_VERSION', '0.0.0' ); + + $this->assertEquals( $this->woopay_adapted_extensions->get_extension_data(), [ 'woocommerce-multicurrency' => [ 'currency' => 'USD' ] ] ); + } }