| 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 |
* @since 2.19.6 Get donation id from donor session. |
| 149 |
* |
| 150 |
* @param $content |
| 151 |
* |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
function give_paypal_success_page_content($content) |
| 155 |
{ |
| 156 |
$session = give_get_purchase_session(); |
| 157 |
$payment_id = give_get_donation_id_by_key($session['purchase_key']); |
| 158 |
|
| 159 |
$payment = get_post($payment_id); |
| 160 |
if ($payment && 'pending' === $payment->post_status) { |
| 161 |
// Payment is still pending so show processing indicator to fix the race condition. |
| 162 |
ob_start(); |
| 163 |
|
| 164 |
give_get_template_part('payment', 'processing'); |
| 165 |
|
| 166 |
$content = ob_get_clean(); |
| 167 |
} |
| 168 |
|
| 169 |
return $content; |
| 170 |
} |
| 171 |
|
| 172 |
add_filter('give_payment_confirm_paypal', 'give_paypal_success_page_content'); |
| 173 |
|
| 174 |
/** |
| 175 |
* Given a transaction ID, generate a link to the PayPal transaction ID details |
| 176 |
* |
| 177 |
* @since 1.0 |
| 178 |
* |
| 179 |
* @param int $payment_id The payment ID for this transaction |
| 180 |
* |
| 181 |
* @param string $transaction_id The Transaction ID |
| 182 |
* |
| 183 |
* @return string A link to the PayPal transaction details |
| 184 |
*/ |
| 185 |
function give_paypal_link_transaction_id($transaction_id, $payment_id) |
| 186 |
{ |
| 187 |
$paypal_base_url = 'https://history.paypal.com/cgi-bin/webscr?cmd=_history-details-from-hub&id='; |
| 188 |
$transaction_url = '<a href="' . esc_url( |
| 189 |
$paypal_base_url . $transaction_id |
| 190 |
) . '" target="_blank">' . $transaction_id . '</a>'; |
| 191 |
|
| 192 |
return apply_filters('give_paypal_link_payment_details_transaction_id', $transaction_url); |
| 193 |
} |
| 194 |
|
| 195 |
add_filter('give_payment_details_transaction_id-paypal', 'give_paypal_link_transaction_id', 10, 2); |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Get pending donation note. |
| 200 |
* |
| 201 |
* @since 1.6.3 |
| 202 |
* |
| 203 |
* @param $pending_reason |
| 204 |
* |
| 205 |
* @return string |
| 206 |
*/ |
| 207 |
function give_paypal_get_pending_donation_note($pending_reason) |
| 208 |
{ |
| 209 |
$note = ''; |
| 210 |
|
| 211 |
switch ($pending_reason) { |
| 212 |
case 'echeck': |
| 213 |
$note = __('Payment made via eCheck and will clear automatically in 5-8 days.', 'give'); |
| 214 |
break; |
| 215 |
|
| 216 |
case 'address': |
| 217 |
$note = __( |
| 218 |
'Payment requires a confirmed donor address and must be accepted manually through PayPal.', |
| 219 |
'give' |
| 220 |
); |
| 221 |
break; |
| 222 |
|
| 223 |
case 'intl': |
| 224 |
$note = __( |
| 225 |
'Payment must be accepted manually through PayPal due to international account regulations.', |
| 226 |
'give' |
| 227 |
); |
| 228 |
break; |
| 229 |
|
| 230 |
case 'multi-currency': |
| 231 |
$note = __('Payment received in non-shop currency and must be accepted manually through PayPal.', 'give'); |
| 232 |
break; |
| 233 |
|
| 234 |
case 'paymentreview': |
| 235 |
case 'regulatory_review': |
| 236 |
$note = __( |
| 237 |
'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations.', |
| 238 |
'give' |
| 239 |
); |
| 240 |
break; |
| 241 |
|
| 242 |
case 'unilateral': |
| 243 |
$note = __('Payment was sent to non-confirmed or non-registered email address.', 'give'); |
| 244 |
break; |
| 245 |
|
| 246 |
case 'upgrade': |
| 247 |
$note = __('PayPal account must be upgraded before this payment can be accepted.', 'give'); |
| 248 |
break; |
| 249 |
|
| 250 |
case 'verify': |
| 251 |
$note = __('PayPal account is not verified. Verify account in order to accept this donation.', 'give'); |
| 252 |
break; |
| 253 |
|
| 254 |
case 'other': |
| 255 |
$note = __('Payment is pending for unknown reasons. Contact PayPal support for assistance.', 'give'); |
| 256 |
break; |
| 257 |
} // End switch(). |
| 258 |
|
| 259 |
return apply_filters('give_paypal_get_pending_donation_note', $note); |
| 260 |
} |
| 261 |
|
| 262 |
/** |
| 263 |
* Build paypal url |
| 264 |
* |
| 265 |
* @deprecated |
| 266 |
* |
| 267 |
* @param int $payment_id Payment ID |
| 268 |
* @param array $payment_data Array of payment data. |
| 269 |
* |
| 270 |
* @return mixed|string |
| 271 |
*/ |
| 272 |
function give_build_paypal_url($payment_id, $payment_data) |
| 273 |
{ |
| 274 |
// Only send to PayPal if the pending payment is created successfully. |
| 275 |
$listener_url = add_query_arg('give-listener', 'IPN', home_url('index.php')); |
| 276 |
|
| 277 |
// Get the success url. |
| 278 |
$return_url = add_query_arg( |
| 279 |
[ |
| 280 |
'payment-confirmation' => 'paypal', |
| 281 |
'payment-id' => $payment_id, |
| 282 |
], |
| 283 |
give_get_success_page_uri() |
| 284 |
); |
| 285 |
|
| 286 |
// Get the PayPal redirect uri. |
| 287 |
$paypal_redirect = trailingslashit(give_get_paypal_redirect()) . '?'; |
| 288 |
|
| 289 |
// Item name. |
| 290 |
$item_name = give_payment_gateway_item_title($payment_data); |
| 291 |
|
| 292 |
// Setup PayPal API params. |
| 293 |
$paypal_args = [ |
| 294 |
'business' => give_get_option('paypal_email', false), |
| 295 |
'first_name' => $payment_data['user_info']['first_name'], |
| 296 |
'last_name' => $payment_data['user_info']['last_name'], |
| 297 |
'email' => $payment_data['user_email'], |
| 298 |
'invoice' => $payment_data['purchase_key'], |
| 299 |
'amount' => $payment_data['price'], |
| 300 |
'item_name' => stripslashes($item_name), |
| 301 |
'no_shipping' => '1', |
| 302 |
'shipping' => '0', |
| 303 |
'no_note' => '1', |
| 304 |
'currency_code' => give_get_currency($payment_id, $payment_data), |
| 305 |
'charset' => get_bloginfo('charset'), |
| 306 |
'custom' => $payment_id, |
| 307 |
'rm' => '2', |
| 308 |
'return' => esc_url_raw( $return_url ), |
| 309 |
'cancel_return' => give_get_failed_transaction_uri(), |
| 310 |
'notify_url' => $listener_url, |
| 311 |
'page_style' => give_get_paypal_page_style(), |
| 312 |
'cbt' => get_bloginfo('name'), |
| 313 |
'bn' => 'givewp_SP', |
| 314 |
]; |
| 315 |
|
| 316 |
// Add user address if present. |
| 317 |
if ( ! empty($payment_data['user_info']['address'])) { |
| 318 |
$default_address = [ |
| 319 |
'line1' => '', |
| 320 |
'line2' => '', |
| 321 |
'city' => '', |
| 322 |
'state' => '', |
| 323 |
'zip' => '', |
| 324 |
'country' => '', |
| 325 |
]; |
| 326 |
|
| 327 |
$address = wp_parse_args($payment_data['user_info']['address'], $default_address); |
| 328 |
|
| 329 |
$paypal_args['address1'] = $address['line1']; |
| 330 |
$paypal_args['address2'] = $address['line2']; |
| 331 |
$paypal_args['city'] = $address['city']; |
| 332 |
$paypal_args['state'] = $address['state']; |
| 333 |
$paypal_args['zip'] = $address['zip']; |
| 334 |
$paypal_args['country'] = $address['country']; |
| 335 |
} |
| 336 |
|
| 337 |
// Donations or regular transactions? |
| 338 |
$paypal_args['cmd'] = give_get_paypal_button_type(); |
| 339 |
|
| 340 |
/** |
| 341 |
* Filter the paypal redirect args. |
| 342 |
* |
| 343 |
* @since 1.8 |
| 344 |
* |
| 345 |
* @param array $payment_data Payment Data. |
| 346 |
* |
| 347 |
* @param array $paypal_args PayPal Arguments. |
| 348 |
*/ |
| 349 |
$paypal_args = apply_filters('give_paypal_redirect_args', $paypal_args, $payment_data); |
| 350 |
|
| 351 |
// Build query. |
| 352 |
$paypal_redirect .= http_build_query($paypal_args); |
| 353 |
|
| 354 |
// Fix for some sites that encode the entities. |
| 355 |
$paypal_redirect = str_replace('&', '&', $paypal_redirect); |
| 356 |
|
| 357 |
return $paypal_redirect; |
| 358 |
} |
| 359 |
|
| 360 |
|
| 361 |
/** |
| 362 |
* Get paypal button type. |
| 363 |
* |
| 364 |
* @since 1.8 |
| 365 |
* @return string |
| 366 |
*/ |
| 367 |
function give_get_paypal_button_type() |
| 368 |
{ |
| 369 |
// paypal_button_type can be donation or standard. |
| 370 |
$paypal_button_type = '_donations'; |
| 371 |
if ('standard' === give_get_option('paypal_button_type')) { |
| 372 |
$paypal_button_type = '_xclick'; |
| 373 |
} |
| 374 |
|
| 375 |
return $paypal_button_type; |
| 376 |
} |
| 377 |
|
| 378 |
/** |
| 379 |
* Update Purchase key for specific gateway. |
| 380 |
* |
| 381 |
* @since 2.2.4 |
| 382 |
* |
| 383 |
* @param string $gateway |
| 384 |
* @param string $purchase_key |
| 385 |
* |
| 386 |
* @param string $custom_purchase_key |
| 387 |
* |
| 388 |
* @return string |
| 389 |
*/ |
| 390 |
function give_paypal_purchase_key($custom_purchase_key, $gateway, $purchase_key) |
| 391 |
{ |
| 392 |
if ('paypal' === $gateway) { |
| 393 |
$invoice_id_prefix = give_get_option('paypal_invoice_prefix', 'GIVE-'); |
| 394 |
$custom_purchase_key = $invoice_id_prefix . $purchase_key; |
| 395 |
} |
| 396 |
|
| 397 |
return $custom_purchase_key; |
| 398 |
} |
| 399 |
|
| 400 |
add_filter('give_donation_purchase_key', 'give_paypal_purchase_key', 10, 3); |
| 401 |
|
| 402 |
|
| 403 |
/** |
| 404 |
* PayPal Standard Connect button. |
| 405 |
* |
| 406 |
* This uses Stripe's Connect button but swaps the link and logo with PayPal's. |
| 407 |
* |
| 408 |
* @since 2.5.0 |
| 409 |
* @return string |
| 410 |
*/ |
| 411 |
function give_paypal_connect_button() |
| 412 |
{ |
| 413 |
ob_start(); ?> |
| 414 |
|
| 415 |
<script> |
| 416 |
function onboardedCallback(authCode, sharedId) { |
| 417 |
fetch('/seller-server/login-seller', { |
| 418 |
method: 'POST', |
| 419 |
headers: { |
| 420 |
'content-type': 'application/json' |
| 421 |
}, |
| 422 |
body: JSON.stringify({ |
| 423 |
authCode: authCode, |
| 424 |
sharedId: sharedId |
| 425 |
}) |
| 426 |
}).then(function (res) { |
| 427 |
if (!response.ok) { |
| 428 |
alert("Something went wrong!"); |
| 429 |
} |
| 430 |
}); |
| 431 |
} |
| 432 |
</script> |
| 433 |
<a target="_blank" data-paypal-onboard-complete="onboardedCallback" href="<Action-URL>&displayMode=minibrowser" |
| 434 |
data-paypal-button="true">Sign up for PayPal</a> |
| 435 |
<script id="paypal-js" |
| 436 |
src="https://www.sandbox.paypal.com/webapps/merchantboarding/js/lib/lightbox/partner.js"></script> |
| 437 |
|
| 438 |
<?php |
| 439 |
return ob_get_clean(); |
| 440 |
|
| 441 |
// Prepare Stripe Connect URL. |
| 442 |
// $link = admin_url( 'edit.php?post_type=give_forms&page=give-settings&tab=gateways§ion=paypal-standard' ); |
| 443 |
// |
| 444 |
// return sprintf( |
| 445 |
// '<a href="%1$s" id="give-paypal-connect"><span>%2$s</span></a>', |
| 446 |
// esc_url( $link ), |
| 447 |
// esc_html__( 'Connect to PayPal', 'give' ) |
| 448 |
// ); |
| 449 |
|
| 450 |
} |
| 451 |
|