get(); $testAccountInfo = self::getAccountInfo($settings, 'test'); $liveAccountInfo = self::getAccountInfo($settings, 'live'); $testConnectRedirect = admin_url('?fluent-cart=fluent_cart_payment_authenticate&payment_method=paypal&type=connect&mode=test'); $liveConnectRedirect = admin_url('?fluent-cart=fluent_cart_payment_authenticate&payment_method=paypal&type=connect&mode=live'); return [ 'connect_config' => [ 'test_redirect' => $testConnectRedirect, 'live_redirect' => $liveConnectRedirect, 'image_url' => Vite::getAssetUrl('images/payment-methods/paypal-icon.svg'), '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') ], 'test_account' => $testAccountInfo, 'live_account' => $liveAccountInfo, 'settings' => $settings ]; } public static function parseConnectInfos($vendorData) { if (!$vendorData || !Arr::get($vendorData, 'permissionsGranted')) { echo '
' . esc_html(__('Invalid PayPal Request. Please try configuring paypal payment gateway again!', 'fluent-cart')) . '
'; die(); } $settingsInstance = App::gateway('paypal')->settings; $mode = Arr::get($vendorData, 'mode'); /* * @todo will verify later * we need to verify merchant manually after they create account * // start verifications if merchant able to receive payments $sellerAccessToken = fluent_cart_get_option('_paypal_partner_connect_access_token_' . Arr::get($vendorData, 'mode')); $accountData = (new PayPalPartner($mode))->verifyMerchant(Arr::get($sellerAccessToken, 'access_token'), Arr::get($vendorData, 'merchantIdInPayPal')); if (is_wp_error($accountData)) { echo '
' . esc_html($accountData->get_error_message()) . '
'; } if (!Arr::get($accountData, 'payments_receivable')) { echo '

Attention: You currently cannot receive payments due to restriction on your PayPal account. Please reach out to PayPal Customer Support or connect to https://www.paypal.com for more information.

'; die(); } if (!Arr::get($accountData, 'primary_email_confirmed')) { echo '

Attention: Please confirm your email address on https://www.paypal.com/businessprofile/settings in order to receive payments! You currently cannot receive payments.

'; die(); } * */ // update all data that verified $data = [ $mode . '_email_address' => sanitize_text_field(Arr::get($vendorData, 'merchantId')), $mode . '_account_status' => sanitize_text_field(Arr::get($vendorData, 'accountStatus')), ]; $settingsInstance->updateNonSensitiveData($data); wp_redirect(admin_url('admin.php?page=fluent-cart#/settings/payments/paypal')); } public function getSellerAuthToken(Request $request) { $authCode = $request->getSafe('authCode', 'sanitize_text_field'); $clientId = $request->getSafe('sharedId', 'sanitize_text_field'); $mode = $request->getSafe('mode', 'sanitize_text_field'); $paypalConnect = new PayPalPartner($mode); try { $response = $paypalConnect->exchangeAuthCode($clientId, $authCode); if (isset($response['access_token'])) { $endpoint = "/v1/customer/partners/" . $paypalConnect->getPartnerId() . "/merchant-integrations/credentials/"; // retrieve the merchant client ID and client secret $credentials = $paypalConnect->makeMerchantRequest($endpoint, $response['access_token']); if (is_wp_error($credentials)) { wp_send_json([ 'message' => $credentials->get_error_message() ], 422); } if (is_array($credentials)) { static::prepareSettingToSave($credentials, $mode); } //update the existing access token for after redirect verification fluent_cart_update_option('_paypal_partner_connect_access_token_' . $mode, $response); } if (isset($response['error'])) { throw new \Exception($response['error_description']); } } catch (\Exception $exception) { // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped throw new \Exception($exception->getMessage()); } } public static function prepareSettingToSave($credentials, $mode = 'live') { $clientId = Arr::get($credentials, 'client_id'); $clientSecret = Arr::get($credentials, 'client_secret'); $data = [ $mode . '_client_id' => $clientId, $mode . '_client_secret' => $clientSecret, $mode . '_account_id' => Arr::get($credentials, 'payer_id'), 'provider' => 'connect', 'is_active' => 'yes', 'payment_mode' => $mode ]; $payPalInstance = App::gateway('paypal'); $oldSettings = $payPalInstance->settings->get(); $settingsData = wp_parse_args($data, $oldSettings); $payPalInstance->updateSettings($settingsData); //create webhook and setup endpoints (new Webhook())->registerWebhook($mode); } private static function getAccountInfo($settings, $mode) { if (Arr::get($settings, 'provider') !== 'connect') { return false; } return API::retrieveAccount($settings, $mode); } public static function disconnect($mode, $sendResponse = true) { $payPalInstance = App::gateway('paypal'); $paypalSettings = $payPalInstance->settings->get(); if (empty($paypalSettings[$mode . '_account_id'])) { if ($sendResponse) { wp_send_json([ 'message' => __('Selected Account does not exist', 'fluent-cart') ], 422); } return false; } $paypalSettings[$mode . '_account_id'] = ''; $paypalSettings[$mode . '_publishable_key'] = ''; $paypalSettings[$mode . '_secret_key'] = ''; $paypalSettings[$mode . '_webhook_events'] = []; $paypalSettings[$mode . '_webhook_id'] = ''; $paypalSettings[$mode . '_client_id'] = ''; $paypalSettings[$mode . '_client_secret'] = ''; if ($mode == 'live') { $alternateMode = 'test'; } else { $alternateMode = 'live'; } if (empty($paypalSettings[$alternateMode . '_account_id'])) { $paypalSettings['is_active'] = 'no'; $paypalSettings['payment_mode'] = 'test'; } else { $paypalSettings['payment_mode'] = $alternateMode; } $sendResponse = $payPalInstance->updateSettings($paypalSettings); if ($sendResponse) { wp_send_json([ 'message' => __('PayPal settings has been disconnected', 'fluent-cart'), 'settings' => $paypalSettings ], 200); } return true; } }