2012-05-26 16:25:07 +00:00
< ? php
2013-02-20 17:14:46 +00:00
2014-09-20 19:42:58 +00:00
if ( ! defined ( 'ABSPATH' ) ) {
2014-11-19 22:22:32 +00:00
exit ;
2014-09-20 19:42:58 +00:00
}
2013-02-20 17:14:46 +00:00
2012-05-26 16:25:07 +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 .
*
* @ class WC_Paypal
2012-12-31 18:25:09 +00:00
* @ extends WC_Gateway_Paypal
2014-10-21 21:31:37 +00:00
* @ version 2.3 . 0
2012-08-15 18:15:06 +00:00
* @ package WooCommerce / Classes / Payment
* @ author WooThemes
2012-05-26 16:25:07 +00:00
*/
2012-12-31 18:25:09 +00:00
class WC_Gateway_Paypal extends WC_Payment_Gateway {
2012-11-27 16:22:47 +00:00
2013-12-31 12:45:02 +00:00
/**
* Constructor for the gateway .
*/
2012-08-10 13:09:02 +00:00
public function __construct () {
2014-10-23 13:27:24 +00:00
$this -> id = 'paypal' ;
$this -> has_fields = false ;
$this -> order_button_text = __ ( 'Proceed to PayPal' , 'woocommerce' );
$this -> method_title = __ ( 'PayPal' , 'woocommerce' );
2015-01-28 17:15:43 +00:00
$this -> method_description = __ ( 'PayPal standard works by sending customers to PayPal where they can 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' ,
'refunds'
);
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
2012-05-26 16:25:07 +00:00
// Define user set variables
2015-01-26 12:58:33 +00:00
$this -> title = $this -> get_option ( 'title' );
$this -> description = $this -> get_option ( 'description' );
$this -> testmode = 'yes' === $this -> get_option ( 'testmode' , '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' );
2012-08-10 13:09:02 +00:00
2012-12-31 18:25:09 +00:00
add_action ( 'woocommerce_update_options_payment_gateways_' . $this -> id , array ( $this , 'process_admin_options' ) );
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 {
2015-01-28 17:06:59 +00:00
include_once ( '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 ) {
2014-11-19 14:34:34 +00:00
include_once ( '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
}
2013-12-31 12:45:02 +00:00
}
2012-08-10 13:09:02 +00:00
2014-11-18 12:39:56 +00:00
/**
* Logging method
* @ param string $message
*/
2014-11-19 14:34:34 +00:00
public function log ( $message ) {
if ( $this -> testmode ) {
2014-11-18 12:39:56 +00:00
if ( empty ( $this -> log ) ) {
$this -> log = new WC_Logger ();
}
$this -> log -> add ( 'paypal' , $message );
}
}
2014-09-19 15:55:14 +00:00
/**
* get_icon function .
*
* @ return string
*/
public function get_icon () {
2014-11-19 14:34:34 +00:00
$icon_html = '' ;
$icon = ( array ) $this -> get_icon_image ( WC () -> countries -> get_base_country () );
foreach ( $icon as $i ) {
$icon_html .= '<img src="' . esc_attr ( $i ) . '" alt="' . __ ( 'PayPal Acceptance Mark' , 'woocommerce' ) . '" />' ;
}
2014-11-18 12:47:40 +00:00
2014-11-19 14:34:34 +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;" title="' . esc_attr__ ( 'What is PayPal?' , 'woocommerce' ) . '">' . esc_attr__ ( 'What is PayPal?' , 'woocommerce' ) . '</a>' , esc_url ( $this -> get_icon_url ( WC () -> countries -> get_base_country () ) ) );
return apply_filters ( 'woocommerce_gateway_icon' , $icon_html , $this -> id );
}
/**
* Get the link for an icon based on country
* @ param string $country
* @ return string
*/
private function get_icon_url ( $country ) {
switch ( $country ) {
case 'MX' :
2015-02-13 18:02:00 +00:00
case 'ZA' :
$link = 'https://www.paypal.com/' . strtolower ( $country ) . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside' ;
2014-11-19 14:34:34 +00:00
break ;
default :
$link = 'https://www.paypal.com/' . strtolower ( $country ) . '/webapps/mpp/paypal-popup' ;
break ;
}
2015-02-13 18:02:00 +00:00
2015-02-13 19:14:12 +00:00
return $link ;
2014-11-19 14:34:34 +00:00
}
/**
* Get PayPal images for a country
* @ param string $country
* @ return array of image URLs
*/
private function get_icon_image ( $country ) {
switch ( $country ) {
2014-09-19 15:55:14 +00:00
case 'US' :
case 'NZ' :
case 'CZ' :
case 'HU' :
case 'MY' :
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg' ;
break ;
case 'TR' :
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg' ;
break ;
case 'GB' :
$icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png' ;
break ;
case 'MX' :
$icon = array (
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png' ,
'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif'
);
break ;
case 'FR' :
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg' ;
break ;
case 'AU' :
$icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg' ;
break ;
case 'DK' :
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg' ;
break ;
case 'RU' :
$icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg' ;
break ;
case 'NO' :
$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg' ;
break ;
case 'CA' :
$icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg' ;
break ;
case 'HK' :
$icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg' ;
break ;
case 'SG' :
$icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg' ;
break ;
case 'TW' :
$icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg' ;
break ;
case 'TH' :
$icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg' ;
break ;
default :
$icon = WC_HTTPS :: force_https_url ( WC () -> plugin_url () . '/includes/gateways/paypal/assets/images/paypal.png' );
break ;
}
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
/**
* Check if this gateway is enabled and available in the user ' s country
*
* @ return bool
*/
2014-11-18 13:46:49 +00:00
public function is_valid_for_use () {
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' ) ) );
2013-12-31 12:45:02 +00:00
}
2012-08-10 13:09:02 +00:00
2012-05-26 16:25:07 +00:00
/**
2012-08-10 13:09:02 +00:00
* Admin Panel Options
2012-05-26 16:25:07 +00:00
* - Options for bits like 'title' and availability on a country - by - country basis
*
* @ 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
?>
2013-12-31 12:45:02 +00:00
< div class = " inline error " >< p >< strong >< ? php _e ( 'Gateway Disabled' , 'woocommerce' ); ?> </strong>: <?php _e( 'PayPal does not support your store currency.', 'woocommerce' ); ?></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
/**
* Initialise Gateway Settings Form Fields
*/
2014-07-07 10:44:15 +00:00
public function init_form_fields () {
2014-11-18 12:43:23 +00:00
$this -> form_fields = include ( '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 .
2012-08-15 18:15:06 +00:00
*
2014-11-18 13:46:49 +00:00
* @ param WC_Order $order
2014-07-31 05:57:13 +00:00
*
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
/**
* Process the payment and return the result
*
* @ param int $order_id
* @ return array
*/
2014-07-07 10:44:15 +00:00
public function process_payment ( $order_id ) {
2014-11-19 14:34:34 +00:00
include_once ( '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' ,
'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
/**
* Can the order be refunded via paypal ?
* @ param WC_Order $order
* @ return bool
*/
public function can_refund_order ( $order ) {
2014-11-19 14:34:34 +00:00
return $order && $order -> get_transaction_id ();
2014-11-18 16:52:55 +00:00
}
2014-07-07 10:44:15 +00:00
/**
* Process a refund if supported
* @ param int $order_id
* @ param float $amount
2014-08-07 18:57:29 +00:00
* @ param string $reason
2015-02-03 14:32:10 +00:00
* @ return boolean True or false based on success , or a WP_Error object
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 ) ) {
2014-11-20 15:03:42 +00:00
$this -> log ( 'Refund Failed: No transaction ID' );
2014-07-07 10:44:15 +00:00
return false ;
}
2012-08-10 13:09:02 +00:00
2014-11-20 15:03:42 +00:00
include_once ( 'includes/class-wc-gateway-paypal-refund.php' );
WC_Gateway_Paypal_Refund :: $api_username = $this -> get_option ( 'api_username' );
WC_Gateway_Paypal_Refund :: $api_password = $this -> get_option ( 'api_password' );
WC_Gateway_Paypal_Refund :: $api_signature = $this -> get_option ( 'api_signature' );
2012-08-10 13:09:02 +00:00
2014-11-19 14:34:34 +00:00
$result = WC_Gateway_Paypal_Refund :: refund_order ( $order , $amount , $reason , $this -> testmode );
2012-08-10 13:09:02 +00:00
2014-11-19 14:34:34 +00:00
if ( is_wp_error ( $result ) ) {
$this -> log ( 'Refund Failed: ' . $result -> get_error_message () );
return false ;
2014-07-07 10:44:15 +00:00
}
2012-08-10 13:09:02 +00:00
2014-11-20 15:03:42 +00:00
$this -> log ( 'Refund Result: ' . print_r ( $result , true ) );
2014-11-19 14:34:34 +00:00
switch ( strtolower ( $result [ 'ACK' ] ) ) {
2014-07-07 10:44:15 +00:00
case 'success' :
case 'successwithwarning' :
2014-11-19 14:34:34 +00:00
$order -> add_order_note ( sprintf ( __ ( 'Refunded %s - Refund ID: %s' , 'woocommerce' ), $result [ 'GROSSREFUNDAMT' ], $result [ 'REFUNDTRANSACTIONID' ] ) );
2014-07-07 10:44:15 +00:00
return true ;
break ;
2012-05-26 16:25:07 +00:00
}
2012-08-10 13:09:02 +00:00
2014-07-07 10:44:15 +00:00
return false ;
2012-05-26 16:25:07 +00:00
}
2013-09-23 12:36:04 +00:00
}