Skip to content

Commit

Permalink
Add WooCommerce Multi-Currency support on WooPay (#7246)
Browse files Browse the repository at this point in the history
Co-authored-by: Hsing-yu Flowers <[email protected]>
  • Loading branch information
alefesouza and hsingyuc authored Nov 1, 2023
1 parent 3e277ff commit 5265b45
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 21 deletions.
4 changes: 4 additions & 0 deletions changelog/fix-woopay-woocommerce-multicurrency-support
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fix

Add WooCommerce Multi-Currency support on WooPay.
19 changes: 19 additions & 0 deletions includes/woopay/class-woopay-adapted-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
44 changes: 23 additions & 21 deletions includes/woopay/class-woopay-session.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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',
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/woopay/test-class-woopay-adapted-extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ] ] );
}
}

0 comments on commit 5265b45

Please sign in to comment.