Banners
2 years ago
DataTransferObjects
4 years ago
Exceptions
3 years ago
Migrations
2 years ago
Models
2 years ago
PayPalCheckoutSdk
2 years ago
Repositories
2 years ago
Webhooks
2 years ago
AccountAdminNotices.php
4 years ago
AdminSettingFields.php
2 years ago
AdvancedCardFields.php
4 years ago
AjaxRequestHandler.php
2 years ago
DonationDetailsPage.php
4 years ago
DonationFormPaymentMethod.php
2 years ago
PayPalClient.php
2 years ago
PayPalCommerce.php
2 years ago
RefreshToken.php
2 years ago
RefundPaymentHandler.php
4 years ago
ScriptLoader.php
2 years ago
Utils.php
2 years ago
onBoardingRedirectHandler.php
2 years ago
AdminSettingFields.php
514 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce; |
| 4 | |
| 5 | use Give\PaymentGateways\PayPalCommerce\Banners\PayPalDonationsSettingPageBanner; |
| 6 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 7 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 8 | use Give\PaymentGateways\PayPalCommerce\Repositories\Settings; |
| 9 | use Give_HTML_Elements; |
| 10 | use Give_License; |
| 11 | |
| 12 | /** |
| 13 | * Class AdminSettingFields |
| 14 | * @package Give\PaymentGateways\PayPalCommerce |
| 15 | * |
| 16 | * @since 2.9.0 |
| 17 | */ |
| 18 | class AdminSettingFields |
| 19 | { |
| 20 | /** |
| 21 | * @var MerchantDetail |
| 22 | */ |
| 23 | private $merchantModel; |
| 24 | |
| 25 | /** |
| 26 | * @var Settings |
| 27 | */ |
| 28 | private $settingRepository; |
| 29 | |
| 30 | /** |
| 31 | * @var MerchantDetails |
| 32 | */ |
| 33 | private $merchantRepository; |
| 34 | |
| 35 | /** |
| 36 | * AdminSettingFields constructor. |
| 37 | * |
| 38 | * @param MerchantDetail $merchantDetail |
| 39 | * @param MerchantDetails $merchantDetailRepository |
| 40 | * @param Settings $settings |
| 41 | */ |
| 42 | public function __construct( |
| 43 | MerchantDetail $merchantDetail, |
| 44 | MerchantDetails $merchantDetailRepository, |
| 45 | Settings $settings |
| 46 | ) { |
| 47 | $this->merchantModel = $merchantDetail; |
| 48 | $this->merchantRepository = $merchantDetailRepository; |
| 49 | $this->settingRepository = $settings; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Bootstrap fields. |
| 54 | * |
| 55 | * @since 2.9.0 |
| 56 | */ |
| 57 | public function boot() |
| 58 | { |
| 59 | add_action('give_admin_field_paypal_commerce_account_manger', [$this, 'payPalCommerceAccountManagerField']); |
| 60 | add_action('give_admin_field_paypal_commerce_account_country', [$this, 'accountCountryField']); |
| 61 | add_action('give_admin_field_paypal_commerce_introduction', [$this, 'introductionSection']); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Render account country field. |
| 66 | * |
| 67 | * @since 2.9.0 |
| 68 | */ |
| 69 | public function accountCountryField() |
| 70 | { |
| 71 | /* @var Give_HTML_Elements $htmlElements */ |
| 72 | $htmlElements = give('html'); |
| 73 | |
| 74 | $settingHtml = $htmlElements->select( |
| 75 | [ |
| 76 | 'id' => 'paypal_commerce_account_country', |
| 77 | 'options' => give_get_country_list(), |
| 78 | 'chosen' => true, |
| 79 | 'placeholder' => esc_html__('Choose a country', 'give'), |
| 80 | 'show_option_all' => false, |
| 81 | 'show_option_none' => false, |
| 82 | 'data' => [ |
| 83 | 'search-type' => 'no_ajax', |
| 84 | ], |
| 85 | 'selected' => $this->merchantModel->accountCountry ?: $this->settingRepository->getAccountCountry(), |
| 86 | ] |
| 87 | ); |
| 88 | ?> |
| 89 | <tr valign="top" class="js-fields-has-custom-saving-logic"> |
| 90 | <th scope="row" class="titledesc"> |
| 91 | <label for="give_paypal_commerce_country"><?php |
| 92 | esc_html_e('Account Country', 'give'); ?></label> |
| 93 | </th> |
| 94 | <td class="give-forminp"> |
| 95 | <?php |
| 96 | printf( |
| 97 | '%1$s<div class="give-field-description">%2$s</div>', |
| 98 | $settingHtml, |
| 99 | esc_html__('The country the PayPal account is based from. Make sure to select before connecting.', 'give') |
| 100 | ) |
| 101 | ?> |
| 102 | </td> |
| 103 | </tr> |
| 104 | <?php |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * PayPal Checkout account manager custom field |
| 109 | * |
| 110 | * @since 3.0.0 Update PayPal sandbox connection button description. |
| 111 | * @since 2.9.0 |
| 112 | */ |
| 113 | public function payPalCommerceAccountManagerField() |
| 114 | { |
| 115 | $recurringAddonInfo = Give_License::get_plugin_by_slug('give-recurring'); |
| 116 | $isRecurringAddonActive = isset($recurringAddonInfo['Status']) && 'active' === $recurringAddonInfo['Status']; |
| 117 | |
| 118 | // Show live PayPal connect button. |
| 119 | $paypalLiveSetting = new \stdClass(); |
| 120 | $paypalLiveSetting->label = esc_html__('PayPal Connection', 'give'); |
| 121 | $paypalLiveSetting->mode = 'live'; |
| 122 | $paypalLiveSetting->connectButtonLabel = esc_html__('Connect with PayPal Live', 'give'); |
| 123 | $paypalLiveSetting->description = esc_html__('PayPal is currently NOT connected.', 'give'); |
| 124 | $paypalLiveSetting->isRecurringAddonActive = $isRecurringAddonActive; |
| 125 | echo $this->getPayPalConnectionSettingView($paypalLiveSetting); |
| 126 | |
| 127 | // Show sandbox PayPal connect button. |
| 128 | $paypalSandboxSetting = new \stdClass(); |
| 129 | $paypalSandboxSetting->label = esc_html__('PayPal Sandbox Connection', 'give'); |
| 130 | $paypalSandboxSetting->mode = 'sandbox'; |
| 131 | $paypalSandboxSetting->connectButtonLabel = esc_html__('Connect with PayPal Sandbox', 'give'); |
| 132 | $paypalSandboxSetting->description = sprintf( |
| 133 | '%1$s <a href="%2$s" target="_blank">%3$s</a> <strong>%4$s</strong>', |
| 134 | esc_html__('PayPal sandbox is currently NOT connected.', 'give'), |
| 135 | esc_url('https://docs.givewp.com/paypal-sandbox-setup'), |
| 136 | esc_html__('Set up a separate PayPal Sandbox account for testing.', 'give'), |
| 137 | esc_html__('Live PayPal accounts will not work.', 'give') |
| 138 | ); |
| 139 | $paypalSandboxSetting->isRecurringAddonActive = $isRecurringAddonActive; |
| 140 | |
| 141 | echo $this->getPayPalConnectionSettingView($paypalSandboxSetting); |
| 142 | |
| 143 | echo $this->getBanner(); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * PayPal Commerce introduction section. |
| 148 | * |
| 149 | * @since 2.9.0 |
| 150 | */ |
| 151 | public function introductionSection() |
| 152 | { |
| 153 | ?> |
| 154 | <div id="give-paypal-commerce-introduction-wrap"> |
| 155 | <div class="hero-section"> |
| 156 | <div> |
| 157 | <h2><?php |
| 158 | esc_html_e('Accept Donations with PayPal Donations', 'give'); ?></h2> |
| 159 | <p class="give-field-description"> |
| 160 | <?php |
| 161 | esc_html_e( |
| 162 | 'Allow your donors to give using Debit or Credit Cards directly on your website with no additional fees.', |
| 163 | 'give' |
| 164 | ); |
| 165 | ?> |
| 166 | </p> |
| 167 | </div> |
| 168 | <div class="paypal-logo"> |
| 169 | <img src="<?php echo GIVE_PLUGIN_URL . '/assets/dist/images/admin/paypal-logo.svg'; ?>" |
| 170 | width="316" |
| 171 | height="84" |
| 172 | alt="<?php esc_attr_e('PayPal Logo Image', 'give'); ?>"> |
| 173 | </div> |
| 174 | </div> |
| 175 | <div class="feature-list"> |
| 176 | <div><i class="fa fa-angle-right"></i><?php |
| 177 | esc_html_e('Credit and Debit Card Donations', 'give'); ?> |
| 178 | </div> |
| 179 | <div> |
| 180 | <i class="fa fa-angle-right"></i><?php |
| 181 | esc_html_e('Improve donation conversion rates', 'give'); ?> |
| 182 | </div> |
| 183 | <div><i class="fa fa-angle-right"></i><?php |
| 184 | esc_html_e('Easy no-API key connection', 'give'); ?></div> |
| 185 | <div> |
| 186 | <i class="fa fa-angle-right"></i><?php |
| 187 | esc_html_e('Accept payments from around the world', 'give'); ?> |
| 188 | </div> |
| 189 | <div><i class="fa fa-angle-right"></i><?php |
| 190 | esc_html_e('Donate via PayPal accounts', 'give'); ?> |
| 191 | </div> |
| 192 | <div><i class="fa fa-angle-right"></i><?php |
| 193 | esc_html_e('Supports 3D Secure payments', 'give'); ?> |
| 194 | </div> |
| 195 | </div> |
| 196 | </div> |
| 197 | <?php |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Return whether or not country is in North America |
| 202 | */ |
| 203 | private function isCountryInNorthAmerica(): bool |
| 204 | { |
| 205 | // Countries list: https://en.wikipedia.org/wiki/List_of_North_American_countries_by_area#Countries |
| 206 | $northAmericaCountryList = [ |
| 207 | 'CA', // Canada |
| 208 | 'US', // United States |
| 209 | 'MX', // Mexico |
| 210 | 'NI', // Nicaragua |
| 211 | 'HN', // Honduras |
| 212 | 'CU', // Cuba |
| 213 | 'GT', // Guatemala |
| 214 | 'PA', // Panama |
| 215 | 'CR', // Costa Rica |
| 216 | 'DO', // Dominican Republic |
| 217 | 'HT', // Haiti |
| 218 | 'BZ', // Belize |
| 219 | 'SV', // EL Salvador |
| 220 | 'BS', // The Bahamas |
| 221 | 'JM', // Jamaica |
| 222 | 'TT', // Trinidad and Tobago |
| 223 | 'DM', // Dominica |
| 224 | 'LC', // Saint Lucia |
| 225 | 'AG', // Antigua and Barbuda |
| 226 | 'BB', // Barbados |
| 227 | 'VC', // Saint Vincent and the Grenadines |
| 228 | 'GD', // Grenada |
| 229 | 'KN', // Saint Kitts and Nevis |
| 230 | ]; |
| 231 | |
| 232 | $accountCountry = $this->settingRepository->getAccountCountry(); |
| 233 | |
| 234 | return in_array($accountCountry, $northAmericaCountryList, true); |
| 235 | } |
| 236 | |
| 237 | /** |
| 238 | * Return admin guidance notice to fix PayPal on boarding error. |
| 239 | * |
| 240 | * @since 2.9.6 |
| 241 | * |
| 242 | * @param bool $completeMessage |
| 243 | * |
| 244 | * @return string |
| 245 | */ |
| 246 | public function getAdminGuidanceNotice($completeMessage = true) |
| 247 | { |
| 248 | if ($this->isCountryInNorthAmerica()) { |
| 249 | $telephone = sprintf( |
| 250 | '<a href="tel:%1$s">%1$s</a>', |
| 251 | '1-888-221-1161' |
| 252 | ); |
| 253 | |
| 254 | $message = sprintf( |
| 255 | esc_html__('Please call a PayPal support representative at %1$s', 'give'), |
| 256 | $telephone |
| 257 | ); |
| 258 | } else { |
| 259 | $message = esc_html__( |
| 260 | 'Please reach out to PayPal support from your PayPal account Resolution Center', |
| 261 | 'give' |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | $message .= $completeMessage ? esc_html__(' and relay the following message:', 'give') : '.'; |
| 266 | |
| 267 | return $message; |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Print on boarding errors. |
| 272 | * |
| 273 | * @since 2.9.6 |
| 274 | */ |
| 275 | private function printErrors(MerchantDetails $merchantDetailsRepository) |
| 276 | { |
| 277 | $accountErrors = $merchantDetailsRepository->getAccountErrors(); |
| 278 | |
| 279 | if (! empty($accountErrors)) : |
| 280 | ?> |
| 281 | <div> |
| 282 | <p class="error-message"><?php esc_html_e('Warning, your account is not ready to accept donations.', 'give'); ?></p> |
| 283 | <p> |
| 284 | <?php |
| 285 | printf( |
| 286 | '%1$s %2$s', |
| 287 | esc_html__( |
| 288 | 'There is an issue with your PayPal account that is preventing you from being able to accept donations.', |
| 289 | 'give' |
| 290 | ), |
| 291 | $this->getAdminGuidanceNotice() |
| 292 | ) |
| 293 | ?> |
| 294 | </p> |
| 295 | <div class="paypal-message-template"> |
| 296 | <?php esc_html_e('Greetings!', 'give'); ?><br><br> |
| 297 | <?php esc_html_e( |
| 298 | 'I am trying to connect my PayPal account to the GiveWP plugin for WordPress. I have gone through the onboarding process to connect my account, but when I finish I\'m given the following message from GiveWP:', |
| 299 | 'give' |
| 300 | ); |
| 301 | ?><br> |
| 302 | <?php echo $this->formatErrors($accountErrors); ?> |
| 303 | <br> |
| 304 | <?php |
| 305 | esc_html_e( |
| 306 | 'Please help me resolve these account errors so I can begin accepting payments via PayPal on GiveWP.', |
| 307 | 'give' |
| 308 | ); |
| 309 | ?> |
| 310 | </div> |
| 311 | |
| 312 | <?php if ($this->merchantRepository->accountIsConnected()) :?> |
| 313 | <?php |
| 314 | $reCheckAccountStatusUrl = add_query_arg( |
| 315 | [ |
| 316 | 'post_type' => 'give_forms', |
| 317 | 'page' => 'give-settings', |
| 318 | 'tab' => 'gateways', |
| 319 | 'section' => 'paypal', |
| 320 | 'group' => 'paypal-commerce', |
| 321 | 'paypalStatusCheck' => '1', |
| 322 | 'mode' => $merchantDetailsRepository->getMode() |
| 323 | ], |
| 324 | admin_url('edit.php') |
| 325 | ); |
| 326 | ?> |
| 327 | <p> |
| 328 | <a href="<?php echo $reCheckAccountStatusUrl; ?>"> |
| 329 | <?php esc_html_e('Re-Check Account Status', 'give'); ?> |
| 330 | </a> |
| 331 | </p> |
| 332 | <?php endif; ?> |
| 333 | |
| 334 | </div> |
| 335 | <?php endif; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Return format errors string. |
| 340 | * |
| 341 | * @since 2.9.6 |
| 342 | * |
| 343 | * @param array $errors |
| 344 | * |
| 345 | * @return string |
| 346 | */ |
| 347 | private function formatErrors($errors) |
| 348 | { |
| 349 | $isSingleError = ! (count($errors) > 1); |
| 350 | $formattedArray = array_map( |
| 351 | static function ($arr) use ($isSingleError) { |
| 352 | if (is_array($arr)) { |
| 353 | switch ($arr['type']) { |
| 354 | case 'url': |
| 355 | return sprintf( |
| 356 | '<%1$s>%2$s<br><code>%3$s</code></%1$s>', |
| 357 | $isSingleError ? 'p' : 'li', |
| 358 | $arr['message'], |
| 359 | urldecode_deep($arr['value']) |
| 360 | ); |
| 361 | |
| 362 | case 'json': |
| 363 | return sprintf( |
| 364 | '<%1$s>%2$s<br><code>%3$s</code></%1$s>', |
| 365 | $isSingleError ? 'p' : 'li', |
| 366 | $arr['message'], |
| 367 | $arr['value'] |
| 368 | ); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | return sprintf( |
| 373 | '<%1$s>%2$s</%1$s>', |
| 374 | $isSingleError ? 'p' : 'li', |
| 375 | $arr |
| 376 | ); |
| 377 | }, |
| 378 | $errors |
| 379 | ); |
| 380 | |
| 381 | $output = implode('', $formattedArray); |
| 382 | |
| 383 | if (! $isSingleError) { |
| 384 | $output = sprintf( |
| 385 | '<ul class="ul-disc">%1$s</ul>', |
| 386 | $output |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | return $output; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * This function return html for "PayPal Connection" and "PayPal Sandbox Connection". |
| 395 | * |
| 396 | * @param \stdClass $paypalSetting PayPal setting data. |
| 397 | */ |
| 398 | private function getPayPalConnectionSettingView(\stdClass $paypalSetting): string |
| 399 | { |
| 400 | ob_start(); |
| 401 | |
| 402 | /** @var MerchantDetails $mechantDetailsRepository */ |
| 403 | $mechantDetailsRepository = give(MerchantDetails::class); |
| 404 | $mechantDetailsRepository->setMode($paypalSetting->mode); |
| 405 | |
| 406 | $merchantDetail = $mechantDetailsRepository->getDetails(); |
| 407 | |
| 408 | $canShowAccountInformation = $mechantDetailsRepository->accountIsConnected(); |
| 409 | ?> |
| 410 | <tr> |
| 411 | <th scope="row" class="titledesc"> |
| 412 | <label for="give_paypal_commerce_country"> |
| 413 | <?php echo $paypalSetting->label ?> |
| 414 | </label> |
| 415 | </th> |
| 416 | <td class="give-forminp"> |
| 417 | <div class="give-paypal-commerce-account-manager-field-wrap"> |
| 418 | <div class="connect-button-wrap"> |
| 419 | <div |
| 420 | class="button-wrap connection-setting<?php echo $canShowAccountInformation ? ' give-hidden' : ''; ?>"> |
| 421 | <div> |
| 422 | <button class="button button-primary button-large js-give-paypal-on-boarding-handler" |
| 423 | data-mode="<?php echo $paypalSetting->mode; ?>"> |
| 424 | <i class="fab fa-paypal"></i> |
| 425 | <?php echo $paypalSetting->connectButtonLabel; ?> |
| 426 | </button> |
| 427 | <?php if ('live' === $paypalSetting->mode) : ?> |
| 428 | <span class="tooltip"> |
| 429 | <span class="left-arrow"></span> |
| 430 | <?php esc_html_e('Click to get started!', 'give'); ?> |
| 431 | </span> |
| 432 | <?php // We are using one PayPal button to handle both sandbox and live mode connection.?> |
| 433 | <a class="give-hidden" target="PPFrame" |
| 434 | data-paypal-onboard-complete="givePayPalOnBoardedCallback" href="#" |
| 435 | data-paypal-button="true"> |
| 436 | <?php esc_html_e('Sign up for PayPal', 'give'); ?> |
| 437 | </a> |
| 438 | <?php endif; ?> |
| 439 | </div> |
| 440 | <span class="give-field-description"> |
| 441 | <i class="fa fa-exclamation"></i><?php echo $paypalSetting->description ?> |
| 442 | </span> |
| 443 | </div> |
| 444 | <div |
| 445 | class="button-wrap disconnection-setting<?php echo ! $canShowAccountInformation ? ' give-hidden' : ''; ?>"> |
| 446 | <div> |
| 447 | <button class="button button-large disabled" disabled="disabled"> |
| 448 | <i class="fab fa-paypal"></i> <?php |
| 449 | esc_html_e('Connected', 'give'); ?> |
| 450 | </button> |
| 451 | </div> |
| 452 | <div> |
| 453 | <span class="give-field-description"> |
| 454 | <i class="fa fa-check"></i> |
| 455 | <?php |
| 456 | if ($merchantDetail->accountIsReady) { |
| 457 | $connectedAccountTypeMessage = $merchantDetail->supportsCustomPayments |
| 458 | ? esc_html__('Connected as Advanced for payments as', 'give') |
| 459 | : esc_html__('Connected as Standard for payments as', 'give'); |
| 460 | } else { |
| 461 | $connectedAccountTypeMessage = esc_html__('Connected for payments as', 'give'); |
| 462 | } |
| 463 | |
| 464 | printf( |
| 465 | '%1$s <span class="paypal-account-email">%2$s</span>', |
| 466 | $connectedAccountTypeMessage, |
| 467 | $merchantDetail->merchantId |
| 468 | ); |
| 469 | ?> |
| 470 | </span> |
| 471 | <span class="actions"> |
| 472 | <button |
| 473 | class="js-give-paypal-disconnect-paypal-account" |
| 474 | data-mode="<?php echo $paypalSetting->mode; ?>"> |
| 475 | <?php esc_html_e('Disconnect', 'give'); ?> |
| 476 | </button> |
| 477 | </span> |
| 478 | </div> |
| 479 | </div> |
| 480 | <?php $this->printErrors($mechantDetailsRepository); ?> |
| 481 | </div> |
| 482 | </div> |
| 483 | </td> |
| 484 | </tr> |
| 485 | <?php |
| 486 | return ob_get_clean(); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * @since 2.33.0 |
| 491 | */ |
| 492 | private function getBanner(): string |
| 493 | { |
| 494 | ob_start(); |
| 495 | |
| 496 | if ( |
| 497 | give(MerchantDetail::class)->accountIsReady |
| 498 | && give_is_gateway_active(PayPalCommerce::id()) |
| 499 | ) { |
| 500 | return ''; |
| 501 | } |
| 502 | ?> |
| 503 | <tr> |
| 504 | <th scope="row" class="titledesc"> |
| 505 | </th> |
| 506 | <td class="give-forminp"> |
| 507 | <?php echo give(PayPalDonationsSettingPageBanner::class)->render(); ?> |
| 508 | </td> |
| 509 | </tr> |
| 510 | <?php |
| 511 | return ob_get_clean(); |
| 512 | } |
| 513 | } |
| 514 |