@@ -218,261 +218,261 @@ discard block |
||
| 218 | 218 | add_filter( 'wpinv_paypal_args', 'wpinv_get_paypal_recurring_args', 10, 3 ); |
| 219 | 219 | |
| 220 | 220 | function wpinv_process_paypal_ipn() { |
| 221 | - // Check the request method is POST |
|
| 222 | - if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) { |
|
| 223 | - return; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - // Set initial post data to empty string |
|
| 227 | - $post_data = ''; |
|
| 228 | - |
|
| 229 | - // Fallback just in case post_max_size is lower than needed |
|
| 230 | - if ( ini_get( 'allow_url_fopen' ) ) { |
|
| 231 | - $post_data = file_get_contents( 'php://input' ); |
|
| 232 | - } else { |
|
| 233 | - // If allow_url_fopen is not enabled, then make sure that post_max_size is large enough |
|
| 234 | - ini_set( 'post_max_size', '12M' ); |
|
| 235 | - } |
|
| 236 | - // Start the encoded data collection with notification command |
|
| 237 | - $encoded_data = 'cmd=_notify-validate'; |
|
| 238 | - |
|
| 239 | - // Get current arg separator |
|
| 240 | - $arg_separator = wpinv_get_php_arg_separator_output(); |
|
| 241 | - |
|
| 242 | - // Verify there is a post_data |
|
| 243 | - if ( $post_data || strlen( $post_data ) > 0 ) { |
|
| 244 | - // Append the data |
|
| 245 | - $encoded_data .= $arg_separator.$post_data; |
|
| 246 | - } else { |
|
| 247 | - // Check if POST is empty |
|
| 248 | - if ( empty( $_POST ) ) { |
|
| 249 | - // Nothing to do |
|
| 250 | - return; |
|
| 251 | - } else { |
|
| 252 | - // Loop through each POST |
|
| 253 | - foreach ( $_POST as $key => $value ) { |
|
| 254 | - // Encode the value and append the data |
|
| 255 | - $encoded_data .= $arg_separator."$key=" . urlencode( $value ); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - // Convert collected post data to an array |
|
| 261 | - parse_str( $encoded_data, $encoded_data_array ); |
|
| 262 | - |
|
| 263 | - foreach ( $encoded_data_array as $key => $value ) { |
|
| 264 | - if ( false !== strpos( $key, 'amp;' ) ) { |
|
| 265 | - $new_key = str_replace( '&', '&', $key ); |
|
| 266 | - $new_key = str_replace( 'amp;', '&' , $new_key ); |
|
| 267 | - |
|
| 268 | - unset( $encoded_data_array[ $key ] ); |
|
| 269 | - $encoded_data_array[ $new_key ] = $value; |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - // Get the PayPal redirect uri |
|
| 274 | - $paypal_redirect = wpinv_get_paypal_redirect( true ); |
|
| 275 | - |
|
| 276 | - if ( !wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
| 277 | - // Validate the IPN |
|
| 278 | - |
|
| 279 | - $remote_post_vars = array( |
|
| 280 | - 'method' => 'POST', |
|
| 281 | - 'timeout' => 45, |
|
| 282 | - 'redirection' => 5, |
|
| 283 | - 'httpversion' => '1.1', |
|
| 284 | - 'blocking' => true, |
|
| 285 | - 'headers' => array( |
|
| 286 | - 'host' => 'www.paypal.com', |
|
| 287 | - 'connection' => 'close', |
|
| 288 | - 'content-type' => 'application/x-www-form-urlencoded', |
|
| 289 | - 'post' => '/cgi-bin/webscr HTTP/1.1', |
|
| 290 | - |
|
| 291 | - ), |
|
| 292 | - 'sslverify' => false, |
|
| 293 | - 'body' => $encoded_data_array |
|
| 294 | - ); |
|
| 295 | - |
|
| 296 | - // Get response |
|
| 297 | - $api_response = wp_remote_post( wpinv_get_paypal_redirect(), $remote_post_vars ); |
|
| 298 | - |
|
| 299 | - if ( is_wp_error( $api_response ) ) { |
|
| 300 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
| 301 | - return; // Something went wrong |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - if ( $api_response['body'] !== 'VERIFIED' && wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
| 305 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
| 306 | - return; // Response not okay |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - // Check if $post_data_array has been populated |
|
| 311 | - if ( !is_array( $encoded_data_array ) && !empty( $encoded_data_array ) ) |
|
| 312 | - return; |
|
| 313 | - |
|
| 314 | - $defaults = array( |
|
| 315 | - 'txn_type' => '', |
|
| 316 | - 'payment_status' => '' |
|
| 317 | - ); |
|
| 318 | - |
|
| 319 | - $encoded_data_array = wp_parse_args( $encoded_data_array, $defaults ); |
|
| 320 | - |
|
| 321 | - $invoice_id = isset( $encoded_data_array['custom'] ) ? absint( $encoded_data_array['custom'] ) : 0; |
|
| 221 | + // Check the request method is POST |
|
| 222 | + if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) { |
|
| 223 | + return; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + // Set initial post data to empty string |
|
| 227 | + $post_data = ''; |
|
| 228 | + |
|
| 229 | + // Fallback just in case post_max_size is lower than needed |
|
| 230 | + if ( ini_get( 'allow_url_fopen' ) ) { |
|
| 231 | + $post_data = file_get_contents( 'php://input' ); |
|
| 232 | + } else { |
|
| 233 | + // If allow_url_fopen is not enabled, then make sure that post_max_size is large enough |
|
| 234 | + ini_set( 'post_max_size', '12M' ); |
|
| 235 | + } |
|
| 236 | + // Start the encoded data collection with notification command |
|
| 237 | + $encoded_data = 'cmd=_notify-validate'; |
|
| 238 | + |
|
| 239 | + // Get current arg separator |
|
| 240 | + $arg_separator = wpinv_get_php_arg_separator_output(); |
|
| 241 | + |
|
| 242 | + // Verify there is a post_data |
|
| 243 | + if ( $post_data || strlen( $post_data ) > 0 ) { |
|
| 244 | + // Append the data |
|
| 245 | + $encoded_data .= $arg_separator.$post_data; |
|
| 246 | + } else { |
|
| 247 | + // Check if POST is empty |
|
| 248 | + if ( empty( $_POST ) ) { |
|
| 249 | + // Nothing to do |
|
| 250 | + return; |
|
| 251 | + } else { |
|
| 252 | + // Loop through each POST |
|
| 253 | + foreach ( $_POST as $key => $value ) { |
|
| 254 | + // Encode the value and append the data |
|
| 255 | + $encoded_data .= $arg_separator."$key=" . urlencode( $value ); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + // Convert collected post data to an array |
|
| 261 | + parse_str( $encoded_data, $encoded_data_array ); |
|
| 262 | + |
|
| 263 | + foreach ( $encoded_data_array as $key => $value ) { |
|
| 264 | + if ( false !== strpos( $key, 'amp;' ) ) { |
|
| 265 | + $new_key = str_replace( '&', '&', $key ); |
|
| 266 | + $new_key = str_replace( 'amp;', '&' , $new_key ); |
|
| 267 | + |
|
| 268 | + unset( $encoded_data_array[ $key ] ); |
|
| 269 | + $encoded_data_array[ $new_key ] = $value; |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + // Get the PayPal redirect uri |
|
| 274 | + $paypal_redirect = wpinv_get_paypal_redirect( true ); |
|
| 275 | + |
|
| 276 | + if ( !wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
| 277 | + // Validate the IPN |
|
| 278 | + |
|
| 279 | + $remote_post_vars = array( |
|
| 280 | + 'method' => 'POST', |
|
| 281 | + 'timeout' => 45, |
|
| 282 | + 'redirection' => 5, |
|
| 283 | + 'httpversion' => '1.1', |
|
| 284 | + 'blocking' => true, |
|
| 285 | + 'headers' => array( |
|
| 286 | + 'host' => 'www.paypal.com', |
|
| 287 | + 'connection' => 'close', |
|
| 288 | + 'content-type' => 'application/x-www-form-urlencoded', |
|
| 289 | + 'post' => '/cgi-bin/webscr HTTP/1.1', |
|
| 290 | + |
|
| 291 | + ), |
|
| 292 | + 'sslverify' => false, |
|
| 293 | + 'body' => $encoded_data_array |
|
| 294 | + ); |
|
| 295 | + |
|
| 296 | + // Get response |
|
| 297 | + $api_response = wp_remote_post( wpinv_get_paypal_redirect(), $remote_post_vars ); |
|
| 298 | + |
|
| 299 | + if ( is_wp_error( $api_response ) ) { |
|
| 300 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
| 301 | + return; // Something went wrong |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + if ( $api_response['body'] !== 'VERIFIED' && wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
| 305 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
| 306 | + return; // Response not okay |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + // Check if $post_data_array has been populated |
|
| 311 | + if ( !is_array( $encoded_data_array ) && !empty( $encoded_data_array ) ) |
|
| 312 | + return; |
|
| 313 | + |
|
| 314 | + $defaults = array( |
|
| 315 | + 'txn_type' => '', |
|
| 316 | + 'payment_status' => '' |
|
| 317 | + ); |
|
| 318 | + |
|
| 319 | + $encoded_data_array = wp_parse_args( $encoded_data_array, $defaults ); |
|
| 320 | + |
|
| 321 | + $invoice_id = isset( $encoded_data_array['custom'] ) ? absint( $encoded_data_array['custom'] ) : 0; |
|
| 322 | 322 | |
| 323 | - wpinv_error_log( $encoded_data_array['txn_type'], 'PayPal txn_type', __FILE__, __LINE__ ); |
|
| 324 | - |
|
| 325 | - if ( has_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'] ) ) { |
|
| 326 | - // Allow PayPal IPN types to be processed separately |
|
| 327 | - do_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'], $encoded_data_array, $invoice_id ); |
|
| 328 | - } else { |
|
| 329 | - // Fallback to web accept just in case the txn_type isn't present |
|
| 330 | - do_action( 'wpinv_paypal_web_accept', $encoded_data_array, $invoice_id ); |
|
| 331 | - } |
|
| 332 | - exit; |
|
| 323 | + wpinv_error_log( $encoded_data_array['txn_type'], 'PayPal txn_type', __FILE__, __LINE__ ); |
|
| 324 | + |
|
| 325 | + if ( has_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'] ) ) { |
|
| 326 | + // Allow PayPal IPN types to be processed separately |
|
| 327 | + do_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'], $encoded_data_array, $invoice_id ); |
|
| 328 | + } else { |
|
| 329 | + // Fallback to web accept just in case the txn_type isn't present |
|
| 330 | + do_action( 'wpinv_paypal_web_accept', $encoded_data_array, $invoice_id ); |
|
| 331 | + } |
|
| 332 | + exit; |
|
| 333 | 333 | } |
| 334 | 334 | add_action( 'wpinv_verify_paypal_ipn', 'wpinv_process_paypal_ipn' ); |
| 335 | 335 | |
| 336 | 336 | function wpinv_process_paypal_web_accept_and_cart( $data, $invoice_id ) { |
| 337 | - if ( $data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded' ) { |
|
| 338 | - return; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - if( empty( $invoice_id ) ) { |
|
| 342 | - return; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - // Collect payment details |
|
| 346 | - $purchase_key = isset( $data['invoice'] ) ? $data['invoice'] : $data['item_number']; |
|
| 347 | - $paypal_amount = $data['mc_gross']; |
|
| 348 | - $payment_status = strtolower( $data['payment_status'] ); |
|
| 349 | - $currency_code = strtolower( $data['mc_currency'] ); |
|
| 350 | - $business_email = isset( $data['business'] ) && is_email( $data['business'] ) ? trim( $data['business'] ) : trim( $data['receiver_email'] ); |
|
| 351 | - $payment_meta = wpinv_get_invoice_meta( $invoice_id ); |
|
| 352 | - |
|
| 353 | - if ( wpinv_get_payment_gateway( $invoice_id ) != 'paypal' ) { |
|
| 354 | - return; // this isn't a PayPal standard IPN |
|
| 355 | - } |
|
| 356 | - |
|
| 357 | - // Verify payment recipient |
|
| 358 | - if ( strcasecmp( $business_email, trim( wpinv_get_option( 'paypal_email', false ) ) ) != 0 ) { |
|
| 359 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid business email in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 360 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 361 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid PayPal business email.', 'invoicing' ) ); |
|
| 362 | - return; |
|
| 363 | - } |
|
| 364 | - |
|
| 365 | - // Verify payment currency |
|
| 366 | - if ( $currency_code != strtolower( $payment_meta['currency'] ) ) { |
|
| 367 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid currency in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 368 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 369 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid currency in PayPal IPN.', 'invoicing' ) ); |
|
| 370 | - return; |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - if ( !wpinv_get_payment_user_email( $invoice_id ) ) { |
|
| 374 | - // This runs when a Buy Now purchase was made. It bypasses checkout so no personal info is collected until PayPal |
|
| 375 | - // No email associated with purchase, so store from PayPal |
|
| 376 | - wpinv_update_invoice_meta( $invoice_id, '_wpinv_email', $data['payer_email'] ); |
|
| 377 | - |
|
| 378 | - // Setup and store the customer's details |
|
| 379 | - $user_info = array( |
|
| 380 | - 'user_id' => '-1', |
|
| 381 | - 'email' => sanitize_text_field( $data['payer_email'] ), |
|
| 382 | - 'first_name' => sanitize_text_field( $data['first_name'] ), |
|
| 383 | - 'last_name' => sanitize_text_field( $data['last_name'] ), |
|
| 384 | - 'discount' => '', |
|
| 385 | - ); |
|
| 386 | - $user_info['address'] = ! empty( $data['address_street'] ) ? sanitize_text_field( $data['address_street'] ) : false; |
|
| 387 | - $user_info['city'] = ! empty( $data['address_city'] ) ? sanitize_text_field( $data['address_city'] ) : false; |
|
| 388 | - $user_info['state'] = ! empty( $data['address_state'] ) ? sanitize_text_field( $data['address_state'] ) : false; |
|
| 389 | - $user_info['country'] = ! empty( $data['address_country_code'] ) ? sanitize_text_field( $data['address_country_code'] ) : false; |
|
| 390 | - $user_info['zip'] = ! empty( $data['address_zip'] ) ? sanitize_text_field( $data['address_zip'] ) : false; |
|
| 391 | - |
|
| 392 | - $payment_meta['user_info'] = $user_info; |
|
| 393 | - wpinv_update_invoice_meta( $invoice_id, '_wpinv_payment_meta', $payment_meta ); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 397 | - // Process a refund |
|
| 398 | - wpinv_process_paypal_refund( $data, $invoice_id ); |
|
| 399 | - } else { |
|
| 400 | - if ( get_post_status( $invoice_id ) == 'publish' ) { |
|
| 401 | - return; // Only paid payments once |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - // Retrieve the total purchase amount (before PayPal) |
|
| 405 | - $payment_amount = wpinv_payment_total( $invoice_id ); |
|
| 406 | - |
|
| 407 | - if ( number_format( (float) $paypal_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
| 408 | - // The prices don't match |
|
| 409 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid payment amount in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 410 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 411 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid amount in PayPal IPN.', 'invoicing' ) ); |
|
| 412 | - return; |
|
| 413 | - } |
|
| 414 | - if ( $purchase_key != wpinv_get_payment_key( $invoice_id ) ) { |
|
| 415 | - // Purchase keys don't match |
|
| 416 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid purchase key in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 417 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 418 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid purchase key in PayPal IPN.', 'invoicing' ) ); |
|
| 419 | - return; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - if ( 'complete' == $payment_status || 'completed' == $payment_status || 'processed' == $payment_status || wpinv_is_test_mode( 'paypal' ) ) { |
|
| 423 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $data['txn_id'] ) ); |
|
| 424 | - wpinv_set_payment_transaction_id( $invoice_id, $data['txn_id'] ); |
|
| 425 | - wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
| 426 | - } else if ( 'wpi-pending' == $payment_status && isset( $data['pending_reason'] ) ) { |
|
| 427 | - // Look for possible pending reasons, such as an echeck |
|
| 428 | - $note = ''; |
|
| 429 | - |
|
| 430 | - switch( strtolower( $data['pending_reason'] ) ) { |
|
| 431 | - case 'echeck' : |
|
| 432 | - $note = __( 'Payment made via eCheck and will clear automatically in 5-8 days', 'invoicing' ); |
|
| 433 | - break; |
|
| 337 | + if ( $data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded' ) { |
|
| 338 | + return; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + if( empty( $invoice_id ) ) { |
|
| 342 | + return; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + // Collect payment details |
|
| 346 | + $purchase_key = isset( $data['invoice'] ) ? $data['invoice'] : $data['item_number']; |
|
| 347 | + $paypal_amount = $data['mc_gross']; |
|
| 348 | + $payment_status = strtolower( $data['payment_status'] ); |
|
| 349 | + $currency_code = strtolower( $data['mc_currency'] ); |
|
| 350 | + $business_email = isset( $data['business'] ) && is_email( $data['business'] ) ? trim( $data['business'] ) : trim( $data['receiver_email'] ); |
|
| 351 | + $payment_meta = wpinv_get_invoice_meta( $invoice_id ); |
|
| 352 | + |
|
| 353 | + if ( wpinv_get_payment_gateway( $invoice_id ) != 'paypal' ) { |
|
| 354 | + return; // this isn't a PayPal standard IPN |
|
| 355 | + } |
|
| 356 | + |
|
| 357 | + // Verify payment recipient |
|
| 358 | + if ( strcasecmp( $business_email, trim( wpinv_get_option( 'paypal_email', false ) ) ) != 0 ) { |
|
| 359 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid business email in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 360 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 361 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid PayPal business email.', 'invoicing' ) ); |
|
| 362 | + return; |
|
| 363 | + } |
|
| 364 | + |
|
| 365 | + // Verify payment currency |
|
| 366 | + if ( $currency_code != strtolower( $payment_meta['currency'] ) ) { |
|
| 367 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid currency in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 368 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 369 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid currency in PayPal IPN.', 'invoicing' ) ); |
|
| 370 | + return; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + if ( !wpinv_get_payment_user_email( $invoice_id ) ) { |
|
| 374 | + // This runs when a Buy Now purchase was made. It bypasses checkout so no personal info is collected until PayPal |
|
| 375 | + // No email associated with purchase, so store from PayPal |
|
| 376 | + wpinv_update_invoice_meta( $invoice_id, '_wpinv_email', $data['payer_email'] ); |
|
| 377 | + |
|
| 378 | + // Setup and store the customer's details |
|
| 379 | + $user_info = array( |
|
| 380 | + 'user_id' => '-1', |
|
| 381 | + 'email' => sanitize_text_field( $data['payer_email'] ), |
|
| 382 | + 'first_name' => sanitize_text_field( $data['first_name'] ), |
|
| 383 | + 'last_name' => sanitize_text_field( $data['last_name'] ), |
|
| 384 | + 'discount' => '', |
|
| 385 | + ); |
|
| 386 | + $user_info['address'] = ! empty( $data['address_street'] ) ? sanitize_text_field( $data['address_street'] ) : false; |
|
| 387 | + $user_info['city'] = ! empty( $data['address_city'] ) ? sanitize_text_field( $data['address_city'] ) : false; |
|
| 388 | + $user_info['state'] = ! empty( $data['address_state'] ) ? sanitize_text_field( $data['address_state'] ) : false; |
|
| 389 | + $user_info['country'] = ! empty( $data['address_country_code'] ) ? sanitize_text_field( $data['address_country_code'] ) : false; |
|
| 390 | + $user_info['zip'] = ! empty( $data['address_zip'] ) ? sanitize_text_field( $data['address_zip'] ) : false; |
|
| 391 | + |
|
| 392 | + $payment_meta['user_info'] = $user_info; |
|
| 393 | + wpinv_update_invoice_meta( $invoice_id, '_wpinv_payment_meta', $payment_meta ); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 397 | + // Process a refund |
|
| 398 | + wpinv_process_paypal_refund( $data, $invoice_id ); |
|
| 399 | + } else { |
|
| 400 | + if ( get_post_status( $invoice_id ) == 'publish' ) { |
|
| 401 | + return; // Only paid payments once |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + // Retrieve the total purchase amount (before PayPal) |
|
| 405 | + $payment_amount = wpinv_payment_total( $invoice_id ); |
|
| 406 | + |
|
| 407 | + if ( number_format( (float) $paypal_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
| 408 | + // The prices don't match |
|
| 409 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid payment amount in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 410 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 411 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid amount in PayPal IPN.', 'invoicing' ) ); |
|
| 412 | + return; |
|
| 413 | + } |
|
| 414 | + if ( $purchase_key != wpinv_get_payment_key( $invoice_id ) ) { |
|
| 415 | + // Purchase keys don't match |
|
| 416 | + wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid purchase key in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 417 | + wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 418 | + wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid purchase key in PayPal IPN.', 'invoicing' ) ); |
|
| 419 | + return; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + if ( 'complete' == $payment_status || 'completed' == $payment_status || 'processed' == $payment_status || wpinv_is_test_mode( 'paypal' ) ) { |
|
| 423 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $data['txn_id'] ) ); |
|
| 424 | + wpinv_set_payment_transaction_id( $invoice_id, $data['txn_id'] ); |
|
| 425 | + wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
| 426 | + } else if ( 'wpi-pending' == $payment_status && isset( $data['pending_reason'] ) ) { |
|
| 427 | + // Look for possible pending reasons, such as an echeck |
|
| 428 | + $note = ''; |
|
| 429 | + |
|
| 430 | + switch( strtolower( $data['pending_reason'] ) ) { |
|
| 431 | + case 'echeck' : |
|
| 432 | + $note = __( 'Payment made via eCheck and will clear automatically in 5-8 days', 'invoicing' ); |
|
| 433 | + break; |
|
| 434 | 434 | |
| 435 | 435 | case 'address' : |
| 436 | - $note = __( 'Payment requires a confirmed customer address and must be accepted manually through PayPal', 'invoicing' ); |
|
| 437 | - break; |
|
| 436 | + $note = __( 'Payment requires a confirmed customer address and must be accepted manually through PayPal', 'invoicing' ); |
|
| 437 | + break; |
|
| 438 | 438 | |
| 439 | 439 | case 'intl' : |
| 440 | - $note = __( 'Payment must be accepted manually through PayPal due to international account regulations', 'invoicing' ); |
|
| 441 | - break; |
|
| 440 | + $note = __( 'Payment must be accepted manually through PayPal due to international account regulations', 'invoicing' ); |
|
| 441 | + break; |
|
| 442 | 442 | |
| 443 | 443 | case 'multi-currency' : |
| 444 | - $note = __( 'Payment received in non-shop currency and must be accepted manually through PayPal', 'invoicing' ); |
|
| 445 | - break; |
|
| 444 | + $note = __( 'Payment received in non-shop currency and must be accepted manually through PayPal', 'invoicing' ); |
|
| 445 | + break; |
|
| 446 | 446 | |
| 447 | 447 | case 'paymentreview' : |
| 448 | 448 | case 'regulatory_review' : |
| 449 | - $note = __( 'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'invoicing' ); |
|
| 450 | - break; |
|
| 449 | + $note = __( 'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'invoicing' ); |
|
| 450 | + break; |
|
| 451 | 451 | |
| 452 | 452 | case 'unilateral' : |
| 453 | - $note = __( 'Payment was sent to non-confirmed or non-registered email address.', 'invoicing' ); |
|
| 454 | - break; |
|
| 453 | + $note = __( 'Payment was sent to non-confirmed or non-registered email address.', 'invoicing' ); |
|
| 454 | + break; |
|
| 455 | 455 | |
| 456 | 456 | case 'upgrade' : |
| 457 | - $note = __( 'PayPal account must be upgraded before this payment can be accepted', 'invoicing' ); |
|
| 458 | - break; |
|
| 457 | + $note = __( 'PayPal account must be upgraded before this payment can be accepted', 'invoicing' ); |
|
| 458 | + break; |
|
| 459 | 459 | |
| 460 | 460 | case 'verify' : |
| 461 | - $note = __( 'PayPal account is not verified. Verify account in order to accept this payment', 'invoicing' ); |
|
| 462 | - break; |
|
| 463 | - |
|
| 464 | - case 'other' : |
|
| 465 | - $note = __( 'Payment is pending for unknown reasons. Contact PayPal support for assistance', 'invoicing' ); |
|
| 466 | - break; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - if ( ! empty( $note ) ) { |
|
| 470 | - wpinv_insert_payment_note( $invoice_id, $note ); |
|
| 471 | - } |
|
| 472 | - } else { |
|
| 473 | - wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal IPN has been received with invalid payment status: %s', 'invoicing' ), $payment_status ) ); |
|
| 474 | - } |
|
| 475 | - } |
|
| 461 | + $note = __( 'PayPal account is not verified. Verify account in order to accept this payment', 'invoicing' ); |
|
| 462 | + break; |
|
| 463 | + |
|
| 464 | + case 'other' : |
|
| 465 | + $note = __( 'Payment is pending for unknown reasons. Contact PayPal support for assistance', 'invoicing' ); |
|
| 466 | + break; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + if ( ! empty( $note ) ) { |
|
| 470 | + wpinv_insert_payment_note( $invoice_id, $note ); |
|
| 471 | + } |
|
| 472 | + } else { |
|
| 473 | + wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal IPN has been received with invalid payment status: %s', 'invoicing' ), $payment_status ) ); |
|
| 474 | + } |
|
| 475 | + } |
|
| 476 | 476 | } |
| 477 | 477 | add_action( 'wpinv_paypal_web_accept', 'wpinv_process_paypal_web_accept_and_cart', 10, 2 ); |
| 478 | 478 | |
@@ -687,27 +687,27 @@ discard block |
||
| 687 | 687 | } |
| 688 | 688 | |
| 689 | 689 | function wpinv_process_paypal_refund( $data, $invoice_id = 0 ) { |
| 690 | - // Collect payment details |
|
| 690 | + // Collect payment details |
|
| 691 | 691 | |
| 692 | - if( empty( $invoice_id ) ) { |
|
| 693 | - return; |
|
| 694 | - } |
|
| 692 | + if( empty( $invoice_id ) ) { |
|
| 693 | + return; |
|
| 694 | + } |
|
| 695 | 695 | |
| 696 | - if ( get_post_status( $invoice_id ) == 'wpi-refunded' ) { |
|
| 697 | - return; // Only refund payments once |
|
| 698 | - } |
|
| 696 | + if ( get_post_status( $invoice_id ) == 'wpi-refunded' ) { |
|
| 697 | + return; // Only refund payments once |
|
| 698 | + } |
|
| 699 | 699 | |
| 700 | - $payment_amount = wpinv_payment_total( $invoice_id ); |
|
| 701 | - $refund_amount = $data['mc_gross'] * -1; |
|
| 700 | + $payment_amount = wpinv_payment_total( $invoice_id ); |
|
| 701 | + $refund_amount = $data['mc_gross'] * -1; |
|
| 702 | 702 | |
| 703 | - if ( number_format( (float) $refund_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
| 704 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'Partial PayPal refund processed: %s', 'invoicing' ), $data['parent_txn_id'] ) ); |
|
| 705 | - return; // This is a partial refund |
|
| 706 | - } |
|
| 703 | + if ( number_format( (float) $refund_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
| 704 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'Partial PayPal refund processed: %s', 'invoicing' ), $data['parent_txn_id'] ) ); |
|
| 705 | + return; // This is a partial refund |
|
| 706 | + } |
|
| 707 | 707 | |
| 708 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Payment #%s Refunded for reason: %s', 'invoicing' ), $data['parent_txn_id'], $data['reason_code'] ) ); |
|
| 709 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Refund Transaction ID: %s', 'invoicing' ), $data['txn_id'] ) ); |
|
| 710 | - wpinv_update_payment_status( $invoice_id, 'wpi-refunded' ); |
|
| 708 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Payment #%s Refunded for reason: %s', 'invoicing' ), $data['parent_txn_id'], $data['reason_code'] ) ); |
|
| 709 | + wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Refund Transaction ID: %s', 'invoicing' ), $data['txn_id'] ) ); |
|
| 710 | + wpinv_update_payment_status( $invoice_id, 'wpi-refunded' ); |
|
| 711 | 711 | } |
| 712 | 712 | |
| 713 | 713 | function wpinv_get_paypal_redirect( $ssl_check = false ) { |
@@ -1,13 +1,13 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | // Exit if accessed directly |
| 3 | -if ( ! defined( 'ABSPATH' ) ) exit; |
|
| 3 | +if (!defined('ABSPATH')) exit; |
|
| 4 | 4 | |
| 5 | -add_action( 'wpinv_paypal_cc_form', '__return_false' ); |
|
| 6 | -add_filter( 'wpinv_paypal_support_subscription', '__return_true' ); |
|
| 5 | +add_action('wpinv_paypal_cc_form', '__return_false'); |
|
| 6 | +add_filter('wpinv_paypal_support_subscription', '__return_true'); |
|
| 7 | 7 | |
| 8 | -function wpinv_process_paypal_payment( $purchase_data ) { |
|
| 9 | - if( ! wp_verify_nonce( $purchase_data['gateway_nonce'], 'wpi-gateway' ) ) { |
|
| 10 | - wp_die( __( 'Nonce verification has failed', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) ); |
|
| 8 | +function wpinv_process_paypal_payment($purchase_data) { |
|
| 9 | + if (!wp_verify_nonce($purchase_data['gateway_nonce'], 'wpi-gateway')) { |
|
| 10 | + wp_die(__('Nonce verification has failed', 'invoicing'), __('Error', 'invoicing'), array('response' => 403)); |
|
| 11 | 11 | } |
| 12 | 12 | |
| 13 | 13 | // Collect payment data |
@@ -25,30 +25,30 @@ discard block |
||
| 25 | 25 | ); |
| 26 | 26 | |
| 27 | 27 | // Record the pending payment |
| 28 | - $invoice = wpinv_get_invoice( $purchase_data['invoice_id'] ); |
|
| 28 | + $invoice = wpinv_get_invoice($purchase_data['invoice_id']); |
|
| 29 | 29 | |
| 30 | 30 | // Check payment |
| 31 | - if ( ! $invoice ) { |
|
| 31 | + if (!$invoice) { |
|
| 32 | 32 | // Record the error |
| 33 | - wpinv_record_gateway_error( __( 'Payment Error', 'invoicing' ), sprintf( __( 'Payment creation failed before sending buyer to PayPal. Payment data: %s', 'invoicing' ), json_encode( $payment_data ) ), $payment ); |
|
| 33 | + wpinv_record_gateway_error(__('Payment Error', 'invoicing'), sprintf(__('Payment creation failed before sending buyer to PayPal. Payment data: %s', 'invoicing'), json_encode($payment_data)), $payment); |
|
| 34 | 34 | // Problems? send back |
| 35 | - wpinv_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['wpi-gateway'] ); |
|
| 35 | + wpinv_send_back_to_checkout('?payment-mode=' . $purchase_data['post_data']['wpi-gateway']); |
|
| 36 | 36 | } else { |
| 37 | 37 | // Only send to PayPal if the pending payment is created successfully |
| 38 | - $listener_url = wpinv_get_ipn_url( 'paypal' ); |
|
| 38 | + $listener_url = wpinv_get_ipn_url('paypal'); |
|
| 39 | 39 | |
| 40 | 40 | // Get the success url |
| 41 | - $return_url = add_query_arg( array( |
|
| 41 | + $return_url = add_query_arg(array( |
|
| 42 | 42 | 'payment-confirm' => 'paypal', |
| 43 | 43 | 'invoice-id' => $invoice->ID |
| 44 | - ), get_permalink( wpinv_get_option( 'success_page', false ) ) ); |
|
| 44 | + ), get_permalink(wpinv_get_option('success_page', false))); |
|
| 45 | 45 | |
| 46 | 46 | // Get the PayPal redirect uri |
| 47 | - $paypal_redirect = trailingslashit( wpinv_get_paypal_redirect() ) . '?'; |
|
| 47 | + $paypal_redirect = trailingslashit(wpinv_get_paypal_redirect()) . '?'; |
|
| 48 | 48 | |
| 49 | 49 | // Setup PayPal arguments |
| 50 | 50 | $paypal_args = array( |
| 51 | - 'business' => wpinv_get_option( 'paypal_email', false ), |
|
| 51 | + 'business' => wpinv_get_option('paypal_email', false), |
|
| 52 | 52 | 'email' => $invoice->get_email(), |
| 53 | 53 | 'first_name' => $invoice->get_first_name(), |
| 54 | 54 | 'last_name' => $invoice->get_last_name(), |
@@ -57,16 +57,16 @@ discard block |
||
| 57 | 57 | 'shipping' => '0', |
| 58 | 58 | 'no_note' => '1', |
| 59 | 59 | 'currency_code' => wpinv_get_currency(), |
| 60 | - 'charset' => get_bloginfo( 'charset' ), |
|
| 60 | + 'charset' => get_bloginfo('charset'), |
|
| 61 | 61 | 'custom' => $invoice->ID, |
| 62 | 62 | 'rm' => '2', |
| 63 | 63 | 'return' => $return_url, |
| 64 | - 'cancel_return' => wpinv_get_failed_transaction_uri( '?invoice-id=' . $invoice->ID ), |
|
| 64 | + 'cancel_return' => wpinv_get_failed_transaction_uri('?invoice-id=' . $invoice->ID), |
|
| 65 | 65 | 'notify_url' => $listener_url, |
| 66 | - 'cbt' => get_bloginfo( 'name' ), |
|
| 66 | + 'cbt' => get_bloginfo('name'), |
|
| 67 | 67 | 'bn' => 'WPInvoicing_SP', |
| 68 | 68 | 'lc' => 'US', // this will force paypal site to english |
| 69 | - 'landing_page' => apply_filters( 'wpinv_paypal_standard_landing_page', 'billing', $invoice ), // 'login' or 'billing'. login - PayPal account login, billing - Non-PayPal account. |
|
| 69 | + 'landing_page' => apply_filters('wpinv_paypal_standard_landing_page', 'billing', $invoice), // 'login' or 'billing'. login - PayPal account login, billing - Non-PayPal account. |
|
| 70 | 70 | ); |
| 71 | 71 | |
| 72 | 72 | $paypal_args['address1'] = $invoice->get_address(); |
@@ -80,57 +80,57 @@ discard block |
||
| 80 | 80 | 'upload' => '1' |
| 81 | 81 | ); |
| 82 | 82 | |
| 83 | - $paypal_args = array_merge( $paypal_extra_args, $paypal_args ); |
|
| 83 | + $paypal_args = array_merge($paypal_extra_args, $paypal_args); |
|
| 84 | 84 | |
| 85 | 85 | // Add cart items |
| 86 | 86 | $i = 1; |
| 87 | - if( is_array( $purchase_data['cart_details'] ) && ! empty( $purchase_data['cart_details'] ) ) { |
|
| 88 | - foreach ( $purchase_data['cart_details'] as $item ) { |
|
| 87 | + if (is_array($purchase_data['cart_details']) && !empty($purchase_data['cart_details'])) { |
|
| 88 | + foreach ($purchase_data['cart_details'] as $item) { |
|
| 89 | 89 | $item['quantity'] = $item['quantity'] > 0 ? $item['quantity'] : 1; |
| 90 | - $item_amount = wpinv_sanitize_amount( $item['subtotal'] / $item['quantity'], 2 ); |
|
| 90 | + $item_amount = wpinv_sanitize_amount($item['subtotal'] / $item['quantity'], 2); |
|
| 91 | 91 | |
| 92 | - if ( $item_amount <= 0 ) { |
|
| 92 | + if ($item_amount <= 0) { |
|
| 93 | 93 | $item_amount = 0; |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | - $paypal_args['item_number_' . $i ] = $item['id']; |
|
| 97 | - $paypal_args['item_name_' . $i ] = stripslashes_deep( html_entity_decode( wpinv_get_cart_item_name( $item ), ENT_COMPAT, 'UTF-8' ) ); |
|
| 98 | - $paypal_args['quantity_' . $i ] = $item['quantity']; |
|
| 99 | - $paypal_args['amount_' . $i ] = $item_amount; |
|
| 100 | - $paypal_args['discount_amount_' . $i ] = wpinv_sanitize_amount( $item['discount'], 2 ); |
|
| 96 | + $paypal_args['item_number_' . $i] = $item['id']; |
|
| 97 | + $paypal_args['item_name_' . $i] = stripslashes_deep(html_entity_decode(wpinv_get_cart_item_name($item), ENT_COMPAT, 'UTF-8')); |
|
| 98 | + $paypal_args['quantity_' . $i] = $item['quantity']; |
|
| 99 | + $paypal_args['amount_' . $i] = $item_amount; |
|
| 100 | + $paypal_args['discount_amount_' . $i] = wpinv_sanitize_amount($item['discount'], 2); |
|
| 101 | 101 | |
| 102 | 102 | $i++; |
| 103 | 103 | } |
| 104 | 104 | } |
| 105 | 105 | |
| 106 | 106 | // Add taxes to the cart |
| 107 | - if ( wpinv_use_taxes() ) { |
|
| 108 | - $paypal_args['tax_cart'] = wpinv_sanitize_amount( (float)$invoice->get_tax(), 2 ); |
|
| 107 | + if (wpinv_use_taxes()) { |
|
| 108 | + $paypal_args['tax_cart'] = wpinv_sanitize_amount((float)$invoice->get_tax(), 2); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | - $paypal_args = apply_filters( 'wpinv_paypal_args', $paypal_args, $purchase_data, $invoice ); |
|
| 111 | + $paypal_args = apply_filters('wpinv_paypal_args', $paypal_args, $purchase_data, $invoice); |
|
| 112 | 112 | |
| 113 | 113 | // Build query |
| 114 | - $paypal_redirect .= http_build_query( $paypal_args ); |
|
| 114 | + $paypal_redirect .= http_build_query($paypal_args); |
|
| 115 | 115 | |
| 116 | 116 | // Fix for some sites that encode the entities |
| 117 | - $paypal_redirect = str_replace( '&', '&', $paypal_redirect ); |
|
| 117 | + $paypal_redirect = str_replace('&', '&', $paypal_redirect); |
|
| 118 | 118 | |
| 119 | 119 | // Get rid of cart contents |
| 120 | 120 | wpinv_empty_cart(); |
| 121 | 121 | |
| 122 | 122 | // Redirect to PayPal |
| 123 | - wp_redirect( $paypal_redirect ); |
|
| 123 | + wp_redirect($paypal_redirect); |
|
| 124 | 124 | exit; |
| 125 | 125 | } |
| 126 | 126 | } |
| 127 | -add_action( 'wpinv_gateway_paypal', 'wpinv_process_paypal_payment' ); |
|
| 127 | +add_action('wpinv_gateway_paypal', 'wpinv_process_paypal_payment'); |
|
| 128 | 128 | |
| 129 | -function wpinv_get_paypal_recurring_args( $paypal_args, $purchase_data, $invoice ) { |
|
| 130 | - if ( $invoice->is_recurring() && $item_id = $invoice->get_recurring() ) { |
|
| 131 | - $item = new WPInv_Item( $item_id ); |
|
| 129 | +function wpinv_get_paypal_recurring_args($paypal_args, $purchase_data, $invoice) { |
|
| 130 | + if ($invoice->is_recurring() && $item_id = $invoice->get_recurring()) { |
|
| 131 | + $item = new WPInv_Item($item_id); |
|
| 132 | 132 | |
| 133 | - if ( empty( $item ) ) { |
|
| 133 | + if (empty($item)) { |
|
| 134 | 134 | return $paypal_args; |
| 135 | 135 | } |
| 136 | 136 | |
@@ -138,24 +138,24 @@ discard block |
||
| 138 | 138 | $interval = $item->get_recurring_interval(); |
| 139 | 139 | $bill_times = (int)$item->get_recurring_limit(); |
| 140 | 140 | |
| 141 | - $initial_amount = wpinv_sanitize_amount( $invoice->get_total(), 2 ); |
|
| 142 | - $recurring_amount = wpinv_sanitize_amount( $invoice->get_recurring_details( 'total' ), 2 ); |
|
| 141 | + $initial_amount = wpinv_sanitize_amount($invoice->get_total(), 2); |
|
| 142 | + $recurring_amount = wpinv_sanitize_amount($invoice->get_recurring_details('total'), 2); |
|
| 143 | 143 | |
| 144 | 144 | $paypal_args['cmd'] = '_xclick-subscriptions'; |
| 145 | 145 | $paypal_args['sra'] = '1'; |
| 146 | 146 | $paypal_args['src'] = '1'; |
| 147 | 147 | |
| 148 | 148 | // Set item description |
| 149 | - $paypal_args['item_name'] = stripslashes_deep( html_entity_decode( wpinv_get_cart_item_name( array( 'id' => $item->ID ) ), ENT_COMPAT, 'UTF-8' ) ); |
|
| 149 | + $paypal_args['item_name'] = stripslashes_deep(html_entity_decode(wpinv_get_cart_item_name(array('id' => $item->ID)), ENT_COMPAT, 'UTF-8')); |
|
| 150 | 150 | |
| 151 | - if ( $invoice->is_free_trial() && $item->has_free_trial() ) { |
|
| 151 | + if ($invoice->is_free_trial() && $item->has_free_trial()) { |
|
| 152 | 152 | $paypal_args['a1'] = $initial_amount; |
| 153 | 153 | $paypal_args['p1'] = $item->get_trial_interval(); |
| 154 | 154 | $paypal_args['t1'] = $item->get_trial_period(); |
| 155 | 155 | |
| 156 | 156 | // Set the recurring amount |
| 157 | 157 | $paypal_args['a3'] = $recurring_amount; |
| 158 | - } else if ( $initial_amount != $recurring_amount && $bill_times != 1 ) { |
|
| 158 | + } else if ($initial_amount != $recurring_amount && $bill_times != 1) { |
|
| 159 | 159 | $paypal_args['a1'] = $initial_amount; |
| 160 | 160 | $paypal_args['p1'] = $interval; |
| 161 | 161 | $paypal_args['t1'] = $period; |
@@ -163,63 +163,63 @@ discard block |
||
| 163 | 163 | // Set the recurring amount |
| 164 | 164 | $paypal_args['a3'] = $recurring_amount; |
| 165 | 165 | |
| 166 | - if ( $bill_times > 1 ) { |
|
| 166 | + if ($bill_times > 1) { |
|
| 167 | 167 | $bill_times--; |
| 168 | 168 | } |
| 169 | 169 | } else { |
| 170 | - $paypal_args['a3'] = $initial_amount; |
|
| 170 | + $paypal_args['a3'] = $initial_amount; |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | $paypal_args['p3'] = $interval; |
| 174 | 174 | $paypal_args['t3'] = $period; |
| 175 | 175 | |
| 176 | - if ( $bill_times > 1 ) { |
|
| 176 | + if ($bill_times > 1) { |
|
| 177 | 177 | // Make sure it's not over the max of 52 |
| 178 | - $paypal_args['srt'] = ( $bill_times <= 52 ? absint( $bill_times ) : 52 ); |
|
| 178 | + $paypal_args['srt'] = ($bill_times <= 52 ? absint($bill_times) : 52); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | // Remove cart items |
| 182 | 182 | $i = 1; |
| 183 | - if( is_array( $purchase_data['cart_details'] ) && ! empty( $purchase_data['cart_details'] ) ) { |
|
| 184 | - foreach ( $purchase_data['cart_details'] as $item ) { |
|
| 185 | - if ( isset( $paypal_args['item_number_' . $i] ) ) { |
|
| 186 | - unset( $paypal_args['item_number_' . $i] ); |
|
| 183 | + if (is_array($purchase_data['cart_details']) && !empty($purchase_data['cart_details'])) { |
|
| 184 | + foreach ($purchase_data['cart_details'] as $item) { |
|
| 185 | + if (isset($paypal_args['item_number_' . $i])) { |
|
| 186 | + unset($paypal_args['item_number_' . $i]); |
|
| 187 | 187 | } |
| 188 | - if ( isset( $paypal_args['item_name_' . $i] ) ) { |
|
| 189 | - unset( $paypal_args['item_name_' . $i] ); |
|
| 188 | + if (isset($paypal_args['item_name_' . $i])) { |
|
| 189 | + unset($paypal_args['item_name_' . $i]); |
|
| 190 | 190 | } |
| 191 | - if ( isset( $paypal_args['quantity_' . $i] ) ) { |
|
| 192 | - unset( $paypal_args['quantity_' . $i] ); |
|
| 191 | + if (isset($paypal_args['quantity_' . $i])) { |
|
| 192 | + unset($paypal_args['quantity_' . $i]); |
|
| 193 | 193 | } |
| 194 | - if ( isset( $paypal_args['amount_' . $i] ) ) { |
|
| 195 | - unset( $paypal_args['amount_' . $i] ); |
|
| 194 | + if (isset($paypal_args['amount_' . $i])) { |
|
| 195 | + unset($paypal_args['amount_' . $i]); |
|
| 196 | 196 | } |
| 197 | - if ( isset( $paypal_args['discount_amount_' . $i] ) ) { |
|
| 198 | - unset( $paypal_args['discount_amount_' . $i] ); |
|
| 197 | + if (isset($paypal_args['discount_amount_' . $i])) { |
|
| 198 | + unset($paypal_args['discount_amount_' . $i]); |
|
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 201 | $i++; |
| 202 | 202 | } |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | - if ( isset( $paypal_args['tax_cart'] ) ) { |
|
| 206 | - unset( $paypal_args['tax_cart'] ); |
|
| 205 | + if (isset($paypal_args['tax_cart'])) { |
|
| 206 | + unset($paypal_args['tax_cart']); |
|
| 207 | 207 | } |
| 208 | 208 | |
| 209 | - if ( isset( $paypal_args['upload'] ) ) { |
|
| 210 | - unset( $paypal_args['upload'] ); |
|
| 209 | + if (isset($paypal_args['upload'])) { |
|
| 210 | + unset($paypal_args['upload']); |
|
| 211 | 211 | } |
| 212 | 212 | |
| 213 | - $paypal_args = apply_filters( 'wpinv_paypal_recurring_args', $paypal_args, $purchase_data, $invoice ); |
|
| 213 | + $paypal_args = apply_filters('wpinv_paypal_recurring_args', $paypal_args, $purchase_data, $invoice); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | return $paypal_args; |
| 217 | 217 | } |
| 218 | -add_filter( 'wpinv_paypal_args', 'wpinv_get_paypal_recurring_args', 10, 3 ); |
|
| 218 | +add_filter('wpinv_paypal_args', 'wpinv_get_paypal_recurring_args', 10, 3); |
|
| 219 | 219 | |
| 220 | 220 | function wpinv_process_paypal_ipn() { |
| 221 | 221 | // Check the request method is POST |
| 222 | - if ( isset( $_SERVER['REQUEST_METHOD'] ) && $_SERVER['REQUEST_METHOD'] != 'POST' ) { |
|
| 222 | + if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] != 'POST') { |
|
| 223 | 223 | return; |
| 224 | 224 | } |
| 225 | 225 | |
@@ -227,11 +227,11 @@ discard block |
||
| 227 | 227 | $post_data = ''; |
| 228 | 228 | |
| 229 | 229 | // Fallback just in case post_max_size is lower than needed |
| 230 | - if ( ini_get( 'allow_url_fopen' ) ) { |
|
| 231 | - $post_data = file_get_contents( 'php://input' ); |
|
| 230 | + if (ini_get('allow_url_fopen')) { |
|
| 231 | + $post_data = file_get_contents('php://input'); |
|
| 232 | 232 | } else { |
| 233 | 233 | // If allow_url_fopen is not enabled, then make sure that post_max_size is large enough |
| 234 | - ini_set( 'post_max_size', '12M' ); |
|
| 234 | + ini_set('post_max_size', '12M'); |
|
| 235 | 235 | } |
| 236 | 236 | // Start the encoded data collection with notification command |
| 237 | 237 | $encoded_data = 'cmd=_notify-validate'; |
@@ -240,43 +240,43 @@ discard block |
||
| 240 | 240 | $arg_separator = wpinv_get_php_arg_separator_output(); |
| 241 | 241 | |
| 242 | 242 | // Verify there is a post_data |
| 243 | - if ( $post_data || strlen( $post_data ) > 0 ) { |
|
| 243 | + if ($post_data || strlen($post_data) > 0) { |
|
| 244 | 244 | // Append the data |
| 245 | - $encoded_data .= $arg_separator.$post_data; |
|
| 245 | + $encoded_data .= $arg_separator . $post_data; |
|
| 246 | 246 | } else { |
| 247 | 247 | // Check if POST is empty |
| 248 | - if ( empty( $_POST ) ) { |
|
| 248 | + if (empty($_POST)) { |
|
| 249 | 249 | // Nothing to do |
| 250 | 250 | return; |
| 251 | 251 | } else { |
| 252 | 252 | // Loop through each POST |
| 253 | - foreach ( $_POST as $key => $value ) { |
|
| 253 | + foreach ($_POST as $key => $value) { |
|
| 254 | 254 | // Encode the value and append the data |
| 255 | - $encoded_data .= $arg_separator."$key=" . urlencode( $value ); |
|
| 255 | + $encoded_data .= $arg_separator . "$key=" . urlencode($value); |
|
| 256 | 256 | } |
| 257 | 257 | } |
| 258 | 258 | } |
| 259 | 259 | |
| 260 | 260 | // Convert collected post data to an array |
| 261 | - parse_str( $encoded_data, $encoded_data_array ); |
|
| 261 | + parse_str($encoded_data, $encoded_data_array); |
|
| 262 | 262 | |
| 263 | - foreach ( $encoded_data_array as $key => $value ) { |
|
| 264 | - if ( false !== strpos( $key, 'amp;' ) ) { |
|
| 265 | - $new_key = str_replace( '&', '&', $key ); |
|
| 266 | - $new_key = str_replace( 'amp;', '&' , $new_key ); |
|
| 263 | + foreach ($encoded_data_array as $key => $value) { |
|
| 264 | + if (false !== strpos($key, 'amp;')) { |
|
| 265 | + $new_key = str_replace('&', '&', $key); |
|
| 266 | + $new_key = str_replace('amp;', '&', $new_key); |
|
| 267 | 267 | |
| 268 | - unset( $encoded_data_array[ $key ] ); |
|
| 269 | - $encoded_data_array[ $new_key ] = $value; |
|
| 268 | + unset($encoded_data_array[$key]); |
|
| 269 | + $encoded_data_array[$new_key] = $value; |
|
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | |
| 273 | 273 | // Get the PayPal redirect uri |
| 274 | - $paypal_redirect = wpinv_get_paypal_redirect( true ); |
|
| 274 | + $paypal_redirect = wpinv_get_paypal_redirect(true); |
|
| 275 | 275 | |
| 276 | - if ( !wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
| 276 | + if (!wpinv_get_option('disable_paypal_verification', false)) { |
|
| 277 | 277 | // Validate the IPN |
| 278 | 278 | |
| 279 | - $remote_post_vars = array( |
|
| 279 | + $remote_post_vars = array( |
|
| 280 | 280 | 'method' => 'POST', |
| 281 | 281 | 'timeout' => 45, |
| 282 | 282 | 'redirection' => 5, |
@@ -294,21 +294,21 @@ discard block |
||
| 294 | 294 | ); |
| 295 | 295 | |
| 296 | 296 | // Get response |
| 297 | - $api_response = wp_remote_post( wpinv_get_paypal_redirect(), $remote_post_vars ); |
|
| 297 | + $api_response = wp_remote_post(wpinv_get_paypal_redirect(), $remote_post_vars); |
|
| 298 | 298 | |
| 299 | - if ( is_wp_error( $api_response ) ) { |
|
| 300 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
| 299 | + if (is_wp_error($api_response)) { |
|
| 300 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid IPN verification response. IPN data: %s', 'invoicing'), json_encode($api_response))); |
|
| 301 | 301 | return; // Something went wrong |
| 302 | 302 | } |
| 303 | 303 | |
| 304 | - if ( $api_response['body'] !== 'VERIFIED' && wpinv_get_option( 'disable_paypal_verification', false ) ) { |
|
| 305 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid IPN verification response. IPN data: %s', 'invoicing' ), json_encode( $api_response ) ) ); |
|
| 304 | + if ($api_response['body'] !== 'VERIFIED' && wpinv_get_option('disable_paypal_verification', false)) { |
|
| 305 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid IPN verification response. IPN data: %s', 'invoicing'), json_encode($api_response))); |
|
| 306 | 306 | return; // Response not okay |
| 307 | 307 | } |
| 308 | 308 | } |
| 309 | 309 | |
| 310 | 310 | // Check if $post_data_array has been populated |
| 311 | - if ( !is_array( $encoded_data_array ) && !empty( $encoded_data_array ) ) |
|
| 311 | + if (!is_array($encoded_data_array) && !empty($encoded_data_array)) |
|
| 312 | 312 | return; |
| 313 | 313 | |
| 314 | 314 | $defaults = array( |
@@ -316,215 +316,215 @@ discard block |
||
| 316 | 316 | 'payment_status' => '' |
| 317 | 317 | ); |
| 318 | 318 | |
| 319 | - $encoded_data_array = wp_parse_args( $encoded_data_array, $defaults ); |
|
| 319 | + $encoded_data_array = wp_parse_args($encoded_data_array, $defaults); |
|
| 320 | 320 | |
| 321 | - $invoice_id = isset( $encoded_data_array['custom'] ) ? absint( $encoded_data_array['custom'] ) : 0; |
|
| 321 | + $invoice_id = isset($encoded_data_array['custom']) ? absint($encoded_data_array['custom']) : 0; |
|
| 322 | 322 | |
| 323 | - wpinv_error_log( $encoded_data_array['txn_type'], 'PayPal txn_type', __FILE__, __LINE__ ); |
|
| 323 | + wpinv_error_log($encoded_data_array['txn_type'], 'PayPal txn_type', __FILE__, __LINE__); |
|
| 324 | 324 | |
| 325 | - if ( has_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'] ) ) { |
|
| 325 | + if (has_action('wpinv_paypal_' . $encoded_data_array['txn_type'])) { |
|
| 326 | 326 | // Allow PayPal IPN types to be processed separately |
| 327 | - do_action( 'wpinv_paypal_' . $encoded_data_array['txn_type'], $encoded_data_array, $invoice_id ); |
|
| 327 | + do_action('wpinv_paypal_' . $encoded_data_array['txn_type'], $encoded_data_array, $invoice_id); |
|
| 328 | 328 | } else { |
| 329 | 329 | // Fallback to web accept just in case the txn_type isn't present |
| 330 | - do_action( 'wpinv_paypal_web_accept', $encoded_data_array, $invoice_id ); |
|
| 330 | + do_action('wpinv_paypal_web_accept', $encoded_data_array, $invoice_id); |
|
| 331 | 331 | } |
| 332 | 332 | exit; |
| 333 | 333 | } |
| 334 | -add_action( 'wpinv_verify_paypal_ipn', 'wpinv_process_paypal_ipn' ); |
|
| 334 | +add_action('wpinv_verify_paypal_ipn', 'wpinv_process_paypal_ipn'); |
|
| 335 | 335 | |
| 336 | -function wpinv_process_paypal_web_accept_and_cart( $data, $invoice_id ) { |
|
| 337 | - if ( $data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded' ) { |
|
| 336 | +function wpinv_process_paypal_web_accept_and_cart($data, $invoice_id) { |
|
| 337 | + if ($data['txn_type'] != 'web_accept' && $data['txn_type'] != 'cart' && $data['payment_status'] != 'Refunded') { |
|
| 338 | 338 | return; |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - if( empty( $invoice_id ) ) { |
|
| 341 | + if (empty($invoice_id)) { |
|
| 342 | 342 | return; |
| 343 | 343 | } |
| 344 | 344 | |
| 345 | 345 | // Collect payment details |
| 346 | - $purchase_key = isset( $data['invoice'] ) ? $data['invoice'] : $data['item_number']; |
|
| 346 | + $purchase_key = isset($data['invoice']) ? $data['invoice'] : $data['item_number']; |
|
| 347 | 347 | $paypal_amount = $data['mc_gross']; |
| 348 | - $payment_status = strtolower( $data['payment_status'] ); |
|
| 349 | - $currency_code = strtolower( $data['mc_currency'] ); |
|
| 350 | - $business_email = isset( $data['business'] ) && is_email( $data['business'] ) ? trim( $data['business'] ) : trim( $data['receiver_email'] ); |
|
| 351 | - $payment_meta = wpinv_get_invoice_meta( $invoice_id ); |
|
| 348 | + $payment_status = strtolower($data['payment_status']); |
|
| 349 | + $currency_code = strtolower($data['mc_currency']); |
|
| 350 | + $business_email = isset($data['business']) && is_email($data['business']) ? trim($data['business']) : trim($data['receiver_email']); |
|
| 351 | + $payment_meta = wpinv_get_invoice_meta($invoice_id); |
|
| 352 | 352 | |
| 353 | - if ( wpinv_get_payment_gateway( $invoice_id ) != 'paypal' ) { |
|
| 353 | + if (wpinv_get_payment_gateway($invoice_id) != 'paypal') { |
|
| 354 | 354 | return; // this isn't a PayPal standard IPN |
| 355 | 355 | } |
| 356 | 356 | |
| 357 | 357 | // Verify payment recipient |
| 358 | - if ( strcasecmp( $business_email, trim( wpinv_get_option( 'paypal_email', false ) ) ) != 0 ) { |
|
| 359 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid business email in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 360 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 361 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid PayPal business email.', 'invoicing' ) ); |
|
| 358 | + if (strcasecmp($business_email, trim(wpinv_get_option('paypal_email', false))) != 0) { |
|
| 359 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid business email in IPN response. IPN data: %s', 'invoicing'), json_encode($data)), $invoice_id); |
|
| 360 | + wpinv_update_payment_status($invoice_id, 'wpi-failed'); |
|
| 361 | + wpinv_insert_payment_note($invoice_id, __('Payment failed due to invalid PayPal business email.', 'invoicing')); |
|
| 362 | 362 | return; |
| 363 | 363 | } |
| 364 | 364 | |
| 365 | 365 | // Verify payment currency |
| 366 | - if ( $currency_code != strtolower( $payment_meta['currency'] ) ) { |
|
| 367 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid currency in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 368 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 369 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid currency in PayPal IPN.', 'invoicing' ) ); |
|
| 366 | + if ($currency_code != strtolower($payment_meta['currency'])) { |
|
| 367 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid currency in IPN response. IPN data: %s', 'invoicing'), json_encode($data)), $invoice_id); |
|
| 368 | + wpinv_update_payment_status($invoice_id, 'wpi-failed'); |
|
| 369 | + wpinv_insert_payment_note($invoice_id, __('Payment failed due to invalid currency in PayPal IPN.', 'invoicing')); |
|
| 370 | 370 | return; |
| 371 | 371 | } |
| 372 | 372 | |
| 373 | - if ( !wpinv_get_payment_user_email( $invoice_id ) ) { |
|
| 373 | + if (!wpinv_get_payment_user_email($invoice_id)) { |
|
| 374 | 374 | // This runs when a Buy Now purchase was made. It bypasses checkout so no personal info is collected until PayPal |
| 375 | 375 | // No email associated with purchase, so store from PayPal |
| 376 | - wpinv_update_invoice_meta( $invoice_id, '_wpinv_email', $data['payer_email'] ); |
|
| 376 | + wpinv_update_invoice_meta($invoice_id, '_wpinv_email', $data['payer_email']); |
|
| 377 | 377 | |
| 378 | 378 | // Setup and store the customer's details |
| 379 | 379 | $user_info = array( |
| 380 | 380 | 'user_id' => '-1', |
| 381 | - 'email' => sanitize_text_field( $data['payer_email'] ), |
|
| 382 | - 'first_name' => sanitize_text_field( $data['first_name'] ), |
|
| 383 | - 'last_name' => sanitize_text_field( $data['last_name'] ), |
|
| 381 | + 'email' => sanitize_text_field($data['payer_email']), |
|
| 382 | + 'first_name' => sanitize_text_field($data['first_name']), |
|
| 383 | + 'last_name' => sanitize_text_field($data['last_name']), |
|
| 384 | 384 | 'discount' => '', |
| 385 | 385 | ); |
| 386 | - $user_info['address'] = ! empty( $data['address_street'] ) ? sanitize_text_field( $data['address_street'] ) : false; |
|
| 387 | - $user_info['city'] = ! empty( $data['address_city'] ) ? sanitize_text_field( $data['address_city'] ) : false; |
|
| 388 | - $user_info['state'] = ! empty( $data['address_state'] ) ? sanitize_text_field( $data['address_state'] ) : false; |
|
| 389 | - $user_info['country'] = ! empty( $data['address_country_code'] ) ? sanitize_text_field( $data['address_country_code'] ) : false; |
|
| 390 | - $user_info['zip'] = ! empty( $data['address_zip'] ) ? sanitize_text_field( $data['address_zip'] ) : false; |
|
| 386 | + $user_info['address'] = !empty($data['address_street']) ? sanitize_text_field($data['address_street']) : false; |
|
| 387 | + $user_info['city'] = !empty($data['address_city']) ? sanitize_text_field($data['address_city']) : false; |
|
| 388 | + $user_info['state'] = !empty($data['address_state']) ? sanitize_text_field($data['address_state']) : false; |
|
| 389 | + $user_info['country'] = !empty($data['address_country_code']) ? sanitize_text_field($data['address_country_code']) : false; |
|
| 390 | + $user_info['zip'] = !empty($data['address_zip']) ? sanitize_text_field($data['address_zip']) : false; |
|
| 391 | 391 | |
| 392 | 392 | $payment_meta['user_info'] = $user_info; |
| 393 | - wpinv_update_invoice_meta( $invoice_id, '_wpinv_payment_meta', $payment_meta ); |
|
| 393 | + wpinv_update_invoice_meta($invoice_id, '_wpinv_payment_meta', $payment_meta); |
|
| 394 | 394 | } |
| 395 | 395 | |
| 396 | - if ( $payment_status == 'refunded' || $payment_status == 'reversed' ) { |
|
| 396 | + if ($payment_status == 'refunded' || $payment_status == 'reversed') { |
|
| 397 | 397 | // Process a refund |
| 398 | - wpinv_process_paypal_refund( $data, $invoice_id ); |
|
| 398 | + wpinv_process_paypal_refund($data, $invoice_id); |
|
| 399 | 399 | } else { |
| 400 | - if ( get_post_status( $invoice_id ) == 'publish' ) { |
|
| 400 | + if (get_post_status($invoice_id) == 'publish') { |
|
| 401 | 401 | return; // Only paid payments once |
| 402 | 402 | } |
| 403 | 403 | |
| 404 | 404 | // Retrieve the total purchase amount (before PayPal) |
| 405 | - $payment_amount = wpinv_payment_total( $invoice_id ); |
|
| 405 | + $payment_amount = wpinv_payment_total($invoice_id); |
|
| 406 | 406 | |
| 407 | - if ( number_format( (float) $paypal_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
| 407 | + if (number_format((float)$paypal_amount, 2) < number_format((float)$payment_amount, 2)) { |
|
| 408 | 408 | // The prices don't match |
| 409 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid payment amount in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 410 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 411 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid amount in PayPal IPN.', 'invoicing' ) ); |
|
| 409 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid payment amount in IPN response. IPN data: %s', 'invoicing'), json_encode($data)), $invoice_id); |
|
| 410 | + wpinv_update_payment_status($invoice_id, 'wpi-failed'); |
|
| 411 | + wpinv_insert_payment_note($invoice_id, __('Payment failed due to invalid amount in PayPal IPN.', 'invoicing')); |
|
| 412 | 412 | return; |
| 413 | 413 | } |
| 414 | - if ( $purchase_key != wpinv_get_payment_key( $invoice_id ) ) { |
|
| 414 | + if ($purchase_key != wpinv_get_payment_key($invoice_id)) { |
|
| 415 | 415 | // Purchase keys don't match |
| 416 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid purchase key in IPN response. IPN data: %s', 'invoicing' ), json_encode( $data ) ), $invoice_id ); |
|
| 417 | - wpinv_update_payment_status( $invoice_id, 'wpi-failed' ); |
|
| 418 | - wpinv_insert_payment_note( $invoice_id, __( 'Payment failed due to invalid purchase key in PayPal IPN.', 'invoicing' ) ); |
|
| 416 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid purchase key in IPN response. IPN data: %s', 'invoicing'), json_encode($data)), $invoice_id); |
|
| 417 | + wpinv_update_payment_status($invoice_id, 'wpi-failed'); |
|
| 418 | + wpinv_insert_payment_note($invoice_id, __('Payment failed due to invalid purchase key in PayPal IPN.', 'invoicing')); |
|
| 419 | 419 | return; |
| 420 | 420 | } |
| 421 | 421 | |
| 422 | - if ( 'complete' == $payment_status || 'completed' == $payment_status || 'processed' == $payment_status || wpinv_is_test_mode( 'paypal' ) ) { |
|
| 423 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $data['txn_id'] ) ); |
|
| 424 | - wpinv_set_payment_transaction_id( $invoice_id, $data['txn_id'] ); |
|
| 425 | - wpinv_update_payment_status( $invoice_id, 'publish' ); |
|
| 426 | - } else if ( 'wpi-pending' == $payment_status && isset( $data['pending_reason'] ) ) { |
|
| 422 | + if ('complete' == $payment_status || 'completed' == $payment_status || 'processed' == $payment_status || wpinv_is_test_mode('paypal')) { |
|
| 423 | + wpinv_insert_payment_note($invoice_id, sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $data['txn_id'])); |
|
| 424 | + wpinv_set_payment_transaction_id($invoice_id, $data['txn_id']); |
|
| 425 | + wpinv_update_payment_status($invoice_id, 'publish'); |
|
| 426 | + } else if ('wpi-pending' == $payment_status && isset($data['pending_reason'])) { |
|
| 427 | 427 | // Look for possible pending reasons, such as an echeck |
| 428 | 428 | $note = ''; |
| 429 | 429 | |
| 430 | - switch( strtolower( $data['pending_reason'] ) ) { |
|
| 430 | + switch (strtolower($data['pending_reason'])) { |
|
| 431 | 431 | case 'echeck' : |
| 432 | - $note = __( 'Payment made via eCheck and will clear automatically in 5-8 days', 'invoicing' ); |
|
| 432 | + $note = __('Payment made via eCheck and will clear automatically in 5-8 days', 'invoicing'); |
|
| 433 | 433 | break; |
| 434 | 434 | |
| 435 | 435 | case 'address' : |
| 436 | - $note = __( 'Payment requires a confirmed customer address and must be accepted manually through PayPal', 'invoicing' ); |
|
| 436 | + $note = __('Payment requires a confirmed customer address and must be accepted manually through PayPal', 'invoicing'); |
|
| 437 | 437 | break; |
| 438 | 438 | |
| 439 | 439 | case 'intl' : |
| 440 | - $note = __( 'Payment must be accepted manually through PayPal due to international account regulations', 'invoicing' ); |
|
| 440 | + $note = __('Payment must be accepted manually through PayPal due to international account regulations', 'invoicing'); |
|
| 441 | 441 | break; |
| 442 | 442 | |
| 443 | 443 | case 'multi-currency' : |
| 444 | - $note = __( 'Payment received in non-shop currency and must be accepted manually through PayPal', 'invoicing' ); |
|
| 444 | + $note = __('Payment received in non-shop currency and must be accepted manually through PayPal', 'invoicing'); |
|
| 445 | 445 | break; |
| 446 | 446 | |
| 447 | 447 | case 'paymentreview' : |
| 448 | 448 | case 'regulatory_review' : |
| 449 | - $note = __( 'Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'invoicing' ); |
|
| 449 | + $note = __('Payment is being reviewed by PayPal staff as high-risk or in possible violation of government regulations', 'invoicing'); |
|
| 450 | 450 | break; |
| 451 | 451 | |
| 452 | 452 | case 'unilateral' : |
| 453 | - $note = __( 'Payment was sent to non-confirmed or non-registered email address.', 'invoicing' ); |
|
| 453 | + $note = __('Payment was sent to non-confirmed or non-registered email address.', 'invoicing'); |
|
| 454 | 454 | break; |
| 455 | 455 | |
| 456 | 456 | case 'upgrade' : |
| 457 | - $note = __( 'PayPal account must be upgraded before this payment can be accepted', 'invoicing' ); |
|
| 457 | + $note = __('PayPal account must be upgraded before this payment can be accepted', 'invoicing'); |
|
| 458 | 458 | break; |
| 459 | 459 | |
| 460 | 460 | case 'verify' : |
| 461 | - $note = __( 'PayPal account is not verified. Verify account in order to accept this payment', 'invoicing' ); |
|
| 461 | + $note = __('PayPal account is not verified. Verify account in order to accept this payment', 'invoicing'); |
|
| 462 | 462 | break; |
| 463 | 463 | |
| 464 | 464 | case 'other' : |
| 465 | - $note = __( 'Payment is pending for unknown reasons. Contact PayPal support for assistance', 'invoicing' ); |
|
| 465 | + $note = __('Payment is pending for unknown reasons. Contact PayPal support for assistance', 'invoicing'); |
|
| 466 | 466 | break; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - if ( ! empty( $note ) ) { |
|
| 470 | - wpinv_insert_payment_note( $invoice_id, $note ); |
|
| 469 | + if (!empty($note)) { |
|
| 470 | + wpinv_insert_payment_note($invoice_id, $note); |
|
| 471 | 471 | } |
| 472 | 472 | } else { |
| 473 | - wpinv_insert_payment_note( $invoice_id, wp_sprintf( __( 'PayPal IPN has been received with invalid payment status: %s', 'invoicing' ), $payment_status ) ); |
|
| 473 | + wpinv_insert_payment_note($invoice_id, wp_sprintf(__('PayPal IPN has been received with invalid payment status: %s', 'invoicing'), $payment_status)); |
|
| 474 | 474 | } |
| 475 | 475 | } |
| 476 | 476 | } |
| 477 | -add_action( 'wpinv_paypal_web_accept', 'wpinv_process_paypal_web_accept_and_cart', 10, 2 ); |
|
| 477 | +add_action('wpinv_paypal_web_accept', 'wpinv_process_paypal_web_accept_and_cart', 10, 2); |
|
| 478 | 478 | |
| 479 | 479 | // Process PayPal subscription sign ups |
| 480 | -add_action( 'wpinv_paypal_subscr_signup', 'wpinv_process_paypal_subscr_signup' ); |
|
| 480 | +add_action('wpinv_paypal_subscr_signup', 'wpinv_process_paypal_subscr_signup'); |
|
| 481 | 481 | |
| 482 | 482 | // Process PayPal subscription payments |
| 483 | -add_action( 'wpinv_paypal_subscr_payment', 'wpinv_process_paypal_subscr_payment' ); |
|
| 483 | +add_action('wpinv_paypal_subscr_payment', 'wpinv_process_paypal_subscr_payment'); |
|
| 484 | 484 | |
| 485 | 485 | // Process PayPal subscription cancellations |
| 486 | -add_action( 'wpinv_paypal_subscr_cancel', 'wpinv_process_paypal_subscr_cancel' ); |
|
| 486 | +add_action('wpinv_paypal_subscr_cancel', 'wpinv_process_paypal_subscr_cancel'); |
|
| 487 | 487 | |
| 488 | 488 | // Process PayPal subscription end of term notices |
| 489 | -add_action( 'wpinv_paypal_subscr_eot', 'wpinv_process_paypal_subscr_eot' ); |
|
| 489 | +add_action('wpinv_paypal_subscr_eot', 'wpinv_process_paypal_subscr_eot'); |
|
| 490 | 490 | |
| 491 | 491 | // Process PayPal payment failed |
| 492 | -add_action( 'wpinv_paypal_subscr_failed', 'wpinv_process_paypal_subscr_failed' ); |
|
| 492 | +add_action('wpinv_paypal_subscr_failed', 'wpinv_process_paypal_subscr_failed'); |
|
| 493 | 493 | |
| 494 | 494 | |
| 495 | 495 | /** |
| 496 | 496 | * Process the subscription started IPN. |
| 497 | 497 | */ |
| 498 | -function wpinv_process_paypal_subscr_signup( $ipn_data ) { |
|
| 499 | - $parent_invoice_id = absint( $ipn_data['custom'] ); |
|
| 500 | - if( empty( $parent_invoice_id ) ) { |
|
| 498 | +function wpinv_process_paypal_subscr_signup($ipn_data) { |
|
| 499 | + $parent_invoice_id = absint($ipn_data['custom']); |
|
| 500 | + if (empty($parent_invoice_id)) { |
|
| 501 | 501 | return; |
| 502 | 502 | } |
| 503 | 503 | |
| 504 | - $invoice = wpinv_get_invoice( $parent_invoice_id ); |
|
| 505 | - if ( empty( $invoice ) ) { |
|
| 504 | + $invoice = wpinv_get_invoice($parent_invoice_id); |
|
| 505 | + if (empty($invoice)) { |
|
| 506 | 506 | return; |
| 507 | 507 | } |
| 508 | 508 | |
| 509 | - if ( $invoice->is_free_trial() && !empty( $ipn_data['invoice'] ) ) { |
|
| 510 | - wpinv_insert_payment_note( $parent_invoice_id, sprintf( __( 'PayPal Invoice ID: %s', 'invoicing' ) , $ipn_data['invoice'] ) ); |
|
| 511 | - wpinv_set_payment_transaction_id( $parent_invoice_id, $ipn_data['invoice'] ); |
|
| 509 | + if ($invoice->is_free_trial() && !empty($ipn_data['invoice'])) { |
|
| 510 | + wpinv_insert_payment_note($parent_invoice_id, sprintf(__('PayPal Invoice ID: %s', 'invoicing'), $ipn_data['invoice'])); |
|
| 511 | + wpinv_set_payment_transaction_id($parent_invoice_id, $ipn_data['invoice']); |
|
| 512 | 512 | } |
| 513 | 513 | |
| 514 | - wpinv_update_payment_status( $parent_invoice_id, 'publish' ); |
|
| 514 | + wpinv_update_payment_status($parent_invoice_id, 'publish'); |
|
| 515 | 515 | sleep(1); |
| 516 | - wpinv_insert_payment_note( $parent_invoice_id, sprintf( __( 'PayPal Subscription ID: %s', 'invoicing' ) , $ipn_data['subscr_id'] ) ); |
|
| 516 | + wpinv_insert_payment_note($parent_invoice_id, sprintf(__('PayPal Subscription ID: %s', 'invoicing'), $ipn_data['subscr_id'])); |
|
| 517 | 517 | |
| 518 | - $subscription = wpinv_get_paypal_subscription( $ipn_data ); |
|
| 519 | - if ( false === $subscription ) { |
|
| 518 | + $subscription = wpinv_get_paypal_subscription($ipn_data); |
|
| 519 | + if (false === $subscription) { |
|
| 520 | 520 | return; |
| 521 | 521 | } |
| 522 | 522 | |
| 523 | - $cart_details = $invoice->cart_details; |
|
| 523 | + $cart_details = $invoice->cart_details; |
|
| 524 | 524 | |
| 525 | - if ( !empty( $cart_details ) ) { |
|
| 526 | - foreach ( $cart_details as $cart_item ) { |
|
| 527 | - $item = new WPInv_Item( $cart_item['id'] ); |
|
| 525 | + if (!empty($cart_details)) { |
|
| 526 | + foreach ($cart_details as $cart_item) { |
|
| 527 | + $item = new WPInv_Item($cart_item['id']); |
|
| 528 | 528 | |
| 529 | 529 | $status = $invoice->is_free_trial() && $item->has_free_trial() ? 'trialing' : 'active'; |
| 530 | 530 | |
@@ -533,15 +533,15 @@ discard block |
||
| 533 | 533 | 'status' => $status, |
| 534 | 534 | 'period' => $item->get_recurring_period(), |
| 535 | 535 | 'initial_amount' => $invoice->get_total(), |
| 536 | - 'recurring_amount' => $invoice->get_recurring_details( 'total' ), |
|
| 536 | + 'recurring_amount' => $invoice->get_recurring_details('total'), |
|
| 537 | 537 | 'interval' => $item->get_recurring_interval(), |
| 538 | 538 | 'bill_times' => $item->get_recurring_limit(), |
| 539 | - 'expiration' => $invoice->get_new_expiration( $cart_item['id'] ), |
|
| 539 | + 'expiration' => $invoice->get_new_expiration($cart_item['id']), |
|
| 540 | 540 | 'profile_id' => $ipn_data['subscr_id'], |
| 541 | - 'created' => date_i18n( 'Y-m-d H:i:s', strtotime( $ipn_data['subscr_date'] ) ) |
|
| 541 | + 'created' => date_i18n('Y-m-d H:i:s', strtotime($ipn_data['subscr_date'])) |
|
| 542 | 542 | ); |
| 543 | 543 | |
| 544 | - if ( $item->has_free_trial() ) { |
|
| 544 | + if ($item->has_free_trial()) { |
|
| 545 | 545 | $args['trial_period'] = $item->get_trial_period(); |
| 546 | 546 | $args['trial_interval'] = $item->get_trial_interval(); |
| 547 | 547 | } else { |
@@ -550,7 +550,7 @@ discard block |
||
| 550 | 550 | } |
| 551 | 551 | |
| 552 | 552 | |
| 553 | - $subscription->update_subscription( $args ); |
|
| 553 | + $subscription->update_subscription($args); |
|
| 554 | 554 | } |
| 555 | 555 | } |
| 556 | 556 | } |
@@ -558,39 +558,39 @@ discard block |
||
| 558 | 558 | /** |
| 559 | 559 | * Process the subscription payment received IPN. |
| 560 | 560 | */ |
| 561 | -function wpinv_process_paypal_subscr_payment( $ipn_data ) { |
|
| 562 | - $parent_invoice_id = absint( $ipn_data['custom'] ); |
|
| 561 | +function wpinv_process_paypal_subscr_payment($ipn_data) { |
|
| 562 | + $parent_invoice_id = absint($ipn_data['custom']); |
|
| 563 | 563 | |
| 564 | - $subscription = wpinv_get_paypal_subscription( $ipn_data ); |
|
| 565 | - if ( false === $subscription ) { |
|
| 564 | + $subscription = wpinv_get_paypal_subscription($ipn_data); |
|
| 565 | + if (false === $subscription) { |
|
| 566 | 566 | return; |
| 567 | 567 | } |
| 568 | 568 | |
| 569 | - $transaction_id = wpinv_get_payment_transaction_id( $parent_invoice_id ); |
|
| 570 | - $signup_date = strtotime( $subscription->get_subscription_created() ); |
|
| 571 | - $today = date_i18n( 'Y-m-d', $signup_date ) == date_i18n( 'Y-m-d', strtotime( $ipn_data['payment_date'] ) ); |
|
| 569 | + $transaction_id = wpinv_get_payment_transaction_id($parent_invoice_id); |
|
| 570 | + $signup_date = strtotime($subscription->get_subscription_created()); |
|
| 571 | + $today = date_i18n('Y-m-d', $signup_date) == date_i18n('Y-m-d', strtotime($ipn_data['payment_date'])); |
|
| 572 | 572 | |
| 573 | 573 | // Look to see if payment is same day as signup and we have set the transaction ID on the parent payment yet. |
| 574 | - if ( $today && ( !$transaction_id || $transaction_id == $parent_invoice_id ) ) { |
|
| 575 | - wpinv_update_payment_status( $parent_invoice_id, 'publish' ); |
|
| 574 | + if ($today && (!$transaction_id || $transaction_id == $parent_invoice_id)) { |
|
| 575 | + wpinv_update_payment_status($parent_invoice_id, 'publish'); |
|
| 576 | 576 | sleep(1); |
| 577 | 577 | |
| 578 | 578 | // This is the very first payment |
| 579 | - wpinv_set_payment_transaction_id( $parent_invoice_id, $ipn_data['txn_id'] ); |
|
| 580 | - wpinv_insert_payment_note( $parent_invoice_id, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $ipn_data['txn_id'] ) ); |
|
| 579 | + wpinv_set_payment_transaction_id($parent_invoice_id, $ipn_data['txn_id']); |
|
| 580 | + wpinv_insert_payment_note($parent_invoice_id, sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $ipn_data['txn_id'])); |
|
| 581 | 581 | return; |
| 582 | 582 | } |
| 583 | 583 | |
| 584 | - if ( wpinv_get_id_by_transaction_id( $ipn_data['txn_id'] ) ) { |
|
| 584 | + if (wpinv_get_id_by_transaction_id($ipn_data['txn_id'])) { |
|
| 585 | 585 | return; // Payment already recorded |
| 586 | 586 | } |
| 587 | 587 | |
| 588 | - $currency_code = strtolower( $ipn_data['mc_currency'] ); |
|
| 588 | + $currency_code = strtolower($ipn_data['mc_currency']); |
|
| 589 | 589 | |
| 590 | 590 | // verify details |
| 591 | - if ( $currency_code != strtolower( wpinv_get_currency() ) ) { |
|
| 591 | + if ($currency_code != strtolower(wpinv_get_currency())) { |
|
| 592 | 592 | // the currency code is invalid |
| 593 | - wpinv_record_gateway_error( __( 'IPN Error', 'invoicing' ), sprintf( __( 'Invalid currency in IPN response. IPN data: ', 'invoicing' ), json_encode( $ipn_data ) ) ); |
|
| 593 | + wpinv_record_gateway_error(__('IPN Error', 'invoicing'), sprintf(__('Invalid currency in IPN response. IPN data: ', 'invoicing'), json_encode($ipn_data))); |
|
| 594 | 594 | return; |
| 595 | 595 | } |
| 596 | 596 | |
@@ -599,11 +599,11 @@ discard block |
||
| 599 | 599 | 'transaction_id' => $ipn_data['txn_id'] |
| 600 | 600 | ); |
| 601 | 601 | |
| 602 | - $invoice = wpinv_recurring_add_subscription_payment( $parent_invoice_id, $args ); |
|
| 602 | + $invoice = wpinv_recurring_add_subscription_payment($parent_invoice_id, $args); |
|
| 603 | 603 | |
| 604 | - if ( !empty( $invoice ) ) { |
|
| 604 | + if (!empty($invoice)) { |
|
| 605 | 605 | sleep(1); |
| 606 | - wpinv_insert_payment_note( $invoice->ID, sprintf( __( 'PayPal Transaction ID: %s', 'invoicing' ) , $ipn_data['txn_id'] ) ); |
|
| 606 | + wpinv_insert_payment_note($invoice->ID, sprintf(__('PayPal Transaction ID: %s', 'invoicing'), $ipn_data['txn_id'])); |
|
| 607 | 607 | |
| 608 | 608 | $invoice->renew_subscription(); |
| 609 | 609 | } |
@@ -612,10 +612,10 @@ discard block |
||
| 612 | 612 | /** |
| 613 | 613 | * Process the subscription canceled IPN. |
| 614 | 614 | */ |
| 615 | -function wpinv_process_paypal_subscr_cancel( $ipn_data ) { |
|
| 616 | - $subscription = wpinv_get_paypal_subscription( $ipn_data ); |
|
| 615 | +function wpinv_process_paypal_subscr_cancel($ipn_data) { |
|
| 616 | + $subscription = wpinv_get_paypal_subscription($ipn_data); |
|
| 617 | 617 | |
| 618 | - if( false === $subscription ) { |
|
| 618 | + if (false === $subscription) { |
|
| 619 | 619 | return; |
| 620 | 620 | } |
| 621 | 621 | |
@@ -625,10 +625,10 @@ discard block |
||
| 625 | 625 | /** |
| 626 | 626 | * Process the subscription expired IPN. |
| 627 | 627 | */ |
| 628 | -function wpinv_process_paypal_subscr_eot( $ipn_data ) { |
|
| 629 | - $subscription = wpinv_get_paypal_subscription( $ipn_data ); |
|
| 628 | +function wpinv_process_paypal_subscr_eot($ipn_data) { |
|
| 629 | + $subscription = wpinv_get_paypal_subscription($ipn_data); |
|
| 630 | 630 | |
| 631 | - if( false === $subscription ) { |
|
| 631 | + if (false === $subscription) { |
|
| 632 | 632 | return; |
| 633 | 633 | } |
| 634 | 634 | |
@@ -638,45 +638,45 @@ discard block |
||
| 638 | 638 | /** |
| 639 | 639 | * Process the subscription payment failed IPN. |
| 640 | 640 | */ |
| 641 | -function wpinv_process_paypal_subscr_failed( $ipn_data ) { |
|
| 642 | - $subscription = wpinv_get_paypal_subscription( $ipn_data ); |
|
| 641 | +function wpinv_process_paypal_subscr_failed($ipn_data) { |
|
| 642 | + $subscription = wpinv_get_paypal_subscription($ipn_data); |
|
| 643 | 643 | |
| 644 | - if( false === $subscription ) { |
|
| 644 | + if (false === $subscription) { |
|
| 645 | 645 | return; |
| 646 | 646 | } |
| 647 | 647 | |
| 648 | 648 | $subscription->failing_subscription(); |
| 649 | 649 | |
| 650 | - do_action( 'wpinv_recurring_payment_failed', $subscription ); |
|
| 650 | + do_action('wpinv_recurring_payment_failed', $subscription); |
|
| 651 | 651 | } |
| 652 | 652 | |
| 653 | 653 | /** |
| 654 | 654 | * Retrieve the subscription this IPN notice is for. |
| 655 | 655 | */ |
| 656 | -function wpinv_get_paypal_subscription( $ipn_data = array() ) { |
|
| 657 | - $parent_invoice_id = absint( $ipn_data['custom'] ); |
|
| 656 | +function wpinv_get_paypal_subscription($ipn_data = array()) { |
|
| 657 | + $parent_invoice_id = absint($ipn_data['custom']); |
|
| 658 | 658 | |
| 659 | - if( empty( $parent_invoice_id ) ) { |
|
| 659 | + if (empty($parent_invoice_id)) { |
|
| 660 | 660 | return false; |
| 661 | 661 | } |
| 662 | 662 | |
| 663 | - $invoice = wpinv_get_invoice( $parent_invoice_id ); |
|
| 664 | - if ( empty( $invoice ) ) { |
|
| 663 | + $invoice = wpinv_get_invoice($parent_invoice_id); |
|
| 664 | + if (empty($invoice)) { |
|
| 665 | 665 | return false; |
| 666 | 666 | } |
| 667 | 667 | |
| 668 | - $subscription = wpinv_get_subscription( $ipn_data['subscr_id'], true ); |
|
| 668 | + $subscription = wpinv_get_subscription($ipn_data['subscr_id'], true); |
|
| 669 | 669 | |
| 670 | - if ( empty( $subscription ) ) { |
|
| 671 | - $subs = wpinv_get_subscriptions( array( 'parent_invoice_id' => $parent_invoice_id, 'numberposts' => 1 ) ); |
|
| 672 | - $subscription = reset( $subs ); |
|
| 670 | + if (empty($subscription)) { |
|
| 671 | + $subs = wpinv_get_subscriptions(array('parent_invoice_id' => $parent_invoice_id, 'numberposts' => 1)); |
|
| 672 | + $subscription = reset($subs); |
|
| 673 | 673 | |
| 674 | - if ( $subscription && $subscription->ID > 0 ) { |
|
| 674 | + if ($subscription && $subscription->ID > 0) { |
|
| 675 | 675 | // Update the profile ID so it is set for future renewals |
| 676 | - $subscription->update_subscription( array( 'profile_id' => sanitize_text_field( $ipn_data['subscr_id'] ) ) ); |
|
| 676 | + $subscription->update_subscription(array('profile_id' => sanitize_text_field($ipn_data['subscr_id']))); |
|
| 677 | 677 | } else { |
| 678 | 678 | $subscription = $invoice; |
| 679 | - $subscription->update_subscription( array( 'profile_id' => sanitize_text_field( $ipn_data['subscr_id'] ) ) ); |
|
| 679 | + $subscription->update_subscription(array('profile_id' => sanitize_text_field($ipn_data['subscr_id']))); |
|
| 680 | 680 | // No subscription found with a matching payment ID, bail |
| 681 | 681 | //return false; |
| 682 | 682 | } |
@@ -686,39 +686,39 @@ discard block |
||
| 686 | 686 | |
| 687 | 687 | } |
| 688 | 688 | |
| 689 | -function wpinv_process_paypal_refund( $data, $invoice_id = 0 ) { |
|
| 689 | +function wpinv_process_paypal_refund($data, $invoice_id = 0) { |
|
| 690 | 690 | // Collect payment details |
| 691 | 691 | |
| 692 | - if( empty( $invoice_id ) ) { |
|
| 692 | + if (empty($invoice_id)) { |
|
| 693 | 693 | return; |
| 694 | 694 | } |
| 695 | 695 | |
| 696 | - if ( get_post_status( $invoice_id ) == 'wpi-refunded' ) { |
|
| 696 | + if (get_post_status($invoice_id) == 'wpi-refunded') { |
|
| 697 | 697 | return; // Only refund payments once |
| 698 | 698 | } |
| 699 | 699 | |
| 700 | - $payment_amount = wpinv_payment_total( $invoice_id ); |
|
| 700 | + $payment_amount = wpinv_payment_total($invoice_id); |
|
| 701 | 701 | $refund_amount = $data['mc_gross'] * -1; |
| 702 | 702 | |
| 703 | - if ( number_format( (float) $refund_amount, 2 ) < number_format( (float) $payment_amount, 2 ) ) { |
|
| 704 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'Partial PayPal refund processed: %s', 'invoicing' ), $data['parent_txn_id'] ) ); |
|
| 703 | + if (number_format((float)$refund_amount, 2) < number_format((float)$payment_amount, 2)) { |
|
| 704 | + wpinv_insert_payment_note($invoice_id, sprintf(__('Partial PayPal refund processed: %s', 'invoicing'), $data['parent_txn_id'])); |
|
| 705 | 705 | return; // This is a partial refund |
| 706 | 706 | } |
| 707 | 707 | |
| 708 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Payment #%s Refunded for reason: %s', 'invoicing' ), $data['parent_txn_id'], $data['reason_code'] ) ); |
|
| 709 | - wpinv_insert_payment_note( $invoice_id, sprintf( __( 'PayPal Refund Transaction ID: %s', 'invoicing' ), $data['txn_id'] ) ); |
|
| 710 | - wpinv_update_payment_status( $invoice_id, 'wpi-refunded' ); |
|
| 708 | + wpinv_insert_payment_note($invoice_id, sprintf(__('PayPal Payment #%s Refunded for reason: %s', 'invoicing'), $data['parent_txn_id'], $data['reason_code'])); |
|
| 709 | + wpinv_insert_payment_note($invoice_id, sprintf(__('PayPal Refund Transaction ID: %s', 'invoicing'), $data['txn_id'])); |
|
| 710 | + wpinv_update_payment_status($invoice_id, 'wpi-refunded'); |
|
| 711 | 711 | } |
| 712 | 712 | |
| 713 | -function wpinv_get_paypal_redirect( $ssl_check = false ) { |
|
| 714 | - if ( is_ssl() || ! $ssl_check ) { |
|
| 713 | +function wpinv_get_paypal_redirect($ssl_check = false) { |
|
| 714 | + if (is_ssl() || !$ssl_check) { |
|
| 715 | 715 | $protocol = 'https://'; |
| 716 | 716 | } else { |
| 717 | 717 | $protocol = 'http://'; |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | // Check the current payment mode |
| 721 | - if ( wpinv_is_test_mode( 'paypal' ) ) { |
|
| 721 | + if (wpinv_is_test_mode('paypal')) { |
|
| 722 | 722 | // Test mode |
| 723 | 723 | $paypal_uri = $protocol . 'www.sandbox.paypal.com/cgi-bin/webscr'; |
| 724 | 724 | } else { |
@@ -726,67 +726,67 @@ discard block |
||
| 726 | 726 | $paypal_uri = $protocol . 'www.paypal.com/cgi-bin/webscr'; |
| 727 | 727 | } |
| 728 | 728 | |
| 729 | - return apply_filters( 'wpinv_paypal_uri', $paypal_uri ); |
|
| 729 | + return apply_filters('wpinv_paypal_uri', $paypal_uri); |
|
| 730 | 730 | } |
| 731 | 731 | |
| 732 | -function wpinv_paypal_success_page_content( $content ) { |
|
| 732 | +function wpinv_paypal_success_page_content($content) { |
|
| 733 | 733 | global $wpi_invoice; |
| 734 | 734 | |
| 735 | 735 | $session = wpinv_get_checkout_session(); |
| 736 | 736 | |
| 737 | - if ( empty( $_GET['invoice-id'] ) && empty( $session['invoice_key'] ) ) { |
|
| 737 | + if (empty($_GET['invoice-id']) && empty($session['invoice_key'])) { |
|
| 738 | 738 | return $content; |
| 739 | 739 | } |
| 740 | 740 | |
| 741 | - $invoice_id = !empty( $_GET['invoice-id'] ) ? absint( $_GET['invoice-id'] ) : wpinv_get_invoice_id_by_key( $session['invoice_key'] ); |
|
| 741 | + $invoice_id = !empty($_GET['invoice-id']) ? absint($_GET['invoice-id']) : wpinv_get_invoice_id_by_key($session['invoice_key']); |
|
| 742 | 742 | |
| 743 | - if ( empty( $invoice_id ) ) { |
|
| 743 | + if (empty($invoice_id)) { |
|
| 744 | 744 | return $content; |
| 745 | 745 | } |
| 746 | 746 | |
| 747 | - $wpi_invoice = wpinv_get_invoice( $invoice_id ); |
|
| 747 | + $wpi_invoice = wpinv_get_invoice($invoice_id); |
|
| 748 | 748 | |
| 749 | - if ( !empty( $wpi_invoice ) && 'wpi-pending' == $wpi_invoice->status ) { |
|
| 749 | + if (!empty($wpi_invoice) && 'wpi-pending' == $wpi_invoice->status) { |
|
| 750 | 750 | // Payment is still pending so show processing indicator to fix the Race Condition, issue # |
| 751 | 751 | ob_start(); |
| 752 | - wpinv_get_template_part( 'wpinv-payment-processing' ); |
|
| 752 | + wpinv_get_template_part('wpinv-payment-processing'); |
|
| 753 | 753 | $content = ob_get_clean(); |
| 754 | 754 | } |
| 755 | 755 | |
| 756 | 756 | return $content; |
| 757 | 757 | } |
| 758 | -add_filter( 'wpinv_payment_confirm_paypal', 'wpinv_paypal_success_page_content' ); |
|
| 758 | +add_filter('wpinv_payment_confirm_paypal', 'wpinv_paypal_success_page_content'); |
|
| 759 | 759 | |
| 760 | -function wpinv_paypal_get_transaction_id( $invoice_id ) { |
|
| 760 | +function wpinv_paypal_get_transaction_id($invoice_id) { |
|
| 761 | 761 | $transaction_id = ''; |
| 762 | - $notes = wpinv_get_invoice_notes( $invoice_id ); |
|
| 762 | + $notes = wpinv_get_invoice_notes($invoice_id); |
|
| 763 | 763 | |
| 764 | - foreach ( $notes as $note ) { |
|
| 765 | - if ( preg_match( '/^PayPal Transaction ID: ([^\s]+)/', $note->comment_content, $match ) ) { |
|
| 764 | + foreach ($notes as $note) { |
|
| 765 | + if (preg_match('/^PayPal Transaction ID: ([^\s]+)/', $note->comment_content, $match)) { |
|
| 766 | 766 | $transaction_id = $match[1]; |
| 767 | 767 | continue; |
| 768 | 768 | } |
| 769 | 769 | } |
| 770 | 770 | |
| 771 | - return apply_filters( 'wpinv_paypal_set_transaction_id', $transaction_id, $invoice_id ); |
|
| 771 | + return apply_filters('wpinv_paypal_set_transaction_id', $transaction_id, $invoice_id); |
|
| 772 | 772 | } |
| 773 | -add_filter( 'wpinv_payment_get_transaction_id-paypal', 'wpinv_paypal_get_transaction_id', 10, 1 ); |
|
| 773 | +add_filter('wpinv_payment_get_transaction_id-paypal', 'wpinv_paypal_get_transaction_id', 10, 1); |
|
| 774 | 774 | |
| 775 | -function wpinv_paypal_link_transaction_id( $transaction_id, $invoice_id, $invoice ) { |
|
| 776 | - if ( $invoice->is_free_trial() || $transaction_id == $invoice_id ) { // Free trial does not have transaction at PayPal. |
|
| 775 | +function wpinv_paypal_link_transaction_id($transaction_id, $invoice_id, $invoice) { |
|
| 776 | + if ($invoice->is_free_trial() || $transaction_id == $invoice_id) { // Free trial does not have transaction at PayPal. |
|
| 777 | 777 | $transaction_url = $invoice->get_view_url(); |
| 778 | 778 | } else { |
| 779 | - $sandbox = wpinv_is_test_mode( 'paypal' ) ? '.sandbox' : ''; |
|
| 779 | + $sandbox = wpinv_is_test_mode('paypal') ? '.sandbox' : ''; |
|
| 780 | 780 | $transaction_url = 'https://www' . $sandbox . '.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=' . $transaction_id; |
| 781 | 781 | } |
| 782 | 782 | |
| 783 | - $transaction_link = '<a href="' . esc_url( $transaction_url ) . '" target="_blank">' . $transaction_id . '</a>'; |
|
| 783 | + $transaction_link = '<a href="' . esc_url($transaction_url) . '" target="_blank">' . $transaction_id . '</a>'; |
|
| 784 | 784 | |
| 785 | - return apply_filters( 'wpinv_paypal_link_payment_details_transaction_id', $transaction_link, $invoice ); |
|
| 785 | + return apply_filters('wpinv_paypal_link_payment_details_transaction_id', $transaction_link, $invoice); |
|
| 786 | 786 | } |
| 787 | -add_filter( 'wpinv_payment_details_transaction_id-paypal', 'wpinv_paypal_link_transaction_id', 10, 3 ); |
|
| 787 | +add_filter('wpinv_payment_details_transaction_id-paypal', 'wpinv_paypal_link_transaction_id', 10, 3); |
|
| 788 | 788 | |
| 789 | 789 | function wpinv_gateway_paypal_button_label($label) { |
| 790 | - return __( 'Proceed to PayPal', 'invoicing' ); |
|
| 790 | + return __('Proceed to PayPal', 'invoicing'); |
|
| 791 | 791 | } |
| 792 | -add_filter( 'wpinv_gateway_paypal_button_label', 'wpinv_gateway_paypal_button_label', 10, 1 ); |
|
| 793 | 792 | \ No newline at end of file |
| 793 | +add_filter('wpinv_gateway_paypal_button_label', 'wpinv_gateway_paypal_button_label', 10, 1); |
|
| 794 | 794 | \ No newline at end of file |