Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try fixing IPN flakiness #102

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion class-wc-gateway-komoju.php
Original file line number Diff line number Diff line change
@@ -11,7 +11,7 @@
* @class WC_Gateway_Komoju
* @extends WC_Payment_Gateway
*
* @version 3.0.7
* @version 3.0.8
*
* @author Komoju
*/
2 changes: 1 addition & 1 deletion class-wc-settings-page-komoju.php
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@
* @class WC_Settings_Page_Komoju
* @extends WC_Settings_Page
*
* @version 3.0.7
* @version 3.0.8
*
* @author Komoju
*/
28 changes: 16 additions & 12 deletions includes/class-wc-gateway-komoju-ipn-handler.php
Original file line number Diff line number Diff line change
@@ -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'])) {
@@ -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)
@@ -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]);
}

22 changes: 21 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
@@ -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
*/
@@ -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);
4 changes: 4 additions & 0 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -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.

Loading