Completed
Push — master ( c671e8...20ee6c )
by Claudio
08:22
created

WC_Gateway_Paypal::order_received_text()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
13 1
if ( ! defined( 'ABSPATH' ) ) {
14
	exit;
15
}
16
17
/**
18
 * WC_Gateway_Paypal Class.
19
 */
20
class WC_Gateway_Paypal extends WC_Payment_Gateway {
21
22
	/**
23
	 * Whether or not logging is enabled
24
	 *
25
	 * @var bool
26
	 */
27
	public static $log_enabled = false;
28
29
	/**
30
	 * Logger instance
31
	 *
32
	 * @var WC_Logger
33
	 */
34
	public static $log = false;
35
36
	/**
37
	 * Constructor for the gateway.
38
	 */
39 2
	public function __construct() {
40 2
		$this->id                = 'paypal';
41 2
		$this->has_fields        = false;
42 2
		$this->order_button_text = __( 'Proceed to PayPal', 'woocommerce' );
43 2
		$this->method_title      = __( 'PayPal', 'woocommerce' );
44
		/* translators: %s: Link to WC system status page */
45 2
		$this->method_description = __( 'PayPal Standard redirects customers to PayPal to enter their payment information.', 'woocommerce' );
46 2
		$this->supports           = array(
47
			'products',
48
			'refunds',
49
		);
50
51
		// Load the settings.
52 2
		$this->init_form_fields();
53 2
		$this->init_settings();
54
55
		// Define user set variables.
56 2
		$this->title          = $this->get_option( 'title' );
57 2
		$this->description    = $this->get_option( 'description' );
58 2
		$this->testmode       = 'yes' === $this->get_option( 'testmode', 'no' );
59 2
		$this->debug          = 'yes' === $this->get_option( 'debug', 'no' );
60 2
		$this->email          = $this->get_option( 'email' );
61 2
		$this->receiver_email = $this->get_option( 'receiver_email', $this->email );
62 2
		$this->identity_token = $this->get_option( 'identity_token' );
63 2
		self::$log_enabled    = $this->debug;
64
65 2
		if ( $this->testmode ) {
66
			/* translators: %s: Link to PayPal sandbox testing guide page */
67 1
			$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/' );
68 1
			$this->description  = trim( $this->description );
69
		}
70
71 2
		add_action( 'admin_enqueue_scripts', array( $this, 'admin_scripts' ) );
72 2
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
73 2
		add_action( 'woocommerce_order_status_processing', array( $this, 'capture_payment' ) );
74 2
		add_action( 'woocommerce_order_status_completed', array( $this, 'capture_payment' ) );
75 2
		add_filter( 'woocommerce_thankyou_order_received_text', array( $this, 'order_received_text' ), 10, 2 );
76
77 2
		if ( ! $this->is_valid_for_use() ) {
78
			$this->enabled = 'no';
79
		} else {
80 2
			include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-ipn-handler.php';
81 2
			new WC_Gateway_Paypal_IPN_Handler( $this->testmode, $this->receiver_email );
82
83 2
			if ( $this->identity_token ) {
84
				include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-pdt-handler.php';
85
				new WC_Gateway_Paypal_PDT_Handler( $this->testmode, $this->identity_token );
86
			}
87
		}
88
	}
89
90
	/**
91
	 * Return whether or not this gateway still requires setup to function.
92
	 *
93
	 * When this gateway is toggled on via AJAX, if this returns true a
94
	 * redirect will occur to the settings page instead.
95
	 *
96
	 * @since 3.4.0
97
	 * @return bool
98
	 */
99
	public function needs_setup() {
100
		return ! is_email( $this->email );
101
	}
102
103
	/**
104
	 * Logging method.
105
	 *
106
	 * @param string $message Log message.
107
	 * @param string $level Optional. Default 'info'. Possible values:
108
	 *                      emergency|alert|critical|error|warning|notice|info|debug.
109
	 */
110
	public static function log( $message, $level = 'info' ) {
111
		if ( self::$log_enabled ) {
112
			if ( empty( self::$log ) ) {
113
				self::$log = wc_get_logger();
114
			}
115
			self::$log->log( $level, $message, array( 'source' => 'paypal' ) );
116
		}
117
	}
118
119
	/**
120
	 * Processes and saves options.
121
	 * If there is an error thrown, will continue to save and validate fields, but will leave the erroring field out.
122
	 *
123
	 * @return bool was anything saved?
124
	 */
125
	public function process_admin_options() {
126
		$saved = parent::process_admin_options();
127
128
		// Maybe clear logs.
129
		if ( 'yes' !== $this->get_option( 'debug', 'no' ) ) {
130
			if ( empty( self::$log ) ) {
131
				self::$log = wc_get_logger();
132
			}
133
			self::$log->clear( 'paypal' );
134
		}
135
136
		return $saved;
137
	}
138
139
	/**
140
	 * Get gateway icon.
141
	 *
142
	 * @return string
143
	 */
144
	public function get_icon() {
145
		// We need a base country for the link to work, bail if in the unlikely event no country is set.
146
		$base_country = WC()->countries->get_base_country();
147
		if ( empty( $base_country ) ) {
148
			return '';
149
		}
150
		$icon_html = '';
151
		$icon      = (array) $this->get_icon_image( $base_country );
152
153
		foreach ( $icon as $i ) {
154
			$icon_html .= '<img src="' . esc_attr( $i ) . '" alt="' . esc_attr__( 'PayPal acceptance mark', 'woocommerce' ) . '" />';
155
		}
156
157
		$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 ) ) );
