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
1 year ago
AdvancedCardFields.php
4 years ago
AjaxRequestHandler.php
1 year 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
1 year ago
ScriptLoader.php
348 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\PayPalCommerce; |
| 4 | |
| 5 | use Give\Helpers\EnqueueScript; |
| 6 | use Give\Helpers\Form\Template\Utils\Frontend as FrontendFormTemplateUtils; |
| 7 | use Give\PaymentGateways\Gateways\PayPalCommerce\PayPalCommerceGateway; |
| 8 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 9 | use Give_Admin_Settings; |
| 10 | |
| 11 | /** |
| 12 | * Class ScriptLoader |
| 13 | * @package Give\PaymentGateways\PayPalCommerce |
| 14 | * |
| 15 | * @since 3.0.0 Implement on-demand PayPal connection. Admin can select Advance or standard card processing. |
| 16 | * @since 2.9.0 |
| 17 | */ |
| 18 | class ScriptLoader |
| 19 | { |
| 20 | /** |
| 21 | * List of connection account type. |
| 22 | * |
| 23 | * @since 3.0.0 |
| 24 | * @var string[] |
| 25 | */ |
| 26 | public static $accountTypes = [ |
| 27 | 'PPCP', |
| 28 | 'EXPRESS_CHECKOUT' |
| 29 | ]; |
| 30 | |
| 31 | /** |
| 32 | * List of countries that support custom payment accounts |
| 33 | * |
| 34 | * @since 3.0.0 |
| 35 | * @var string[] |
| 36 | */ |
| 37 | private $countriesAvailableForAdvanceConnection = [ |
| 38 | 'AU', |
| 39 | 'AT', |
| 40 | 'BE', |
| 41 | 'BG', |
| 42 | 'CY', |
| 43 | 'CZ', |
| 44 | 'DK', |
| 45 | 'EE', |
| 46 | 'FI', |
| 47 | 'FR', |
| 48 | 'GR', |
| 49 | 'HU', |
| 50 | 'IT', |
| 51 | 'LV', |
| 52 | 'LI', |
| 53 | 'LT', |
| 54 | 'LU', |
| 55 | 'MT', |
| 56 | 'NL', |
| 57 | 'NO', |
| 58 | 'PL', |
| 59 | 'PT', |
| 60 | 'RO', |
| 61 | 'SK', |
| 62 | 'SI', |
| 63 | 'ES', |
| 64 | 'SE', |
| 65 | 'GB', |
| 66 | 'US', |
| 67 | ]; |
| 68 | |
| 69 | /** |
| 70 | * @since 2.9.0 |
| 71 | * |
| 72 | * @var PayPalCommerceGateway |
| 73 | */ |
| 74 | private $payPalCommerceGateway; |
| 75 | |
| 76 | /** |
| 77 | * ScriptLoader constructor. |
| 78 | * |
| 79 | * @since 3.0.0 Remove $merchantRepository parameter and add $payPalCommerceGateway parameter. |
| 80 | * @since 2.9.0 |
| 81 | * |
| 82 | * @param PayPalCommerceGateway $payPalCommerceGateway |
| 83 | */ |
| 84 | public function __construct(PayPalCommerceGateway $payPalCommerceGateway) |
| 85 | { |
| 86 | $this->payPalCommerceGateway = $payPalCommerceGateway; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Load admin scripts |
| 91 | * |
| 92 | * @since 3.13.0 Add new "keepWebhooksAfterDisconnect" string |
| 93 | * @since 2.9.0 |
| 94 | */ |
| 95 | public function loadAdminScripts() |
| 96 | { |
| 97 | if (!Give_Admin_Settings::is_setting_page('gateway', 'paypal')) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | wp_enqueue_script( |
| 102 | 'give-paypal-partner-js', |
| 103 | $this->getPartnerJsUrl(), |
| 104 | [], |
| 105 | null, |
| 106 | true |
| 107 | ); |
| 108 | |
| 109 | wp_enqueue_style( |
| 110 | 'give-admin-paypal-commerce-css', |
| 111 | GIVE_PLUGIN_URL . 'assets/dist/css/admin-paypal-commerce.css', |
| 112 | [], |
| 113 | GIVE_VERSION |
| 114 | ); |
| 115 | |
| 116 | wp_localize_script( |
| 117 | 'give-paypal-partner-js', |
| 118 | 'givePayPalCommerce', |
| 119 | [ |
| 120 | 'countriesAvailableForAdvanceConnection' => $this->countriesAvailableForAdvanceConnection, |
| 121 | 'accountTypes' => self::$accountTypes, |
| 122 | 'translations' => [ |
| 123 | 'confirmPaypalAccountDisconnection' => esc_html__('Disconnect PayPal Account', 'give'), |
| 124 | 'disconnectPayPalAccount' => esc_html__( |
| 125 | 'Are you sure you want to disconnect your PayPal account?', |
| 126 | 'give' |
| 127 | ), |
| 128 | 'keepWebhooksAfterDisconnect' => esc_html__("Continue to receive events, including subscription renewals, from the disconnected account? If you have existing recurring donations with this account, you'll want to leave this checked. This is not reversible.", |
| 129 | 'give'), |
| 130 | 'keepWebhooksAfterDisconnectLearnMore' => esc_html__('Learn why this is critically important.', |
| 131 | 'give') . ' »', |
| 132 | 'connectSuccessTitle' => esc_html__('You’re connected to PayPal! Here’s what’s next...', 'give'), |
| 133 | 'pciWarning' => sprintf( |
| 134 | __( |
| 135 | 'PayPal allows you to accept credit or debit cards directly on your website. Because of |
| 136 | this, your site needs to maintain <a href="%1$s" target="_blank">PCI-DDS compliance</a>. |
| 137 | GiveWP never stores sensitive information like card details to your server and works |
| 138 | seamlessly with SSL certificates. Compliance is comprised of, but not limited to:', |
| 139 | 'give' |
| 140 | ), |
| 141 | 'https://givewp.com/documentation/resources/pci-compliance/' |
| 142 | ), |
| 143 | 'pciComplianceInstructions' => [ |
| 144 | esc_html__( |
| 145 | 'Using a trusted, secure hosting provider – preferably one which claims and actively promotes PCI compliance.', |
| 146 | 'give' |
| 147 | ), |
| 148 | esc_html__( |
| 149 | 'Maintain security best practices when setting passwords and limit access to your server.', |
| 150 | 'give' |
| 151 | ), |
| 152 | esc_html__('Implement an SSL certificate to keep your donations secure.', 'give'), |
| 153 | esc_html__('Keep plugins up to date to ensure latest security fixes are present.', 'give'), |
| 154 | ] |
| 155 | ], |
| 156 | ] |
| 157 | ); |
| 158 | |
| 159 | $script = <<<EOT |
| 160 | function giveSandboxPayPalOnBoardedCallback(authCode, sharedId){ |
| 161 | givePayPalOnBoardedCallback('sandbox', authCode, sharedId); |
| 162 | } |
| 163 | |
| 164 | function giveLivePayPalOnBoardedCallback(authCode, sharedId){ |
| 165 | givePayPalOnBoardedCallback('live', authCode, sharedId); |
| 166 | } |
| 167 | |
| 168 | function givePayPalOnBoardedCallback(mode, authCode, sharedId) { |
| 169 | const query = '&mode=' + mode + '&authCode=' + authCode + '&sharedId=' + sharedId; |
| 170 | |
| 171 | fetch( ajaxurl + '?action=give_paypal_commerce_user_on_boarded' + query ) |
| 172 | .then(function(res){ return res.json() }) |
| 173 | .then(function(res) { |
| 174 | if ( true !== res.success ) { |
| 175 | console.log(res); |
| 176 | alert('Something went wrong!'); |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | // Remove PayPal quick help container. |
| 181 | const paypalErrorQuickHelp = document.getElementById('give-paypal-onboarding-trouble-notice'); |
| 182 | paypalErrorQuickHelp && paypalErrorQuickHelp.remove(); |
| 183 | }); |
| 184 | } |
| 185 | EOT; |
| 186 | |
| 187 | wp_add_inline_script( |
| 188 | 'give-paypal-partner-js', |
| 189 | $script |
| 190 | ); |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Load public assets. |
| 195 | * |
| 196 | * @since 3.2.0 Get form id from post content if form id is not available. |
| 197 | * @since 3.2.0 Use EnqueueScript to register and enqueue script. |
| 198 | * @since 2.32.0 Handle exception if client token is not generated. |
| 199 | * @since 2.9.0 |
| 200 | */ |
| 201 | public function loadPublicAssets() |
| 202 | { |
| 203 | $formId = FrontendFormTemplateUtils::getFormId(); |
| 204 | |
| 205 | if (! $formId) { |
| 206 | $formId = $this->getFormIdFromPostContent(); |
| 207 | } |
| 208 | |
| 209 | if ( |
| 210 | ! $formId |
| 211 | || \Give\Helpers\Form\Utils::isV3Form($formId) |
| 212 | || ! Utils::gatewayIsActive() |
| 213 | || ! Utils::isAccountReadyToAcceptPayment() |
| 214 | ) { |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | try { |
| 219 | $formSettings = $this->payPalCommerceGateway->formSettings($formId); |
| 220 | $paypalSDKOptions = $formSettings['sdkOptions']; |
| 221 | |
| 222 | // Remove v3 donation form related param. |
| 223 | unset($paypalSDKOptions['data-namespace']); |
| 224 | } catch (\Exception $e) { |
| 225 | give_set_error( |
| 226 | 'give-paypal-commerce-client-token-error', |
| 227 | sprintf( |
| 228 | esc_html__( |
| 229 | 'Unable to load PayPal Commerce client token. Please try again later. Error: %1$s', |
| 230 | 'give' |
| 231 | ), |
| 232 | $e->getMessage() |
| 233 | ) |
| 234 | ); |
| 235 | |
| 236 | return; |
| 237 | } |
| 238 | |
| 239 | /* @var MerchantDetail $merchant */ |
| 240 | $merchant = give(MerchantDetail::class); |
| 241 | |
| 242 | $scriptId = 'give-paypal-commerce-js'; |
| 243 | $data = [ |
| 244 | 'paypalCardInfoErrorPrefixes' => [ |
| 245 | 'expirationDateField' => esc_html__('Card Expiration Date:', 'give'), |
| 246 | 'cardNumberField' => esc_html__('Card Number:', 'give'), |
| 247 | 'cardCvcField' => esc_html__('Card CVC:', 'give'), |
| 248 | ], |
| 249 | 'cardFieldPlaceholders' => [ |
| 250 | 'cardNumber' => esc_html__('Card Number', 'give'), |
| 251 | 'cardCvc' => esc_html__('CVC', 'give'), |
| 252 | 'expirationDate' => esc_html__('MM/YY', 'give'), |
| 253 | ], |
| 254 | 'threeDsCardAuthenticationFailedNotice' => esc_html__( |
| 255 | 'There was a problem authenticating your payment method. Please try again. If the problem persists, please try another payment method.', |
| 256 | 'give' |
| 257 | ), |
| 258 | 'errorCodeLabel' => esc_html__('Error Code', 'give'), |
| 259 | 'genericDonorErrorMessage' => __( |
| 260 | 'There was an error processing your donation. Please contact the administrator.', |
| 261 | 'give' |
| 262 | ), |
| 263 | // List of style properties support by PayPal for advanced card fields: https://developer.paypal.com/docs/business/checkout/reference/style-guide/#style-the-card-payments-fields |
| 264 | 'hostedCardFieldStyles' => apply_filters('give_paypal_commerce_hosted_field_style', []), |
| 265 | 'supportsCustomPayments' => $merchant->supportsCustomPayments ? 1 : '', |
| 266 | 'separatorLabel' => esc_html__('Or pay with card', 'give'), |
| 267 | 'payPalSdkQueryParameters' => $paypalSDKOptions, |
| 268 | 'textForOverlayScreen' => sprintf( |
| 269 | '<h3>%1$s</h3><p>%2$s</p><p>%3$s</p>', |
| 270 | esc_html__('Donation Processing...', 'give'), |
| 271 | esc_html__('Checking donation status with PayPal.', 'give'), |
| 272 | esc_html__('This will only take a second!', 'give') |
| 273 | ) |
| 274 | ]; |
| 275 | |
| 276 | EnqueueScript::make($scriptId, 'assets/dist/js/paypal-commerce.js') |
| 277 | ->registerTranslations() |
| 278 | ->loadInFooter() |
| 279 | ->registerLocalizeData('givePayPalCommerce', $data) |
| 280 | ->enqueue(); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Get PayPal partner js url. |
| 285 | * |
| 286 | * @since 2.30.0 sandbox PayPal partner js loads slow. So we are using live url for now. |
| 287 | * @since 2.9.0 |
| 288 | * |
| 289 | * @return string |
| 290 | */ |
| 291 | private function getPartnerJsUrl() |
| 292 | { |
| 293 | return 'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js'; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * This function should return form id from page, post content. |
| 298 | * |
| 299 | * @since 3.4.0 Check if $attributes is a valid array before trying to use it as it were |
| 300 | * @since 3.2.0 |
| 301 | */ |
| 302 | private function getFormIdFromPostContent(): ?int |
| 303 | { |
| 304 | global $post; |
| 305 | $formId = null; |
| 306 | |
| 307 | if (!$post) { |
| 308 | return null; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | // Donation form can be in page, post content as shortcode. |
| 313 | $has_shortcode = $post && has_shortcode($post->post_content, 'give_form'); |
| 314 | if ($has_shortcode) { |
| 315 | $pattern = get_shortcode_regex(['give_form']); |
| 316 | |
| 317 | if ( |
| 318 | preg_match('/' . $pattern . '/s', $post->post_content, $matches) |
| 319 | && array_key_exists(2, $matches) |
| 320 | && 'give_form' === $matches[2] |
| 321 | ) { |
| 322 | $attributes = shortcode_parse_atts($matches[3]); |
| 323 | |
| 324 | if (is_array($attributes) && array_key_exists('id', $attributes)) { |
| 325 | $formId = $attributes['id']; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // Donation form can be in page, post content as Donation Form block. |
| 331 | if (!$formId) { |
| 332 | $blocks = parse_blocks($post->post_content); |
| 333 | |
| 334 | foreach ($blocks as $block) { |
| 335 | if ( |
| 336 | $block['blockName'] === 'give/donation-form' |
| 337 | && array_key_exists('id', $block['attrs']) |
| 338 | ) { |
| 339 | $formId = $block['attrs']['id']; |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return absint($formId); |
| 346 | } |
| 347 | } |
| 348 |