Test Failed
Push — develop ( e9a01d...a0fd80 )
by Reüel
04:40
created

Gateway::update_status()   F

Complexity

Conditions 19
Paths 1283

Size

Total Lines 69
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 48.6739

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 19
eloc 37
c 5
b 0
f 0
nc 1283
nop 1
dl 0
loc 69
ccs 13
cts 23
cp 0.5652
crap 48.6739
rs 0.3499

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Mollie gateway.
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use DateInterval;
14
use Pronamic\WordPress\DateTime\DateTime;
15
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
16
use Pronamic\WordPress\Pay\Core\PaymentMethods;
17
use Pronamic\WordPress\Pay\Core\Recurring as Core_Recurring;
18
use Pronamic\WordPress\Pay\Payments\BankAccountDetails;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\Payments\BankAccountDetails was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
use Pronamic\WordPress\Pay\Payments\BankTransferDetails;
0 ignored issues
show
Bug introduced by
The type Pronamic\WordPress\Pay\P...nts\BankTransferDetails was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use Pronamic\WordPress\Pay\Payments\PaymentStatus;
21
use Pronamic\WordPress\Pay\Payments\Payment;
22
23
/**
24
 * Title: Mollie
25
 * Description:
26
 * Copyright: 2005-2019 Pronamic
27
 * Company: Pronamic
28
 *
29
 * @author  Remco Tolsma
30
 * @version 2.0.8
31
 * @since   1.1.0
32
 */
33
class Gateway extends Core_Gateway {
34
	/**
35
	 * Client.
36
	 *
37
	 * @var Client
38
	 */
39
	protected $client;
40
41
	/**
42
	 * Meta key for customer ID.
43
	 *
44
	 * @var string
45
	 */
46
	private $meta_key_customer_id = '_pronamic_pay_mollie_customer_id';
47
48
	/**
49
	 * Constructs and initializes an Mollie gateway
50
	 *
51 38
	 * @param Config $config Config.
52 38
	 */
53
	public function __construct( Config $config ) {
54 38
		parent::__construct( $config );
55
56
		$this->set_method( self::METHOD_HTTP_REDIRECT );
57 38
58
		// Supported features.
59
		$this->supports = array(
60
			'payment_status_request',
61
			'recurring_direct_debit',
62
			'recurring_credit_card',
63
			'recurring',
64
			'webhook',
65 38
			'webhook_log',
66 38
			'webhook_no_config',
67
		);
68
69 38
		// Client.
70 37
		$this->client = new Client( \strval( $config->api_key ) );
71
		$this->client->set_mode( $config->mode );
72
73
		// Mollie customer ID meta key.
74 38
		if ( self::MODE_TEST === $config->mode ) {
75 38
			$this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test';
76
		}
77
78
		// Actions.
79
		add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 );
80
	}
81
82 2
	/**
83 2
	 * Get issuers
84
	 *
85 2
	 * @see Core_Gateway::get_issuers()
86
	 */
87 2
	public function get_issuers() {
88 2
		$groups = array();
89
90 2
		try {
91
			$result = $this->client->get_issuers();
92
93
			$groups[] = array(
94
				'options' => $result,
95
			);
96
		} catch ( Error $e ) {
97
			// Catch Mollie error.
98
			$error = new \WP_Error(
99
				'mollie_error',
100
				sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() )
101
			);
102
103
			$this->set_error( $error );
104
		} catch ( \Exception $e ) {
105 1
			// Catch exceptions.
106 1
			$error = new \WP_Error( 'mollie_error', $e->getMessage() );
107
108
			$this->set_error( $error );
109 1
		}
110
111 1
		return $groups;
112
	}
113 1
114
	/**
115 1
	 * Get available payment methods.
116
	 *
117 1
	 * @see Core_Gateway::get_available_payment_methods()
118 1
	 */
119
	public function get_available_payment_methods() {
120 1
		$payment_methods = array();
121
122
		// Set sequence types to get payment methods for.
123
		$sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST );
124
125
		$results = array();
