Refactor hooks in GooglePay

Add payment support in Cart and Checkout in GooglePay
This commit is contained in:
Pedro Silva 2023-08-24 16:01:41 +01:00
parent e39e29aece
commit efe59f81ef
No known key found for this signature in database
GPG key ID: E2EE20C0669D24B3
3 changed files with 113 additions and 52 deletions

View file

@ -463,6 +463,7 @@ class SmartButton implements SmartButtonInterface {
}
$this->button_renderer( PayPalGateway::ID );
do_action( 'woocommerce_paypal_payments_single_product_button_render' );
},
31
);
@ -483,6 +484,7 @@ class SmartButton implements SmartButtonInterface {
id="ppc-button-minicart"
class="woocommerce-mini-cart__buttons buttons"
></p>';
do_action( 'woocommerce_paypal_payments_minicart_button_render' );
},
30
);
@ -496,6 +498,7 @@ class SmartButton implements SmartButtonInterface {
function (): void {
$this->button_renderer( PayPalGateway::ID );
$this->button_renderer( CardButtonGateway::ID );
do_action( 'woocommerce_paypal_payments_payorder_button_render' );
},
20
);
@ -504,6 +507,7 @@ class SmartButton implements SmartButtonInterface {
function (): void {
$this->button_renderer( PayPalGateway::ID );
$this->button_renderer( CardButtonGateway::ID );
do_action( 'woocommerce_paypal_payments_checkout_button_render' );
}
);
@ -516,6 +520,7 @@ class SmartButton implements SmartButtonInterface {
}
$this->button_renderer( PayPalGateway::ID );
do_action( 'woocommerce_paypal_payments_cart_button_render' );
},
20
);

View file

