PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.3
WooCommerce Square v5.4.3
5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Framework / PaymentGateway / Payment_Gateway_Privacy.php
woocommerce-square / includes / Framework / PaymentGateway Last commit date
Admin 1 month ago Api 2 years ago ApplePay 4 months ago Handlers 11 months ago Integrations 1 year ago PaymentTokens 1 year ago Payment_Gateway.php 4 months ago Payment_Gateway_Direct.php 1 year ago Payment_Gateway_Helper.php 3 years ago Payment_Gateway_My_Payment_Methods.php 3 years ago Payment_Gateway_Payment_Form.php 4 months ago Payment_Gateway_Plugin.php 3 months ago Payment_Gateway_Privacy.php 1 year ago
Payment_Gateway_Privacy.php
402 lines
1 <?php
2 /**
3 * WooCommerce Plugin Framework
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@skyverge.com so we can send you a copy immediately.
12 *
13 * @since 3.0.0
14 * @author WooCommerce / SkyVerge
15 * @copyright Copyright (c) 2013-2019, SkyVerge, Inc.
16 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0 or later
17 *
18 * Modified by WooCommerce on 15 December 2021.
19 */
20
21 namespace WooCommerce\Square\Framework\PaymentGateway;
22
23 use \WooCommerce\Square\Framework\PaymentGateway\PaymentTokens\Square_Credit_Card_Payment_Token;
24
25 defined( 'ABSPATH' ) || exit;
26
27 /**
28 * The payment gateway privacy handler class.
29 *
30 * @since 3.0.0
31 */
32 class Payment_Gateway_Privacy extends \WC_Abstract_Privacy {
33
34 /** @var Payment_Gateway_Plugin payment gateway plugin instance */
35 private $plugin;
36
37
38 /**
39 * Constructs the class.
40 *
41 * @since 3.0.0
42 *
43 * @param Payment_Gateway_Plugin payment gateway plugin instance
44 */
45 public function __construct( Payment_Gateway_Plugin $plugin ) {
46
47 $this->plugin = $plugin;
48
49 parent::__construct();
50
51 // add the action & filter hooks
52 $this->add_hooks();
53 }
54
55 /**
56 * Adds the action & filter hooks.
57 *
58 * @since 3.0.0
59 */
60 protected function add_hooks() {
61 // Initialize data exporters and erasers.
62 add_action( 'init', array( $this, 'register_erasers_exporters' ) );
63
64 // add the gateway data to customer data exports
65 add_filter( 'woocommerce_privacy_export_customer_personal_data', array( $this, 'add_export_customer_data' ), 10, 2 );
66
67 // removes the gateway data during a customer data erasure
68 add_action( 'woocommerce_privacy_erase_personal_data_customer', array( $this, 'remove_customer_personal_data' ), 10, 2 );
69
70 // add the gateway data to order data exports
71 add_filter( 'woocommerce_privacy_export_order_personal_data', array( $this, 'add_export_order_data' ), 10, 2 );
72
73 // removes the gateway data during an order data erasure
74 add_action( 'woocommerce_privacy_remove_order_personal_data', array( $this, 'remove_order_personal_data' ) );
75 }
76
77 /**
78 * Initial registration of privacy erasers and exporters.
79 */
80 public function register_erasers_exporters() {
81 $this->name = $this->plugin->get_plugin_name();
82
83 // add the token exporters & erasers
84 // translators: Placeholder %s - Plugin name
85 $this->add_exporter( "wc-{$this->plugin->get_id_dasherized()}-customer-tokens", sprintf( esc_html__( '%s Payment Tokens', 'woocommerce-square' ), $this->plugin->get_plugin_name() ), array( $this, 'customer_tokens_exporter' ) );
86
87 // translators: Placeholder %s - Plugin name
88 $this->add_eraser( "wc-{$this->plugin->get_id_dasherized()}-customer-tokens", sprintf( esc_html__( '%s Payment Tokens', 'woocommerce-square' ), $this->plugin->get_plugin_name() ), array( $this, 'customer_tokens_eraser' ) );
89 }
90
91
92 /** Customer methods ******************************************************/
93
94
95 /**
96 * Adds the gateway data to customer data exports.
97 *
98 * @internal
99 *
100 * @since 3.0.0
101 *
102 * @param array $data customer personal data to export
103 * @param \WC_Customer $customer customer object
104 * @return array
105 */
106 public function add_export_customer_data( $data, $customer ) {
107
108 if ( $customer instanceof \WC_Customer ) {
109
110 foreach ( $this->get_plugin()->get_gateways() as $gateway ) {
111
112 // skip gateways that don't support customer ID
113 if ( ! $gateway->supports_customer_id() ) {
114 continue;
115 }
116
117 $customer_id = $gateway->get_customer_id( $customer->get_id(), array( 'autocreate' => false ) );
118
119 if ( $customer_id ) {
120
121 $data[] = array(
122 // translators: Placeholder %s - Customer ID
123 'name' => sprintf( esc_html__( '%s Customer ID', 'woocommerce-square' ), $gateway->get_method_title() ),
124 'value' => $customer_id,
125 );
126 }
127 }
128 }
129
130 return $data;
131 }
132
133
134 /**
135 * Removes the gateway data during an order data erasure.
136 *
137 * @since 3.0.0
138 *
139 * @param array $response customer data erasure response
140 * @param \WC_Customer $customer customer object
141 * @return array
142 */
143 public function remove_customer_personal_data( $response, $customer ) {
144
145 if ( $customer instanceof \WC_Customer ) {
146
147 foreach ( $this->get_plugin()->get_gateways() as $gateway ) {
148
149 // skip gateways that don't support customer ID
150 if ( ! $gateway->supports_customer_id() ) {
151 continue;
152 }
153
154 $gateway->remove_customer_id( $customer->get_id() );
155 }
156 }
157
158 return $response;
159 }
160
161
162 /**
163 * Handles the customer token exporter.
164 *
165 * @internal
166 *
167 * @param string $email_address email address for the user to export
168 * @param int $page page offset - unused as we don't page tokens
169 * @return array token export data
170 */
171 public function customer_tokens_exporter( $email_address, $page ) {
172
173 $data = array();
174 $user = get_user_by( 'email', $email_address );
175
176 if ( $user instanceof \WP_User ) {
177
178 foreach ( $this->get_plugin()->get_gateways() as $gateway ) {
179
180 // skip gateways that don't support tokenization
181 if ( ! $gateway->supports_tokenization() ) {
182 continue;
183 }
184
185 /** @var Square_Credit_Card_Payment_Token $token */
186 foreach ( $gateway->get_payment_tokens_handler()->get_tokens( $user->ID ) as $token ) {
187
188 $token_data = array();
189
190 if ( $token->get_card_type() ) {
191
192 $token_data[] = array(
193 'name' => __( 'Type', 'woocommerce-square' ),
194 'value' => $token->get_card_type(),
195 );
196 }
197
198 if ( $token->get_last4() ) {
199
200 $token_data[] = array(
201 'name' => __( 'Last Four', 'woocommerce-square' ),
202 'value' => $token->get_last4(),
203 );
204 }
205
206 if ( $token->get_nickname() ) {
207
208 $token_data[] = array(
209 'name' => __( 'Nickname', 'woocommerce-square' ),
210 'value' => $token->get_nickname(),
211 );
212 }
213
214 if ( ! empty( $token_data ) ) {
215
216 $data[] = array(
217 'group_id' => 'wc_' . $gateway->get_id() . '_tokens',
218 // translators: Placeholder %s - Payment token
219 'group_label' => sprintf( esc_html__( '%s Payment Tokens', 'woocommerce-square' ), $gateway->get_method_title() ),
220 'item_id' => 'token-' . $token->get_id(),
221 'data' => $token_data,
222 );
223 }
224 }
225 }
226 }
227
228 return array(
229 'data' => $data,
230 'done' => true,
231 );
232 }
233
234
235 /**
236 * Handles the customer token eraser.
237 *
238 * @internal
239 *
240 * @param string $email_address email address for the user to erase
241 * @param int $page page offset - unused as we don't page tokens
242 * @return array token eraser data
243 */
244 public function customer_tokens_eraser( $email_address, $page ) {
245
246 $removed = false;
247 $messages = array();
248
249 $user = get_user_by( 'email', $email_address );
250
251 if ( $user instanceof \WP_User ) {
252
253 foreach ( $this->get_plugin()->get_gateways() as $gateway ) {
254
255 // skip gateways that don't support tokenization
256 if ( ! $gateway->supports_tokenization() ) {
257 continue;
258 }
259
260 foreach ( $gateway->get_payment_tokens_handler()->get_tokens( $user->ID ) as $token ) {
261
262 $gateway->get_payment_tokens_handler()->remove_token( null, $token );
263
264 // translators: Placeholder %d - Payment token
265 $messages[] = sprintf( esc_html__( 'Removed payment token "%d"', 'woocommerce-square' ), $token->get_id() );
266 $removed = true;
267 }
268
269 // completely remove the user meta in case there is an API failure
270 delete_user_meta( $user->ID, $gateway->get_payment_tokens_handler()->get_user_meta_name() );
271
272 $gateway->get_payment_tokens_handler()->clear_transient( $user->ID );
273 }
274 }
275
276 return array(
277 'items_removed' => $removed,
278 'items_retained' => false,
279 'messages' => $messages,
280 'done' => true,
281 );
282 }
283
284
285 /** Order methods *********************************************************/
286
287
288 /**
289 * Adds the gateway data to order data exports.
290 *
291 * @internal
292 *
293 * @since 3.0.0
294 *
295 * @param array $data order personal data to export
296 * @param \WC_Order $order order object
297 * @return array
298 */
299 public function add_export_order_data( $data, $order ) {
300
301 $order = wc_get_order( $order );
302
303 // ensure we have a full order object and it belongs to the plugin's gateway
304 if ( $order && $this->get_plugin()->has_gateway( $order->get_payment_method() ) ) {
305
306 $gateway = $this->get_plugin()->get_gateway( $order->get_payment_method() );
307
308 $meta_to_export = array(
309 'account_four' => __( 'Last Four', 'woocommerce-square' ),
310 'account_type' => __( 'Account Type', 'woocommerce-square' ),
311 'card_type' => __( 'Card Type', 'woocommerce-square' ),
312 'card_expiry_date' => __( 'Expiry Date', 'woocommerce-square' ),
313 );
314
315 foreach ( $meta_to_export as $key => $label ) {
316
317 $value = $gateway->get_order_meta( $order, $key );
318
319 if ( $value ) {
320
321 $data[] = array(
322 'name' => $label,
323 'value' => $value,
324 );
325 }
326 }
327 }
328
329 return $data;
330 }
331
332
333 /**
334 * Removes the gateway data during an order data erasure.
335 *
336 * @since 3.0.0
337 *
338 * @param \WC_Order $order order object
339 */
340 public function remove_order_personal_data( $order ) {
341
342 $order = wc_get_order( $order );
343
344 // ensure we have a full order object and it belongs to the plugin's gateway
345 if ( $order && $this->get_plugin()->has_gateway( $order->get_payment_method() ) ) {
346
347 $gateway = $this->get_plugin()->get_gateway( $order->get_payment_method() );
348
349 $meta_to_remove = array(
350 'account_four' => 'XXXX',
351 'account_type' => '',
352 'card_type' => '',
353 'card_expiry_date' => 'XXXX',
354 );
355
356 /**
357 * Filters the personal order meta data to remove during a customer erasure request.
358 *
359 * @since 3.0.0
360 *
361 * @param array $meta_keys personal order meta data to remove during a customer erasure request, in the form of $meta_key => $anonymized_value
362 * @param \WC_Order $order order object
363 */
364 $meta_to_remove = apply_filters( 'wc_' . $gateway->get_id() . '_order_personal_data_to_remove', $meta_to_remove, $order );
365
366 foreach ( $meta_to_remove as $key => $anonymized_value ) {
367
368 // if the meta value already exists (don't add new meta to orders)
369 $value = $gateway->get_order_meta( $order, $key );
370
371 if ( $value ) {
372
373 // if no anon value was specified, let WP use its default
374 if ( empty( $anonymized_value ) && function_exists( 'wp_privacy_anonymize_data' ) ) {
375 $anonymized_value = wp_privacy_anonymize_data( 'text', $value );
376 }
377
378 $gateway->update_order_meta( $order, $key, $anonymized_value );
379 }
380 }
381
382 // clear the payment token (we don't want any "[deleted]" value stored)
383 if ( $gateway->get_order_meta( $order, 'payment_token' ) ) {
384 $gateway->update_order_meta( $order, 'payment_token', '' );
385 }
386 }
387 }
388
389
390 /**
391 * Gets the payment gateway plugin instance.
392 *
393 * @since 3.0.0
394 *
395 * @return Payment_Gateway_Plugin
396 */
397 protected function get_plugin() {
398
399 return $this->plugin;
400 }
401 }
402