Issues (197)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

includes/class-wc-stripe-payment-tokens.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Handles and process WC payment tokens API.
8
 * Seen in checkout page and my account->add payment method page.
9
 *
10
 * @since 4.0.0
11
 */
12
class WC_Stripe_Payment_Tokens {
13
	private static $_this;
14
15
	/**
16
	 * Constructor.
17
	 *
18
	 * @since 4.0.0
19
	 * @version 4.0.0
20
	 */
21
	public function __construct() {
22
		self::$_this = $this;
23
24
		add_filter( 'woocommerce_get_customer_payment_tokens', array( $this, 'woocommerce_get_customer_payment_tokens' ), 10, 3 );
25
		add_filter( 'woocommerce_payment_methods_list_item', array( $this, 'get_account_saved_payment_methods_list_item_sepa' ), 10, 2 );
26
		add_filter( 'woocommerce_get_credit_card_type_label', array( $this, 'normalize_sepa_label' ) );
27
		add_action( 'woocommerce_payment_token_deleted', array( $this, 'woocommerce_payment_token_deleted' ), 10, 2 );
28
		add_action( 'woocommerce_payment_token_set_default', array( $this, 'woocommerce_payment_token_set_default' ) );
29
	}
30
31
	/**
32
	 * Public access to instance object.
33
	 *
34
	 * @since 4.0.0
35
	 * @version 4.0.0
36
	 */
37
	public static function get_instance() {
38
		return self::$_this;
39
	}
40
41
	/**
42
	 * Normalizes the SEPA IBAN label on My Account page.
43
	 *
44
	 * @since 4.0.0
45
	 * @version 4.0.0
46
	 * @param string $label
47
	 * @return string $label
48
	 */
49
	public function normalize_sepa_label( $label ) {
50
		if ( 'sepa iban' === strtolower( $label ) ) {
51
			return 'SEPA IBAN';
52
		}
53
54
		return $label;
55
	}
56
57
	/**
58
	 * Checks if customer has saved payment methods.
59
	 *
60
	 * @since 4.1.0
61
	 * @param int $customer_id
62
	 * @return bool
63
	 */
64
	public static function customer_has_saved_methods( $customer_id ) {
65
		$gateways = array( 'stripe', 'stripe_sepa' );
66
67
		if ( empty( $customer_id ) ) {
68
			return false;
69
		}
70
71
		$has_token = false;
72
73
		foreach ( $gateways as $gateway ) {
74
			$tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, $gateway );
75
76
			if ( ! empty( $tokens ) ) {
77
				$has_token = true;
78
				break;
79
			}
80
		}
81
82
		return $has_token;
83
	}
84
85
	/**
86
	 * Gets saved tokens from API if they don't already exist in WooCommerce.
87
	 *
88
	 * @since 3.1.0
89
	 * @version 4.0.0
90
	 * @param array $tokens
91
	 * @return array
92
	 */
