From 886f214dc6391bc9c5933bd1196649ea00ef3882 Mon Sep 17 00:00:00 2001
From: Pedro Silva
Date: Fri, 13 Oct 2023 17:57:03 +0100
Subject: [PATCH] Fix GooglePay button on Pay Now page.
---
.../ppcp-button/src/Assets/SmartButton.php | 31 ++++++++++++++++
.../src/Endpoint/CartScriptParamsEndpoint.php | 2 +-
.../js/Context/ContextHandlerFactory.js | 4 ++-
.../resources/js/Context/PayNowHandler.js | 35 +++++++++++++++++++
4 files changed, 70 insertions(+), 2 deletions(-)
create mode 100644 modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js
diff --git a/modules/ppcp-button/src/Assets/SmartButton.php b/modules/ppcp-button/src/Assets/SmartButton.php
index 426bb9251..390ec32f2 100644
--- a/modules/ppcp-button/src/Assets/SmartButton.php
+++ b/modules/ppcp-button/src/Assets/SmartButton.php
@@ -14,6 +14,7 @@ use Psr\Log\LoggerInterface;
use WC_Order;
use WC_Product;
use WC_Product_Variation;
+use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
use WooCommerce\PayPalCommerce\ApiClient\Helper\DccApplies;
@@ -971,6 +972,10 @@ class SmartButton implements SmartButtonInterface {
'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' ) {
$localize['button']['mini_cart_style']['tagline'] = false;
}
@@ -991,6 +996,32 @@ class SmartButton implements SmartButtonInterface {
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.
*
diff --git a/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php b/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php
index dbf18e67b..83d4c74cf 100644
--- a/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php
+++ b/modules/ppcp-button/src/Endpoint/CartScriptParamsEndpoint.php
@@ -76,7 +76,7 @@ class CartScriptParamsEndpoint implements EndpointInterface {
// Shop settings.
$base_location = wc_get_base_location();
- $shop_country_code = $base_location['country'];
+ $shop_country_code = $base_location['country'] ?? '';
$currency_code = get_woocommerce_currency();
wp_send_json_success(
diff --git a/modules/ppcp-googlepay/resources/js/Context/ContextHandlerFactory.js b/modules/ppcp-googlepay/resources/js/Context/ContextHandlerFactory.js
index 445ab08ae..8c6bc261d 100644
--- a/modules/ppcp-googlepay/resources/js/Context/ContextHandlerFactory.js
+++ b/modules/ppcp-googlepay/resources/js/Context/ContextHandlerFactory.js
@@ -4,6 +4,7 @@ import CheckoutHandler from "./CheckoutHandler";
import CartBlockHandler from "./CartBlockHandler";
import CheckoutBlockHandler from "./CheckoutBlockHandler";
import MiniCartHandler from "./MiniCartHandler";
+import PayNowHandler from "./PayNowHandler";
import PreviewHandler from "./PreviewHandler";
class ContextHandlerFactory {
@@ -15,8 +16,9 @@ class ContextHandlerFactory {
case 'cart':
return new CartHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'checkout':
- case 'pay-now': // same as checkout
return new CheckoutHandler(buttonConfig, ppcpConfig, externalActionHandler);
+ case 'pay-now':
+ return new PayNowHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'mini-cart':
return new MiniCartHandler(buttonConfig, ppcpConfig, externalActionHandler);
case 'cart-block':
diff --git a/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js b/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js
new file mode 100644
index 000000000..add275608
--- /dev/null
+++ b/modules/ppcp-googlepay/resources/js/Context/PayNowHandler.js
@@ -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;