From abea82a467be8d2a540fb21b067660a23058cbd6 Mon Sep 17 00:00:00 2001 From: Nigel Baillie Date: Fri, 2 Feb 2024 10:38:30 +0900 Subject: [PATCH] explicitly catch unhandled IPN --- .../class-wc-gateway-komoju-ipn-handler.php | 21 ++++++++++--------- index.php | 14 +++++++++++-- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/includes/class-wc-gateway-komoju-ipn-handler.php b/includes/class-wc-gateway-komoju-ipn-handler.php index 5a9accf..f00ec0f 100644 --- a/includes/class-wc-gateway-komoju-ipn-handler.php +++ b/includes/class-wc-gateway-komoju-ipn-handler.php @@ -23,21 +23,21 @@ class WC_Gateway_Komoju_IPN_Handler extends WC_Gateway_Komoju_Response */ public function __construct($gateway, $webhookSecretToken = '', $secret_key = '', $invoice_prefix = '', $useOnHold = false) { - add_action('invoke_komoju_ipn_handler', [$this, 'check_response']); + add_filter('invoke_komoju_ipn_handler', [$this, 'check_response'], 10, 1); add_action('valid_komoju_standard_ipn_request', [$this, 'valid_response']); add_action('komoju_capture_payment_async', [$this, 'payment_complete_async'], 10, 3); - $this->gateway = $gateway; - $this->webhookSecretToken = $webhookSecretToken; - $this->secret_key = $secret_key; - $this->invoice_prefix = $invoice_prefix; - $this->useOnHold = $useOnHold; + $this->gateway = $gateway; + $this->webhookSecretToken = $webhookSecretToken; + $this->secret_key = $secret_key; + $this->invoice_prefix = $invoice_prefix; + $this->useOnHold = $useOnHold; } /** * Check for Komoju IPN or Session Response */ - public function check_response() + public function check_response($_handled) { // callback from session page if (isset($_GET['session_id'])) { @@ -62,13 +62,13 @@ public function check_response() $payment_url = $order->get_checkout_payment_url(false); wp_redirect($payment_url); } - exit; + return true; } // Quick setup POST from KOMOJU if (isset($_POST['secret_key'])) { $this->quick_setup($_POST); - exit; + return true; } // Webhook (IPN) @@ -78,8 +78,9 @@ public function check_response() // NOTE: direct function call doesn't work do_action('valid_komoju_standard_ipn_request', $webhookEvent); - exit; + return true; } + wp_die('Failed to verify KOMOJU authenticity', 'Komoju IPN', ['response' => 401]); } diff --git a/index.php b/index.php index 12edee6..a38d9e8 100755 --- a/index.php +++ b/index.php @@ -78,8 +78,18 @@ function woocommerce_komoju_handle_http_request() // Force WC to load our gateway, causing WC_Gateway_Komoju_IPN_Handler to get instantiated. WC()->payment_gateways()->payment_gateways(); - // This action is registered when WC_Gateway_Komoju_IPN_Handler is instantiated. - do_action('invoke_komoju_ipn_handler'); + // When WC_Gateway_Komoju_IPN_Handler is instantiated, this filter should be registered. + $handled = apply_filters('invoke_komoju_ipn_handler', false); + + // Catch unexpected case where the filter is NOT registered + if (!$handled) { + header('X-Komoju-Error: komoju gateway not loaded'); + wp_die( + 'gateway (and thus IPN handler) not loaded', + 'KOMOJU WooCommerce plugin', + ['status' => 500] + ); + } } add_filter('woocommerce_payment_gateways', 'woocommerce_add_komoju_gateway');