Completed
Push — master ( 6d8164...bf7249 )
by Roy
03:26
created

WC_Gateway_Stripe_Alipay::is_available()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Class that handles Alipay payment method.
8
 *
9
 * @extends WC_Gateway_Stripe
10
 *
11
 * @since 4.0.0
12
 */
13
class WC_Gateway_Stripe_Alipay extends WC_Stripe_Payment_Gateway {
14
	/**
15
	 * Notices (array)
16
	 * @var array
17
	 */
18
	public $notices = array();
19
20
	/**
21
	 * Is test mode active?
22
	 *
23
	 * @var bool
24
	 */
25
	public $testmode;
26
27
	/**
28
	 * Alternate credit card statement name
29
	 *
30
	 * @var bool
31
	 */
32
	public $statement_descriptor;
33
34
	/**
35
	 * API access secret key
36
	 *
37
	 * @var string
38
	 */
39
	public $secret_key;
40
41
	/**
42
	 * Api access publishable key
43
	 *
44
	 * @var string
45
	 */
46
	public $publishable_key;
47
48
	/**
49
	 * Should we store the users credit cards?
50
	 *
51
	 * @var bool
52
	 */
53
	public $saved_cards;
54
55
	/**
56
	 * Constructor
57
	 */
58
	public function __construct() {
59
		$this->id                   = 'stripe_alipay';
60
		$this->method_title         = __( 'Stripe Alipay', 'woocommerce-gateway-stripe' );
61
		/* translators: link */
62
		$this->method_description   = sprintf( __( 'All other general Stripe settings can be adjusted <a href="%s">here</a>.', 'woocommerce-gateway-stripe' ), admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) );
63
		$this->supports             = array(
64
			'products',
65
			'refunds',
66
		);
67
68
		// Load the form fields.
69
		$this->init_form_fields();
70
71
		// Load the settings.
72
		$this->init_settings();
73
74
		$main_settings              = get_option( 'woocommerce_stripe_settings' );
75
		$this->title                = $this->get_option( 'title' );
76
		$this->description          = $this->get_option( 'description' );
77
		$this->enabled              = $this->get_option( 'enabled' );
78
		$this->testmode             = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
79
		$this->saved_cards          = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
80
		$this->publishable_key      = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
81
		$this->secret_key           = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
82
		$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
83
84
		if ( $this->testmode ) {
85
			$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
86
			$this->secret_key      = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
87
		}
88
89
		add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
90
		add_action( 'admin_notices', array( $this, 'check_environment' ) );
91
		add_action( 'admin_head', array( $this, 'remove_admin_notice' ) );
92
		add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
93
	}
94
95
	/**
96
	 * Checks to make sure environment is setup correctly to use this payment method.
97
	 *
98
	 * @since 4.0.0
99
	 * @version 4.0.0
100
	 */
101
	public function check_environment() {
102
		if ( ! current_user_can( 'manage_woocommerce' ) ) {
103
			return;
104
		}
105
106
		$environment_warning = $this->get_environment_warning();
107
108
		if ( $environment_warning ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $environment_warning of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
109
			$this->add_admin_notice( 'bad_environment', 'error', $environment_warning );
110
		}
111
112
		foreach ( (array) $this->notices as $notice_key => $notice ) {
113
			echo "<div class='" . esc_attr( $notice['class'] ) . "'><p>";
114
			echo wp_kses( $notice['message'], array( 'a' => array( 'href' => array() ) ) );
115
			echo '</p></div>';
116
		}
117
	}
118
119
	/**
120
	 * Checks the environment for compatibility problems. Returns a string with the first incompatibility
121
	 * found or false if the environment has no problems.
122
	 *
123
	 * @since 4.0.0
124
	 * @version 4.0.0
125
	 */
126
	public function get_environment_warning() {
127
		if (
128
			'yes' === $this->enabled && ! in_array( get_woocommerce_currency(), $this->get_supported_currency() )
129
		) {
130
			/* translators: supported currency list */
131
			$message = sprintf( __( 'Alipay is enabled - it requires store currency to be set to %s', 'woocommerce-gateway-stripe' ), implode( ', ', $this->get_supported_currency() ) );
132
133
			return $message;
134
		}
135
136
		return false;
137
	}
138
139
	/**
140
	 * Returns all supported currencies for this payment method.
141
	 *
142
	 * @since 4.0.0
143
	 * @version 4.0.0
144
	 * @return array
145
	 */
146
	public function get_supported_currency() {
147
		return apply_filters( 'wc_stripe_alipay_supported_currencies', array(
148
			'EUR',
149
			'AUD',
150
			'CAD',
151
			'GBP',
152
			'HKD',
153
			'JPY',
154
			'NZD',
155
			'SGD',
156
			'USD',
157
		) );
158
	}
159
160
	/**
161
	 * Checks to see if all criteria is met before showing payment method.
162
	 *
163
	 * @since 4.0.0
164
	 * @version 4.0.0
165
	 * @return bool
166
	 */
167
	public function is_available() {
168
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
169
			return false;
170
		}
171
172
		return parent::is_available();
173
	}
174
175
	/**
176
	 * All payment icons that work with Stripe.
177
	 *
178
	 * @since 4.0.0
179
	 * @version 4.0.0
180
	 * @return array
181
	 */
182
	public function payment_icons() {
183
		return apply_filters( 'wc_stripe_payment_icons', array(
184
			'alipay' => '<i class="stripe-pf stripe-pf-alipay stripe-pf-right" alt="Alipay" aria-hidden="true"></i>',
185
		) );
186
	}
