| 1 |
<?php |
| 2 |
|
| 3 |
namespace Payplug\PayplugWoocommerce\Admin; |
| 4 |
|
| 5 |
// Exit if accessed directly |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
use Exception; |
| 11 |
use Payplug\Payplug; |
| 12 |
use Payplug\PayplugWoocommerce\Gateway\PayplugGateway; |
| 13 |
use Payplug\PayplugWoocommerce\Traits\GatewayGetter; |
| 14 |
use Payplug\PayplugWoocommerce\Traits\ServiceGetter; |
| 15 |
|
| 16 |
/** |
| 17 |
* PayPlug Setup Callback Handler |
| 18 |
* Handles the callback after a user has been authenticated on the PayPlug Portal |
| 19 |
*/ |
| 20 |
class SetupCallback |
| 21 |
{ |
| 22 |
use ServiceGetter; |
| 23 |
use GatewayGetter; |
| 24 |
|
| 25 |
/** |
| 26 |
* Initialize the handler |
| 27 |
*/ |
| 28 |
public function __construct() |
| 29 |
{ |
| 30 |
add_action('admin_init', [$this, 'handle_setup_callback'], 5); |
| 31 |
add_action('admin_init', [$this, 'handle_oauth_callback'], 5); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Handle the setup callback from PayPlug Portal |
| 36 |
*/ |
| 37 |
public function handle_setup_callback(): void |
| 38 |
{ |
| 39 |
// Check if we're on the PayPlug settings page |
| 40 |
if (!$this->is_payplug_settings_page()) { |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
// Check if we have the client_id and company_id parameters |
| 45 |
if (isset($_GET['client_id']) && isset($_GET['company_id'])) { |
| 46 |
$client_id = sanitize_text_field($_GET['client_id']); |
| 47 |
$company_id = sanitize_text_field($_GET['company_id']); |
| 48 |
$this |
| 49 |
->get_gateway('account') |
| 50 |
->initialize_jwt($client_id, $company_id); |
| 51 |
} |
| 52 |
|
| 53 |
// Display success message if setup was successful |
| 54 |
if (isset($_GET['setup_success']) && $_GET['setup_success'] == '1') { |
| 55 |
add_action('admin_notices', function (): void { |
| 56 |
echo '<div class="notice notice-success is-dismissible">' . |
| 57 |
'<p>' . esc_html__('PayPlug account successfully connected!', 'payplug') . '</p>' . |
| 58 |
'</div>'; |
| 59 |
}); |
| 60 |
} |
| 61 |
|
| 62 |
// Display error message if setup failed |
| 63 |
if (isset($_GET['setup_error'])) { |
| 64 |
add_action('admin_notices', function (): void { |
| 65 |
$error_type = sanitize_text_field($_GET['setup_error']); |
| 66 |
$error_message = ''; |
| 67 |
|
| 68 |
switch ($error_type) { |
| 69 |
case 'oauth_init': |
| 70 |
$error_message = __('Failed to initiate OAuth authentication. Please try again.', 'payplug'); |
| 71 |
break; |
| 72 |
case 'token_exchange': |
| 73 |
$error_message = __('Failed to exchange authorization code for access token.', 'payplug'); |
| 74 |
break; |
| 75 |
default: |
| 76 |
$error_message = __('An error occurred during authentication.', 'payplug'); |
| 77 |
} |
| 78 |
|
| 79 |
echo '<div class="notice notice-error is-dismissible">' . |
| 80 |
'<p>' . esc_html($error_message) . '</p>' . |
| 81 |
'</div>'; |
| 82 |
}); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Handle the OAuth callback |
| 88 |
*/ |
| 89 |
public function handle_oauth_callback(): void |
| 90 |
{ |
| 91 |
// Only handle the callback when we're on the PayPlug settings page — prevents |
| 92 |
// intercepting OAuth callbacks from other plugins (e.g. Google Site Kit) that |
| 93 |
// also use a ?code= parameter on wp-admin pages. |
| 94 |
if (!$this->is_payplug_settings_page() || !current_user_can('manage_woocommerce')) { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
$account_gateway = $this->get_gateway('account'); |
| 99 |
if (!isset($_GET['code'])) { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
// Bail if no OAuth flow was initiated by PayPlug (missing stored verifier). |
| 104 |
if (empty($this->get_configuration()->get_option('oauth_code_verifier'))) { |
| 105 |
return; |
| 106 |
} |
| 107 |
$register_jwt = false; |
| 108 |
|
| 109 |
try { |
| 110 |
$authorization_code = sanitize_text_field($_GET['code']); |
| 111 |
$register_jwt = $account_gateway->register_jwt((string) $authorization_code); |
| 112 |
if ($register_jwt) { |
| 113 |
PayplugGateway::log('Successfully obtained and stored access token'); |
| 114 |
} |
| 115 |
} catch (Exception $e) { |
| 116 |
PayplugGateway::log(sprintf('Token exchange error: %s', $e->getMessage()), 'error'); |
| 117 |
} |
| 118 |
|
| 119 |
if (!$register_jwt) { |
| 120 |
$this->get_configuration()->clean_option(); |
| 121 |
} |
| 122 |
|
| 123 |
// Redirect to success page |
| 124 |
wp_safe_redirect(admin_url('admin.php?page=wc-settings&tab=checkout§ion=payplug')); |
| 125 |
exit; |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Check if we're on the PayPlug settings page |
| 130 |
*/ |
| 131 |
private function is_payplug_settings_page() |
| 132 |
{ |
| 133 |
return is_admin() && |
| 134 |
isset($_GET['page']) && $_GET['page'] === 'wc-settings' && |
| 135 |
isset($_GET['tab']) && $_GET['tab'] === 'checkout' && |
| 136 |
isset($_GET['section']) && $_GET['section'] === 'payplug'; |
| 137 |
} |
| 138 |
} |
| 139 |
|