126
127
		foreach ( $sequence_types as $sequence_type ) {
128
			// Get active payment methods for Mollie account.
129
			try {
130
				$result = $this->client->get_payment_methods( $sequence_type );
131
			} catch ( Error $e ) {
132
				// Catch Mollie error.
133
				$error = new \WP_Error(
134
					'mollie_error',
135
					sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() )
136
				);
137
138
				$this->set_error( $error );
139
140
				break;
141
			} catch ( \Exception $e ) {
142
				// Catch exceptions.
143 1
				$error = new \WP_Error( 'mollie_error', $e->getMessage() );
144
145
				$this->set_error( $error );
146
147
				break;
148
			}
149
150
			if ( Sequence::FIRST === $sequence_type ) {
151
				foreach ( $result as $method => $title ) {
152
					unset( $result[ $method ] );
153
154
					// Get WordPress payment method for direct debit method.
155 1
					$method         = Methods::transform_gateway_method( $method );
156
					$payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true );
157 1
158
					if ( $payment_method ) {
159
						$results[ $payment_method ] = $title;
160
					}
161
				}
162
			}
163
164
			if ( is_array( $result ) ) {
165 2
				$results = array_merge( $results, $result );
166
			}
167 2
		}
168
169
		// Transform to WordPress payment methods.
170
		foreach ( $results as $method => $title ) {
171
			if ( PaymentMethods::is_recurring_method( $method ) ) {
172
				$payment_method = $method;
173
			} else {
174
				$payment_method = Methods::transform_gateway_method( $method );
175
			}
176
177
			if ( $payment_method ) {
178
				$payment_methods[] = $payment_method;
179
			}
180
		}
181
182
		$payment_methods = array_unique( $payment_methods );
183
184
		return $payment_methods;
185
	}
186
187
	/**
188
	 * Get supported payment methods
189 4
	 *
190 4
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
191
	 */
192 4
	public function get_supported_payment_methods() {
193
		return array(
194 4
			PaymentMethods::BANCONTACT,
195
			PaymentMethods::BANK_TRANSFER,
196
			PaymentMethods::BELFIUS,
197
			PaymentMethods::CREDIT_CARD,
198
			PaymentMethods::DIRECT_DEBIT,
199 4
			PaymentMethods::DIRECT_DEBIT_BANCONTACT,
200
			PaymentMethods::DIRECT_DEBIT_IDEAL,
201 1
			PaymentMethods::DIRECT_DEBIT_SOFORT,
202 3
			PaymentMethods::EPS,
203
			PaymentMethods::GIROPAY,
204 1
			PaymentMethods::IDEAL,
205 2
			PaymentMethods::KBC,
206
			PaymentMethods::PAYPAL,
207 1
			PaymentMethods::SOFORT,
208 1
		);
209
	}
210
211
	/**
212
	 * Get webhook URL for Mollie.
213 1
	 *
214
	 * @return string|null
215 1
	 */
216
	public function get_webhook_url() {
217
		$url = home_url( '/' );
218
219
		$host = wp_parse_url( $url, PHP_URL_HOST );
220
221
		if ( is_array( $host ) ) {
222
			// Parsing failure.
223
			$host = '';
224
		}
225
226
		if ( 'localhost' === $host ) {
227
			// Mollie doesn't allow localhost.
228
			return null;
229
		} elseif ( '.dev' === substr( $host, -4 ) ) {
230
			// Mollie doesn't allow the .dev TLD.
231
			return null;
232
		} elseif ( '.local' === substr( $host, -6 ) ) {
233
			// Mollie doesn't allow the .local TLD.
234
			return null;
235
		} elseif ( '.test' === substr( $host, -5 ) ) {
236
			// Mollie doesn't allow the .test TLD.
237
			return null;
238
		}
239
240
		$url = add_query_arg( 'mollie_webhook', '', $url );
241
242
		return $url;
243
	}
244
245
	/**
246
	 * Start
247
	 *
248
	 * @see Pronamic_WP_Pay_Gateway::start()
249
	 *
250
	 * @param Payment $payment Payment.
251
	 */
252
	public function start( Payment $payment ) {
253
		$request               = new PaymentRequest();
254
		$request->amount       = AmountTransformer::transform( $payment->get_total_amount() );
255
		$request->description  = \strval( $payment->get_description() );
256
		$request->redirect_url = $payment->get_return_url();
257
		$request->webhook_url  = $this->get_webhook_url();
258
259
		// Locale.
260
		if ( null !== $payment->get_customer() ) {
261
			$request->locale = LocaleHelper::transform( $payment->get_customer()->get_locale() );
262
		}
263
264
		// Customer ID.
265
		$customer_id = $this->get_customer_id_for_payment( $payment );
266
267
		if ( is_string( $customer_id ) && ! empty( $customer_id ) ) {
268
			$request->customer_id = $customer_id;
269
		}
270
271
		// Payment method.
272
		$payment_method = $payment->get_method();
273
274
		// Recurring payment method.
275
		$is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) );
