| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Modules\PaymentMethods\StripeGateway\Connect; |
| 4 |
|
| 5 |
use FluentCart\App\Helpers\Helper; |
| 6 |
use FluentCart\App\Modules\PaymentMethods\Core\GatewayManager; |
| 7 |
use FluentCart\App\Modules\PaymentMethods\StripeGateway\API\Account; |
| 8 |
use FluentCart\App\Modules\PaymentMethods\StripeGateway\Stripe; |
| 9 |
use FluentCart\App\Modules\PaymentMethods\StripeGateway\StripeSettingsBase; |
| 10 |
use FluentCart\App\Modules\PaymentMethods\StripeGateway\Webhook\Webhook; |
| 11 |
use FluentCart\App\Vite; |
| 12 |
use FluentCart\Framework\Support\Arr; |
| 13 |
|
| 14 |
class ConnectConfig |
| 15 |
{ |
| 16 |
private static $connectBase = 'https://api.fluentcart.com/connect/'; |
| 17 |
|
| 18 |
public static function handleConnect($data) |
| 19 |
{ |
| 20 |
// Permission check - only administrators can perform Stripe connect |
| 21 |
if (!current_user_can('manage_options')) { |
| 22 |
wp_die(__('You do not have permission to perform this action.', 'fluent-cart'), __('Security Check Failed', 'fluent-cart'), ['response' => 403]); |
| 23 |
} |
| 24 |
|
| 25 |
$intent = Arr::get($data, 'intent', ''); |
| 26 |
|
| 27 |
// Nonce verification for connect intent (initiated from admin) |
| 28 |
if ($intent == 'connect') { |
| 29 |
if (!isset($_REQUEST['_wpnonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_wpnonce'])), 'fluentcart')) { |
| 30 |
wp_die(__('Security check failed. Please try again.', 'fluent-cart'), __('Security Check Failed', 'fluent-cart'), ['response' => 403]); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
$stripeSettings = new StripeSettingsBase(); |
| 35 |
|
| 36 |
$mode = Arr::get($data, 'mode', '') === 'test' ? 'test' : 'live'; |
| 37 |
|
| 38 |
if (!$intent) { |
| 39 |
wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/stripe')); |
| 40 |
exit; |
| 41 |
} |
| 42 |
|
| 43 |
if ($intent == 'connect') { |
| 44 |
if ($stripeSettings->getApiKey($mode)) { |
| 45 |
// already connected |
| 46 |
wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/stripe')); |
| 47 |
exit; |
| 48 |
} |
| 49 |
|
| 50 |
$connectHash = md5('stripe_connect_' . $mode . '_' . wp_generate_uuid4()); |
| 51 |
$prevSettings = $stripeSettings->get(); |
| 52 |
$prevSettings[$mode . '_connect_hash'] = $connectHash; |
| 53 |
$stripeSettings->updateSettings($prevSettings); |
| 54 |
|
| 55 |
$redirectUrl = add_query_arg([ |
| 56 |
'mode' => $mode, |
| 57 |
'hash' => $connectHash, |
| 58 |
'url_base' => home_url('?fluent-cart=stripe_connect') |
| 59 |
], self::$connectBase); |
| 60 |
wp_redirect($redirectUrl); |
| 61 |
exit; |
| 62 |
} |
| 63 |
if ($intent == 'success') { |
| 64 |
$apiUrl = self::$connectBase . 'stripe_tokens'; |
| 65 |
|
| 66 |
$paypload = [ |
| 67 |
'hash' => $stripeSettings->get($mode . '_connect_hash'), |
| 68 |
'state' => Arr::get($data, 'state', ''), |
| 69 |
'mode' => Arr::get($data, 'mode', '') === 'live' ? 'live' : 'test', |
| 70 |
'set_webhook' => Webhook::getURL(), |
| 71 |
'code' => Arr::get($data, 'code', '') |
| 72 |
]; |
| 73 |
|
| 74 |
$response = wp_remote_request($apiUrl, [ |
| 75 |
'method' => 'POST', |
| 76 |
'timeout' => 45, |
| 77 |
'redirection' => 5, |
| 78 |
'blocking' => true, |
| 79 |
'body' => $paypload, |
| 80 |
'cookies' => [] |
| 81 |
]); |
| 82 |
|
| 83 |
if (!is_wp_error($response)) { |
| 84 |
$responseBody = json_decode(wp_remote_retrieve_body($response), true); |
| 85 |
$tokens = Arr::get($responseBody, 'tokens', []); |
| 86 |
$prevSettings = $stripeSettings->get(); |
| 87 |
if ($tokens) { |
| 88 |
$updateData = [ |
| 89 |
'is_active' => 'yes', |
| 90 |
$mode . '_account_id' => Arr::get($tokens, 'stripe_user_id', ''), |
| 91 |
$mode . '_publishable_key' => Arr::get($tokens, 'stripe_publishable_key', ''), |
| 92 |
$mode . '_secret_key' => Helper::encryptKey(Arr::get($tokens, 'access_token', '')), |
| 93 |
$mode . '_is_encrypted' => 'yes' |
| 94 |
]; |
| 95 |
$prevSettings = wp_parse_args($updateData, $prevSettings); |
| 96 |
$stripeSettings->updateSettings($prevSettings); |
| 97 |
} |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/stripe')); |
| 102 |
} |
| 103 |
|
| 104 |
public static function getConnectConfig(): array |
| 105 |
{ |
| 106 |
$settings = (new StripeSettingsBase())->get(); |
| 107 |
|
| 108 |
// Generate nonces for secure connect URLs |
| 109 |
$liveNonce = wp_create_nonce('fluentcart'); |
| 110 |
$testNonce = wp_create_nonce('fluentcart'); |
| 111 |
|
| 112 |
return [ |
| 113 |
'connect_config' => [ |
| 114 |
'live_redirect' => add_query_arg([ |
| 115 |
'fluent-cart' => 'stripe_connect', |
| 116 |
'intent' => 'connect', |
| 117 |
'mode' => 'live', |
| 118 |
'_wpnonce' => $liveNonce |
| 119 |
], home_url()), |
| 120 |
'test_redirect' => add_query_arg([ |
| 121 |
'fluent-cart' => 'stripe_connect', |
| 122 |
'intent' => 'connect', |
| 123 |
'mode' => 'test', |
| 124 |
'_wpnonce' => $testNonce |
| 125 |
], home_url()), |
| 126 |
'image_url' => Vite::getAssetUrl('images/payment-methods/stripe-icon.svg') |
| 127 |
], |
| 128 |
'test_account' => self::getAccountInfo($settings, 'test'), |
| 129 |
'live_account' => self::getAccountInfo($settings, 'live'), |
| 130 |
'settings' => $settings |
| 131 |
]; |
| 132 |
} |
| 133 |
|
| 134 |
public static function verifyAuthorizeSuccess($data) |
| 135 |
{ |
| 136 |
$response = wp_remote_post(self::$connectBase . 'stripe-verify-code', [ |
| 137 |
'method' => 'POST', |
| 138 |
'timeout' => 45, |
| 139 |
'redirection' => 5, |
| 140 |
'httpversion' => '1.0', |
| 141 |
'blocking' => true, |
| 142 |
'headers' => array(), |
| 143 |
'body' => $data, |
| 144 |
'cookies' => array() |
| 145 |
]); |
| 146 |
|
| 147 |
if (is_wp_error($response)) { |
| 148 |
$message = $response->get_error_message(); |
| 149 |
echo '<div class="fct_message fct_message_error">' . esc_html($message) . '</div>'; |
| 150 |
return; |
| 151 |
} |
| 152 |
|
| 153 |
$response = json_decode(wp_remote_retrieve_body($response), true); |
| 154 |
|
| 155 |
if (empty($response['stripe_user_id'])) { |
| 156 |
$message = Arr::get($response, 'message'); |
| 157 |
if (!$message) { |
| 158 |
$message = __('Invalid Stripe Request. Please configure stripe payment gateway again', 'fluent-cart'); |
| 159 |
} |
| 160 |
echo '<div class="fct_message fct_message_error">' . esc_html($message) . '</div>'; |
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$settings = (new StripeSettingsBase())->get(); |
| 165 |
|
| 166 |
$settings['provider'] = 'connect'; |
| 167 |
|
| 168 |
$settings['is_active'] = 'yes'; |
| 169 |
|
| 170 |
if (!empty($response['livemode'])) { |
| 171 |
$settings['payment_mode'] = 'live'; |
| 172 |
$settings['live_account_id'] = $response['stripe_user_id']; |
| 173 |
$settings['live_publishable_key'] = $response['stripe_publishable_key']; |
| 174 |
$settings['live_secret_key'] = $response['access_token']; |
| 175 |
} else { |
| 176 |
$settings['payment_mode'] = 'test'; |
| 177 |
$settings['test_account_id'] = $response['stripe_user_id']; |
| 178 |
$settings['test_publishable_key'] = $response['stripe_publishable_key']; |
| 179 |
$settings['test_secret_key'] = $response['access_token']; |
| 180 |
} |
| 181 |
|
| 182 |
(new Stripe())->updateSettings($settings); |
| 183 |
} |
| 184 |
|
| 185 |
private static function getAccountInfo($settings, $mode) |
| 186 |
{ |
| 187 |
|
| 188 |
// if ($settings['is_active'] != 'yes') { |
| 189 |
// return false; |
| 190 |
// } |
| 191 |
|
| 192 |
if ($settings['provider'] != 'connect') { |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
$apiKey = Helper::decryptKey($settings[$mode . '_secret_key']); |
| 197 |
|
| 198 |
$accountId = Arr::get($settings, $mode . '_account_id'); |
| 199 |
|
| 200 |
if (!$accountId) { |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
$account = Account::retrive($accountId, $apiKey); |
| 205 |
|
| 206 |
if (is_wp_error($account)) { |
| 207 |
return [ |
| 208 |
'error' => $account->get_error_message() |
| 209 |
]; |
| 210 |
} |
| 211 |
|
| 212 |
// Find the email. |
| 213 |
$email = isset($account->email) |
| 214 |
? esc_html($account->email) |
| 215 |
: ''; |
| 216 |
|
| 217 |
// Find a Display Name. |
| 218 |
$display_name = isset($account->display_name) |
| 219 |
? esc_html($account->display_name) |
| 220 |
: ''; |
| 221 |
|
| 222 |
if ( |
| 223 |
empty($display_name) && |
| 224 |
isset($account->settings) && |
| 225 |
isset($account->settings->dashboard) && |
| 226 |
isset($account->settings->dashboard->display_name) |
| 227 |
) { |
| 228 |
$display_name = esc_html($account->settings->dashboard->display_name); |
| 229 |
} |
| 230 |
|
| 231 |
return [ |
| 232 |
'account_id' => $accountId, |
| 233 |
'display_name' => $display_name, |
| 234 |
'email' => $email |
| 235 |
]; |
| 236 |
|
| 237 |
} |
| 238 |
|
| 239 |
public static function disconnect($mode, $sendResponse = true) |
| 240 |
{ |
| 241 |
$stripeSettings = (new StripeSettingsBase())->get(); |
| 242 |
|
| 243 |
if (empty($stripeSettings[$mode . '_account_id'])) { |
| 244 |
if ($sendResponse) { |
| 245 |
wp_send_json([ |
| 246 |
'message' => __('Selected Account does not exist', 'fluent-cart') |
| 247 |
], 422); |
| 248 |
} |
| 249 |
return false; |
| 250 |
} |
| 251 |
|
| 252 |
$stripeSettings[$mode . '_account_id'] = ''; |
| 253 |
$stripeSettings[$mode . '_publishable_key'] = ''; |
| 254 |
$stripeSettings[$mode . '_secret_key'] = ''; |
| 255 |
|
| 256 |
if ($mode == 'live') { |
| 257 |
$alternateMode = 'test'; |
| 258 |
} else { |
| 259 |
$alternateMode = 'live'; |
| 260 |
} |
| 261 |
|
| 262 |
if (empty($stripeSettings[$alternateMode . '_account_id'])) { |
| 263 |
$stripeSettings['is_active'] = 'no'; |
| 264 |
$stripeSettings['payment_mode'] = 'test'; |
| 265 |
} else { |
| 266 |
$stripeSettings['payment_mode'] = $alternateMode; |
| 267 |
} |
| 268 |
|
| 269 |
$stripe = GatewayManager::getInstance('stripe'); |
| 270 |
$sendResponse = $stripe->updateSettings($stripeSettings); |
| 271 |
|
| 272 |
if ($sendResponse) { |
| 273 |
wp_send_json([ |
| 274 |
'message' => __('Stripe settings has been disconnected', 'fluent-cart'), |
| 275 |
'settings' => $stripeSettings |
| 276 |
], 200); |
| 277 |
} |
| 278 |
|
| 279 |
return true; |
| 280 |
} |
| 281 |
} |
| 282 |
|