Skip to content

Commit

Permalink
Merge pull request #102 from degica/fix-ipn-handler-loading-issue
Browse files Browse the repository at this point in the history
Try fixing IPN flakiness
  • Loading branch information
Resonious authored Feb 6, 2024
2 parents b9a1190 + c09b4d8 commit d34f4b7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 15 deletions.
2 changes: 1 addition & 1 deletion class-wc-gateway-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @class WC_Gateway_Komoju
* @extends WC_Payment_Gateway
*
* @version 3.0.7
* @version 3.0.8
*
* @author Komoju
*/
Expand Down
2 changes: 1 addition & 1 deletion class-wc-settings-page-komoju.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* @class WC_Settings_Page_Komoju
* @extends WC_Settings_Page
*
* @version 3.0.7
* @version 3.0.8
*
* @author Komoju
*/
Expand Down
28 changes: 16 additions & 12 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('woocommerce_api_wc_gateway_komoju', [$this, 'check_response']);
add_action('valid-komoju-standard-ipn-request', [$this, 'valid_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,15 @@ 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 @@ -77,9 +79,11 @@ public function check_response()
$webhookEvent = new WC_Gateway_Komoju_Webhook_Event($entityBody);

// NOTE: direct function call doesn't work
do_action('valid-komoju-standard-ipn-request', $webhookEvent);
exit;
do_action('valid_komoju_standard_ipn_request', $webhookEvent);

return true;
}

wp_die('Failed to verify KOMOJU authenticity', 'Komoju IPN', ['response' => 401]);
}

Expand Down
22 changes: 21 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: KOMOJU Payments
Plugin URI: https://github.com/komoju/komoju-woocommerce
Description: Extends WooCommerce with KOMOJU gateway.
Version: 3.0.7
Version: 3.0.8
Author: KOMOJU
Author URI: https://komoju.com
*/
Expand Down Expand Up @@ -73,8 +73,28 @@ function woocommerce_komoju_load_script_as_module($tag, $handle, $src)
return '<script type="module" src="' . esc_attr($src) . '"></script>';
}

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();

// 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');
add_filter('woocommerce_get_settings_pages', 'woocommerce_add_komoju_settings_page');
add_action('woocommerce_api_wc_gateway_komoju', 'woocommerce_komoju_handle_http_request');

add_action('wp_enqueue_scripts', 'woocommerce_komoju_load_scripts');
add_filter('script_loader_tag', 'woocommerce_komoju_load_script_as_module', 10, 3);
Expand Down
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ Go back to your Wordpress instance and set the "Webhook Secret Token" value on t

== Changelog ==

= 3.0.8 =
Register IPN handler outside of gateway initializer.
Hopefully fixes an issue where automatic updates cause webhooks to stop working.

= 3.0.7 =
Add JA translations for plugin store page FAQ.

Expand Down

0 comments on commit d34f4b7

Please sign in to comment.