158
159
		return apply_filters( 'woocommerce_gateway_icon', $icon_html, $this->id );
160
	}
161
162
	/**
163
	 * Get the link for an icon based on country.
164
	 *
165
	 * @param  string $country Country two letter code.
166
	 * @return string
167
	 */
168
	protected function get_icon_url( $country ) {
169
		$url           = 'https://www.paypal.com/' . strtolower( $country );
170
		$home_counties = array( 'BE', 'CZ', 'DK', 'HU', 'IT', 'JP', 'NL', 'NO', 'ES', 'SE', 'TR', 'IN' );
171
		$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' );
172
173
		if ( in_array( $country, $home_counties, true ) ) {
174
			return $url . '/webapps/mpp/home';
175
		} elseif ( in_array( $country, $countries, true ) ) {
176
			return $url . '/webapps/mpp/paypal-popup';
177
		} else {
178
			return $url . '/cgi-bin/webscr?cmd=xpt/Marketing/general/WIPaypal-outside';
179
		}
180
	}
181
182
	/**
183
	 * Get PayPal images for a country.
184
	 *
185
	 * @param string $country Country code.
186
	 * @return array of image URLs
187
	 */
188
	protected function get_icon_image( $country ) {
189
		switch ( $country ) {
190
			case 'US':
191
			case 'NZ':
192
			case 'CZ':
193
			case 'HU':
194
			case 'MY':
195
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg';
196
				break;
197
			case 'TR':
198
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_odeme_secenekleri.jpg';
199
				break;
200
			case 'GB':
201
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/Logo/AM_mc_vs_ms_ae_UK.png';
202
				break;
203
			case 'MX':
204
				$icon = array(
205
					'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_visa_mastercard_amex.png',
206
					'https://www.paypal.com/es_XC/Marketing/i/banner/paypal_debit_card_275x60.gif',
207
				);
208
				break;
209
			case 'FR':
210
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_paypal_moyens_paiement_fr.jpg';
211
				break;
212
			case 'AU':
213
				$icon = 'https://www.paypalobjects.com/webstatic/en_AU/mktg/logo/Solutions-graphics-1-184x80.jpg';
214
				break;
215
			case 'DK':
216
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/logo_PayPal_betalingsmuligheder_dk.jpg';
217
				break;
218
			case 'RU':
219
				$icon = 'https://www.paypalobjects.com/webstatic/ru_RU/mktg/business/pages/logo-center/AM_mc_vs_dc_ae.jpg';
220
				break;
221
			case 'NO':
222
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo-center/banner_pl_just_pp_319x110.jpg';
223
				break;
224
			case 'CA':
225
				$icon = 'https://www.paypalobjects.com/webstatic/en_CA/mktg/logo-image/AM_mc_vs_dc_ae.jpg';
226
				break;
227
			case 'HK':
228
				$icon = 'https://www.paypalobjects.com/webstatic/en_HK/mktg/logo/AM_mc_vs_dc_ae.jpg';
229
				break;
230
			case 'SG':
231
				$icon = 'https://www.paypalobjects.com/webstatic/en_SG/mktg/Logos/AM_mc_vs_dc_ae.jpg';
232
				break;
233
			case 'TW':
234
				$icon = 'https://www.paypalobjects.com/webstatic/en_TW/mktg/logos/AM_mc_vs_dc_ae.jpg';
235
				break;
236
			case 'TH':
237
				$icon = 'https://www.paypalobjects.com/webstatic/en_TH/mktg/Logos/AM_mc_vs_dc_ae.jpg';
238
				break;
239
			case 'JP':
240
				$icon = 'https://www.paypal.com/ja_JP/JP/i/bnr/horizontal_solution_4_jcb.gif';
241
				break;
242
			case 'IN':
243
				$icon = 'https://www.paypalobjects.com/webstatic/mktg/logo/AM_mc_vs_dc_ae.jpg';
244
				break;
245
			default:
246
				$icon = WC_HTTPS::force_https_url( WC()->plugin_url() . '/includes/gateways/paypal/assets/images/paypal.png' );
247
				break;
248
		}
249
		return apply_filters( 'woocommerce_paypal_icon', $icon );
250
	}
