From 52c3cc72a89d03e924e6d57528bf3251220aec0f Mon Sep 17 00:00:00 2001 From: dinamiko Date: Tue, 12 Jul 2022 14:44:08 +0200 Subject: [PATCH] Open payer action in modal window --- modules/ppcp-wc-gateway/resources/js/oxxo.js | 15 +++++++ modules/ppcp-wc-gateway/services.php | 7 ++- .../ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php | 45 ++++++++++++++++++- .../Gateway/PayUponInvoice/PayUponInvoice.php | 18 ++++++-- modules/ppcp-wc-gateway/webpack.config.js | 1 + 5 files changed, 79 insertions(+), 7 deletions(-) create mode 100644 modules/ppcp-wc-gateway/resources/js/oxxo.js diff --git a/modules/ppcp-wc-gateway/resources/js/oxxo.js b/modules/ppcp-wc-gateway/resources/js/oxxo.js new file mode 100644 index 000000000..e2b9cb254 --- /dev/null +++ b/modules/ppcp-wc-gateway/resources/js/oxxo.js @@ -0,0 +1,15 @@ +window.addEventListener('load', function() { + const oxxoButton = document.getElementById('ppcp-oxxo-payer-action'); + if(oxxoButton) { + oxxoButton.addEventListener('click', (event) => { + event.preventDefault(); + window.open( + oxxoButton.href, + '_blank', + 'popup' + ); + }); + + window.open(oxxoButton.href); + } +}); diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 6eb0cc5c2..1f865d484 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -2226,12 +2226,15 @@ return array( $container->get( 'wcgateway.current-ppcp-settings-page-id' ), $container->get( 'wcgateway.pay-upon-invoice-product-status' ), $container->get( 'wcgateway.pay-upon-invoice-helper' ), - $container->get( 'wcgateway.checkout-helper' ) + $container->get( 'wcgateway.checkout-helper' ), + $container->get( 'api.factory.capture' ) ); }, 'wcgateway.oxxo' => static function( ContainerInterface $container ): OXXO { return new OXXO( - $container->get( 'wcgateway.checkout-helper' ) + $container->get( 'wcgateway.checkout-helper' ), + $container->get( 'wcgateway.url' ), + $container->get( 'ppcp.asset-version' ) ); }, 'wcgateway.oxxo-gateway' => static function( ContainerInterface $container ): OXXOGateway { diff --git a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php index e1769860e..eadb41835 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php +++ b/modules/ppcp-wc-gateway/src/Gateway/OXXO/OXXO.php @@ -24,14 +24,36 @@ class OXXO { */ protected $checkout_helper; + /** + * The module URL. + * + * @var string + */ + protected $module_url; + + /** + * The asset version. + * + * @var string + */ + protected $asset_version; + /** * OXXO constructor. * * @param CheckoutHelper $checkout_helper The checkout helper. + * @param string $module_url The module URL. + * @param string $asset_version The asset version. */ - public function __construct( CheckoutHelper $checkout_helper ) { + public function __construct( + CheckoutHelper $checkout_helper, + string $module_url, + string $asset_version + ) { $this->checkout_helper = $checkout_helper; + $this->module_url = $module_url; + $this->asset_version = $asset_version; } /** @@ -58,7 +80,7 @@ class OXXO { $button = ''; if ( $payer_action ) { - $button = '

See OXXO Voucher/Ticket

'; + $button = '

See OXXO Voucher/Ticket

'; } return $message . ' ' . $button; @@ -66,6 +88,11 @@ class OXXO { 10, 2 ); + + add_action( + 'wp_enqueue_scripts', + array( $this, 'register_assets' ) + ); } /** @@ -89,4 +116,18 @@ class OXXO { return true; } + + public function register_assets(): void { + $gateway_settings = get_option( 'woocommerce_ppcp-oxxo-gateway_settings' ); + $gateway_enabled = $gateway_settings['enabled'] ?? ''; + if ( $gateway_enabled === 'yes' && is_checkout() && !empty( is_wc_endpoint_url('order-received') ) ) { + wp_enqueue_script( + 'ppcp-pay-upon-invoice', + trailingslashit($this->module_url) . 'assets/js/oxxo.js', + array(), + $this->asset_version, + true + ); + } + } } diff --git a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php index 00290ebec..6ac81f118 100644 --- a/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php +++ b/modules/ppcp-wc-gateway/src/Gateway/PayUponInvoice/PayUponInvoice.php @@ -117,10 +117,19 @@ class PayUponInvoice { protected $pui_product_status; /** + * The checkout helper. + * * @var CheckoutHelper */ protected $checkout_helper; + /** + * The capture factory. + * + * @var CaptureFactory + */ + protected $capture_factory; + /** * PayUponInvoice constructor. * @@ -136,7 +145,8 @@ class PayUponInvoice { * @param string $current_ppcp_settings_page_id Current PayPal settings page id. * @param PayUponInvoiceProductStatus $pui_product_status The PUI product status. * @param PayUponInvoiceHelper $pui_helper The PUI helper. - * @param CheckoutHelper $checkout_helper The checkout helper. + * @param CheckoutHelper $checkout_helper The checkout helper. + * @param CaptureFactory $capture_factory The capture factory. */ public function __construct( string $module_url, @@ -151,7 +161,8 @@ class PayUponInvoice { string $current_ppcp_settings_page_id, PayUponInvoiceProductStatus $pui_product_status, PayUponInvoiceHelper $pui_helper, - CheckoutHelper $checkout_helper + CheckoutHelper $checkout_helper, + CaptureFactory $capture_factory ) { $this->module_url = $module_url; $this->fraud_net = $fraud_net; @@ -165,7 +176,8 @@ class PayUponInvoice { $this->current_ppcp_settings_page_id = $current_ppcp_settings_page_id; $this->pui_product_status = $pui_product_status; $this->pui_helper = $pui_helper; - $this->checkout_helper = $checkout_helper; + $this->checkout_helper = $checkout_helper; + $this->capture_factory = $capture_factory; } /** diff --git a/modules/ppcp-wc-gateway/webpack.config.js b/modules/ppcp-wc-gateway/webpack.config.js index 5abb1f94a..d66aad695 100644 --- a/modules/ppcp-wc-gateway/webpack.config.js +++ b/modules/ppcp-wc-gateway/webpack.config.js @@ -8,6 +8,7 @@ module.exports = { entry: { 'gateway-settings': path.resolve('./resources/js/gateway-settings.js'), 'pay-upon-invoice': path.resolve('./resources/js/pay-upon-invoice.js'), + 'oxxo': path.resolve('./resources/js/oxxo.js'), }, output: { path: path.resolve(__dirname, 'assets/'),