187
188
	/**
189
	 * Get_icon function.
190
	 *
191
	 * @since 1.0.0
192
	 * @version 4.0.0
193
	 * @return string
194
	 */
195
	public function get_icon() {
196
		$icons = $this->payment_icons();
197
198
		$icons_str = '';
199
200
		$icons_str .= $icons['alipay'];
201
202
		return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
203
	}
204
205
	/**
206
	 * Payment_scripts function.
207
	 *
208
	 * @since 4.0.0
209
	 * @version 4.0.0
210
	 */
211
	public function payment_scripts() {
212
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
213
			return;
214
		}
215
216
		wp_enqueue_style( 'stripe_paymentfonts' );
217
		wp_enqueue_script( 'woocommerce_stripe' );
218
	}
219
220
	/**
221
	 * Initialize Gateway Settings Form Fields.
222
	 */
223
	public function init_form_fields() {
224
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-alipay-settings.php' );
225
	}
226
227
	/**
228
	 * Payment form on checkout page
229
	 */
230
	public function payment_fields() {
231
		$user                 = wp_get_current_user();
0 ignored issues
show
Unused Code introduced by
$user 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...
232
		$total                = WC()->cart->total;
233
234
		// If paying from order, we need to get total from order not cart.
235
		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
236
			$order = wc_get_order( wc_get_order_id_by_order_key( wc_clean( $_GET['key'] ) ) );
237
			$total = $order->get_total();
238
		}
239
240
		if ( is_add_payment_method_page() ) {
241
			$pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
0 ignored issues
show
Unused Code introduced by
$pay_button_text 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...
242
			$total        = '';
243
		} else {
244
			$pay_button_text = '';
0 ignored issues
show
Unused Code introduced by
$pay_button_text 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...
245
		}
246
247
		echo '<div
248
			id="stripe-alipay-payment-data"
249
			data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
250
			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
251
252
		if ( $this->description ) {
253
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $this->description ) ) );
254
		}
255
256
		echo '</div>';
257
	}
258
259
	/**
260
	 * Creates the source for charge.
261
	 *
262
	 * @since 4.0.0
263
	 * @version 4.0.0
264
	 * @param object $order
265
	 * @return mixed
266
	 */
267
	public function create_source( $order ) {
268
		$currency                          = WC_Stripe_Helper::is_pre_30() ? $order->get_order_currency() : $order->get_currency();
269
		$order_id                          = WC_Stripe_Helper::is_pre_30() ? $order->id : $order->get_id();
0 ignored issues
show
Unused Code introduced by
$order_id 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...
270
		$return_url                        = $this->get_stripe_return_url( $order );
271
		$post_data                         = array();
272
		$post_data['amount']               = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
273
		$post_data['currency']             = strtolower( $currency );
274
		$post_data['type']                 = 'alipay';
275
		$post_data['owner']                = $this->get_owner_details( $order );
276
		$post_data['redirect']             = array( 'return_url' => $return_url );
277
278
		if ( ! empty( $this->statement_descriptor ) ) {
279
			$post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
0 ignored issues
show
Documentation introduced by
$this->statement_descriptor is of type boolean, but the function expects a string.

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...
280
		}
281
282
		WC_Stripe_Logger::log( 'Info: Begin creating Alipay source' );
283
284
		return WC_Stripe_API::request( $post_data, 'sources' );
285
	}
286
287
	/**
288
	 * Process the payment
289
	 *
290
	 * @param int  $order_id Reference.
291
	 * @param bool $retry Should we retry on fail.
292
	 * @param bool $force_save_source Force payment source to be saved.
0 ignored issues
show
Bug introduced by
There is no parameter named $force_save_source. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
293
	 *
294
	 * @throws Exception If payment will not be accepted.
295
	 *
296
	 * @return array|void
297
	 */
298
	public function process_payment( $order_id, $retry = true, $force_save_save = false ) {
299
		try {
300
			$order = wc_get_order( $order_id );
301
302
			// This will throw exception if not valid.
303
			$this->validate_minimum_order_amount( $order );
304
305
			// This comes from the create account checkbox in the checkout page.
306
			$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
307
308
			if ( $create_account ) {
309
				$new_customer_id     = WC_Stripe_Helper::is_pre_30() ? $order->customer_user : $order->get_customer_id();
310
				$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
311
				$new_stripe_customer->create_customer();
312
			}
313
314
			$response = $this->create_source( $order );
315
316
			if ( ! empty( $response->error ) ) {
317
				$order->add_order_note( $response->error->message );
318
319
				throw new Exception( $response->error->message );
320
			}
321
322
			if ( WC_Stripe_Helper::is_pre_30() ) {
323
				update_post_meta( $order_id, '_stripe_source_id', $response->id );
324
			} else {
325
				$order->update_meta_data( '_stripe_source_id', $response->id );
326
				$order->save();
327
			}
328
329
			WC_Stripe_Logger::log( 'Info: Redirecting to Alipay...' );
330
331
			return array(
332
				'result'   => 'success',
333
				'redirect' => esc_url_raw( $response->redirect->url ),
334
			);
335
		} catch ( Exception $e ) {
336
			wc_add_notice( $e->getMessage(), 'error' );
337
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
338
339
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
340
341
			$statuses = array( 'pending', 'failed' );
342
343
			if ( $order->has_status( $statuses ) ) {
344
				$this->send_failed_order_email( $order_id );
345
			}
346
347
			return array(
348
				'result'   => 'fail',
349
				'redirect' => '',
350
			);
351
		}
352
	}
353
}
354