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