93
	public function woocommerce_get_customer_payment_tokens( $tokens, $customer_id, $gateway_id ) {
94
		if ( is_user_logged_in() && class_exists( 'WC_Payment_Token_CC' ) ) {
95
			$stored_tokens = array();
96
97
			foreach ( $tokens as $token ) {
98
				$stored_tokens[] = $token->get_token();
99
			}
100
101
			if ( 'stripe' === $gateway_id ) {
102
				$stripe_customer = new WC_Stripe_Customer( $customer_id );
103
				$stripe_sources  = $stripe_customer->get_sources();
104
105
				foreach ( $stripe_sources as $source ) {
106
					if ( isset( $source->type ) && 'card' === $source->type ) {
107
						if ( ! in_array( $source->id, $stored_tokens ) ) {
108
							$token = new WC_Payment_Token_CC();
109
							$token->set_token( $source->id );
110
							$token->set_gateway_id( 'stripe' );
111
112
							if ( 'source' === $source->object && 'card' === $source->type ) {
113
								$token->set_card_type( strtolower( $source->card->brand ) );
114
								$token->set_last4( $source->card->last4 );
115
								$token->set_expiry_month( $source->card->exp_month );
116
								$token->set_expiry_year( $source->card->exp_year );
117
							}
118
119
							$token->set_user_id( $customer_id );
120
							$token->save();
121
							$tokens[ $token->get_id() ] = $token;
122
						}
123
					} else {
124
						if ( ! in_array( $source->id, $stored_tokens ) && 'card' === $source->object ) {
125
							$token = new WC_Payment_Token_CC();
126
							$token->set_token( $source->id );
127
							$token->set_gateway_id( 'stripe' );
128
							$token->set_card_type( strtolower( $source->brand ) );
129
							$token->set_last4( $source->last4 );
130
							$token->set_expiry_month( $source->exp_month );
131
							$token->set_expiry_year( $source->exp_year );
132
							$token->set_user_id( $customer_id );
133
							$token->save();
134
							$tokens[ $token->get_id() ] = $token;
135
						}
136
					}
137
				}
138
			}
139
140
			if ( 'stripe_sepa' === $gateway_id ) {
141
				$stripe_customer = new WC_Stripe_Customer( $customer_id );
142
				$stripe_sources  = $stripe_customer->get_sources();
143
144
				foreach ( $stripe_sources as $source ) {
145
					if ( isset( $source->type ) && 'sepa_debit' === $source->type ) {
146
						if ( ! in_array( $source->id, $stored_tokens ) ) {
147
							$token = new WC_Payment_Token_SEPA();
148
							$token->set_token( $source->id );
149
							$token->set_gateway_id( 'stripe_sepa' );
150
							$token->set_last4( $source->sepa_debit->last4 );
151
							$token->set_user_id( $customer_id );
152
							$token->save();
153
							$tokens[ $token->get_id() ] = $token;
154
						}
155
					}
156
				}
157
			}
158
		}
159
160
		return $tokens;
161
	}
162
163
	/**
164
	 * Controls the output for SEPA on the my account page.
165
	 *
166
	 * @since 4.0.0
167
	 * @version 4.0.0
168
	 * @param  array             $item         Individual list item from woocommerce_saved_payment_methods_list
169
	 * @param  WC_Payment_Token $payment_token The payment token associated with this method entry
170
	 * @return array                           Filtered item
171
	 */
172
	public function get_account_saved_payment_methods_list_item_sepa( $item, $payment_token ) {
173
		if ( 'sepa' === strtolower( $payment_token->get_type() ) ) {
174
			$item['method']['last4'] = $payment_token->get_last4();
175
			$item['method']['brand'] = esc_html__( 'SEPA IBAN', 'woocommerce-gateway-stripe' );
176
		}
177
178
		return $item;
179
	}
180
181
	/**
182
	 * Delete token from Stripe.
183
	 *
184
	 * @since 3.1.0
185
	 * @version 4.0.0
186
	 */
187
	public function woocommerce_payment_token_deleted( $token_id, $token ) {
188 View Code Duplication
		if ( 'stripe' === $token->get_gateway_id() || 'stripe_sepa' === $token->get_gateway_id() ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
189
			$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
190
			$stripe_customer->delete_source( $token->get_token() );
191
		}
192
	}
193
194
	/**
195
	 * Set as default in Stripe.
196
	 *
197
	 * @since 3.1.0
198
	 * @version 4.0.0
199
	 */
200
	public function woocommerce_payment_token_set_default( $token_id ) {
201
		$token = WC_Payment_Tokens::get( $token_id );
202
203 View Code Duplication
		if ( 'stripe' === $token->get_gateway_id() || 'stripe_sepa' === $token->get_gateway_id() ) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
204
			$stripe_customer = new WC_Stripe_Customer( get_current_user_id() );
205
			$stripe_customer->set_default_source( $token->get_token() );
206
		}
207
	}
208
}
209
210
new WC_Stripe_Payment_Tokens();
211