2012-05-26 16:25:07 +00:00
< ? php
/**
2015-11-03 13:31:20 +00:00
* PayPal Standard Payment Gateway .
2012-08-10 13:09:02 +00:00
*
2012-05-26 16:25:07 +00:00
* Provides a PayPal Standard Payment Gateway .
*
2018-03-09 12:53:10 +00:00
* @ class WC_Gateway_Paypal
* @ extends WC_Payment_Gateway
* @ version 2.3 . 0
2020-08-05 16:36:24 +00:00
* @ package WooCommerce\Classes\Payment
2012-05-26 16:25:07 +00:00
*/
2016-01-05 05:23:01 +00:00
2020-01-29 05:21:29 +00:00
use Automattic\Jetpack\Constants ;
2016-01-05 05:23:01 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* WC_Gateway_Paypal Class .
*/
2012-12-31 18:25:09 +00:00
class WC_Gateway_Paypal extends WC_Payment_Gateway {
2012-11-27 16:22:47 +00:00
2018-03-09 18:45:00 +00:00
/**
* Whether or not logging is enabled
*
* @ var bool
*/
2015-04-09 11:00:42 +00:00
public static $log_enabled = false ;
2018-03-09 18:45:00 +00:00
/**
* Logger instance
*
* @ var WC_Logger
*/
2015-04-09 11:00:42 +00:00
public static $log = false ;
2013-12-31 12:45:02 +00:00
/**
* Constructor for the gateway .
*/
2012-08-10 13:09:02 +00:00
public function __construct () {
2018-03-09 18:45:00 +00:00
$this -> id = 'paypal' ;
$this -> has_fields = false ;
$this -> order_button_text = __ ( 'Proceed to PayPal' , 'woocommerce' );
2020-08-25 02:25:44 +00:00
$this -> method_title = __ ( 'PayPal Standard' , 'woocommerce' );
2018-03-09 18:45:00 +00:00
/* translators: %s: Link to WC system status page */
2018-04-12 15:59:42 +00:00
$this -> method_description = __ ( 'PayPal Standard redirects customers to PayPal to enter their payment information.' , 'woocommerce' );
2014-10-23 13:27:24 +00:00
$this -> supports = array (
2014-07-07 10:44:15 +00:00
'products' ,
2016-08-27 02:08:49 +00:00
'refunds' ,
2014-07-07 10:44:15 +00:00
);
2012-08-10 13:09:02 +00:00
2012-05-26 16:25:07 +00:00
// Load the settings.
2013-01-02 13:38:33 +00:00
$this -> init_form_fields ();
2012-05-26 16:25:07 +00:00
$this -> init_settings ();
2012-08-10 13:09:02 +00:00
2016-01-05 05:23:01 +00:00
// Define user set variables.
2015-03-13 09:38:09 +00:00
$this -> title = $this -> get_option ( 'title' );
$this -> description = $this -> get_option ( 'description' );
$this -> testmode = 'yes' === $this -> get_option ( 'testmode' , 'no' );
$this -> debug = 'yes' === $this -> get_option ( 'debug' , 'no' );
$this -> email = $this -> get_option ( 'email' );
$this -> receiver_email = $this -> get_option ( 'receiver_email' , $this -> email );
$this -> identity_token = $this -> get_option ( 'identity_token' );
2018-04-30 17:04:52 +00:00
self :: $log_enabled = $this -> debug ;
2015-04-09 11:00:42 +00:00
2017-09-29 09:41:09 +00:00
if ( $this -> testmode ) {
2018-03-09 18:45:00 +00:00
/* translators: %s: Link to PayPal sandbox testing guide page */
2017-10-27 14:48:50 +00:00
$this -> description .= ' ' . sprintf ( __ ( 'SANDBOX ENABLED. You can use sandbox testing accounts only. See the <a href="%s">PayPal Sandbox Testing Guide</a> for more details.' , 'woocommerce' ), 'https://developer.paypal.com/docs/classic/lifecycle/ug_sandbox/' );
2017-09-29 09:41:09 +00:00
$this -> description = trim ( $this -> description );
}
2012-12-31 18:25:09 +00:00
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'process_admin_options' ) );
2019-05-07 10:57:59 +00:00
add_action ( 'woocommerce_order_status_processing' , array ( $this , 'capture_payment' ) );
add_action ( 'woocommerce_order_status_completed' , array ( $this , 'capture_payment' ) );
2020-01-28 13:13:03 +00:00
add_action ( 'admin_enqueue_scripts' , array ( $this , 'admin_scripts' ) );
2012-08-10 13:09:02 +00:00
2013-12-31 12:53:55 +00:00
if ( ! $this -> is_valid_for_use () ) {
2014-09-29 10:08:19 +00:00
$this -> enabled = 'no' ;
2014-11-19 14:34:34 +00:00
} else {
2018-03-09 12:53:10 +00:00
include_once dirname ( __FILE__ ) . '/includes/class-wc-gateway-paypal-ipn-handler.php' ;
2015-02-03 16:24:01 +00:00
new WC_Gateway_Paypal_IPN_Handler ( $this -> testmode , $this -> receiver_email );
2015-01-28 17:06:59 +00:00
if ( $this -> identity_token ) {
2018-03-09 12:53:10 +00:00
include_once dirname ( __FILE__ ) . '/includes/class-wc-gateway-paypal-pdt-handler.php' ;
2015-02-03 16:24:01 +00:00
new WC_Gateway_Paypal_PDT_Handler ( $this -> testmode , $this -> identity_token );
2014-11-19 14:34:34 +00:00
}
2013-12-31 12:53:55 +00:00
}
2020-01-28 13:07:50 +00:00
if ( 'yes' === $this -> enabled ) {
add_filter ( 'woocommerce_thankyou_order_received_text' , array ( $this , 'order_received_text' ), 10 , 2 );
}
2013-12-31 12:45:02 +00:00
}
2012-08-10 13:09:02 +00:00
2018-04-12 15:59:42 +00:00
/**
* Return whether or not this gateway still requires setup to function .
*
* When this gateway is toggled on via AJAX , if this returns true a
* redirect will occur to the settings page instead .
*
* @ since 3.4 . 0
* @ return bool
*/
public function needs_setup () {
return ! is_email ( $this -> email );
}
2014-11-18 12:39:56 +00:00
/**
2015-11-03 13:31:20 +00:00
* Logging method .
2016-11-21 19:04:59 +00:00
*
* @ param string $message Log message .
2018-03-09 18:45:00 +00:00
* @ param string $level Optional . Default 'info' . Possible values :
* emergency | alert | critical | error | warning | notice | info | debug .
2014-11-18 12:39:56 +00:00
*/
2016-11-21 19:04:59 +00:00
public static function log ( $message , $level = 'info' ) {
2015-04-09 11:00:42 +00:00
if ( self :: $log_enabled ) {
if ( empty ( self :: $log ) ) {
2016-08-08 12:59:23 +00:00
self :: $log = wc_get_logger ();
2014-11-18 12:39:56 +00:00
}
2016-12-21 19:15:19 +00:00
self :: $log -> log ( $level , $message , array ( 'source' => 'paypal' ) );
2014-11-18 12:39:56 +00:00
}
}
2018-04-30 17:04:52 +00:00
/**
* Processes and saves options .
* If there is an error thrown , will continue to save and validate fields , but will leave the erroring field out .
*
* @ return bool was anything saved ?
*/
public function process_admin_options () {
$saved = parent :: process_admin_options ();
// Maybe clear logs.
if ( 'yes' !== $this -> get_option ( 'debug' , 'no' ) ) {
if ( empty ( self :: $log ) ) {
self :: $log = wc_get_logger ();
}
self :: $log -> clear ( 'paypal' );
}
return $saved ;
}
2014-09-19 15:55:14 +00:00
/**
2016-01-05 19:19:12 +00:00
* Get gateway icon .
2018-03-09 12:53:10 +00:00
*
2014-09-19 15:55:14 +00:00
* @ return string
*/
public function get_icon () {
2018-07-16 09:44:58 +00:00
// We need a base country for the link to work, bail if in the unlikely event no country is set.
$base_country = WC () -> countries -> get_base_country ();
if ( empty ( $base_country ) ) {
return '' ;
}
2014-11-19 14:34:34 +00:00
$icon_html = '' ;
2018-07-16 09:44:58 +00:00
$icon = ( array ) $this -> get_icon_image ( $base_country );
2014-11-19 14:34:34 +00:00
foreach ( $icon as $i ) {
2016-10-12 10:16:30 +00:00
$icon_html .= '<img src="' . esc_attr ( $i ) . '" alt="' . esc_attr__ ( 'PayPal acceptance mark' , 'woocommerce' ) . '" />' ;
2014-11-19 14:34:34 +00:00
}
2014-11-18 12:47:40 +00:00
2018-07-16 09:44:58 +00:00
$icon_html .= sprintf ( '<a href="%1$s" class="about_paypal" onclick="javascript:window.open(\'%1$s\',\'WIPaypal\',\'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=1060, height=700\'); return false;">' . esc_attr__ ( 'What is PayPal?' , 'woocommerce' ) . '</a>' , esc_url ( $this -> get_icon_url ( $base_country ) ) );
2014-11-19 14:34:34 +00:00
return apply_filters ( 'woocommerce_gateway_icon' , $icon_html , $this -> id );
}
/**
2015-11-03 13:31:20 +00:00
* Get the link for an icon based on country .
2018-03-09 12:53:10 +00:00
*
2018-03-09 18:45:00 +00:00
* @ param string $country Country two letter code .
2014-11-19 14:34:34 +00:00
* @ return string
*/
2015-03-19 18:26:28 +00:00
protected function get_icon_url ( $country ) {
2016-01-21 21:28:14 +00:00
$url = 'https://www.paypal.com/' . strtolower ( $country );
2017-09-27 15:14:26 +00:00
$home_counties = array ( 'BE' , 'CZ' , 'DK' , 'HU' , 'IT' , 'JP' , 'NL' , 'NO' , 'ES' , 'SE' , 'TR' , 'IN' );
$countries = array ( 'DZ' , 'AU' , 'BH' , 'BQ' , 'BW' , 'CA' , 'CN' , 'CW' , 'FI' , 'FR' , 'DE' , 'GR' , 'HK' , 'ID' , 'JO' , 'KE' , 'KW' , 'LU' , 'MY' , 'MA' , 'OM' , 'PH' , 'PL' , 'PT' , 'QA' , 'IE' , 'RU' , 'BL' , 'SX' , 'MF' , 'SA' , 'SG' , 'SK' , 'KR' , 'SS' , 'TW' , 'TH' , 'AE' , 'GB' , 'US' , 'VN' );
2015-05-27 16:23:21 +00:00
2018-03-09 18:45:00 +00:00
if ( in_array ( $country , $home_counties , true ) ) {
2018-03-09 12:53:10 +00:00
return $url . '/webapps/mpp/home' ;
2018-03-09 18:45:00 +00:00
} elseif ( in_array ( $country , $countries , true ) ) {
2016-01-21 21:28:14 +00:00
return $url . '/webapps/mpp/paypal-popup' ;
} else {
return $url . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside' ;
2014-11-19 14:34:34 +00:00
}
}
/**
2015-11-03 13:31:20 +00:00
* Get PayPal images for a country .
2017-09-27 15:14:26 +00:00
*
* @ param string $country Country code .
2014-11-19 14:34:34 +00:00
* @ return array of image URLs
*/
2015-03-19 18:26:28 +00:00
protected function get_icon_image ( $country ) {
2014-11-19 14:34:34 +00:00
switch ( $country ) {
2018-03-09 12:53:10 +00:00
case 'US' :
case 'NZ' :
case 'CZ' :
case 'HU' :
case 'MY' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'TR' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'GB' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png' ;
2018-03-09 12:53:10 +00:00
break ;
case 'MX' :
2014-09-19 15:55:14 +00:00
$icon = array (
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png' ,
2016-08-27 02:08:49 +00:00
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif' ,
2014-09-19 15:55:14 +00:00
);
2018-03-09 12:53:10 +00:00
break ;
case 'FR' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'AU' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'DK' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'RU' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'NO' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'CA' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'HK' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'SG' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'TW' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'TH' :
2014-09-19 15:55:14 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg' ;
2018-03-09 12:53:10 +00:00
break ;
case 'JP' :
2015-05-06 21:17:19 +00:00
$icon = 'https://www.paypal.com/ja_JP/JP/i/bnr/horizontal_solution_4_jcb.gif' ;
break ;
2018-03-09 12:53:10 +00:00
case 'IN' :
2017-09-27 15:14:26 +00:00
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg' ;
break ;
2018-03-09 12:53:10 +00:00
default :
2014-09-19 15:55:14 +00:00
$icon = WC_HTTPS :: force_https_url ( WC () -> plugin_url () . '/includes/gateways/paypal/assets/images/paypal.png' );
2018-03-09 12:53:10 +00:00
break ;
2014-09-19 15:55:14 +00:00
}
2014-11-19 14:34:34 +00:00
return apply_filters ( 'woocommerce_paypal_icon' , $icon );
2014-09-19 15:55:14 +00:00
}
2013-12-31 12:45:02 +00:00
/**
2020-01-28 13:10:10 +00:00
* Check if this gateway is available in the user ' s country based on currency .
2018-03-09 12:53:10 +00:00
*
2013-12-31 12:45:02 +00:00
* @ return bool
*/
2014-11-18 13:46:49 +00:00
public function is_valid_for_use () {
2018-03-09 18:45:00 +00:00
return in_array (
get_woocommerce_currency (),
apply_filters (
'woocommerce_paypal_supported_currencies' ,
array ( 'AUD' , 'BRL' , 'CAD' , 'MXN' , 'NZD' , 'HKD' , 'SGD' , 'USD' , 'EUR' , 'JPY' , 'TRY' , 'NOK' , 'CZK' , 'DKK' , 'HUF' , 'ILS' , 'MYR' , 'PHP' , 'PLN' , 'SEK' , 'CHF' , 'TWD' , 'THB' , 'GBP' , 'RMB' , 'RUB' , 'INR' )
),
true
);
2013-12-31 12:45:02 +00:00
}
2012-08-10 13:09:02 +00:00
2012-05-26 16:25:07 +00:00
/**
2015-11-03 13:31:20 +00:00
* Admin Panel Options .
* - Options for bits like 'title' and availability on a country - by - country basis .
2012-05-26 16:25:07 +00:00
*
* @ since 1.0 . 0
*/
public function admin_options () {
2014-07-31 05:57:13 +00:00
if ( $this -> is_valid_for_use () ) {
parent :: admin_options ();
} else {
2012-12-31 12:07:43 +00:00
?>
2018-03-09 18:45:00 +00:00
< div class = " inline error " >
< p >
2020-08-25 02:25:44 +00:00
< strong >< ? php esc_html_e ( 'Gateway disabled' , 'woocommerce' ); ?> </strong>: <?php esc_html_e( 'PayPal Standard does not support your store currency.', 'woocommerce' ); ?>
2018-03-09 18:45:00 +00:00
</ p >
</ div >
2014-07-31 05:57:13 +00:00
< ? php
}
2012-12-27 10:44:58 +00:00
}
2012-08-10 13:09:02 +00:00
2013-12-31 12:45:02 +00:00
/**
2015-11-03 13:31:20 +00:00
* Initialise Gateway Settings Form Fields .
2013-12-31 12:45:02 +00:00
*/
2014-07-07 10:44:15 +00:00
public function init_form_fields () {
2020-08-26 21:46:44 +00:00
$this -> form_fields = include __DIR__ . '/includes/settings-paypal.php' ;
2013-12-31 12:45:02 +00:00
}
2012-08-10 13:09:02 +00:00
2014-02-24 14:49:01 +00:00
/**
2014-11-19 14:34:34 +00:00
* Get the transaction URL .
2018-03-09 12:53:10 +00:00
*
2018-03-09 18:45:00 +00:00
* @ param WC_Order $order Order object .
2014-11-19 14:34:34 +00:00
* @ return string
2014-11-18 13:46:49 +00:00
*/
2014-11-19 14:34:34 +00:00
public function get_transaction_url ( $order ) {
if ( $this -> testmode ) {
$this -> view_transaction_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s' ;
} else {
$this -> view_transaction_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s' ;
2014-11-18 13:46:49 +00:00
}
2014-11-19 14:34:34 +00:00
return parent :: get_transaction_url ( $order );
2012-05-26 16:25:07 +00:00
}
2013-12-31 12:45:02 +00:00
/**
2015-11-03 13:31:20 +00:00
* Process the payment and return the result .
2018-03-09 12:53:10 +00:00
*
2018-03-09 18:45:00 +00:00
* @ param int $order_id Order ID .
2013-12-31 12:45:02 +00:00
* @ return array
*/
2014-07-07 10:44:15 +00:00
public function process_payment ( $order_id ) {
2018-03-09 12:53:10 +00:00
include_once dirname ( __FILE__ ) . '/includes/class-wc-gateway-paypal-request.php' ;
2014-07-07 10:44:15 +00:00
2014-11-19 14:34:34 +00:00
$order = wc_get_order ( $order_id );
$paypal_request = new WC_Gateway_Paypal_Request ( $this );
2014-07-07 10:44:15 +00:00
return array (
2014-11-19 14:34:34 +00:00
'result' => 'success' ,
2016-08-27 01:46:45 +00:00
'redirect' => $paypal_request -> get_request_url ( $order , $this -> testmode ),
2014-07-07 10:44:15 +00:00
);
}
2012-08-10 13:09:02 +00:00
2014-11-18 16:52:55 +00:00
/**
2015-03-12 20:39:47 +00:00
* Can the order be refunded via PayPal ?
2018-03-09 12:53:10 +00:00
*
2018-03-09 18:45:00 +00:00
* @ param WC_Order $order Order object .
2014-11-18 16:52:55 +00:00
* @ return bool
*/
public function can_refund_order ( $order ) {
2018-04-03 16:09:09 +00:00
$has_api_creds = false ;
if ( $this -> testmode ) {
$has_api_creds = $this -> get_option ( 'sandbox_api_username' ) && $this -> get_option ( 'sandbox_api_password' ) && $this -> get_option ( 'sandbox_api_signature' );
} else {
$has_api_creds = $this -> get_option ( 'api_username' ) && $this -> get_option ( 'api_password' ) && $this -> get_option ( 'api_signature' );
}
return $order && $order -> get_transaction_id () && $has_api_creds ;
2014-11-18 16:52:55 +00:00
}
2016-07-27 12:48:10 +00:00
/**
* Init the API class and set the username / password etc .
*/
protected function init_api () {
2018-03-09 12:53:10 +00:00
include_once dirname ( __FILE__ ) . '/includes/class-wc-gateway-paypal-api-handler.php' ;
2016-07-27 12:48:10 +00:00
2017-09-29 09:41:09 +00:00
WC_Gateway_Paypal_API_Handler :: $api_username = $this -> testmode ? $this -> get_option ( 'sandbox_api_username' ) : $this -> get_option ( 'api_username' );
WC_Gateway_Paypal_API_Handler :: $api_password = $this -> testmode ? $this -> get_option ( 'sandbox_api_password' ) : $this -> get_option ( 'api_password' );
WC_Gateway_Paypal_API_Handler :: $api_signature = $this -> testmode ? $this -> get_option ( 'sandbox_api_signature' ) : $this -> get_option ( 'api_signature' );
2016-07-27 15:55:42 +00:00
WC_Gateway_Paypal_API_Handler :: $sandbox = $this -> testmode ;
2016-07-27 12:48:10 +00:00
}
2014-07-07 10:44:15 +00:00
/**
2015-11-03 13:31:20 +00:00
* Process a refund if supported .
2018-03-09 12:53:10 +00:00
*
2018-03-09 18:45:00 +00:00
* @ param int $order_id Order ID .
* @ param float $amount Refund amount .
* @ param string $reason Refund reason .
2017-05-15 11:50:52 +00:00
* @ return bool | WP_Error
2014-07-07 10:44:15 +00:00
*/
2014-08-11 13:07:09 +00:00
public function process_refund ( $order_id , $amount = null , $reason = '' ) {
2014-08-15 12:29:21 +00:00
$order = wc_get_order ( $order_id );
2012-08-10 13:09:02 +00:00
2014-11-18 16:52:55 +00:00
if ( ! $this -> can_refund_order ( $order ) ) {
2018-04-03 16:09:09 +00:00
return new WP_Error ( 'error' , __ ( 'Refund failed.' , 'woocommerce' ) );
2014-07-07 10:44:15 +00:00
}
2012-08-10 13:09:02 +00:00
2016-07-27 12:48:10 +00:00
$this -> init_api ();
2012-08-10 13:09:02 +00:00
2016-07-27 15:55:42 +00:00
$result = WC_Gateway_Paypal_API_Handler :: refund_transaction ( $order , $amount , $reason );
2012-08-10 13:09:02 +00:00
2014-11-19 14:34:34 +00:00
if ( is_wp_error ( $result ) ) {
2016-11-21 19:18:42 +00:00
$this -> log ( 'Refund Failed: ' . $result -> get_error_message (), 'error' );
2015-10-06 13:32:18 +00:00
return new WP_Error ( 'error' , $result -> get_error_message () );
2014-07-07 10:44:15 +00:00
}
2012-08-10 13:09:02 +00:00
2016-12-16 21:51:20 +00:00
$this -> log ( 'Refund Result: ' . wc_print_r ( $result , true ) );
2014-11-20 15:03:42 +00:00
2019-10-02 17:21:12 +00:00
switch ( strtolower ( $result -> ACK ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2014-07-07 10:44:15 +00:00
case 'success' :
case 'successwithwarning' :
2018-03-09 18:45:00 +00:00
$order -> add_order_note (
/* translators: 1: Refund amount, 2: Refund ID */
2019-10-02 17:21:12 +00:00
sprintf ( __ ( 'Refunded %1$s - Refund ID: %2$s' , 'woocommerce' ), $result -> GROSSREFUNDAMT , $result -> REFUNDTRANSACTIONID ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2018-03-09 18:45:00 +00:00
);
2014-07-07 10:44:15 +00:00
return true ;
2012-05-26 16:25:07 +00:00
}
2012-08-10 13:09:02 +00:00
2019-10-02 17:21:12 +00:00
return isset ( $result -> L_LONGMESSAGE0 ) ? new WP_Error ( 'error' , $result -> L_LONGMESSAGE0 ) : false ; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2012-05-26 16:25:07 +00:00
}
2016-07-27 12:48:10 +00:00
/**
* Capture payment when the order is changed from on - hold to complete or processing
*
2018-03-09 18:45:00 +00:00
* @ param int $order_id Order ID .
2016-07-27 12:48:10 +00:00
*/
public function capture_payment ( $order_id ) {
$order = wc_get_order ( $order_id );
2019-05-07 10:57:59 +00:00
if ( 'paypal' === $order -> get_payment_method () && 'pending' === $order -> get_meta ( '_paypal_status' , true ) && $order -> get_transaction_id () ) {
2016-07-27 12:48:10 +00:00
$this -> init_api ();
2016-07-27 15:55:42 +00:00
$result = WC_Gateway_Paypal_API_Handler :: do_capture ( $order );
if ( is_wp_error ( $result ) ) {
2016-11-21 19:18:42 +00:00
$this -> log ( 'Capture Failed: ' . $result -> get_error_message (), 'error' );
2018-03-09 18:45:00 +00:00
/* translators: %s: Paypal gateway error message */
2019-08-30 13:01:55 +00:00
$order -> add_order_note ( sprintf ( __ ( 'Payment could not be captured: %s' , 'woocommerce' ), $result -> get_error_message () ) );
2016-07-27 15:55:42 +00:00
return ;
}
2016-12-16 21:51:20 +00:00
$this -> log ( 'Capture Result: ' . wc_print_r ( $result , true ) );
2016-07-27 15:55:42 +00:00
2019-10-02 17:21:12 +00:00
// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
2016-07-27 15:55:42 +00:00
if ( ! empty ( $result -> PAYMENTSTATUS ) ) {
switch ( $result -> PAYMENTSTATUS ) {
2018-03-09 12:53:10 +00:00
case 'Completed' :
2018-03-09 18:45:00 +00:00
/* translators: 1: Amount, 2: Authorization ID, 3: Transaction ID */
2016-09-01 20:50:14 +00:00
$order -> add_order_note ( sprintf ( __ ( 'Payment of %1$s was captured - Auth ID: %2$s, Transaction ID: %3$s' , 'woocommerce' ), $result -> AMT , $result -> AUTHORIZATIONID , $result -> TRANSACTIONID ) );
2016-08-05 14:56:23 +00:00
update_post_meta ( $order -> get_id (), '_paypal_status' , $result -> PAYMENTSTATUS );
update_post_meta ( $order -> get_id (), '_transaction_id' , $result -> TRANSACTIONID );
2018-03-09 12:53:10 +00:00
break ;
default :
2018-03-09 18:45:00 +00:00
/* translators: 1: Authorization ID, 2: Payment status */
2019-08-26 05:32:40 +00:00
$order -> add_order_note ( sprintf ( __ ( 'Payment could not be captured - Auth ID: %1$s, Status: %2$s' , 'woocommerce' ), $result -> AUTHORIZATIONID , $result -> PAYMENTSTATUS ) );
2018-03-09 12:53:10 +00:00
break ;
2016-07-27 15:55:42 +00:00
}
}
2018-03-09 18:45:00 +00:00
// phpcs:enable
2016-07-27 12:48:10 +00:00
}
}
2017-09-29 09:41:09 +00:00
/**
* Load admin scripts .
*
2017-10-27 14:48:50 +00:00
* @ since 3.3 . 0
2017-09-29 09:41:09 +00:00
*/
public function admin_scripts () {
2017-10-27 14:48:50 +00:00
$screen = get_current_screen ();
2018-03-09 12:53:10 +00:00
$screen_id = $screen ? $screen -> id : '' ;
2017-10-27 14:48:50 +00:00
if ( 'woocommerce_page_wc-settings' !== $screen_id ) {
2017-09-29 09:41:09 +00:00
return ;
}
2020-02-04 22:02:57 +00:00
$suffix = Constants :: is_true ( 'SCRIPT_DEBUG' ) ? '' : '.min' ;
$version = Constants :: get_constant ( 'WC_VERSION' );
2017-09-29 09:41:09 +00:00
2020-02-04 22:02:57 +00:00
wp_enqueue_script ( 'woocommerce_paypal_admin' , WC () -> plugin_url () . '/includes/gateways/paypal/assets/js/paypal-admin' . $suffix . '.js' , array (), $version , true );
2017-09-29 09:41:09 +00:00
}
2019-10-02 17:21:12 +00:00
/**
* Custom PayPal order received text .
*
* @ since 3.9 . 0
* @ param string $text Default text .
* @ param WC_Order $order Order data .
* @ return string
*/
public function order_received_text ( $text , $order ) {
2020-01-28 13:06:02 +00:00
if ( $order && $this -> id === $order -> get_payment_method () ) {
2019-10-02 17:21:12 +00:00
return esc_html__ ( 'Thank you for your payment. Your transaction has been completed, and a receipt for your purchase has been emailed to you. Log into your PayPal account to view transaction details.' , 'woocommerce' );
}
return $text ;
}
2021-05-23 23:36:07 +00:00
/**
* Determines whether PayPal Standard should be loaded or not .
*
* By default PayPal Standard isn 't loaded on new installs or on existing sites which haven' t set up the gateway .
*
* @ since 5.5 . 0
*
* @ return bool Whether PayPal Standard should be loaded .
*/
public function should_load () {
$option_key = '_should_load' ;
$should_load = $this -> get_option ( $option_key );
if ( '' === $should_load ) {
// New installs without PayPal Standard enabled don't load it.
if ( 'no' === $this -> enabled && WC_Install :: is_new_install () ) {
$should_load = false ;
} else {
$should_load = true ;
}
$this -> update_option ( $option_key , wc_bool_to_string ( $should_load ) );
} else {
$should_load = wc_string_to_bool ( $should_load );
}
/**
* Allow third - parties to filter whether PayPal Standard should be loaded or not .
*
* @ since 5.5 . 0
*
* @ param bool $should_load Whether PayPal Standard should be loaded .
* @ param WC_Gateway_Paypal $this The WC_Gateway_Paypal instance .
*/
return apply_filters ( 'woocommerce_should_load_paypal_standard' , $should_load , $this );
}
2013-09-23 12:36:04 +00:00
}