276
277
		if ( false === $is_recurring_method ) {
278
			// Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method.
279
			$is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method );
280
		}
281
282
		if ( $is_recurring_method ) {
283
			$request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST;
284
285
			if ( Sequence::FIRST === $request->sequence_type ) {
286
				$payment_method = PaymentMethods::get_first_payment_method( $payment_method );
287
			}
288
289
			if ( Sequence::RECURRING === $request->sequence_type ) {
290
				$payment->set_action_url( $payment->get_return_url() );
291
			}
292
		}
293
294
		// Leap of faith if the WordPress payment method could not transform to a Mollie method?
295
		$request->method = Methods::transform( $payment_method, $payment_method );
296
297
		// Issuer.
298
		if ( Methods::IDEAL === $request->method ) {
299
			$request->issuer = $payment->get_issuer();
300
		}
301
302
		// Due date.
303
		try {
304
			$due_date = new DateTime( sprintf( '+%s days', $this->config->due_date_days ) );
305
		} catch ( \Exception $e ) {
306
			$due_date = null;
307
		}
308
309
		$request->set_due_date( $due_date );
310
311
		// Create payment.
312
		$result = $this->client->create_payment( $request );
313
314
		// Set transaction ID.
315
		if ( isset( $result->id ) ) {
316
			$payment->set_transaction_id( $result->id );
317
		}
318
319
		// Set expiry date.
320
		/* phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
321
		if ( isset( $result->expiresAt ) ) {
322
			try {
323
				$expires_at = new DateTime( $result->expiresAt );
324
			} catch ( \Exception $e ) {
325
				$expires_at = null;
326
			}
327
328
			$payment->set_expiry_date( $expires_at );
0 ignored issues
show
Bug introduced by
The method set_expiry_date() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

328
			$payment->/** @scrutinizer ignore-call */ 
329
             set_expiry_date( $expires_at );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
329
		}
330
		/* phpcs:enable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase */
331
332
		// Set status.
333
		if ( isset( $result->status ) ) {
334
			$payment->set_status( Statuses::transform( $result->status ) );
335
		}
336
337
		// Set bank transfer recipient details.
338
		if ( isset( $result->details ) ) {
339
			$bank_transfer_recipient_details = $payment->get_bank_transfer_recipient_details();
0 ignored issues
show
Bug introduced by
The method get_bank_transfer_recipient_details() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

339
			/** @scrutinizer ignore-call */ 
340
   $bank_transfer_recipient_details = $payment->get_bank_transfer_recipient_details();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
340
341
			if ( null === $bank_transfer_recipient_details ) {
342
				$bank_transfer_recipient_details = new BankTransferDetails();
343
344
				$payment->set_bank_transfer_recipient_details( $bank_transfer_recipient_details );
0 ignored issues
show
Bug introduced by
The method set_bank_transfer_recipient_details() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

344
				$payment->/** @scrutinizer ignore-call */ 
345
              set_bank_transfer_recipient_details( $bank_transfer_recipient_details );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
345
			}
346
347
			$bank_details = $bank_transfer_recipient_details->get_bank_account();
348
349
			if ( null === $bank_details ) {
350
				$bank_details = new BankAccountDetails();
351
352
				$bank_transfer_recipient_details->set_bank_account( $bank_details );
353
			}
354
355
			$details = $result->details;
356
357
			/*
358
			 * @codingStandardsIgnoreStart
359
			 *
360
			 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
361
			 */
362
			if ( isset( $details->bankName ) ) {
363
				/**
364
				 * Set `bankName` as bank details name, as result "Stichting Mollie Payments"
365
				 * is not the name of a bank, but the account holder name.
366
				 */
367
				$bank_details->set_name( $details->bankName );
368 11
			}
369
370 11
			if ( isset( $details->bankAccount ) ) {
371
				$bank_details->set_iban( $details->bankAccount );
372
			}
373 11
374
			if ( isset( $details->bankBic ) ) {
375 11
				$bank_details->set_bic( $details->bankBic );
376
			}
377
378 11
			if ( isset( $details->transferReference ) ) {
379 11
				$bank_transfer_recipient_details->set_reference( $details->transferReference );
380
			}
381
			// @codingStandardsIgnoreEnd
382 11
		}
