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