@ -4,6 +4,9 @@ import UpdateCart from '../../../ppcp-button/resources/js/modules/Helper/UpdateC
import ErrorHandler from '../../../ppcp-button/resources/js/modules/ErrorHandler';
import SimulateCart from "../../../ppcp-button/resources/js/modules/Helper/SimulateCart";
import onApprove from '../../../ppcp-button/resources/js/modules/OnApproveHandler/onApproveForContinue';
import CheckoutActionHandler
from "../../../ppcp-button/resources/js/modules/ActionHandler/CheckoutActionHandler";
import Spinner from "../../../ppcp-button/resources/js/modules/Helper/Spinner";
class GooglepayManager {
@ -155,40 +158,71 @@ class GooglepayManager {
document.querySelector('.woocommerce-notices-wrapper')
);
// == product page ==
function form() {
return document.querySelector('form.cart');
}
const actionHandler = new SingleProductActionHandler(
null,
null,
form(),
errorHandler,
);
const hasSubscriptions = PayPalCommerceGateway.data_client_id.has_subscriptions
&& PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled;
const products = hasSubscriptions
? actionHandler.getSubscriptionProducts()
: actionHandler.getProducts();
//== cart & checkout page ==
return new Promise((resolve, reject) => {
(new SimulateCart(
this.ppcpConfig.ajax.simulate_cart.endpoint,
this.ppcpConfig.ajax.simulate_cart.nonce,
)).simulate((data) => {
resolve({
countryCode: data.country_code,
currencyCode: data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.total_str // Your amount
fetch(
this.ppcpConfig.ajax.cart_script_params.endpoint,
{
method: 'GET',
credentials: 'same-origin',
}
)
.then(result => result.json())
.then(result => {
if (! result.success) {
return;
}
// handle script reload
const data = result.data;
resolve({
countryCode: 'US', // data.country_code,
currencyCode: 'USD', // data.currency_code,
totalPriceStatus: 'FINAL',
totalPrice: data.amount // Your amount
});
});
}, products);
});
// == product page ==
// function form() {
// return document.querySelector('form.cart');
// }
//
// const actionHandler = new SingleProductActionHandler(
// null,
// null,
// form(),
// errorHandler,
// );
//
// const hasSubscriptions = PayPalCommerceGateway.data_client_id.has_subscriptions
// && PayPalCommerceGateway.data_client_id.paypal_subscriptions_enabled;
//
// const products = hasSubscriptions
// ? actionHandler.getSubscriptionProducts()
// : actionHandler.getProducts();
//
// return new Promise((resolve, reject) => {
// (new SimulateCart(
// this.ppcpConfig.ajax.simulate_cart.endpoint,
// this.ppcpConfig.ajax.simulate_cart.nonce,
// )).simulate((data) => {
//
// resolve({
// countryCode: data.country_code,
// currencyCode: data.currency_code,
// totalPriceStatus: 'FINAL',
// totalPrice: data.total_str // Your amount
// });
//
// }, products);
// });
//-------------
}
@ -227,35 +261,43 @@ class GooglepayManager {
document.querySelector('.woocommerce-notices-wrapper')
);
// == checkout page ==
const spinner = new Spinner();
const actionHandler = new CheckoutActionHandler(
this.ppcpConfig,
errorHandler,
spinner
);
let id = await actionHandler.configuration().createOrder(null, null);
// == cart page ==
// const actionHandler = new CartActionHandler(
// this.ppcpConfig,
// errorHandler,
// );
//
// let id = await actionHandler.configuration().createOrder(null, null);
// == product page ==
// function form() {
// return document.querySelector('form.cart');
// }
// const actionHandler = new SingleProductActionHandler(
// this.ppcpConfig,
// new UpdateCart(
// this.ppcpConfig.ajax.change_cart.endpoint,
// this.ppcpConfig.ajax.change_cart.nonce,
// ),
// form(),
// errorHandler,
// );
//
// const createOrderInPayPal = actionHandler.createOrder();
//
// let id = await createOrderInPayPal(null, null);
// == product page ==
function form() {
return document.querySelector('form.cart');
}
const actionHandler = new SingleProductActionHandler(
this.ppcpConfig,
new UpdateCart(
this.ppcpConfig.ajax.change_cart.endpoint,
this.ppcpConfig.ajax.change_cart.nonce,
),
form(),
errorHandler,
);
const createOrderInPayPal = actionHandler.createOrder();
let id = await createOrderInPayPal(null, null);
console.log('PayPal Order ID:', id);
//-------------

View file

@ -114,8 +114,9 @@ class GooglepayButton implements ButtonInterface {
* @return bool
*/
public function render_buttons(): bool {
$button_enabled_product = $this->settings->has( 'googlepay_button_enabled_product' ) ? $this->settings->get( 'googlepay_button_enabled_product' ) : false;
$button_enabled_cart = $this->settings->has( 'googlepay_button_enabled_cart' ) ? $this->settings->get( 'googlepay_button_enabled_cart' ) : false;
$button_enabled_product = $this->settings->has( 'googlepay_button_enabled_product' ) ? $this->settings->get( 'googlepay_button_enabled_product' ) : false;
$button_enabled_cart = $this->settings->has( 'googlepay_button_enabled_cart' ) ? $this->settings->get( 'googlepay_button_enabled_cart' ) : false;
$button_enabled_checkout = true; // TODO: change enable / disable checks
/**
* Param types removed to avoid third-party issues.
@ -131,8 +132,9 @@ class GooglepayButton implements ButtonInterface {
);
if ( $button_enabled_product ) {
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_googlepay_render_hook_product', 'woocommerce_single_product_summary' );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : 'woocommerce_single_product_summary';
$default_hookname = 'woocommerce_paypal_payments_single_product_button_render';
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_googlepay_render_hook_product', $default_hookname );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : $default_hookname;
add_action(
$render_placeholder,
function () {
@ -158,6 +160,18 @@ class GooglepayButton implements ButtonInterface {
);
}
if ( $button_enabled_checkout ) {
$render_placeholder = apply_filters( 'woocommerce_paypal_payments_googlepay_render_hook_checkout', 'woocommerce_review_order_after_payment' );
$render_placeholder = is_string( $render_placeholder ) ? $render_placeholder : 'woocommerce_review_order_after_payment';
add_action(
$render_placeholder,
function () {
$this->googlepay_button();
},
21
);
}
return true;
}