PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.19.2
GiveWP – Donation Plugin and Fundraising Platform v2.19.2
4.16.4 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 / includes / gateways / paypal / paypal-standard.php
give / includes / gateways / paypal Last commit date
paypal-standard.php 4 years ago
paypal-standard.php
458 lines
1 <?php
2 /**
3 * PayPal Standard Gateway
4 *
5 * @package Give
6 * @since 1.0
7 * @copyright Copyright (c) 2016, GiveWP
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @subpackage Gateways
10 */
11
12 use Give\PaymentGateways\Gateways\PayPalStandard\PayPalStandard;
13
14 if ( ! defined('ABSPATH')) {
15 exit;
16 }
17
18 /**
19 * Listens for a PayPal IPN requests and then sends to the processing function.
20 *
21 * @since 1.0
22 * @return void
23 */
24 function give_listen_for_paypal_ipn()
25 {
26 // Regular PayPal IPN.
27 if (isset($_GET['give-listener']) && 'IPN' === $_GET['give-listener']) {
28 /**
29 * Fires while verifying PayPal IPN
30 *
31 * @deprecated
32 * @since 1.0
33 */
34 do_action('give_verify_paypal_ipn');
35
36 give(PayPalStandard::class)->handleIpnNotification();
37 }
38 }
39
40 add_action('init', 'give_listen_for_paypal_ipn');
41
42 /**
43 * Process PayPal IPN Refunds
44 *
45 * @since 1.0
46 * @deprecated
47 *
48 * @param int $payment_id The payment ID.
49 *
50 * @param array $data IPN Data
51 *
52 * @return void
53 */
54 function give_process_paypal_refund($data, $payment_id = 0)
55 {
56 // Collect payment details.
57 if (empty($payment_id)) {
58 return;
59 }
60
61 // Only refund payments once.
62 if ('refunded' === get_post_status($payment_id)) {
63 return;
64 }
65
66 $payment_amount = give_donation_amount($payment_id);
67 $refund_amount = $data['payment_gross'] * -1;
68
69 if (number_format((float)$refund_amount, 2) < number_format((float)$payment_amount, 2)) {
70 give_insert_payment_note(
71 $payment_id,
72 sprintf( /* translators: %s: Paypal parent transaction ID */
73 __('Partial PayPal refund processed: %s', 'give'),
74 $data['parent_txn_id']
75 )
76 );
77
78 return; // This is a partial refund
79
80 }
81
82 give_insert_payment_note(
83 $payment_id,
84 sprintf( /* translators: 1: Paypal parent transaction ID 2. Paypal reason code */
85 __('PayPal Payment #%1$s Refunded for reason: %2$s', 'give'),
86 $data['parent_txn_id'],
87 $data['reason_code']
88 )
89 );
90 give_insert_payment_note(
91 $payment_id,
92 sprintf( /* translators: %s: Paypal transaction ID */
93 __('PayPal Refund Transaction ID: %s', 'give'),
94 $data['txn_id']
95 )
96 );
97 give_update_payment_status($payment_id, 'refunded');
98 }
99
100 /**
101 * Get PayPal Redirect
102 *
103 * @since 1.0
104 *
105 * @param bool $ssl_check Is SSL?
106 *
107 * @return string
108 */
109 function give_get_paypal_redirect($ssl_check = false)
110 {
111 if (is_ssl() || ! $ssl_check) {
112 $protocol = 'https://';
113 } else {
114 $protocol = 'http://';
115 }
116
117 // Check the current payment mode
118 if (give_is_test_mode()) {
119 // Test mode
120 $paypal_uri = $protocol . 'www.sandbox.paypal.com/cgi-bin/webscr';
121 } else {
122 // Live mode
123 $paypal_uri = $protocol . 'www.paypal.com/cgi-bin/webscr';
124 }
125
126 return apply_filters('give_paypal_uri', $paypal_uri);
127 }
128
129 /**
130 * Set the Page Style for offsite PayPal page.
131 *
132 * @since 1.0
133 * @return string
134 */
135 function give_get_paypal_page_style()
136 {
137 $page_style = trim(give_get_option('paypal_page_style', 'PayPal'));
138
139 return apply_filters('give_paypal_page_style', $page_style);
140 }
141
142 /**
143 * PayPal Success Page
144 *
145 * Shows "Donation Processing" message for PayPal payments that are still pending on site return
146 *
147 * @since 1.0
148 *
149 * @param $content
150 *
151 * @return string
152 */
153 function give_paypal_success_page_content($content)
154 {
155 if ( ! isset($_GET['payment-id']) && ! give_get_purchase_session()) {
156 return $content;
157 }
158
159 $payment_id = isset($_GET['payment-id']) ? absint($_GET['payment-id']) : false;
160
161 if ( ! $payment_id) {
162 $session = give_get_purchase_session();
163 $payment_id = give_get_donation_id_by_key($session['purchase_key']);
164 }
165
166 $payment = get_post($payment_id);
167 if ($payment && 'pending' === $payment->post_status) {
168 // Payment is still pending so show processing indicator to fix the race condition.
169 ob_start();
170
171 give_get_template_part('payment', 'processing');
172
173 $content = ob_get_clean();
174 }
175
176 return $content;
177 }
178
179 add_filter('give_payment_confirm_paypal', 'give_paypal_success_page_content');
180
181 /**
182 * Given a transaction ID, generate a link to the PayPal transaction ID details
183 *
184 * @since 1.0
185 *
186 * @param int $payment_id The payment ID for this transaction
187 *
188 * @param string $transaction_id The Transaction ID
189 *
190 * @return string A link to the PayPal transaction details
191 */
192 function give_paypal_link_transaction_id($transaction_id, $payment_id)
193 {
194 $paypal_base_url = 'https://history.paypal.com/cgi-bin/webscr?cmd=_history-details-from-hub&id=';
195 $transaction_url = '<a href="' . esc_url(
196 $paypal_base_url . $transaction_id
197 ) . '" target="_blank">' . $transaction_id . '</a>';
198
199 return apply_filters('give_paypal_link_payment_details_transaction_id', $transaction_url);
200 }
201
202 add_filter('give_payment_details_transaction_id-paypal', 'give_paypal_link_transaction_id', 10, 2);
203
204
205 /**
206 * Get pending donation note.
207 *
208 * @since 1.6.3
209 *
210 * @param $pending_reason
211 *
212 * @return string
213 */
214 function give_paypal_get_pending_donation_note($pending_reason)
215 {
216 $note = '';
217
218 switch ($pending_reason) {
219 case 'echeck':
220 $note = __('Payment made via eCheck and will clear automatically in 5-8 days.', 'give');
221 break;
222
223 case 'address':
224 $note = __(
225 'Payment requires a confirmed donor address and must be accepted manually through PayPal.',
226 'give'
227 );
228 break;
229
230 case 'intl':
231 $note = __(
232 'Payment must be accepted manually through PayPal due to international account regulations.',
233 'give'
234 );
235 break;
236
237 case 'multi-currency':
238 $note = __('Payment received in non-shop currency and must be accepted manually through PayPal.', 'give');
239 break;
240
241 case 'paymentreview':
242 case 'regulatory_review':
243 $note = __(
244 'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations.',
245 'give'
246 );
247 break;
248
249 case 'unilateral':
250 $note = __('Payment was sent to non-confirmed or non-registered email address.', 'give');
251 break;
252
253 case 'upgrade':
254 $note = __('PayPal account must be upgraded before this payment can be accepted.', 'give');
255 break;
256
257 case 'verify':
258 $note = __('PayPal account is not verified. Verify account in order to accept this donation.', 'give');
259 break;
260
261 case 'other':
262 $note = __('Payment is pending for unknown reasons. Contact PayPal support for assistance.', 'give');
263 break;
264 } // End switch().
265
266 return apply_filters('give_paypal_get_pending_donation_note', $note);
267 }
268
269 /**
270 * Build paypal url
271 *
272 * @deprecated
273 *
274 * @param int $payment_id Payment ID
275 * @param array $payment_data Array of payment data.
276 *
277 * @return mixed|string
278 */
279 function give_build_paypal_url($payment_id, $payment_data)
280 {
281 // Only send to PayPal if the pending payment is created successfully.
282 $listener_url = add_query_arg('give-listener', 'IPN', home_url('index.php'));
283
284 // Get the success url.
285 $return_url = add_query_arg(
286 [
287 'payment-confirmation' => 'paypal',
288 'payment-id' => $payment_id,
289 ],
290 give_get_success_page_uri()
291 );
292
293 // Get the PayPal redirect uri.
294 $paypal_redirect = trailingslashit(give_get_paypal_redirect()) . '?';
295
296 // Item name.
297 $item_name = give_payment_gateway_item_title($payment_data);
298
299 // Setup PayPal API params.
300 $paypal_args = [
301 'business' => give_get_option('paypal_email', false),
302 'first_name' => $payment_data['user_info']['first_name'],
303 'last_name' => $payment_data['user_info']['last_name'],
304 'email' => $payment_data['user_email'],
305 'invoice' => $payment_data['purchase_key'],
306 'amount' => $payment_data['price'],
307 'item_name' => stripslashes($item_name),
308 'no_shipping' => '1',
309 'shipping' => '0',
310 'no_note' => '1',
311 'currency_code' => give_get_currency($payment_id, $payment_data),
312 'charset' => get_bloginfo('charset'),
313 'custom' => $payment_id,
314 'rm' => '2',
315 'return' => $return_url,
316 'cancel_return' => give_get_failed_transaction_uri(),
317 'notify_url' => $listener_url,
318 'page_style' => give_get_paypal_page_style(),
319 'cbt' => get_bloginfo('name'),
320 'bn' => 'givewp_SP',
321 ];
322
323 // Add user address if present.
324 if ( ! empty($payment_data['user_info']['address'])) {
325 $default_address = [
326 'line1' => '',
327 'line2' => '',
328 'city' => '',
329 'state' => '',
330 'zip' => '',
331 'country' => '',
332 ];
333
334 $address = wp_parse_args($payment_data['user_info']['address'], $default_address);
335
336 $paypal_args['address1'] = $address['line1'];
337 $paypal_args['address2'] = $address['line2'];
338 $paypal_args['city'] = $address['city'];
339 $paypal_args['state'] = $address['state'];
340 $paypal_args['zip'] = $address['zip'];
341 $paypal_args['country'] = $address['country'];
342 }
343
344 // Donations or regular transactions?
345 $paypal_args['cmd'] = give_get_paypal_button_type();
346
347 /**
348 * Filter the paypal redirect args.
349 *
350 * @since 1.8
351 *
352 * @param array $payment_data Payment Data.
353 *
354 * @param array $paypal_args PayPal Arguments.
355 */
356 $paypal_args = apply_filters('give_paypal_redirect_args', $paypal_args, $payment_data);
357
358 // Build query.
359 $paypal_redirect .= http_build_query($paypal_args);
360
361 // Fix for some sites that encode the entities.
362 $paypal_redirect = str_replace('&amp;', '&', $paypal_redirect);
363
364 return $paypal_redirect;
365 }
366
367
368 /**
369 * Get paypal button type.
370 *
371 * @since 1.8
372 * @return string
373 */
374 function give_get_paypal_button_type()
375 {
376 // paypal_button_type can be donation or standard.
377 $paypal_button_type = '_donations';
378 if ('standard' === give_get_option('paypal_button_type')) {
379 $paypal_button_type = '_xclick';
380 }
381
382 return $paypal_button_type;
383 }
384
385 /**
386 * Update Purchase key for specific gateway.
387 *
388 * @since 2.2.4
389 *
390 * @param string $gateway
391 * @param string $purchase_key
392 *
393 * @param string $custom_purchase_key
394 *
395 * @return string
396 */
397 function give_paypal_purchase_key($custom_purchase_key, $gateway, $purchase_key)
398 {
399 if ('paypal' === $gateway) {
400 $invoice_id_prefix = give_get_option('paypal_invoice_prefix', 'GIVE-');
401 $custom_purchase_key = $invoice_id_prefix . $purchase_key;
402 }
403
404 return $custom_purchase_key;
405 }
406
407 add_filter('give_donation_purchase_key', 'give_paypal_purchase_key', 10, 3);
408
409
410 /**
411 * PayPal Standard Connect button.
412 *
413 * This uses Stripe's Connect button but swaps the link and logo with PayPal's.
414 *
415 * @since 2.5.0
416 * @return string
417 */
418 function give_paypal_connect_button()
419 {
420 ob_start(); ?>
421
422 <script>
423 function onboardedCallback(authCode, sharedId) {
424 fetch('/seller-server/login-seller', {
425 method: 'POST',
426 headers: {
427 'content-type': 'application/json'
428 },
429 body: JSON.stringify({
430 authCode: authCode,
431 sharedId: sharedId
432 })
433 }).then(function (res) {
434 if (!response.ok) {
435 alert("Something went wrong!");
436 }
437 });
438 }
439 </script>
440 <a target="_blank" data-paypal-onboard-complete="onboardedCallback" href="<Action-URL>&displayMode=minibrowser"
441 data-paypal-button="true">Sign up for PayPal</a>
442 <script id="paypal-js"
443 src="https://www.sandbox.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js"></script>
444
445 <?php
446 return ob_get_clean();
447
448 // Prepare Stripe Connect URL.
449 // $link = admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways&section=paypal-standard' );
450 //
451 // return sprintf(
452 // '<a href="%1$s" id="give-paypal-connect"><span>%2$s</span></a>',
453 // esc_url( $link ),
454 // esc_html__( 'Connect to PayPal', 'give' )
455 // );
456
457 }
458