Skip to content

Commit

Permalink
explicitly catch unhandled IPN
Browse files Browse the repository at this point in the history
  • Loading branch information
Resonious committed Feb 2, 2024
1 parent 0e5ab66 commit dd4476f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
21 changes: 11 additions & 10 deletions includes/class-wc-gateway-komoju-ipn-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'])) {
Expand All @@ -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)
Expand All @@ -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]);
}

Expand Down
14 changes: 12 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit dd4476f

Please sign in to comment.