WC_Gateway_Stripe_Alipay::get_icon()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
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( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
91
	}
92
93
	/**
94
	 * Returns all supported currencies for this payment method.
95
	 *
96
	 * @since 4.0.0
97
	 * @version 4.0.0
98
	 * @return array
99
	 */
100
	public function get_supported_currency() {
101
		return apply_filters(
102
			'wc_stripe_alipay_supported_currencies',
103
			array(
104
				'EUR',
105
				'AUD',
106
				'CAD',
107
				'CNY',
108
				'GBP',
109
				'HKD',
110
				'JPY',
111
				'NZD',
112
				'SGD',
113
				'USD',
114
			)
115
		);
116
	}
117
118
	/**
119
	 * Checks to see if all criteria is met before showing payment method.
120
	 *
121
	 * @since 4.0.0
122
	 * @version 4.0.0
123
	 * @return bool
124
	 */
125
	public function is_available() {
126
		if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
127
			return false;
128
		}
129
130
		return parent::is_available();
131
	}
132
133
	/**
134
	 * Get_icon function.
135
	 *
136
	 * @since 1.0.0
137
	 * @version 4.0.0
138
	 * @return string
139
	 */
140
	public function get_icon() {
141
		$icons = $this->payment_icons();
142
143
		$icons_str = '';
144
145
		$icons_str .= isset( $icons['alipay'] ) ? $icons['alipay'] : '';
146
147
		return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
148
	}
149
150
	/**
151
	 * Payment_scripts function.
152
	 *
153
	 * @since 4.0.0
154
	 * @version 4.0.0
155
	 */
156
	public function payment_scripts() {
157
		if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
158
			return;
159
		}
160
161
		wp_enqueue_style( 'stripe_styles' );
162
		wp_enqueue_script( 'woocommerce_stripe' );
163
	}
164
165
	/**
166
	 * Initialize Gateway Settings Form Fields.
167
	 */
168
	public function init_form_fields() {
169
		$this->form_fields = require( WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-alipay-settings.php' );
170
	}
171
172
	/**
173
	 * Payment form on checkout page
174
	 */
175
	public function payment_fields() {
176
		global $wp;
177
		$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...
178
		$total       = WC()->cart->total;
179
		$description = $this->get_description();
180
181
		// If paying from order, we need to get total from order not cart.
182
		if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
183
			$order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
184
			$total = $order->get_total();
185
		}
186
187
		if ( is_add_payment_method_page() ) {
188
			$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...
189
			$total           = '';
190
		} else {
191
			$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...
192
		}
193
194
		echo '<div
195
			id="stripe-alipay-payment-data"
196
			data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
197
			data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
198
199
		if ( $description ) {
200
			echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
201
		}
202
203
		echo '</div>';
204
	}
205
206
	/**
207
	 * Creates the source for charge.
208
	 *
209
	 * @since 4.0.0
210
	 * @version 4.0.0
211
	 * @param object $order
212
	 * @return mixed
213
	 */
214
	public function create_source( $order ) {
215
		$currency              = $order->get_currency();
216
		$return_url            = $this->get_stripe_return_url( $order );
217
		$post_data             = array();
218
		$post_data['amount']   = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
219
		$post_data['currency'] = strtolower( $currency );
220
		$post_data['type']     = 'alipay';
221
		$post_data['owner']    = $this->get_owner_details( $order );
222
		$post_data['redirect'] = array( 'return_url' => $return_url );
223
224
		if ( ! empty( $this->statement_descriptor ) ) {
225
			$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...
226
		}
227
228
		WC_Stripe_Logger::log( 'Info: Begin creating Alipay source' );
229
230
		return WC_Stripe_API::request( apply_filters( 'wc_stripe_alipay_source', $post_data, $order ), 'sources' );
231
	}
232
233
	/**
234
	 * Process the payment
235
	 *
236
	 * @param int  $order_id Reference.
237
	 * @param bool $retry Should we retry on fail.
238
	 * @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...
239
	 *
240
	 * @throws Exception If payment will not be accepted.
241
	 *
242
	 * @return array|void
243
	 */
244
	public function process_payment( $order_id, $retry = true, $force_save_save = false ) {
245
		try {
246
			$order = wc_get_order( $order_id );
247
248
			// This will throw exception if not valid.
249
			$this->validate_minimum_order_amount( $order );
250
251
			// This comes from the create account checkbox in the checkout page.
252
			$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
253
254
			if ( $create_account ) {
255
				$new_customer_id     = $order->get_customer_id();
256
				$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
257
				$new_stripe_customer->create_customer();
258
			}
259
260
			$response = $this->create_source( $order );
261
262
			if ( ! empty( $response->error ) ) {
263
				$order->add_order_note( $response->error->message );
264
265
				throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
266
			}
267
268
			$order->update_meta_data( '_stripe_source_id', $response->id );
269
			$order->save();
270
271
			WC_Stripe_Logger::log( 'Info: Redirecting to Alipay...' );
272
273
			return array(
274
				'result'   => 'success',
275
				'redirect' => esc_url_raw( $response->redirect->url ),
276
			);
277
		} catch ( WC_Stripe_Exception $e ) {
278
			wc_add_notice( $e->getLocalizedMessage(), 'error' );
279
			WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
280
281
			do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
282
283
			$statuses = array( 'pending', 'failed' );
284
285
			if ( $order->has_status( $statuses ) ) {
286
				$this->send_failed_order_email( $order_id );
287
			}
288
289
			return array(
290
				'result'   => 'fail',
291
				'redirect' => '',
292
			);
293
		}
294
	}
295
}
296