251
252
	/**
253
	 * Check if this gateway is enabled and available in the user's country.
254
	 *
255
	 * @return bool
256
	 */
257 2
	public function is_valid_for_use() {
258 2
		return in_array(
259 2
			get_woocommerce_currency(),
260 2
			apply_filters(
261 2
				'woocommerce_paypal_supported_currencies',
262 2
				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' )
263
			),
264 2
			true
265
		);
266
	}
267
268
	/**
269
	 * Admin Panel Options.
270
	 * - Options for bits like 'title' and availability on a country-by-country basis.
271
	 *
272
	 * @since 1.0.0
273
	 */
274
	public function admin_options() {
275
		if ( $this->is_valid_for_use() ) {
276
			parent::admin_options();
277
		} else {
278
			?>
279
			<div class="inline error">
280
				<p>
281
					<strong><?php esc_html_e( 'Gateway disabled', 'woocommerce' ); ?></strong>: <?php esc_html_e( 'PayPal does not support your store currency.', 'woocommerce' ); ?>
282
				</p>
283
			</div>
284
			<?php
285
		}
286
	}
287
288
	/**
289
	 * Initialise Gateway Settings Form Fields.
290
	 */
291 2
	public function init_form_fields() {
292 2
		$this->form_fields = include 'includes/settings-paypal.php';
293
	}
294
295
	/**
296
	 * Get the transaction URL.
297
	 *
298
	 * @param  WC_Order $order Order object.
299
	 * @return string
300
	 */
301
	public function get_transaction_url( $order ) {
302
		if ( $this->testmode ) {
303
			$this->view_transaction_url = 'https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';
304
		} else {
305
			$this->view_transaction_url = 'https://www.paypal.com/cgi-bin/webscr?cmd=_view-a-trans&id=%s';
306
		}
307
		return parent::get_transaction_url( $order );
308
	}
309
310
	/**
311
	 * Process the payment and return the result.
312
	 *
313
	 * @param  int $order_id Order ID.
314
	 * @return array
315
	 */
316
	public function process_payment( $order_id ) {
317
		include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-request.php';
318
319
		$order          = wc_get_order( $order_id );
320
		$paypal_request = new WC_Gateway_Paypal_Request( $this );
321
322
		return array(
323
			'result'   => 'success',
324
			'redirect' => $paypal_request->get_request_url( $order, $this->testmode ),
0 ignored issues
show
Documentation introduced by
$order is of type false|object, but the function expects a object<WC_Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
325
		);
326
	}
327
328
	/**
329
	 * Can the order be refunded via PayPal?
330
	 *
331
	 * @param  WC_Order $order Order object.
332
	 * @return bool
333
	 */
334 1
	public function can_refund_order( $order ) {
335 1
		$has_api_creds = false;
0 ignored issues
show
Unused Code introduced by
$has_api_creds is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
336
337 1
		if ( $this->testmode ) {
338 1
			$has_api_creds = $this->get_option( 'sandbox_api_username' ) && $this->get_option( 'sandbox_api_password' ) && $this->get_option( 'sandbox_api_signature' );
339
		} else {
340 1
			$has_api_creds = $this->get_option( 'api_username' ) && $this->get_option( 'api_password' ) && $this->get_option( 'api_signature' );
341
		}
342
343 1
		return $order && $order->get_transaction_id() && $has_api_creds;
344
	}
345
346
	/**
347
	 * Init the API class and set the username/password etc.
348
	 */
349
	protected function init_api() {
350
		include_once dirname( __FILE__ ) . '/includes/class-wc-gateway-paypal-api-handler.php';
351
352
		WC_Gateway_Paypal_API_Handler::$api_username  = $this->testmode ? $this->get_option( 'sandbox_api_username' ) : $this->get_option( 'api_username' );
353
		WC_Gateway_Paypal_API_Handler::$api_password  = $this->testmode ? $this->get_option( 'sandbox_api_password' ) : $this->get_option( 'api_password' );
354
		WC_Gateway_Paypal_API_Handler::$api_signature = $this->testmode ? $this->get_option( 'sandbox_api_signature' ) : $this->get_option( 'api_signature' );
355
		WC_Gateway_Paypal_API_Handler::$sandbox       = $this->testmode;
356
	}
357
358
	/**
359
	 * Process a refund if supported.
360
	 *
361
	 * @param  int    $order_id Order ID.
362
	 * @param  float  $amount Refund amount.
363
	 * @param  string $reason Refund reason.
364
	 * @return bool|WP_Error
365
	 */
