DataTransferObjects
4 years ago
Exceptions
3 years ago
Migrations
4 years ago
Models
3 years ago
PayPalCheckoutSdk
3 years ago
Repositories
3 years ago
Webhooks
4 years ago
AccountAdminNotices.php
4 years ago
AdminSettingFields.php
3 years ago
AdvancedCardFields.php
4 years ago
AjaxRequestHandler.php
3 years ago
DonationDetailsPage.php
4 years ago
DonationFormPaymentMethod.php
4 years ago
PayPalClient.php
3 years ago
PayPalCommerce.php
3 years ago
RefreshToken.php
3 years ago
RefundPaymentHandler.php
4 years ago
ScriptLoader.php
3 years ago
Utils.php
4 years ago
onBoardingRedirectHandler.php
3 years ago
onBoardingRedirectHandler.php
487 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\Framework\Exceptions\Primitives\Exception as GiveException; |
| 7 | use Give\Log\Log; |
| 8 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 9 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 10 | use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalAuth; |
| 11 | use Give\PaymentGateways\PayPalCommerce\Repositories\Settings; |
| 12 | use Give\PaymentGateways\PayPalCommerce\Repositories\Webhooks; |
| 13 | use Give_Admin_Settings; |
| 14 | |
| 15 | /** |
| 16 | * Class PayPalOnBoardingRedirectHandler |
| 17 | * @package Give\PaymentGateways\PayPalCommerce |
| 18 | * |
| 19 | * @since 2.9.0 |
| 20 | */ |
| 21 | class onBoardingRedirectHandler |
| 22 | { |
| 23 | /** |
| 24 | * @since 2.9.0 |
| 25 | * |
| 26 | * @var PayPalAuth |
| 27 | */ |
| 28 | private $payPalAuth; |
| 29 | |
| 30 | /** |
| 31 | * @since 2.9.0 |
| 32 | * |
| 33 | * @var Webhooks |
| 34 | */ |
| 35 | private $webhooksRepository; |
| 36 | |
| 37 | /** |
| 38 | * @since 2.9.0 |
| 39 | * |
| 40 | * @var MerchantDetails |
| 41 | */ |
| 42 | private $merchantRepository; |
| 43 | |
| 44 | /** |
| 45 | * @since 2.9.0 |
| 46 | * |
| 47 | * @var Settings |
| 48 | */ |
| 49 | private $settings; |
| 50 | |
| 51 | /** |
| 52 | * onBoardingRedirectHandler constructor. |
| 53 | * |
| 54 | * @since 2.9.0 |
| 55 | * |
| 56 | * @param Webhooks $webhooks |
| 57 | * @param MerchantDetails $merchantRepository |
| 58 | * @param Settings $settings |
| 59 | * @param PayPalAuth $payPalAuth |
| 60 | */ |
| 61 | public function __construct( |
| 62 | Webhooks $webhooks, |
| 63 | MerchantDetails $merchantRepository, |
| 64 | Settings $settings, |
| 65 | PayPalAuth $payPalAuth |
| 66 | ) { |
| 67 | $this->webhooksRepository = $webhooks; |
| 68 | $this->merchantRepository = $merchantRepository; |
| 69 | $this->settings = $settings; |
| 70 | $this->payPalAuth = $payPalAuth; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Bootstrap class |
| 75 | * |
| 76 | * @since 2.9.0 |
| 77 | */ |
| 78 | public function boot() |
| 79 | { |
| 80 | if ($this->isPayPalUserRedirected()) { |
| 81 | $details = $this->savePayPalMerchantDetails(); |
| 82 | $this->setUpWebhook($details); |
| 83 | $this->redirectAccountConnected(); |
| 84 | } |
| 85 | |
| 86 | if ($this->isPayPalAccountDetailsSaved()) { |
| 87 | $this->registerPayPalSSLNotice(); |
| 88 | $this->registerPayPalAccountConnectedNotice(); |
| 89 | } |
| 90 | |
| 91 | if ($this->isStatusRefresh()) { |
| 92 | $this->refreshAccountStatus(); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Save PayPal merchant details |
| 98 | * |
| 99 | * @since 2.25.0 Handle exception. |
| 100 | * @since 2.9.0 |
| 101 | * |
| 102 | * @return MerchantDetail |
| 103 | */ |
| 104 | private function savePayPalMerchantDetails() |
| 105 | { |
| 106 | $paypalGetData = wp_parse_args($_SERVER['QUERY_STRING']); |
| 107 | $partnerLinkInfo = $this->settings->getPartnerLinkDetails(); |
| 108 | $tokenInfo = $this->settings->getAccessToken(); |
| 109 | |
| 110 | $allowedPayPalData = [ |
| 111 | 'merchantId', |
| 112 | 'merchantIdInPayPal', |
| 113 | ]; |
| 114 | |
| 115 | $payPalAccount = array_intersect_key($paypalGetData, array_flip($allowedPayPalData)); |
| 116 | |
| 117 | if (! array_key_exists('merchantIdInPayPal', $payPalAccount) || empty($payPalAccount['merchantIdInPayPal'])) { |
| 118 | $errors[] = [ |
| 119 | 'type' => 'url', |
| 120 | 'message' => esc_html__( |
| 121 | 'There was a problem with PayPal return url and we could not find valid merchant ID. Paypal return URL is:', |
| 122 | 'give' |
| 123 | ) . "\n", |
| 124 | 'value' => urlencode($_SERVER['QUERY_STRING']), |
| 125 | ]; |
| 126 | |
| 127 | $this->merchantRepository->saveAccountErrors($errors); |
| 128 | $this->redirectWhenOnBoardingFail(); |
| 129 | } |
| 130 | |
| 131 | $restApiCredentials = (array)$this->payPalAuth->getSellerRestAPICredentials( |
| 132 | $tokenInfo ? $tokenInfo['accessToken'] : '' |
| 133 | ); |
| 134 | $this->didWeGetValidSellerRestApiCredentials($restApiCredentials); |
| 135 | |
| 136 | try { |
| 137 | $tokenInfo = $this->payPalAuth->getTokenFromClientCredentials( |
| 138 | $restApiCredentials['client_id'], |
| 139 | $restApiCredentials['client_secret'] |
| 140 | ); |
| 141 | } catch (GiveException $e) { |
| 142 | give(Log::class)->warning( |
| 143 | 'PayPal Commerce: Error retrieving access token on boarding redirect.', |
| 144 | [ |
| 145 | 'category' => 'Payment Gateway', |
| 146 | 'source' => 'Paypal Commerce', |
| 147 | 'exception' => $e, |
| 148 | ] |
| 149 | ); |
| 150 | |
| 151 | $errors[] = esc_html__( |
| 152 | 'There was a problem with retrieving PayPal access token. Please try again or contact support.', |
| 153 | 'give' |
| 154 | ); |
| 155 | |
| 156 | $this->merchantRepository->saveAccountErrors($errors); |
| 157 | $this->redirectWhenOnBoardingFail(); |
| 158 | } |
| 159 | |
| 160 | $payPalAccount['clientId'] = $restApiCredentials['client_id']; |
| 161 | $payPalAccount['clientSecret'] = $restApiCredentials['client_secret']; |
| 162 | $payPalAccount['token'] = $tokenInfo; |
| 163 | $payPalAccount['supportsCustomPayments'] = 'PPCP' === $partnerLinkInfo['product']; |
| 164 | $payPalAccount['accountIsReady'] = true; |
| 165 | $payPalAccount['accountCountry'] = $this->settings->getAccountCountry(); |
| 166 | |
| 167 | $merchantDetails = MerchantDetail::fromArray($payPalAccount); |
| 168 | $this->merchantRepository->save($merchantDetails); |
| 169 | |
| 170 | // Preserve the seller access token. |
| 171 | // This is required to get the merchant rest api credentials. |
| 172 | $this->settings->updateSellerAccessToken($this->settings->getAccessToken()); |
| 173 | |
| 174 | $this->deleteTempOptions(); |
| 175 | |
| 176 | return $merchantDetails; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Redirects the user to the account connected url |
| 181 | * |
| 182 | * @since 2.9.0 |
| 183 | */ |
| 184 | private function redirectAccountConnected() |
| 185 | { |
| 186 | $this->refreshAccountStatus(); |
| 187 | |
| 188 | wp_redirect( |
| 189 | admin_url( |
| 190 | 'edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=paypal&group=paypal-commerce&paypal-commerce-account-connected=1' |
| 191 | ) |
| 192 | ); |
| 193 | |
| 194 | exit(); |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Sets up the webhook for the connected account |
| 199 | * |
| 200 | * @since 2.9.0 |
| 201 | * |
| 202 | * @param MerchantDetail $merchant_details |
| 203 | */ |
| 204 | private function setUpWebhook(MerchantDetail $merchant_details) |
| 205 | { |
| 206 | if (! is_ssl()) { |
| 207 | return; |
| 208 | } |
| 209 | |
| 210 | try { |
| 211 | $webhookConfig = $this->webhooksRepository->createWebhook($merchant_details->accessToken); |
| 212 | $this->webhooksRepository->saveWebhookConfig($webhookConfig); |
| 213 | } catch (Exception $ex) { |
| 214 | $errors[] = esc_html__( |
| 215 | 'There was a problem with creating webhook on PayPal. A gateway error log also added to get details information about PayPal response.', |
| 216 | 'give' |
| 217 | ); |
| 218 | |
| 219 | $this->merchantRepository->saveAccountErrors($errors); |
| 220 | $this->redirectWhenOnBoardingFail(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Delete temp data |
| 226 | * |
| 227 | * @since 2.9.0 |
| 228 | * @return void |
| 229 | */ |
| 230 | private function deleteTempOptions() |
| 231 | { |
| 232 | $this->settings->deletePartnerLinkDetails(); |
| 233 | $this->settings->deleteAccessToken(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Register notice if account connect success fully. |
| 238 | * |
| 239 | * @since 2.9.0 |
| 240 | */ |
| 241 | private function registerPayPalAccountConnectedNotice() |
| 242 | { |
| 243 | Give_Admin_Settings::add_message( |
| 244 | 'paypal-commerce-account-connected', |
| 245 | esc_html__('PayPal account connected successfully.', 'give') |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Returns whether or not the current request is for refreshing the account status |
| 251 | * |
| 252 | * @since 2.9.0 |
| 253 | * |
| 254 | * @return bool |
| 255 | */ |
| 256 | private function isStatusRefresh() |
| 257 | { |
| 258 | return isset($_GET['paypalStatusCheck']) && Give_Admin_Settings::is_setting_page('gateways', 'paypal'); |
| 259 | } |
| 260 | |
| 261 | /** |
| 262 | * Return whether or not PayPal user redirect to GiveWP setting page after successful onboarding. |
| 263 | * |
| 264 | * @since 2.9.0 |
| 265 | * |
| 266 | * @return bool |
| 267 | */ |
| 268 | private function isPayPalUserRedirected() |
| 269 | { |
| 270 | return isset($_GET['merchantIdInPayPal']) && Give_Admin_Settings::is_setting_page('gateways', 'paypal'); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Return whether or not PayPal account details saved. |
| 275 | * |
| 276 | * @since 2.9.0 |
| 277 | * |
| 278 | * @return bool |
| 279 | */ |
| 280 | private function isPayPalAccountDetailsSaved() |
| 281 | { |
| 282 | return isset($_GET['paypal-commerce-account-connected']) && Give_Admin_Settings::is_setting_page( |
| 283 | 'gateways', |
| 284 | 'paypal' |
| 285 | ); |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * validate rest api credential. |
| 290 | * |
| 291 | * @since 2.9.0 |
| 292 | * |
| 293 | * @param array $array |
| 294 | * |
| 295 | */ |
| 296 | private function didWeGetValidSellerRestApiCredentials($array) |
| 297 | { |
| 298 | $required = ['client_id', 'client_secret']; |
| 299 | $array = array_filter($array); // Remove empty values. |
| 300 | |
| 301 | if (array_diff($required, array_keys($array))) { |
| 302 | $errors[] = [ |
| 303 | 'type' => 'json', |
| 304 | 'message' => esc_html__('PayPal client access token API request response is:', 'give'), |
| 305 | 'value' => wp_json_encode($this->settings->getAccessToken()), |
| 306 | ]; |
| 307 | |
| 308 | $errors[] = [ |
| 309 | 'type' => 'json', |
| 310 | 'message' => esc_html__('PayPal client rest api credentials API request response is:', 'give'), |
| 311 | 'value' => wp_json_encode($array), |
| 312 | ]; |
| 313 | |
| 314 | $errors[] = esc_html__( |
| 315 | 'There was a problem with PayPal client rest API request and we could not find valid client id and secret.', |
| 316 | 'give' |
| 317 | ); |
| 318 | |
| 319 | $this->merchantRepository->saveAccountErrors($errors); |
| 320 | $this->redirectWhenOnBoardingFail(); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Handles the request for refreshing the account status |
| 326 | * |
| 327 | * @since 2.9.0 |
| 328 | */ |
| 329 | private function refreshAccountStatus() |
| 330 | { |
| 331 | $merchantDetails = $this->merchantRepository->getDetails(); |
| 332 | |
| 333 | $statusErrors = $this->isAdminSuccessfullyOnBoarded( |
| 334 | $merchantDetails->merchantIdInPayPal, |
| 335 | $merchantDetails->accessToken, |
| 336 | $merchantDetails->supportsCustomPayments |
| 337 | ); |
| 338 | if ($statusErrors !== true) { |
| 339 | $merchantDetails->accountIsReady = false; |
| 340 | $this->merchantRepository->saveAccountErrors($statusErrors); |
| 341 | } else { |
| 342 | $merchantDetails->accountIsReady = true; |
| 343 | $this->merchantRepository->deleteAccountErrors(); |
| 344 | } |
| 345 | |
| 346 | $this->merchantRepository->save($merchantDetails); |
| 347 | } |
| 348 | |
| 349 | /** |
| 350 | * Validate seller on Boarding status |
| 351 | * |
| 352 | * @since 2.9.0 |
| 353 | * |
| 354 | * @param string $merchantId |
| 355 | * @param string $accessToken |
| 356 | * @param bool $usesCustomPayments |
| 357 | * |
| 358 | * @return true|string[] |
| 359 | */ |
| 360 | private function isAdminSuccessfullyOnBoarded($merchantId, $accessToken, $usesCustomPayments) |
| 361 | { |
| 362 | $onBoardedData = (array)$this->payPalAuth->getSellerOnBoardingDetailsFromPayPal($merchantId, $accessToken); |
| 363 | $onBoardedData = array_filter($onBoardedData); // Remove empty values. |
| 364 | $errorMessages[] = [ |
| 365 | 'type' => 'json', |
| 366 | 'message' => esc_html__('PayPal merchant status check API request response is:', 'give'), |
| 367 | 'value' => wp_json_encode($onBoardedData), |
| 368 | ]; |
| 369 | |
| 370 | if (! is_ssl()) { |
| 371 | $errorMessages[] = esc_html__( |
| 372 | 'A valid SSL certificate is required to accept donations and set up your PayPal account. Once a |
| 373 | certificate is installed and the site is using https, please disconnect and reconnect your account.', |
| 374 | 'give' |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | if (array_diff(['payments_receivable', 'primary_email_confirmed'], array_keys($onBoardedData))) { |
| 379 | $errorMessages[] = esc_html__( |
| 380 | 'There was a problem with the status check for your account. Please try disconnecting and connecting again. If the problem persists, please contact support.', |
| 381 | 'give' |
| 382 | ); |
| 383 | |
| 384 | // Return here since the rest of the validations will definitely fail |
| 385 | return $errorMessages; |
| 386 | } |
| 387 | |
| 388 | if (! $onBoardedData['payments_receivable']) { |
| 389 | $errorMessages[] = esc_html__('Set up an account to receive payment from PayPal', 'give'); |
| 390 | } |
| 391 | |
| 392 | if (! $onBoardedData['primary_email_confirmed']) { |
| 393 | $errorMessage[] = esc_html__('Confirm your primary email address', 'give'); |
| 394 | } |
| 395 | |
| 396 | if (! $usesCustomPayments) { |
| 397 | return count($errorMessages) > 1 ? $errorMessages : true; |
| 398 | } |
| 399 | |
| 400 | if (array_diff(['products', 'capabilities'], array_keys($onBoardedData))) { |
| 401 | $errorMessages[] = esc_html__( |
| 402 | 'Your account was expected to be able to accept custom payments, but is not. Please make sure your |
| 403 | account country matches the country setting. If the problem persists, please contact PayPal.', |
| 404 | 'give' |
| 405 | ); |
| 406 | |
| 407 | // Return here since the rest of the validations will definitely fail |
| 408 | return $errorMessages; |
| 409 | } |
| 410 | |
| 411 | // Grab the PPCP_CUSTOM product from the status data |
| 412 | $customProduct = current( |
| 413 | array_filter( |
| 414 | $onBoardedData['products'], |
| 415 | function ($product) { |
| 416 | return $product['name'] === 'PPCP_CUSTOM'; |
| 417 | } |
| 418 | ) |
| 419 | ); |
| 420 | |
| 421 | if (empty($customProduct) || $customProduct['vetting_status'] !== 'SUBSCRIBED') { |
| 422 | $errorMessages[] = esc_html__('Reach out to PayPal to enable PPCP_CUSTOM for your account', 'give'); |
| 423 | } |
| 424 | |
| 425 | // Loop through the capabilities and see if any are not active |
| 426 | $invalidCapabilities = []; |
| 427 | foreach ($onBoardedData['capabilities'] as $capability) { |
| 428 | if ($capability['status'] !== 'ACTIVE') { |
| 429 | $invalidCapabilities[] = $capability['name']; |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if (! empty($invalidCapabilities)) { |
| 434 | $errorMessages[] = esc_html__( |
| 435 | 'Reach out to PayPal to resolve the following capabilities:', |
| 436 | 'give' |
| 437 | ) . ' ' . implode(', ', $invalidCapabilities); |
| 438 | } |
| 439 | |
| 440 | // If there were errors then redirect the user with notices |
| 441 | return count($errorMessages) > 1 ? $errorMessages : true; |
| 442 | } |
| 443 | |
| 444 | /** |
| 445 | * Redirect admin to setting section with error. |
| 446 | * |
| 447 | * @since 2.9.0 |
| 448 | */ |
| 449 | private function redirectWhenOnBoardingFail() |
| 450 | { |
| 451 | wp_redirect( |
| 452 | admin_url( |
| 453 | 'edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=paypal&group=paypal-commerce&paypal-error=1' |
| 454 | ) |
| 455 | ); |
| 456 | |
| 457 | exit(); |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Displays a notice of the site is not using SSL |
| 462 | * |
| 463 | * @since 2.9.0 |
| 464 | */ |
| 465 | private function registerPayPalSSLNotice() |
| 466 | { |
| 467 | if (is_ssl() && empty($this->webhooksRepository->getWebhookConfig())) { |
| 468 | $logLink = sprintf( |
| 469 | '<a href="%1$s">%2$s</a>', |
| 470 | admin_url('/edit.php?post_type=give_forms&page=give-tools&tab=logs'), |
| 471 | esc_html__('logs data', 'give') |
| 472 | ); |
| 473 | |
| 474 | Give_Admin_Settings::add_error( |
| 475 | 'paypal-webhook-error', |
| 476 | sprintf( |
| 477 | esc_html__( |
| 478 | 'There was a problem setting up the webhooks for your PayPal account. Please try disconnecting and reconnecting your PayPal account. If the problem persists, please contact support and provide them with the latest %1$s', |
| 479 | 'give' |
| 480 | ), |
| 481 | $logLink |
| 482 | ) |
| 483 | ); |
| 484 | } |
| 485 | } |
| 486 | } |
| 487 |