383 7
384
		// Set action URL.
385 7
		if ( isset( $result->_links ) ) {
386
			if ( isset( $result->_links->checkout->href ) ) {
387
				$payment->set_action_url( $result->_links->checkout->href );
388 11
			}
389 5
		}
390
	}
391
392
	/**
393
	 * Update status of the specified payment
394 11
	 *
395
	 * @param Payment $payment Payment.
396
	 *
397
	 * @return void
398
	 */
399
	public function update_status( Payment $payment ) {
400
		$transaction_id = $payment->get_transaction_id();
401
402
		if ( null === $transaction_id ) {
403
			return;
404
		}
405
406
		$mollie_payment = $this->client->get_payment( $transaction_id );
407 11
408
		if ( isset( $mollie_payment->status ) ) {
409
			$payment->set_status( Statuses::transform( $mollie_payment->status ) );
410
		}
411
412 11
		if ( isset( $mollie_payment->details ) ) {
413
			$consumer_bank_details = $payment->get_consumer_bank_details();
0 ignored issues
show
Bug introduced by
The method get_consumer_bank_details() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

413
			/** @scrutinizer ignore-call */ 
414
   $consumer_bank_details = $payment->get_consumer_bank_details();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
414 11
415
			if ( null === $consumer_bank_details ) {
416
				$consumer_bank_details = new BankAccountDetails();
417
418
				$payment->set_consumer_bank_details( $consumer_bank_details );
0 ignored issues
show
Bug introduced by
The method set_consumer_bank_details() does not exist on Pronamic\WordPress\Pay\Payments\Payment. Did you maybe mean set_consumer_bic()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

418
				$payment->/** @scrutinizer ignore-call */ 
419
              set_consumer_bank_details( $consumer_bank_details );

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
419
			}
420
421
			$details = $mollie_payment->details;
422
423
			/*
424 28
			 * @codingStandardsIgnoreStart
425 28
			 *
426 11
			 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
427
			 */
428
			if ( isset( $details->consumerName ) ) {
429 17
				$consumer_bank_details->set_name( $details->consumerName );
430
			}
431
432
			if ( isset( $details->cardHolder ) ) {
433
				$consumer_bank_details->set_name( $details->cardHolder );
434
			}
435
436
			if ( isset( $details->cardNumber ) ) {
437
				// The last four digits of the card number.
438
				$consumer_bank_details->set_account_number( $details->cardNumber );
439
			}
440
441
			if ( isset( $details->cardCountryCode ) ) {
442
				// The ISO 3166-1 alpha-2 country code of the country the card was issued in.
443
				$consumer_bank_details->set_country( $details->cardCountryCode );
444
			}
445
446
			if ( isset( $details->consumerAccount ) ) {
447
				switch ( $mollie_payment->method ) {
448
					case Methods::BELFIUS:
449
					case Methods::DIRECT_DEBIT:
450
					case Methods::IDEAL:
451
					case Methods::KBC:
452
					case Methods::SOFORT:
453
						$consumer_bank_details->set_iban( $details->consumerAccount );
454
455
						break;
456
					case Methods::BANCONTACT:
457
					case Methods::BANKTRANSFER:
458
					case Methods::PAYPAL:
459 28
					default:
460 28
						$consumer_bank_details->set_account_number( $details->consumerAccount );
461 1
462
						break;
463
				}
464 27
			}
465
466 27
			if ( isset( $details->consumerBic ) ) {
467 27
				$consumer_bank_details->set_bic( $details->consumerBic );
468
			}
469
			// @codingStandardsIgnoreEnd
470
		}
471
	}
472
473
	/**
474
	 * Get Mollie customer ID for payment.
475
	 *
476
	 * @param Payment $payment Payment.
477
	 *
478
	 * @return bool|string
479
	 */
480
	public function get_customer_id_for_payment( Payment $payment ) {
481
		// Get WordPress user ID from payment customer.
482
		$user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() );
