PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 3.4.1
GiveWP – Donation Plugin and Fundraising Platform v3.4.1
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / src / PaymentGateways / PayPalCommerce / ScriptLoader.php
give / src / PaymentGateways / PayPalCommerce Last commit date
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
ScriptLoader.php
343 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 2.9.0
93 */
94 public function loadAdminScripts()
95 {
96 if (!Give_Admin_Settings::is_setting_page('gateway', 'paypal')) {
97 return;
98 }
99
100 wp_enqueue_script(
101 'give-paypal-partner-js',
102 $this->getPartnerJsUrl(),
103 [],
104 null,
105 true
106 );
107
108 wp_enqueue_style(
109 'give-admin-paypal-commerce-css',
110 GIVE_PLUGIN_URL . 'assets/dist/css/admin-paypal-commerce.css',
111 [],
112 GIVE_VERSION
113 );
114
115 wp_localize_script(
116 'give-paypal-partner-js',
117 'givePayPalCommerce',
118 [
119 'countriesAvailableForAdvanceConnection' => $this->countriesAvailableForAdvanceConnection,
120 'accountTypes' => self::$accountTypes,
121 'translations' => [
122 'confirmPaypalAccountDisconnection' => esc_html__('Disconnect PayPal Account', 'give'),
123 'disconnectPayPalAccount' => esc_html__(
124 'Are you sure you want to disconnect your PayPal account?',
125 'give'
126 ),
127 'connectSuccessTitle' => esc_html__('You’re connected to PayPal! Here’s what’s next...', 'give'),
128 'pciWarning' => sprintf(
129 __(
130 'PayPal allows you to accept credit or debit cards directly on your website. Because of
131 this, your site needs to maintain <a href="%1$s" target="_blank">PCI-DDS compliance</a>.
132 GiveWP never stores sensitive information like card details to your server and works
133 seamlessly with SSL certificates. Compliance is comprised of, but not limited to:',
134 'give'
135 ),
136 'https://givewp.com/documentation/resources/pci-compliance/'
137 ),
138 'pciComplianceInstructions' => [
139 esc_html__(
140 'Using a trusted, secure hosting provider – preferably one which claims and actively promotes PCI compliance.',
141 'give'
142 ),
143 esc_html__(
144 'Maintain security best practices when setting passwords and limit access to your server.',
145 'give'
146 ),
147 esc_html__('Implement an SSL certificate to keep your donations secure.', 'give'),
148 esc_html__('Keep plugins up to date to ensure latest security fixes are present.', 'give'),
149 ],
150 ],
151 ]
152 );
153
154 $script = <<<EOT
155 function giveSandboxPayPalOnBoardedCallback(authCode, sharedId){
156 givePayPalOnBoardedCallback('sandbox', authCode, sharedId);
157 }
158
159 function giveLivePayPalOnBoardedCallback(authCode, sharedId){
160 givePayPalOnBoardedCallback('live', authCode, sharedId);
161 }
162
163 function givePayPalOnBoardedCallback(mode, authCode, sharedId) {
164 const query = '&mode=' + mode + '&authCode=' + authCode + '&sharedId=' + sharedId;
165
166 fetch( ajaxurl + '?action=give_paypal_commerce_user_on_boarded' + query )
167 .then(function(res){ return res.json() })
168 .then(function(res) {
169 if ( true !== res.success ) {
170 console.log(res);
171 alert('Something went wrong!');
172 return;
173 }
174
175 // Remove PayPal quick help container.
176 const paypalErrorQuickHelp = document.getElementById('give-paypal-onboarding-trouble-notice');
177 paypalErrorQuickHelp && paypalErrorQuickHelp.remove();
178 });
179 }
180 EOT;
181
182 wp_add_inline_script(
183 'give-paypal-partner-js',
184 $script
185 );
186 }
187
188 /**
189 * Load public assets.
190 *
191 * @since 3.2.0 Get form id from post content if form id is not available.
192 * @since 3.2.0 Use EnqueueScript to register and enqueue script.
193 * @since 2.32.0 Handle exception if client token is not generated.
194 * @since 2.9.0
195 */
196 public function loadPublicAssets()
197 {
198 $formId = FrontendFormTemplateUtils::getFormId();
199
200 if (! $formId) {
201 $formId = $this->getFormIdFromPostContent();
202 }
203
204 if (
205 ! $formId
206 || \Give\Helpers\Form\Utils::isV3Form($formId)
207 || ! Utils::gatewayIsActive()
208 || ! Utils::isAccountReadyToAcceptPayment()
209 ) {
210 return;
211 }
212
213 try {
214 $formSettings = $this->payPalCommerceGateway->formSettings($formId);
215 $paypalSDKOptions = $formSettings['sdkOptions'];
216
217 // Remove v3 donation form related param.
218 unset($paypalSDKOptions['data-namespace']);
219 } catch (\Exception $e) {
220 give_set_error(
221 'give-paypal-commerce-client-token-error',
222 sprintf(
223 esc_html__(
224 'Unable to load PayPal Commerce client token. Please try again later. Error: %1$s',
225 'give'
226 ),
227 $e->getMessage()
228 )
229 );
230
231 return;
232 }
233
234 /* @var MerchantDetail $merchant */
235 $merchant = give(MerchantDetail::class);
236
237 $scriptId = 'give-paypal-commerce-js';
238 $data = [
239 'paypalCardInfoErrorPrefixes' => [
240 'expirationDateField' => esc_html__('Card Expiration Date:', 'give'),
241 'cardNumberField' => esc_html__('Card Number:', 'give'),
242 'cardCvcField' => esc_html__('Card CVC:', 'give'),
243 ],
244 'cardFieldPlaceholders' => [
245 'cardNumber' => esc_html__('Card Number', 'give'),
246 'cardCvc' => esc_html__('CVC', 'give'),
247 'expirationDate' => esc_html__('MM/YY', 'give'),
248 ],
249 'threeDsCardAuthenticationFailedNotice' => esc_html__(
250 'There was a problem authenticating your payment method. Please try again. If the problem persists, please try another payment method.',
251 'give'
252 ),
253 'errorCodeLabel' => esc_html__('Error Code', 'give'),
254 'genericDonorErrorMessage' => __(
255 'There was an error processing your donation. Please contact the administrator.',
256 'give'
257 ),
258 // 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
259 'hostedCardFieldStyles' => apply_filters('give_paypal_commerce_hosted_field_style', []),
260 'supportsCustomPayments' => $merchant->supportsCustomPayments ? 1 : '',
261 'separatorLabel' => esc_html__('Or pay with card', 'give'),
262 'payPalSdkQueryParameters' => $paypalSDKOptions,
263 'textForOverlayScreen' => sprintf(
264 '<h3>%1$s</h3><p>%2$s</p><p>%3$s</p>',
265 esc_html__('Donation Processing...', 'give'),
266 esc_html__('Checking donation status with PayPal.', 'give'),
267 esc_html__('This will only take a second!', 'give')
268 )
269 ];
270
271 EnqueueScript::make($scriptId, 'assets/dist/js/paypal-commerce.js')
272 ->registerTranslations()
273 ->loadInFooter()
274 ->registerLocalizeData('givePayPalCommerce', $data)
275 ->enqueue();
276 }
277
278 /**
279 * Get PayPal partner js url.
280 *
281 * @since 2.30.0 sandbox PayPal partner js loads slow. So we are using live url for now.
282 * @since 2.9.0
283 *
284 * @return string
285 */
286 private function getPartnerJsUrl()
287 {
288 return 'https://www.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js';
289 }
290
291 /**
292 * This function should return form id from page, post content.
293 *
294 * @since 3.4.0 Check if $attributes is a valid array before trying to use it as it were
295 * @since 3.2.0
296 */
297 private function getFormIdFromPostContent(): ?int
298 {
299 global $post;
300 $formId = null;
301
302 if (!$post) {
303 return null;
304 }
305
306
307 // Donation form can be in page, post content as shortcode.
308 $has_shortcode = $post && has_shortcode($post->post_content, 'give_form');
309 if ($has_shortcode) {
310 $pattern = get_shortcode_regex(['give_form']);
311
312 if (
313 preg_match('/' . $pattern . '/s', $post->post_content, $matches)
314 && array_key_exists(2, $matches)
315 && 'give_form' === $matches[2]
316 ) {
317 $attributes = shortcode_parse_atts($matches[3]);
318
319 if (is_array($attributes) && array_key_exists('id', $attributes)) {
320 $formId = $attributes['id'];
321 }
322 }
323 }
324
325 // Donation form can be in page, post content as Donation Form block.
326 if (!$formId) {
327 $blocks = parse_blocks($post->post_content);
328
329 foreach ($blocks as $block) {
330 if (
331 $block['blockName'] === 'give/donation-form'
332 && array_key_exists('id', $block['attrs'])
333 ) {
334 $formId = $block['attrs']['id'];
335 break;
336 }
337 }
338 }
339
340 return absint($formId);
341 }
342 }
343