WC_Gateway_Paypal

WC_Gateway_Paypal Class.

Defined (1)

The class is defined in the following location(s).

/includes/gateways/paypal/class-wc-gateway-paypal.php  
  1. class WC_Gateway_Paypal extends WC_Payment_Gateway { 
  2.  
  3. /** @var bool Whether or not logging is enabled */ 
  4. public static $log_enabled = false; 
  5.  
  6. /** @var WC_Logger Logger instance */ 
  7. public static $log = false; 
  8.  
  9. /** 
  10. * Constructor for the gateway. 
  11. */ 
  12. public function __construct() { 
  13. $this->id = 'paypal'; 
  14. $this->has_fields = false; 
  15. $this->order_button_text = __( 'Proceed to PayPal', 'woocommerce' ); 
  16. $this->method_title = __( 'PayPal', 'woocommerce' ); 
  17. $this->method_description = sprintf( __( 'PayPal Standard sends customers to PayPal to enter their payment information. PayPal IPN requires fsockopen/cURL support to update order statuses after payment. Check the <a href="%s">system status</a> page for more details.', 'woocommerce' ), admin_url( 'admin.php?page=wc-status' ) ); 
  18. $this->supports = array( 
  19. 'products',  
  20. 'refunds',  
  21. ); 
  22.  
  23. // Load the settings. 
  24. $this->init_form_fields(); 
  25. $this->init_settings(); 
  26.  
  27. // Define user set variables. 
  28. $this->title = $this->get_option( 'title' ); 
  29. $this->description = $this->get_option( 'description' ); 
  30. $this->testmode = 'yes' === $this->get_option( 'testmode', 'no' ); 
  31. $this->debug = 'yes' === $this->get_option( 'debug', 'no' ); 
  32. $this->email = $this->get_option( 'email' ); 
  33. $this->receiver_email = $this->get_option( 'receiver_email', $this->email ); 
  34. $this->identity_token = $this->get_option( 'identity_token' ); 
  35.  
  36. self::$log_enabled = $this->debug; 
  37.  
  38. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); 
  39. add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'capture_payment' ) ); 
  40. add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'capture_payment' ) ); 
  41.  
  42. if ( ! $this->is_valid_for_use() ) { 
  43. $this->enabled = 'no'; 
  44. } else { 
  45. include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-ipn-handler.php' ); 
  46. new WC_Gateway_Paypal_IPN_Handler( $this->testmode, $this->receiver_email ); 
  47.  
  48. if ( $this->identity_token ) { 
  49. include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-pdt-handler.php' ); 
  50. new WC_Gateway_Paypal_PDT_Handler( $this->testmode, $this->identity_token ); 
  51.  
  52. /** 
  53. * Logging method. 
  54. * @param string $message Log message. 
  55. * @param string $level Optional. Default 'info'. 
  56. * emergency|alert|critical|error|warning|notice|info|debug 
  57. */ 
  58. public static function log( $message, $level = 'info' ) { 
  59. if ( self::$log_enabled ) { 
  60. if ( empty( self::$log ) ) { 
  61. self::$log = wc_get_logger(); 
  62. self::$log->log( $level, $message, array( 'source' => 'paypal' ) ); 
  63.  
  64. /** 
  65. * Get gateway icon. 
  66. * @return string 
  67. */ 
  68. public function get_icon() { 
  69. $icon_html = ''; 
  70. $icon = (array) $this->get_icon_image( WC()->countries->get_base_country() ); 
  71.  
  72. foreach ( $icon as $i ) { 
  73. $icon_html .= '<img src="' . esc_attr( $i ) . '" alt="' . esc_attr__( 'PayPal acceptance mark', 'woocommerce' ) . '" />'; 
  74.  
  75. $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( WC()->countries->get_base_country() ) ) ); 
  76.  
  77. return apply_filters( 'woocommerce_gateway_icon', $icon_html, $this->id ); 
  78.  
  79. /** 
  80. * Get the link for an icon based on country. 
  81. * @param string $country 
  82. * @return string 
  83. */ 
  84. protected function get_icon_url( $country ) { 
  85. $url = 'https://www.paypal.com/' . strtolower( $country ); 
  86. $home_counties = array( 'BE', 'CZ', 'DK', 'HU', 'IT', 'JP', 'NL', 'NO', 'ES', 'SE', 'TR' ); 
  87. $countries = array( 'DZ', 'AU', 'BH', 'BQ', 'BW', 'CA', 'CN', 'CW', 'FI', 'FR', 'DE', 'GR', 'HK', 'IN', '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' ); 
  88.  
  89. if ( in_array( $country, $home_counties ) ) { 
  90. return $url . '/webapps/mpp/home'; 
  91. } elseif ( in_array( $country, $countries ) ) { 
  92. return $url . '/webapps/mpp/paypal-popup'; 
  93. } else { 
  94. return $url . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside'; 
  95.  
  96. /** 
  97. * Get PayPal images for a country. 
  98. * @param string $country 
  99. * @return array of image URLs 
  100. */ 
  101. protected function get_icon_image( $country ) { 
  102. switch ( $country ) { 
  103. case 'US' : 
  104. case 'NZ' : 
  105. case 'CZ' : 
  106. case 'HU' : 
  107. case 'MY' : 
  108. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg'; 
  109. break; 
  110. case 'TR' : 
  111. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg'; 
  112. break; 
  113. case 'GB' : 
  114. $icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png'; 
  115. break; 
  116. case 'MX' : 
  117. $icon = array( 
  118. 'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png',  
  119. 'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif',  
  120. ); 
  121. break; 
  122. case 'FR' : 
  123. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg'; 
  124. break; 
  125. case 'AU' : 
  126. $icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg'; 
  127. break; 
  128. case 'DK' : 
  129. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg'; 
  130. break; 
  131. case 'RU' : 
  132. $icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg'; 
  133. break; 
  134. case 'NO' : 
  135. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg'; 
  136. break; 
  137. case 'CA' : 
  138. $icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg'; 
  139. break; 
  140. case 'HK' : 
  141. $icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg'; 
  142. break; 
  143. case 'SG' : 
  144. $icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg'; 
  145. break; 
  146. case 'TW' : 
  147. $icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg'; 
  148. break; 
  149. case 'TH' : 
  150. $icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg'; 
  151. break; 
  152. case 'JP' : 
  153. $icon = 'https://www.paypal.com/ja_JP/JP/i/bnr/horizontal_solution_4_jcb.gif'; 
  154. break; 
  155. default : 
  156. $icon = WC_HTTPS::force_https_url( WC()->plugin_url() . '/includes/gateways/paypal/assets/images/paypal.png' ); 
  157. break; 
  158. return apply_filters( 'woocommerce_paypal_icon', $icon ); 
  159.  
  160. /** 
  161. * Check if this gateway is enabled and available in the user's country. 
  162. * @return bool 
  163. */ 
  164. public function is_valid_for_use() { 
  165. 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' ) ) ); 
  166.  
  167. /** 
  168. * Admin Panel Options. 
  169. * - Options for bits like 'title' and availability on a country-by-country basis. 
  170. * @since 1.0.0 
  171. */ 
  172. public function admin_options() { 
  173. if ( $this->is_valid_for_use() ) { 
  174. parent::admin_options(); 
  175. } else { 
  176. ?> 
  177. <div class="inline error"><p><strong><?php _e( 'Gateway disabled', 'woocommerce' ); ?></strong>: <?php _e( 'PayPal does not support your store currency.', 'woocommerce' ); ?></p></div> 
  178. <?php 
  179.  
  180. /** 
  181. * Initialise Gateway Settings Form Fields. 
  182. */ 
  183. public function init_form_fields() { 
  184. $this->form_fields = include( 'includes/settings-paypal.php' ); 
  185.  
  186. /** 
  187. * Get the transaction URL. 
  188. * @param WC_Order $order 
  189. * @return string 
  190. */ 
  191. public function get_transaction_url( $order ) { 
  192. if ( $this->testmode ) { 
  193. $this->view_transaction_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s'; 
  194. } else { 
  195. $this->view_transaction_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s'; 
  196. return parent::get_transaction_url( $order ); 
  197.  
  198. /** 
  199. * Process the payment and return the result. 
  200. * @param int $order_id 
  201. * @return array 
  202. */ 
  203. public function process_payment( $order_id ) { 
  204. include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-request.php' ); 
  205.  
  206. $order = wc_get_order( $order_id ); 
  207. $paypal_request = new WC_Gateway_Paypal_Request( $this ); 
  208.  
  209. return array( 
  210. 'result' => 'success',  
  211. 'redirect' => $paypal_request->get_request_url( $order, $this->testmode ),  
  212. ); 
  213.  
  214. /** 
  215. * Can the order be refunded via PayPal? 
  216. * @param WC_Order $order 
  217. * @return bool 
  218. */ 
  219. public function can_refund_order( $order ) { 
  220. return $order && $order->get_transaction_id(); 
  221.  
  222. /** 
  223. * Init the API class and set the username/password etc. 
  224. */ 
  225. protected function init_api() { 
  226. include_once( dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-api-handler.php' ); 
  227.  
  228. WC_Gateway_Paypal_API_Handler::$api_username = $this->get_option( 'api_username' ); 
  229. WC_Gateway_Paypal_API_Handler::$api_password = $this->get_option( 'api_password' ); 
  230. WC_Gateway_Paypal_API_Handler::$api_signature = $this->get_option( 'api_signature' ); 
  231. WC_Gateway_Paypal_API_Handler::$sandbox = $this->testmode; 
  232.  
  233. /** 
  234. * Process a refund if supported. 
  235. * @param int $order_id 
  236. * @param float $amount 
  237. * @param string $reason 
  238. * @return bool True or false based on success, or a WP_Error object 
  239. */ 
  240. public function process_refund( $order_id, $amount = null, $reason = '' ) { 
  241. $order = wc_get_order( $order_id ); 
  242.  
  243. if ( ! $this->can_refund_order( $order ) ) { 
  244. $this->log( 'Refund Failed: No transaction ID', 'error' ); 
  245. return new WP_Error( 'error', __( 'Refund failed: No transaction ID', 'woocommerce' ) ); 
  246.  
  247. $this->init_api(); 
  248.  
  249. $result = WC_Gateway_Paypal_API_Handler::refund_transaction( $order, $amount, $reason ); 
  250.  
  251. if ( is_wp_error( $result ) ) { 
  252. $this->log( 'Refund Failed: ' . $result->get_error_message(), 'error' ); 
  253. return new WP_Error( 'error', $result->get_error_message() ); 
  254.  
  255. $this->log( 'Refund Result: ' . wc_print_r( $result, true ) ); 
  256.  
  257. switch ( strtolower( $result->ACK ) ) { 
  258. case 'success': 
  259. case 'successwithwarning': 
  260. $order->add_order_note( sprintf( __( 'Refunded %1$s - Refund ID: %2$s', 'woocommerce' ), $result->GROSSREFUNDAMT, $result->REFUNDTRANSACTIONID ) ); 
  261. return true; 
  262. break; 
  263.  
  264. return isset( $result->L_LONGMESSAGE0 ) ? new WP_Error( 'error', $result->L_LONGMESSAGE0 ) : false; 
  265.  
  266. /** 
  267. * Capture payment when the order is changed from on-hold to complete or processing 
  268. * @param int $order_id 
  269. */ 
  270. public function capture_payment( $order_id ) { 
  271. $order = wc_get_order( $order_id ); 
  272.  
  273. if ( 'paypal' === $order->get_payment_method() && 'pending' === get_post_meta( $order->get_id(), '_paypal_status', true ) && $order->get_transaction_id() ) { 
  274. $this->init_api(); 
  275. $result = WC_Gateway_Paypal_API_Handler::do_capture( $order ); 
  276.  
  277. if ( is_wp_error( $result ) ) { 
  278. $this->log( 'Capture Failed: ' . $result->get_error_message(), 'error' ); 
  279. $order->add_order_note( sprintf( __( 'Payment could not captured: %s', 'woocommerce' ), $result->get_error_message() ) ); 
  280. return; 
  281.  
  282. $this->log( 'Capture Result: ' . wc_print_r( $result, true ) ); 
  283.  
  284. if ( ! empty( $result->PAYMENTSTATUS ) ) { 
  285. switch ( $result->PAYMENTSTATUS ) { 
  286. case 'Completed' : 
  287. $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 ) ); 
  288. update_post_meta( $order->get_id(), '_paypal_status', $result->PAYMENTSTATUS ); 
  289. update_post_meta( $order->get_id(), '_transaction_id', $result->TRANSACTIONID ); 
  290. break; 
  291. default : 
  292. $order->add_order_note( sprintf( __( 'Payment could not captured - Auth ID: %1$s, Status: %2$s', 'woocommerce' ), $result->AUTHORIZATIONID, $result->PAYMENTSTATUS ) ); 
  293. break;