← All changes
|
app/Modules/PaymentMethods/PayPalGateway/ConnectConfig.php
+54
-59
1.4.1
→
1.6.5
View file →
| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use FluentCart\App\App; |
| 6 | 6 | use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\API; |
| 7 | 7 | use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\PayPalPartner; |
| 8 | +use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\PayPalPartnerRenderer; | |
| 8 | 9 | use FluentCart\App\Modules\PaymentMethods\PayPalGateway\API\Webhook; |
| 9 | 10 | use FluentCart\App\Vite; |
| 10 | 11 | use FluentCart\Framework\Http\Request\Request; |
| 11 | 12 | use FluentCart\Framework\Support\Arr; |
| @@ -19,16 +20,26 @@ | ||
| 19 | 20 | |
| 20 | 21 | $testAccountInfo = self::getAccountInfo($settings, 'test'); |
| 21 | 22 | $liveAccountInfo = self::getAccountInfo($settings, 'live'); |
| 22 | 23 | |
| 23 | - $testConnectRedirect = admin_url('?fluent-cart=fluent_cart_payment_authenticate&payment_method=paypal&type=connect&mode=test'); | |
| 24 | - $liveConnectRedirect = admin_url('?fluent-cart=fluent_cart_payment_authenticate&payment_method=paypal&type=connect&mode=live'); | |
| 24 | + $testConnectRedirect = add_query_arg([ | |
| 25 | + 'fluent-cart' => 'paypal_connect', | |
| 26 | + 'intent' => 'connect', | |
| 27 | + 'mode' => 'test', | |
| 28 | + '_wpnonce' => wp_create_nonce('fluent_cart_paypal_connect_test'), | |
| 29 | + ], home_url()); | |
| 30 | + $liveConnectRedirect = add_query_arg([ | |
| 31 | + 'fluent-cart' => 'paypal_connect', | |
| 32 | + 'intent' => 'connect', | |
| 33 | + 'mode' => 'live', | |
| 34 | + '_wpnonce' => wp_create_nonce('fluent_cart_paypal_connect_live'), | |
| 35 | + ], home_url()); | |
| 25 | 36 | |
| 26 | 37 | return [ |
| 27 | 38 | 'connect_config' => [ |
| 28 | 39 | 'test_redirect' => $testConnectRedirect, |
| 29 | 40 | 'live_redirect' => $liveConnectRedirect, |
| 30 | - 'image_url' => Vite::getAssetUrl('images/payment-methods/paypal-icon.png'), | |
| 41 | + 'image_url' => Vite::getAssetUrl('images/payment-methods/paypal-icon.svg'), | |
| 31 | 42 | 'disconnect_note' => __('Disconnecting your PayPal account will prevent you from offering PayPal services and products on your website. Do you wish to continue?', 'fluent-cart') |
| 32 | 43 | ], |
| 33 | 44 | 'test_account' => $testAccountInfo, |
| 34 | 45 | 'live_account' => $liveAccountInfo, |
| @@ -35,10 +46,29 @@ | ||
| 35 | 46 | 'settings' => $settings |
| 36 | 47 | ]; |
| 37 | 48 | } |
| 38 | 49 | |
| 50 | + public static function handleConnect($data): void | |
| 51 | + { | |
| 52 | + $intent = Arr::get($data, 'intent'); | |
| 53 | + if ($intent === 'return') { | |
| 54 | + self::parseConnectInfos($data); | |
| 55 | + return; | |
| 56 | + } | |
| 57 | + | |
| 58 | + if ($intent !== 'connect') { | |
| 59 | + wp_die(esc_html__('Invalid PayPal connection request.', 'fluent-cart'), '', ['response' => 400]); | |
| 60 | + } | |
| 61 | + | |
| 62 | + $mode = self::validateConnectRequest($data, 'connect'); | |
| 63 | + // WebRoutes terminates the request after dispatching this action. | |
| 64 | + (new PayPalPartnerRenderer($mode))->template($data); | |
| 65 | + } | |
| 66 | + | |
| 39 | 67 | public static function parseConnectInfos($vendorData) |
| 40 | 68 | { |
| 69 | + $mode = self::validateConnectRequest($vendorData, 'return'); | |
| 70 | + | |
| 41 | 71 | if (!$vendorData || !Arr::get($vendorData, 'permissionsGranted')) { |
| 42 | 72 | echo '<div class="fct_message fct_message_error">' . esc_html(__('Invalid PayPal Request. Please try configuring paypal payment gateway again!', 'fluent-cart')) . '</div>'; |
| 43 | 73 | die(); |
| 44 | 74 | } |
| @@ -44,10 +74,8 @@ | ||
| 44 | 74 | } |
| 45 | 75 | |
| 46 | 76 | $settingsInstance = App::gateway('paypal')->settings; |
| 47 | 77 | |
| 48 | - $mode = Arr::get($vendorData, 'mode'); | |
| 49 | - | |
| 50 | 78 | /* |
| 51 | 79 | * @todo will verify later |
| 52 | 80 | * we need to verify merchant manually after they create account |
| 53 | 81 | * |
| @@ -80,9 +108,9 @@ | ||
| 80 | 108 | |
| 81 | 109 | * |
| 82 | 110 | */ |
| 83 | 111 | |
| 84 | - // update all data that verified | |
| 112 | + // Store metadata only after authorizing the connection return. | |
| 85 | 113 | $data = [ |
| 86 | 114 | $mode . '_email_address' => sanitize_text_field(Arr::get($vendorData, 'merchantId')), |
| 87 | 115 | $mode . '_account_status' => sanitize_text_field(Arr::get($vendorData, 'accountStatus')), |
| 88 | 116 | ]; |
| @@ -90,8 +118,28 @@ | ||
| 90 | 118 | |
| 91 | 119 | wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/paypal')); |
| 92 | 120 | } |
| 93 | 121 | |
| 122 | + public static function validateConnectRequest($data, string $intent): string | |
| 123 | + { | |
| 124 | + if (!current_user_can('manage_options')) { | |
| 125 | + wp_die(esc_html__('You do not have permission to configure PayPal.', 'fluent-cart'), '', ['response' => 403]); | |
| 126 | + } | |
| 127 | + | |
| 128 | + $mode = Arr::get($data, 'mode'); | |
| 129 | + if (!in_array($mode, ['live', 'test'], true)) { | |
| 130 | + wp_die(esc_html__('Invalid PayPal payment mode.', 'fluent-cart'), '', ['response' => 400]); | |
| 131 | + } | |
| 132 | + | |
| 133 | + $nonce = Arr::get($data, '_wpnonce'); | |
| 134 | + $action = $intent === 'return' ? 'fluent_cart_paypal_connect_return_' : 'fluent_cart_paypal_connect_'; | |
| 135 | + if (!is_string($nonce) || !wp_verify_nonce(sanitize_text_field($nonce), $action . $mode)) { | |
| 136 | + wp_die(esc_html__('Security check failed. Please try connecting PayPal again.', 'fluent-cart'), '', ['response' => 403]); | |
| 137 | + } | |
| 138 | + | |
| 139 | + return $mode; | |
| 140 | + } | |
| 141 | + | |
| 94 | 142 | public function getSellerAuthToken(Request $request) |
| 95 | 143 | { |
| 96 | 144 | |
| 97 | 145 | $authCode = $request->getSafe('authCode', 'sanitize_text_field'); |
| @@ -152,61 +200,8 @@ | ||
| 152 | 200 | $payPalInstance->updateSettings($settingsData); |
| 153 | 201 | |
| 154 | 202 | //create webhook and setup endpoints |
| 155 | 203 | (new Webhook())->registerWebhook($mode); |
| 156 | - } | |
| 157 | - | |
| 158 | - public static function verifyAuthorizeSuccess($data) | |
| 159 | - { | |
| 160 | - $response = wp_remote_post(self::$connectBase . 'paypal-verify-code', [ | |
| 161 | - 'method' => 'POST', | |
| 162 | - 'timeout' => 60, | |
| 163 | - 'redirection' => 5, | |
| 164 | - 'httpversion' => '1.0', | |
| 165 | - 'sslverify' => false, | |
| 166 | - 'blocking' => true, | |
| 167 | - 'headers' => array(), | |
| 168 | - 'body' => $data, | |
| 169 | - 'cookies' => array() | |
| 170 | - ]); | |
| 171 | - | |
| 172 | - if (is_wp_error($response)) { | |
| 173 | - $message = $response->get_error_message(); | |
| 174 | - echo '<div class="fct_message fct_message_error">' . esc_html($message) . '</div>'; | |
| 175 | - return; | |
| 176 | - } | |
| 177 | - | |
| 178 | - $response = json_decode(wp_remote_retrieve_body($response), true); | |
| 179 | - | |
| 180 | - if (empty($response['paypal_user_id'])) { | |
| 181 | - $message = Arr::get($response, 'message'); | |
| 182 | - if (!$message) { | |
| 183 | - $message = __('Invalid PayPal Request. Please configure paypal payment gateway again', 'fluent-cart'); | |
| 184 | - } | |
| 185 | - echo '<div class="fct_message fct_message_error">' . esc_html($message) . '</div>'; | |
| 186 | - return; | |
| 187 | - } | |
| 188 | - | |
| 189 | - $payPalInstance = App::gateway('paypal'); | |
| 190 | - $settings = $payPalInstance->settings->get(); | |
| 191 | - | |
| 192 | - $settings['provider'] = 'connect'; | |
| 193 | - | |
| 194 | - $settings['is_active'] = 'yes'; | |
| 195 | - | |
| 196 | - if (!empty($response['livemode'])) { | |
| 197 | - $settings['payment_mode'] = 'live'; | |
| 198 | - $settings['live_account_id'] = $response['paypal_user_id']; | |
| 199 | - $settings['live_publishable_key'] = $response['paypal_publishable_key']; | |
| 200 | - $settings['live_secret_key'] = $response['access_token']; | |
| 201 | - } else { | |
| 202 | - $settings['payment_mode'] = 'test'; | |
| 203 | - $settings['test_account_id'] = $response['paypal_user_id']; | |
| 204 | - $settings['test_publishable_key'] = $response['paypal_publishable_key']; | |
| 205 | - $settings['test_secret_key'] = $response['access_token']; | |
| 206 | - } | |
| 207 | - | |
| 208 | - $payPalInstance->updateSettings($settings); | |
| 209 | 204 | } |
| 210 | 205 | |
| 211 | 206 | private static function getAccountInfo($settings, $mode) |
| 212 | 207 | { |