Нет описания

class-wc-gateway-paypal.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  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. */
  12. use Automattic\Jetpack\Constants;
  13. if ( ! defined( 'ABSPATH' ) ) {
  14. exit;
  15. }
  16. /**
  17. * WC_Gateway_Paypal Class.
  18. */
  19. class WC_Gateway_Paypal extends WC_Payment_Gateway {
  20. /**
  21. * Whether or not logging is enabled
  22. *
  23. * @var bool
  24. */
  25. public static $log_enabled = false;
  26. /**
  27. * Logger instance
  28. *
  29. * @var WC_Logger
  30. */
  31. public static $log = false;
  32. /**
  33. * Constructor for the gateway.
  34. */
  35. public function __construct() {
  36. $this->id = 'paypal';
  37. $this->has_fields = false;
  38. $this->order_button_text = __( 'Proceed to PayPal', 'woocommerce' );
  39. $this->method_title = __( 'PayPal Standard', 'woocommerce' );
  40. /* translators: %s: Link to WC system status page */
  41. $this->method_description = __( 'PayPal Standard redirects customers to PayPal to enter their payment information.', 'woocommerce' );
  42. $this->supports = array(
  43. 'products',
  44. 'refunds',
  45. );
  46. // Load the settings.
  47. $this->init_form_fields();
  48. $this->init_settings();
  49. // Define user set variables.
  50. $this->title = $this->get_option( 'title' );
  51. $this->description = $this->get_option( 'description' );
  52. $this->testmode = 'yes' === $this->get_option( 'testmode', 'no' );
  53. $this->debug = 'yes' === $this->get_option( 'debug', 'no' );
  54. $this->email = $this->get_option( 'email' );
  55. $this->receiver_email = $this->get_option( 'receiver_email', $this->email );
  56. $this->identity_token = $this->get_option( 'identity_token' );
  57. self::$log_enabled = $this->debug;
  58. if ( $this->testmode ) {
  59. /* translators: %s: Link to PayPal sandbox testing guide page */
  60. $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/' );
  61. $this->description = trim( $this->description );
  62. }
  63. add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
  64. add_action( 'woocommerce_order_status_processing', array( $this, 'capture_payment' ) );
  65. add_action( 'woocommerce_order_status_completed', array( $this, 'capture_payment' ) );
  66. add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
  67. if ( ! $this->is_valid_for_use() ) {
  68. $this->enabled = 'no';
  69. } else {
  70. include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-ipn-handler.php';
  71. new WC_Gateway_Paypal_IPN_Handler( $this->testmode, $this->receiver_email );
  72. if ( $this->identity_token ) {
  73. include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-pdt-handler.php';
  74. $pdt_handler = new WC_Gateway_Paypal_PDT_Handler( $this->testmode, $this->identity_token );
  75. $pdt_handler->set_receiver_email( $this->receiver_email );
  76. }
  77. }
  78. if ( 'yes' === $this->enabled ) {
  79. add_filter( 'woocommerce_thankyou_order_received_text', array( $this, 'order_received_text' ), 10, 2 );
  80. }
  81. }
  82. /**
  83. * Return whether or not this gateway still requires setup to function.
  84. *
  85. * When this gateway is toggled on via AJAX, if this returns true a
  86. * redirect will occur to the settings page instead.
  87. *
  88. * @since 3.4.0
  89. * @return bool
  90. */
  91. public function needs_setup() {
  92. return ! is_email( $this->email );
  93. }
  94. /**
  95. * Logging method.
  96. *
  97. * @param string $message Log message.
  98. * @param string $level Optional. Default 'info'. Possible values:
  99. * emergency|alert|critical|error|warning|notice|info|debug.
  100. */
  101. public static function log( $message, $level = 'info' ) {
  102. if ( self::$log_enabled ) {
  103. if ( empty( self::$log ) ) {
  104. self::$log = wc_get_logger();
  105. }
  106. self::$log->log( $level, $message, array( 'source' => 'paypal' ) );
  107. }
  108. }
  109. /**
  110. * Processes and saves options.
  111. * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
  112. *
  113. * @return bool was anything saved?
  114. */
  115. public function process_admin_options() {
  116. $saved = parent::process_admin_options();
  117. // Maybe clear logs.
  118. if ( 'yes' !== $this->get_option( 'debug', 'no' ) ) {
  119. if ( empty( self::$log ) ) {
  120. self::$log = wc_get_logger();
  121. }
  122. self::$log->clear( 'paypal' );
  123. }
  124. return $saved;
  125. }
  126. /**
  127. * Get gateway icon.
  128. *
  129. * @return string
  130. */
  131. public function get_icon() {
  132. // We need a base country for the link to work, bail if in the unlikely event no country is set.
  133. $base_country = WC()->countries->get_base_country();
  134. if ( empty( $base_country ) ) {
  135. return '';
  136. }
  137. $icon_html = '';
  138. $icon = (array) $this->get_icon_image( $base_country );
  139. foreach ( $icon as $i ) {
  140. $icon_html .= '<img src="' . esc_attr( $i ) . '" alt="' . esc_attr__( 'PayPal acceptance mark', 'woocommerce' ) . '" />';
  141. }
  142. $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 ) ) );
  143. return apply_filters( 'woocommerce_gateway_icon', $icon_html, $this->id );
  144. }
  145. /**
  146. * Get the link for an icon based on country.
  147. *
  148. * @param string $country Country two letter code.
  149. * @return string
  150. */
  151. protected function get_icon_url( $country ) {
  152. $url = 'https://www.paypal.com/' . strtolower( $country );
  153. $home_counties = array( 'BE', 'CZ', 'DK', 'HU', 'IT', 'JP', 'NL', 'NO', 'ES', 'SE', 'TR', 'IN' );
  154. $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' );
  155. if ( in_array( $country, $home_counties, true ) ) {
  156. return $url . '/webapps/mpp/home';
  157. } elseif ( in_array( $country, $countries, true ) ) {
  158. return $url . '/webapps/mpp/paypal-popup';
  159. } else {
  160. return $url . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside';
  161. }
  162. }
  163. /**
  164. * Get PayPal images for a country.
  165. *
  166. * @param string $country Country code.
  167. * @return array of image URLs
  168. */
  169. protected function get_icon_image( $country ) {
  170. switch ( $country ) {
  171. case 'US':
  172. case 'NZ':
  173. case 'CZ':
  174. case 'HU':
  175. case 'MY':
  176. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg';
  177. break;
  178. case 'TR':
  179. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg';
  180. break;
  181. case 'GB':
  182. $icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png';
  183. break;
  184. case 'MX':
  185. $icon = array(
  186. 'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png',
  187. 'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif',
  188. );
  189. break;
  190. case 'FR':
  191. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg';
  192. break;
  193. case 'AU':
  194. $icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg';
  195. break;
  196. case 'DK':
  197. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg';
  198. break;
  199. case 'RU':
  200. $icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg';
  201. break;
  202. case 'NO':
  203. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg';
  204. break;
  205. case 'CA':
  206. $icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg';
  207. break;
  208. case 'HK':
  209. $icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg';
  210. break;
  211. case 'SG':
  212. $icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg';
  213. break;
  214. case 'TW':
  215. $icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg';
  216. break;
  217. case 'TH':
  218. $icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg';
  219. break;
  220. case 'JP':
  221. $icon = 'https://www.paypal.com/ja_JP/JP/i/bnr/horizontal_solution_4_jcb.gif';
  222. break;
  223. case 'IN':
  224. $icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg';
  225. break;
  226. default:
  227. $icon = WC_HTTPS::force_https_url( WC()->plugin_url() . '/includes/gateways/paypal/assets/images/paypal.png' );
  228. break;
  229. }
  230. return apply_filters( 'woocommerce_paypal_icon', $icon );
  231. }
  232. /**
  233. * Check if this gateway is available in the user's country based on currency.
  234. *
  235. * @return bool
  236. */
  237. public function is_valid_for_use() {
  238. return in_array(
  239. get_woocommerce_currency(),
  240. apply_filters(
  241. 'woocommerce_paypal_supported_currencies',
  242. 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' )
  243. ),
  244. true
  245. );
  246. }
  247. /**
  248. * Admin Panel Options.
  249. * - Options for bits like 'title' and availability on a country-by-country basis.
  250. *
  251. * @since 1.0.0
  252. */
  253. public function admin_options() {
  254. if ( $this->is_valid_for_use() ) {
  255. parent::admin_options();
  256. } else {
  257. ?>
  258. <div class="inline error">
  259. <p>
  260. <strong><?php esc_html_e( 'Gateway disabled', 'woocommerce' ); ?></strong>: <?php esc_html_e( 'PayPal Standard does not support your store currency.', 'woocommerce' ); ?>
  261. </p>
  262. </div>
  263. <?php
  264. }
  265. }
  266. /**
  267. * Initialise Gateway Settings Form Fields.
  268. */
  269. public function init_form_fields() {
  270. $this->form_fields = include __DIR__ . '/includes/settings-paypal.php';
  271. }
  272. /**
  273. * Get the transaction URL.
  274. *
  275. * @param WC_Order $order Order object.
  276. * @return string
  277. */
  278. public function get_transaction_url( $order ) {
  279. if ( $this->testmode ) {
  280. $this->view_transaction_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';
  281. } else {
  282. $this->view_transaction_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';
  283. }
  284. return parent::get_transaction_url( $order );
  285. }
  286. /**
  287. * Process the payment and return the result.
  288. *
  289. * @param int $order_id Order ID.
  290. * @return array
  291. */
  292. public function process_payment( $order_id ) {
  293. include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-request.php';
  294. $order = wc_get_order( $order_id );
  295. $paypal_request = new WC_Gateway_Paypal_Request( $this );
  296. return array(
  297. 'result' => 'success',
  298. 'redirect' => $paypal_request->get_request_url( $order, $this->testmode ),
  299. );
  300. }
  301. /**
  302. * Can the order be refunded via PayPal?
  303. *
  304. * @param WC_Order $order Order object.
  305. * @return bool
  306. */
  307. public function can_refund_order( $order ) {
  308. $has_api_creds = false;
  309. if ( $this->testmode ) {
  310. $has_api_creds = $this->get_option( 'sandbox_api_username' ) && $this->get_option( 'sandbox_api_password' ) && $this->get_option( 'sandbox_api_signature' );
  311. } else {
  312. $has_api_creds = $this->get_option( 'api_username' ) && $this->get_option( 'api_password' ) && $this->get_option( 'api_signature' );
  313. }
  314. return $order && $order->get_transaction_id() && $has_api_creds;
  315. }
  316. /**
  317. * Init the API class and set the username/password etc.
  318. */
  319. protected function init_api() {
  320. include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-api-handler.php';
  321. WC_Gateway_Paypal_API_Handler::$api_username = $this->testmode ? $this->get_option( 'sandbox_api_username' ) : $this->get_option( 'api_username' );
  322. WC_Gateway_Paypal_API_Handler::$api_password = $this->testmode ? $this->get_option( 'sandbox_api_password' ) : $this->get_option( 'api_password' );
  323. WC_Gateway_Paypal_API_Handler::$api_signature = $this->testmode ? $this->get_option( 'sandbox_api_signature' ) : $this->get_option( 'api_signature' );
  324. WC_Gateway_Paypal_API_Handler::$sandbox = $this->testmode;
  325. }
  326. /**
  327. * Process a refund if supported.
  328. *
  329. * @param int $order_id Order ID.
  330. * @param float $amount Refund amount.
  331. * @param string $reason Refund reason.
  332. * @return bool|WP_Error
  333. */
  334. public function process_refund( $order_id, $amount = null, $reason = '' ) {
  335. $order = wc_get_order( $order_id );
  336. if ( ! $this->can_refund_order( $order ) ) {
  337. return new WP_Error( 'error', __( 'Refund failed.', 'woocommerce' ) );
  338. }
  339. $this->init_api();
  340. $result = WC_Gateway_Paypal_API_Handler::refund_transaction( $order, $amount, $reason );
  341. if ( is_wp_error( $result ) ) {
  342. $this->log( 'Refund Failed: ' . $result->get_error_message(), 'error' );
  343. return new WP_Error( 'error', $result->get_error_message() );
  344. }
  345. $this->log( 'Refund Result: ' . wc_print_r( $result, true ) );
  346. switch ( strtolower( $result->ACK ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  347. case 'success':
  348. case 'successwithwarning':
  349. $order->add_order_note(
  350. /* translators: 1: Refund amount, 2: Refund ID */
  351. sprintf( __( 'Refunded %1$s - Refund ID: %2$s', 'woocommerce' ), $result->GROSSREFUNDAMT, $result->REFUNDTRANSACTIONID ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  352. );
  353. return true;
  354. }
  355. return isset( $result->L_LONGMESSAGE0 ) ? new WP_Error( 'error', $result->L_LONGMESSAGE0 ) : false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  356. }
  357. /**
  358. * Capture payment when the order is changed from on-hold to complete or processing
  359. *
  360. * @param int $order_id Order ID.
  361. */
  362. public function capture_payment( $order_id ) {
  363. $order = wc_get_order( $order_id );
  364. if ( 'paypal' === $order->get_payment_method() && 'pending' === $order->get_meta( '_paypal_status', true ) && $order->get_transaction_id() ) {
  365. $this->init_api();
  366. $result = WC_Gateway_Paypal_API_Handler::do_capture( $order );
  367. if ( is_wp_error( $result ) ) {
  368. $this->log( 'Capture Failed: ' . $result->get_error_message(), 'error' );
  369. /* translators: %s: Paypal gateway error message */
  370. $order->add_order_note( sprintf( __( 'Payment could not be captured: %s', 'woocommerce' ), $result->get_error_message() ) );
  371. return;
  372. }
  373. $this->log( 'Capture Result: ' . wc_print_r( $result, true ) );
  374. // phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
  375. if ( ! empty( $result->PAYMENTSTATUS ) ) {
  376. switch ( $result->PAYMENTSTATUS ) {
  377. case 'Completed':
  378. /* translators: 1: Amount, 2: Authorization ID, 3: Transaction ID */
  379. $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 ) );
  380. update_post_meta( $order->get_id(), '_paypal_status', $result->PAYMENTSTATUS );
  381. update_post_meta( $order->get_id(), '_transaction_id', $result->TRANSACTIONID );
  382. break;
  383. default:
  384. /* translators: 1: Authorization ID, 2: Payment status */
  385. $order->add_order_note( sprintf( __( 'Payment could not be captured - Auth ID: %1$s, Status: %2$s', 'woocommerce' ), $result->AUTHORIZATIONID, $result->PAYMENTSTATUS ) );
  386. break;
  387. }
  388. }
  389. // phpcs:enable
  390. }
  391. }
  392. /**
  393. * Load admin scripts.
  394. *
  395. * @since 3.3.0
  396. */
  397. public function admin_scripts() {
  398. $screen = get_current_screen();
  399. $screen_id = $screen ? $screen->id : '';
  400. if ( 'woocommerce_page_wc-settings' !== $screen_id ) {
  401. return;
  402. }
  403. $suffix = Constants::is_true( 'SCRIPT_DEBUG' ) ? '' : '.min';
  404. $version = Constants::get_constant( 'WC_VERSION' );
  405. wp_enqueue_script( 'woocommerce_paypal_admin', WC()->plugin_url() . '/includes/gateways/paypal/assets/js/paypal-admin' . $suffix . '.js', array(), $version, true );
  406. }
  407. /**
  408. * Custom PayPal order received text.
  409. *
  410. * @since 3.9.0
  411. * @param string $text Default text.
  412. * @param WC_Order $order Order data.
  413. * @return string
  414. */
  415. public function order_received_text( $text, $order ) {
  416. if ( $order && $this->id === $order->get_payment_method() ) {
  417. 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' );
  418. }
  419. return $text;
  420. }
  421. /**
  422. * Determines whether PayPal Standard should be loaded or not.
  423. *
  424. * By default PayPal Standard isn't loaded on new installs or on existing sites which haven't set up the gateway.
  425. *
  426. * @since 5.5.0
  427. *
  428. * @return bool Whether PayPal Standard should be loaded.
  429. */
  430. public function should_load() {
  431. $option_key = '_should_load';
  432. $should_load = $this->get_option( $option_key );
  433. if ( '' === $should_load ) {
  434. // New installs without PayPal Standard enabled don't load it.
  435. if ( 'no' === $this->enabled && WC_Install::is_new_install() ) {
  436. $should_load = false;
  437. } else {
  438. $should_load = true;
  439. }
  440. $this->update_option( $option_key, wc_bool_to_string( $should_load ) );
  441. } else {
  442. $should_load = wc_string_to_bool( $should_load );
  443. }
  444. /**
  445. * Allow third-parties to filter whether PayPal Standard should be loaded or not.
  446. *
  447. * @since 5.5.0
  448. *
  449. * @param bool $should_load Whether PayPal Standard should be loaded.
  450. * @param WC_Gateway_Paypal $this The WC_Gateway_Paypal instance.
  451. */
  452. return apply_filters( 'woocommerce_should_load_paypal_standard', $should_load, $this );
  453. }
  454. }