366
	public function process_refund( $order_id, $amount = null, $reason = '' ) {
367
		$order = wc_get_order( $order_id );
368
369
		if ( ! $this->can_refund_order( $order ) ) {
0 ignored issues
show
Documentation introduced by
$order is of type false|object, but the function expects a object<WC_Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
370
			return new WP_Error( 'error', __( 'Refund failed.', 'woocommerce' ) );
371
		}
372
373
		$this->init_api();
374
375
		$result = WC_Gateway_Paypal_API_Handler::refund_transaction( $order, $amount, $reason );
0 ignored issues
show
Documentation introduced by
$order is of type false|object, but the function expects a object<WC_Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
376
377
		if ( is_wp_error( $result ) ) {
378
			$this->log( 'Refund Failed: ' . $result->get_error_message(), 'error' );
379
			return new WP_Error( 'error', $result->get_error_message() );
380
		}
381
382
		$this->log( 'Refund Result: ' . wc_print_r( $result, true ) );
383
384
		switch ( strtolower( $result->ACK ) ) { // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
385
			case 'success':
386
			case 'successwithwarning':
387
				$order->add_order_note(
388
					/* translators: 1: Refund amount, 2: Refund ID */
389
					sprintf( __( 'Refunded %1$s - Refund ID: %2$s', 'woocommerce' ), $result->GROSSREFUNDAMT, $result->REFUNDTRANSACTIONID ) // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
390
				);
391
				return true;
392
		}
393
394
		return isset( $result->L_LONGMESSAGE0 ) ? new WP_Error( 'error', $result->L_LONGMESSAGE0 ) : false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
395
	}
396
397
	/**
398
	 * Capture payment when the order is changed from on-hold to complete or processing
399
	 *
400
	 * @param  int $order_id Order ID.
401
	 */
402
	public function capture_payment( $order_id ) {
403
		$order = wc_get_order( $order_id );
404
405
		if ( 'paypal' === $order->get_payment_method() && 'pending' === $order->get_meta( '_paypal_status', true ) && $order->get_transaction_id() ) {
406
			$this->init_api();
407
			$result = WC_Gateway_Paypal_API_Handler::do_capture( $order );
0 ignored issues
show
Documentation introduced by
$order is of type false|object, but the function expects a object<WC_Order>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
408
409
			if ( is_wp_error( $result ) ) {
410
				$this->log( 'Capture Failed: ' . $result->get_error_message(), 'error' );
411
				/* translators: %s: Paypal gateway error message */
412
				$order->add_order_note( sprintf( __( 'Payment could not be captured: %s', 'woocommerce' ), $result->get_error_message() ) );
413
				return;
414
			}
415
416
			$this->log( 'Capture Result: ' . wc_print_r( $result, true ) );
417
418
			// phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
419
			if ( ! empty( $result->PAYMENTSTATUS ) ) {
420
				switch ( $result->PAYMENTSTATUS ) {
421
					case 'Completed':
422
						/* translators: 1: Amount, 2: Authorization ID, 3: Transaction ID */
423
						$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 ) );
424
						update_post_meta( $order->get_id(), '_paypal_status', $result->PAYMENTSTATUS );
425
						update_post_meta( $order->get_id(), '_transaction_id', $result->TRANSACTIONID );
426
						break;
427
					default:
428
						/* translators: 1: Authorization ID, 2: Payment status */
429
						$order->add_order_note( sprintf( __( 'Payment could not be captured - Auth ID: %1$s, Status: %2$s', 'woocommerce' ), $result->AUTHORIZATIONID, $result->PAYMENTSTATUS ) );
430
						break;
431
				}
432
			}
433
			// phpcs:enable
434
		}
435
	}
436
437
	/**
438
	 * Load admin scripts.
439
	 *
440
	 * @since 3.3.0
441
	 */
442
	public function admin_scripts() {
443
		$screen    = get_current_screen();
444
		$screen_id = $screen ? $screen->id : '';
445
446
		if ( 'woocommerce_page_wc-settings' !== $screen_id ) {
447
			return;
448
		}
449
450
		$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
451
452
		wp_enqueue_script( 'woocommerce_paypal_admin', WC()->plugin_url() . '/includes/gateways/paypal/assets/js/paypal-admin' . $suffix . '.js', array(), WC_VERSION, true );
453
	}
454
455
	/**
456
	 * Custom PayPal order received text.
457
	 *
458
	 * @since 3.9.0
459
	 * @param string   $text Default text.
460
	 * @param WC_Order $order Order data.
461
	 * @return string
462
	 */
463
	public function order_received_text( $text, $order ) {
0 ignored issues
show
Unused Code introduced by
The parameter $order is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
464
		if ( $this->id ) {
465
			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' );
466
		}
467
468
		return $text;
469
	}
470
}
471