/includes/gateways/paypal/class-wc-gateway-paypal.php

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