PluginProbe
PayPlug for WooCommerce (Official) / trunk
PayPlug for WooCommerce (Official) vtrunk
3.0.0 2.18.0 1.0.17 1.0.18 1.0.19 1.0.20 1.0.21 1.0.22 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.1.0 1.10.0 1.10.1 1.2.1 1.2.10 1.2.11 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 All 101 releases
payplug / src / Admin / SetupCallback.php

SetupCallback.php in PayPlug for WooCommerce (Official) trunk, at src/Admin/SetupCallback.php

139 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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&section=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