Fix GooglePay button on Pay Now page.

This commit is contained in:
Pedro Silva 2023-10-13 17:57:03 +01:00
parent bdf472c3dc
commit 886f214dc6
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
4 changed files with 70 additions and 2 deletions

View file

@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface;
use WC_Order; use WC_Order;
use WC_Product; use WC_Product;
use WC_Product_Variation; use WC_Product_Variation;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken; use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory; use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies; use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
@ -971,6 +972,10 @@ class SmartButton implements SmartButtonInterface {
'funding_sources_without_redirect' => $this->funding_sources_without_redirect, 'funding_sources_without_redirect' => $this->funding_sources_without_redirect,
); );
if ( 'pay-now' === $this->context() ) {
$localize['pay_now'] = $this->pay_now_script_data();
}
if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) { if ( $this->style_for_context( 'layout', 'mini-cart' ) !== 'horizontal' ) {
$localize['button']['mini_cart_style']['tagline'] = false; $localize['button']['mini_cart_style']['tagline'] = false;
} }
@ -991,6 +996,32 @@ class SmartButton implements SmartButtonInterface {
return $localize; return $localize;
} }
/**
* Returns pay-now payment data.
*
* @return array
*/
private function pay_now_script_data(): array {
$order_id = $this->get_order_pay_id();
$base_location = wc_get_base_location();
$shop_country_code = $base_location['country'] ?? '';
$currency_code = get_woocommerce_currency();
$wc_order = wc_get_order( $order_id );
if ( ! $wc_order instanceof WC_Order ) {
return array();
}
$total = (float) $wc_order->get_total( 'numeric' );
return array(
'total' => $total,
'total_str' => ( new Money( $total, $currency_code ) )->value_str(),
'currency_code' => $currency_code,
'country_code' => $shop_country_code,
);
}
/** /**
* If we can find the payer data for a current customer, we will return it. * If we can find the payer data for a current customer, we will return it.
* *

View file

@ -76,7 +76,7 @@ class CartScriptParamsEndpoint implements EndpointInterface {
// Shop settings. // Shop settings.
$base_location = wc_get_base_location(); $base_location = wc_get_base_location();
$shop_country_code = $base_location['country']; $shop_country_code = $base_location['country'] ?? '';
$currency_code = get_woocommerce_currency(); $currency_code = get_woocommerce_currency();
wp_send_json_success( wp_send_json_success(

View file

@ -4,6 +4,7 @@ import CheckoutHandler from "./CheckoutHandler";
import CartBlockHandler from "./CartBlockHandler"; import CartBlockHandler from "./CartBlockHandler";
import CheckoutBlockHandler from "./CheckoutBlockHandler"; import CheckoutBlockHandler from "./CheckoutBlockHandler";
import MiniCartHandler from "./MiniCartHandler"; import MiniCartHandler from "./MiniCartHandler";
import PayNowHandler from "./PayNowHandler";
import PreviewHandler from "./PreviewHandler"; import PreviewHandler from "./PreviewHandler";
class ContextHandlerFactory { class ContextHandlerFactory {
@ -15,8 +16,9 @@ class ContextHandlerFactory {
case 'cart': case 'cart':
return new CartHandler(buttonConfig, ppcpConfig, externalActionHandler); return new CartHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'checkout': case 'checkout':
case 'pay-now': // same as checkout
return new CheckoutHandler(buttonConfig, ppcpConfig, externalActionHandler); return new CheckoutHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'pay-now':
return new PayNowHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'mini-cart': case 'mini-cart':
return new MiniCartHandler(buttonConfig, ppcpConfig, externalActionHandler); return new MiniCartHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'cart-block': case 'cart-block':

View file

@ -0,0 +1,35 @@
import Spinner from "../../../../ppcp-button/resources/js/modules/Helper/Spinner";
import BaseHandler from "./BaseHandler";
import CheckoutActionHandler
from "../../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler";
class PayNowHandler extends BaseHandler {
shippingAllowed() {
return false;
}
transactionInfo() {
return new Promise(async (resolve, reject) => {
const data = this.ppcpConfig['pay_now'];
resolve({
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str
});
});
}
actionHandler() {
return new CheckoutActionHandler(
this.ppcpConfig,
this.errorHandler(),
new Spinner()
);
}
}
export default PayNowHandler;