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

Fix/issue 270 estimated order amount multicurrency #213

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 6 additions & 2 deletions includes/class-wc-amazon-payments-advanced-api-abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,16 @@ public static function get_payment_regions() {
* @since 1.8.0
* @version 1.8.0
*
* @param string $currency Currency code.
*
* @return bool Returns true if shop currency is supported by current payment region.
*/
public static function is_region_supports_shop_currency() {
public static function is_region_supports_shop_currency( $currency = false ) {
$region = self::get_region();
// Take into consideration external multi-currency plugins when not supported multicurrency region.
$currency = apply_filters( 'woocommerce_amazon_pa_active_currency', get_option( 'woocommerce_currency' ) );
if ( ! $currency ) {
$currency = apply_filters( 'woocommerce_amazon_pa_active_currency', get_option( 'woocommerce_currency' ) );
}

switch ( $region ) {
case 'eu':
Expand Down
8 changes: 7 additions & 1 deletion includes/class-wc-gateway-amazon-payments-advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -3030,10 +3030,16 @@ protected static function get_estimated_order_amount() {
return '';
}

$active_currency = WC_Amazon_Payments_Advanced_Multi_Currency::is_active() ? WC_Amazon_Payments_Advanced_Multi_Currency::get_selected_currency() : get_woocommerce_currency();

if ( ! WC_Amazon_Payments_Advanced_API::is_region_supports_shop_currency( $active_currency ) ) {
return '';
}

return wp_json_encode(
array(
'amount' => WC()->cart->get_total( 'amount' ),
'currencyCode' => get_woocommerce_currency(),
'currencyCode' => $active_currency,
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,31 @@ protected function get_allowed_currencies() {

return array_values( WC_Amazon_Payments_Advanced_API::get_selected_currencies() );
}

/**
* Get base currency per region.
*
* @param string $region The current region.
*
* @return array
*/
protected function region_base_currencies( $region = '' ) {

if ( empty( $region ) ) {
$region = WC_Amazon_Payments_Advanced_API::get_region();
}

switch ( $region ) {
case 'eu':
return array( 'EUR' );
case 'gb':
return array( 'GBP' );
case 'us':
return array( 'USD' );
case 'jp':
return array( 'JPY' );
default:
return array();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ public function is_active() {
*/
public function get_payment_method_data() {
return array(
'title' => $this->settings['title'],
'description' => $this->settings['description'],
'supports' => $this->get_supported_features(),
'amazonPayPreviewUrl' => esc_url( wc_apa()->plugin_url . '/build/images/amazon-pay-preview.png' ),
'action' => wc_apa()->get_gateway()->get_current_cart_action(),
'allowedCurrencies' => $this->get_allowed_currencies(),
'title' => $this->settings['title'],
'description' => $this->settings['description'],
'supports' => $this->get_supported_features(),
'amazonPayPreviewUrl' => esc_url( wc_apa()->plugin_url . '/build/images/amazon-pay-preview.png' ),
'action' => wc_apa()->get_gateway()->get_current_cart_action(),
'allowedCurrencies' => $this->get_allowed_currencies(),
'regionBaseCurrencies' => $this->region_base_currencies(),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,28 +42,29 @@ public function is_active() {
* @return array
*/
public function get_payment_method_data() {
$wc_apa_gateway = wc_apa()->get_gateway();

$checkout_session = $wc_apa_gateway->get_checkout_session_id() ? $wc_apa_gateway->get_checkout_session() : null;

return array(
'title' => $this->settings['title'],
'description' => $this->settings['description'],
'hide_button_mode' => $this->settings['hide_button_mode'],
'loggedIn' => ! is_admin() && $checkout_session && ! is_wp_error( $checkout_session ) && true === $wc_apa_gateway->is_checkout_session_still_valid( $checkout_session ),
'supports' => $this->get_supported_features(),
'logoutUrl' => $wc_apa_gateway->get_amazon_logout_url(),
'logoutMessage' => apply_filters( 'woocommerce_amazon_pa_checkout_logout_message', __( 'You\'re logged in with your Amazon Account.', 'woocommerce-gateway-amazon-payments-advanced' ) ),
'selectedPaymentMethod' => esc_html( $wc_apa_gateway->get_selected_payment_label( $checkout_session ) ),
'hasPaymentPreferences' => $wc_apa_gateway->has_payment_preferences( $checkout_session ),
'allOtherGateways' => $this->gateways_to_unset_on_fe(),
'allowedCurrencies' => $this->get_allowed_currencies(),
'amazonPayPreviewUrl' => esc_url( wc_apa()->plugin_url . '/build/images/amazon-pay-preview.png' ),
'amazonAddress' => array(
'amazonBilling' => $checkout_session && ! is_wp_error( $checkout_session ) && ! empty( $checkout_session->billingAddress ) ? WC_Amazon_Payments_Advanced_API::format_address( $checkout_session->billingAddress ) : null, // phpcs:ignore WordPress.NamingConventions
'amazonShipping' => $checkout_session && ! is_wp_error( $checkout_session ) && ! empty( $checkout_session->shippingAddress ) ? WC_Amazon_Payments_Advanced_API::format_address( $checkout_session->shippingAddress ) : null, // phpcs:ignore WordPress.NamingConventions
),
);
$wc_apa_gateway = wc_apa()->get_gateway();

$checkout_session = $wc_apa_gateway->get_checkout_session_id() ? $wc_apa_gateway->get_checkout_session() : null;

return array(
'title' => $this->settings['title'],
'description' => $this->settings['description'],
'hide_button_mode' => $this->settings['hide_button_mode'],
'loggedIn' => ! is_admin() && $checkout_session && ! is_wp_error( $checkout_session ) && true === $wc_apa_gateway->is_checkout_session_still_valid( $checkout_session ),
'supports' => $this->get_supported_features(),
'logoutUrl' => $wc_apa_gateway->get_amazon_logout_url(),
'logoutMessage' => apply_filters( 'woocommerce_amazon_pa_checkout_logout_message', __( 'You\'re logged in with your Amazon Account.', 'woocommerce-gateway-amazon-payments-advanced' ) ),
'selectedPaymentMethod' => esc_html( $wc_apa_gateway->get_selected_payment_label( $checkout_session ) ),
'hasPaymentPreferences' => $wc_apa_gateway->has_payment_preferences( $checkout_session ),
'allOtherGateways' => $this->gateways_to_unset_on_fe(),
'allowedCurrencies' => $this->get_allowed_currencies(),
'amazonPayPreviewUrl' => esc_url( wc_apa()->plugin_url . '/build/images/amazon-pay-preview.png' ),
'amazonAddress' => array(
'amazonBilling' => $checkout_session && ! is_wp_error( $checkout_session ) && ! empty( $checkout_session->billingAddress ) ? WC_Amazon_Payments_Advanced_API::format_address( $checkout_session->billingAddress ) : null, // phpcs:ignore WordPress.NamingConventions
'amazonShipping' => $checkout_session && ! is_wp_error( $checkout_session ) && ! empty( $checkout_session->shippingAddress ) ? WC_Amazon_Payments_Advanced_API::format_address( $checkout_session->shippingAddress ) : null, // phpcs:ignore WordPress.NamingConventions
),
'regionBaseCurrencies' => $this->region_base_currencies(),
);

}

Expand Down
5 changes: 4 additions & 1 deletion src/js/_renderAmazonButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ const getButtonSettings = ( buttonSettingsFlag, checkoutConfig, estimatedOrderAm
// customize the buyer experience
placement: amazon_payments_advanced.placement,
buttonColor: amazon_payments_advanced.button_color,
estimatedOrderAmount: estimatedOrderAmount,
checkoutLanguage:
amazon_payments_advanced.button_language !== ''
? amazon_payments_advanced.button_language.replace( '-', '_' )
: undefined
};

if ( estimatedOrderAmount ) {
obj.estimatedOrderAmount = estimatedOrderAmount;
}

if ( 'express' === buttonSettingsFlag ) {
obj.productType = amazon_payments_advanced.action;
obj.createCheckoutSessionConfig = amazon_payments_advanced.create_checkout_session_config;
Expand Down
4 changes: 3 additions & 1 deletion src/js/non-block/amazon-wc-checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,9 @@
}
} else {
obj.createCheckoutSessionConfig = amazon_payments_advanced.create_checkout_session_config;
obj.estimatedOrderAmount = amazon_payments_advanced.estimated_order_amount;
if ( amazon_payments_advanced.estimated_order_amount ) {
obj.estimatedOrderAmount = amazon_payments_advanced.estimated_order_amount;
}
}
return obj;
}
Expand Down
15 changes: 15 additions & 0 deletions src/js/payments-methods/express/_payment-methods-express.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { useEffect, useState } from '@wordpress/element';
* Internal dependencies
*/
import { renderAmazonButton } from '../../_renderAmazonButton';
import { settings } from './_settings';

/**
* Returns a react component and also sets an observer for the onCheckoutAfterProcessingWithSuccess event.
Expand All @@ -23,6 +24,16 @@ const AmazonPayExpressBtn = ( props ) => {
return <div id="pay_with_amazon_express" />;
};


/**
* Is estimated order amount supported.
* @param {object} currency
* @returns {boolean} True if estimated order amount is supported.
*/
const isEstimatedOrderAmountSupported = ( currency ) => {
return currency.code && settings.regionBaseCurrencies && settings.regionBaseCurrencies.includes( currency.code );
};

/**
* Returns the estimated order amount button attribute.
* @param {object} props
Expand All @@ -32,6 +43,10 @@ const calculateEstimatedOrderAmount = ( props ) => {
const { billing } = props;
const { currency } = billing;

if ( ! isEstimatedOrderAmountSupported( currency ) ) {
return null;
}

/**
* Get how many charactes are present in the cart's total value.
* So if the checkout value was 23.76,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,17 +113,44 @@ public function test_classic_process_payment_succeeds() : void {
'result' => 'success',
'redirect' => '#amazon-pay-classic-id-that-should-not-exist',
'amazonCreateCheckoutParams' => wp_json_encode( WC_Mocker_Amazon_Payments_Advanced_API::get_create_checkout_classic_session_config( array( 'test' ) ) ),
'amazonEstimatedOrderAmount' => wp_json_encode(
array(
'amount' => $order_total,
'currencyCode' => get_woocommerce_currency(),
)
),
'amazonEstimatedOrderAmount' => $mock_gateway::get_estimated_order_amount(),
),
$mock_gateway->process_payment( $order->get_id() )
);
}

/**
* Test estimated order amount with multi-currency.
*
* @return void
*/
public function test_estimated_order_amount_output() : void {
$checkout_session_key = apply_filters( 'woocommerce_amazon_pa_checkout_session_key', 'amazon_checkout_session_id' );
WC()->session->set( $checkout_session_key, null );
WC()->session->save_data();

$order_total = 100;

update_option( 'woocommerce_default_country', 'ES:B' );
update_option( 'woocommerce_currency', 'EUR' );

$eu_mock_gateway = new WC_Mocker_Gateway_Amazon_Payments_Advanced( $order_total );

$this->assertEquals(
wp_json_encode(
array(
'amount' => $order_total,
'currencyCode' => 'EUR',
)
),
$eu_mock_gateway::get_estimated_order_amount(),
);

update_option( 'woocommerce_currency', 'USD' );

$this->assertEquals( '', $eu_mock_gateway::get_estimated_order_amount() );
}

/**
* Test maybe_separator_and_checkout_button_single_product method.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ public static function get_create_checkout_classic_session_config( array $payloa
'signature' => $signature,
);
}

public static function is_region_supports_shop_currency( $currency = false ) : bool {

$region = WC_Amazon_Payments_Advanced_API::get_payment_region_from_country( WC()->countries->get_base_country() );

if ( ! $currency ) {
$currency = get_woocommerce_currency();
}

switch ( $region ) {
case 'eu':
return 'EUR' === $currency;
case 'gb':
return 'GBP' === $currency;
case 'us':
return 'USD' === $currency;
case 'jp':
return 'JPY' === $currency;
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,21 @@ protected function get_create_checkout_classic_session_config( $payload ) : arra
*
* @return string
*/
protected static function get_estimated_order_amount() : string {
public static function get_estimated_order_amount() : string {
if ( null === WC()->cart ) {
return '';
}

$active_currency = get_woocommerce_currency();

if ( ! WC_Mocker_Amazon_Payments_Advanced_API::is_region_supports_shop_currency( $active_currency ) ) {
return '';
}

return wp_json_encode(
array(
'amount' => self::$order_total,
'currencyCode' => get_woocommerce_currency(),
'currencyCode' => $active_currency,
)
);
}
Expand Down