483
484
		// Get Mollie customer ID from user meta.
485
		$customer_id = $this->get_customer_id_by_wp_user_id( $user_id );
486
487
		$subscription = $payment->get_subscription();
488
489
		// Get customer ID from subscription meta.
490
		if ( $subscription ) {
491
			$subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' );
492
493
			// Try to get (legacy) customer ID from first payment.
494
			if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) {
495
				$first_payment = $subscription->get_first_payment();
496
497
				$subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' );
498
			}
499
500
			if ( ! empty( $subscription_customer_id ) ) {
501
				$customer_id = $subscription_customer_id;
502
			}
503
		}
504
505
		// Create new customer if the customer does not exist at Mollie.
506
		if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) {
507
			$customer_name = null;
508
509
			if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) {
510
				$customer_name = strval( $payment->get_customer()->get_name() );
511
			}
512
513
			$customer_id = $this->client->create_customer( $payment->get_email(), $customer_name );
514
515
			$this->update_wp_user_customer_id( $user_id, $customer_id );
516
		}
517
518
		// Store customer ID in subscription meta.
519
		if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) {
520
			$subscription->set_meta( 'mollie_customer_id', $customer_id );
521
		}
522
523
		// Copy customer ID from subscription to user meta.
524
		$this->copy_customer_id_to_wp_user( $payment );
525
526
		return $customer_id;
527
	}
528
529
	/**
530
	 * Get Mollie customer ID by the specified WordPress user ID.
531
	 *
532
	 * @param int $user_id WordPress user ID.
533
	 *
534
	 * @return string|bool
535
	 */
536
	public function get_customer_id_by_wp_user_id( $user_id ) {
537
		if ( empty( $user_id ) ) {
538
			return false;
539
		}
540
541
		return get_user_meta( $user_id, $this->meta_key_customer_id, true );
542
	}
543
544
	/**
545
	 * Update Mollie customer ID meta for WordPress user.
546
	 *
547
	 * @param int    $user_id     WordPress user ID.
548
	 * @param string $customer_id Mollie Customer ID.
549
	 *
550
	 * @return bool
551
	 */
552
	private function update_wp_user_customer_id( $user_id, $customer_id ) {
553
		if ( empty( $user_id ) || is_bool( $user_id ) ) {
554
			return false;
555
		}
556
557
		if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) {
0 ignored issues
show
introduced by
The condition is_string($customer_id) is always true.
Loading history...
558
			return false;
559
		}
560
561
		update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id );
562
	}
563
564
	/**
565
	 * Copy Mollie customer ID from subscription meta to WordPress user meta.
566
	 *
567
	 * @param Payment $payment Payment.
568
	 *
569
	 * @return void
570
	 */
571
	public function copy_customer_id_to_wp_user( Payment $payment ) {
572
		if ( $this->config->id !== $payment->config_id ) {
573
			return;
574
		}
575
576
		$subscription = $payment->get_subscription();
577
578
		if ( ! $subscription || empty( $subscription->user_id ) ) {
579
			return;
580
		}
581
582
		// Get customer ID from subscription meta.
583
		$customer_id = $subscription->get_meta( 'mollie_customer_id' );
584
585
		$user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id );
0 ignored issues
show
Bug introduced by
It seems like $subscription->user_id can also be of type string; however, parameter $user_id of Pronamic\WordPress\Pay\G...omer_id_by_wp_user_id() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

585
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
586
587
		if ( empty( $user_customer_id ) ) {
588
			// Set customer ID as user meta.
589
			$this->update_wp_user_customer_id( $subscription->user_id, $customer_id );
0 ignored issues
show
Bug introduced by
It seems like $customer_id can also be of type false; however, parameter $customer_id of Pronamic\WordPress\Pay\G...e_wp_user_customer_id() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

589
			$this->update_wp_user_customer_id( $subscription->user_id, /** @scrutinizer ignore-type */ $customer_id );
Loading history...
Bug introduced by
It seems like $subscription->user_id can also be of type string; however, parameter $user_id of Pronamic\WordPress\Pay\G...e_wp_user_customer_id() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

589
			$this->update_wp_user_customer_id( /** @scrutinizer ignore-type */ $subscription->user_id, $customer_id );
Loading history...
590
		}
591
	}
592
}
593