Refactor GooglePay context handlers to use response currency and country.

This commit is contained in:
Pedro Silva 2023-08-30 08:39:31 +01:00
parent 2f6545aa57
commit 78d58f1a03
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
4 changed files with 22 additions and 8 deletions

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\Button\Endpoint;
use Psr\Log\LoggerInterface;
use Throwable;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\Button\Assets\SmartButton;
/**
@ -71,12 +72,24 @@ class CartScriptParamsEndpoint implements EndpointInterface {
$script_data = $this->smart_button->script_data();
$total = (float) WC()->cart->get_total( 'numeric' );
// Shop settings.
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'];
$currency_code = get_woocommerce_currency();
wp_send_json_success(
array(
'url_params' => $script_data['url_params'],
'button' => $script_data['button'],
'messages' => $script_data['messages'],
'amount' => WC()->cart->get_total( 'raw' ),
'url_params' => $script_data['url_params'],
'button' => $script_data['button'],
'messages' => $script_data['messages'],
'amount' => WC()->cart->get_total( 'raw' ),
'total' => $total,
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
'currency_code' => $currency_code,
'country_code' => $shop_country_code,
)
);

View file

@ -101,6 +101,7 @@ class SimulateCartEndpoint extends AbstractCartEndpoint {
$button_enabled = $button_enabled && ! $this->smart_button->is_button_disabled( 'product', $context_data );
}
// Shop settings.
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'];
$currency_code = get_woocommerce_currency();

View file

@ -31,10 +31,10 @@ class BaseHandler {
const data = result.data;
resolve({
countryCode: 'US', // data.country_code,
currencyCode: 'USD', // data.currency_code,
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.amount // Your amount
totalPrice: data.total_str
});
});

View file

@ -41,7 +41,7 @@ class SingleProductHandler extends BaseHandler {
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str // Your amount
totalPrice: data.total_str
});
}, products);