Test Failed
Push — develop ( 9b8657...429647 )
by Reüel
05:35
created

src/Gateway.php (3 issues)

Labels
Severity
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\PaymentStatus;
19
use Pronamic\WordPress\Pay\Payments\Payment;
20
21
/**
22
 * Title: Mollie
23
 * Description:
24
 * Copyright: 2005-2019 Pronamic
25
 * Company: Pronamic
26
 *
27
 * @author  Remco Tolsma
28
 * @version 2.0.8
29
 * @since   1.1.0
30
 */
31
class Gateway extends Core_Gateway {
32
	/**
33
	 * Client.
34
	 *
35
	 * @var Client
36
	 */
37
	protected $client;
38
39
	/**
40
	 * Meta key for customer ID.
41
	 *
42
	 * @var string
43
	 */
44
	private $meta_key_customer_id = '_pronamic_pay_mollie_customer_id';
45
46
	/**
47
	 * Constructs and initializes an Mollie gateway
48
	 *
49
	 * @param Config $config Config.
50
	 */
51 38
	public function __construct( Config $config ) {
52 38
		parent::__construct( $config );
53
54 38
		$this->set_method( self::METHOD_HTTP_REDIRECT );
55
56
		// Supported features.
57 38
		$this->supports = array(
58
			'payment_status_request',
59
			'recurring_direct_debit',
60
			'recurring_credit_card',
61
			'recurring',
62
		);
63
64
		// Client.
65 38
		$this->client = new Client( \strval( $config->api_key ) );
66 38
		$this->client->set_mode( $config->mode );
67
68
		// Mollie customer ID meta key.
69 38
		if ( self::MODE_TEST === $config->mode ) {
70 37
			$this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test';
71
		}
72
73
		// Actions.
74 38
		add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 );
75 38
	}
76
77
	/**
78
	 * Get issuers
79
	 *
80
	 * @see Core_Gateway::get_issuers()
81
	 */
82 2
	public function get_issuers() {
83 2
		$groups = array();
84
85 2
		try {
86
			$result = $this->client->get_issuers();
87 2
88 2
			$groups[] = array(
89
				'options' => $result,
90 2
			);
91
		} catch ( Error $e ) {
92
			// Catch Mollie error.
93
			$error = new \WP_Error(
94
				'mollie_error',
95
				sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() )
96
			);
97
98
			$this->set_error( $error );
99
		} catch ( \Exception $e ) {
100
			// Catch exceptions.
101
			$error = new \WP_Error( $e->getCode(), $e->getMessage() );
102
103
			$this->set_error( $error );
104
		}
105 1
106 1
		return $groups;
107
	}
108
109 1
	/**
110
	 * Get available payment methods.
111 1
	 *
112
	 * @see Core_Gateway::get_available_payment_methods()
113 1
	 */
114
	public function get_available_payment_methods() {
115 1
		$payment_methods = array();
116
117 1
		// Set sequence types to get payment methods for.
118 1
		$sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST );
119
120 1
		$results = array();
121
122
		foreach ( $sequence_types as $sequence_type ) {
123
			// Get active payment methods for Mollie account.
124
			try {
125
				$result = $this->client->get_payment_methods( $sequence_type );
126
			} catch ( Error $e ) {
127
				// Catch Mollie error.
128
				$error = new \WP_Error(
129
					'mollie_error',
130
					sprintf( '%1$s (%2$s) - %3$s', $e->get_title(), $e->getCode(), $e->get_detail() )
131
				);
132
133
				$this->set_error( $error );
134
135
				break;
136
			} catch ( \Exception $e ) {
137
				// Catch exceptions.
138
				$error = new \WP_Error( $e->getCode(), $e->getMessage() );
139
140
				$this->set_error( $error );
141
142
				break;
143 1
			}
144
145
			if ( Sequence::FIRST === $sequence_type ) {
146
				foreach ( $result as $method => $title ) {
147
					unset( $result[ $method ] );
148
149
					// Get WordPress payment method for direct debit method.
150
					$method         = Methods::transform_gateway_method( $method );
151
					$payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true );
152
153
					if ( $payment_method ) {
154
						$results[ $payment_method ] = $title;
155 1
					}
156
				}
157 1
			}
158
159
			if ( is_array( $result ) ) {
160
				$results = array_merge( $results, $result );
161
			}
162
		}
163
164
		// Transform to WordPress payment methods.
165 2
		foreach ( $results as $method => $title ) {
166
			if ( PaymentMethods::is_recurring_method( $method ) ) {
167 2
				$payment_method = $method;
168
			} else {
169
				$payment_method = Methods::transform_gateway_method( $method );
170
			}
171
172
			if ( $payment_method ) {
173
				$payment_methods[] = $payment_method;
174
			}
175
		}
176
177
		$payment_methods = array_unique( $payment_methods );
178
179
		return $payment_methods;
180
	}
181
182
	/**
183
	 * Get supported payment methods
184
	 *
185
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
186
	 */
187
	public function get_supported_payment_methods() {
188
		return array(
189 4
			PaymentMethods::BANCONTACT,
190 4
			PaymentMethods::BANK_TRANSFER,
191
			PaymentMethods::BELFIUS,
192 4
			PaymentMethods::CREDIT_CARD,
193
			PaymentMethods::DIRECT_DEBIT,
194 4
			PaymentMethods::DIRECT_DEBIT_BANCONTACT,
195
			PaymentMethods::DIRECT_DEBIT_IDEAL,
196
			PaymentMethods::DIRECT_DEBIT_SOFORT,
197
			PaymentMethods::EPS,
198
			PaymentMethods::GIROPAY,
199 4
			PaymentMethods::IDEAL,
200
			PaymentMethods::KBC,
201 1
			PaymentMethods::PAYPAL,
202 3
			PaymentMethods::SOFORT,
203
		);
204 1
	}
205 2
206
	/**
207 1
	 * Get webhook URL for Mollie.
208 1
	 *
209
	 * @return string|null
210
	 */
211
	public function get_webhook_url() {
212
		$url = home_url( '/' );
213 1
214
		$host = wp_parse_url( $url, PHP_URL_HOST );
215 1
216
		if ( is_array( $host ) ) {
217
			// Parsing failure.
218
			$host = '';
219
		}
220
221
		if ( 'localhost' === $host ) {
222
			// Mollie doesn't allow localhost.
223
			return null;
224
		} elseif ( '.dev' === substr( $host, -4 ) ) {
225
			// Mollie doesn't allow the .dev TLD.
226
			return null;
227
		} elseif ( '.local' === substr( $host, -6 ) ) {
228
			// Mollie doesn't allow the .local TLD.
229
			return null;
230
		} elseif ( '.test' === substr( $host, -5 ) ) {
231
			// Mollie doesn't allow the .test TLD.
232
			return null;
233
		}
234
235
		$url = add_query_arg( 'mollie_webhook', '', $url );
236
237
		return $url;
238
	}
239
240
	/**
241
	 * Start
242
	 *
243
	 * @see Pronamic_WP_Pay_Gateway::start()
244
	 *
245
	 * @param Payment $payment Payment.
246
	 */
247
	public function start( Payment $payment ) {
248
		$request               = new PaymentRequest();
249
		$request->amount       = AmountTransformer::transform( $payment->get_total_amount() );
250
		$request->description  = \strval( $payment->get_description() );
251
		$request->redirect_url = $payment->get_return_url();
252
		$request->webhook_url  = $this->get_webhook_url();
253
254
		// Locale.
255
		if ( null !== $payment->get_customer() ) {
256
			$request->locale = LocaleHelper::transform( $payment->get_customer()->get_locale() );
257
		}
258
259
		// Customer ID.
260
		$customer_id = $this->get_customer_id_for_payment( $payment );
261
262
		if ( is_string( $customer_id ) && ! empty( $customer_id ) ) {
263
			$request->customer_id = $customer_id;
264
		}
265
266
		// Payment method.
267
		$payment_method = $payment->get_method();
268
269
		// Recurring payment method.
270
		$is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) );
271
272
		if ( false === $is_recurring_method ) {
273
			// Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method.
274
			$is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method );
275
		}
276
277
		if ( $is_recurring_method ) {
278
			$request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST;
279
280
			if ( Sequence::FIRST === $request->sequence_type ) {
281
				$payment_method = PaymentMethods::get_first_payment_method( $payment_method );
282
			}
283
284
			if ( Sequence::RECURRING === $request->sequence_type ) {
285
				$payment->set_action_url( $payment->get_return_url() );
286
			}
287
		}
288
289
		// Leap of faith if the WordPress payment method could not transform to a Mollie method?
290
		$request->method = Methods::transform( $payment_method, $payment_method );
291
292
		// Issuer.
293
		if ( Methods::IDEAL === $request->method ) {
294
			$request->issuer = $payment->get_issuer();
295
		}
296
297
		// Due date.
298
		try {
299
			$due_date = new DateTime( sprintf( '+%s days', $this->config->due_date_days ) );
300
		} catch ( \Exception $e ) {
301
			$due_date = null;
302
		}
303
304
		$request->set_due_date( $due_date );
305
306
		// Create payment.
307
		$result = $this->client->create_payment( $request );
308
309
		// Set transaction ID.
310
		if ( isset( $result->id ) ) {
311
			$payment->set_transaction_id( $result->id );
312
		}
313
314
		// Set status.
315
		if ( isset( $result->status ) ) {
316
			$payment->set_status( Statuses::transform( $result->status ) );
317
		}
318
319
		// Set transfer reference.
320
		if ( isset( $result->details ) ) {
321
			if ( isset( $result->details->transferReference ) ) {
322
				$payment->set_meta( 'mollie_transfer_reference', $result->details->transferReference );
323
			}
324
		}
325
326
		// Set action URL.
327
		if ( isset( $result->_links ) ) {
328
			if ( isset( $result->_links->checkout->href ) ) {
329
				$payment->set_action_url( $result->_links->checkout->href );
330
			}
331
		}
332
	}
333
334
	/**
335
	 * Update status of the specified payment
336
	 *
337
	 * @param Payment $payment Payment.
338
	 *
339
	 * @return void
340
	 */
341
	public function update_status( Payment $payment ) {
342
		$transaction_id = $payment->get_transaction_id();
343
344
		if ( null === $transaction_id ) {
345
			return;
346
		}
347
348
		$mollie_payment = $this->client->get_payment( $transaction_id );
349
350
		if ( isset( $mollie_payment->status ) ) {
351
			$payment->set_status( Statuses::transform( $mollie_payment->status ) );
352
		}
353
354
		if ( isset( $mollie_payment->details ) ) {
355
			$details = $mollie_payment->details;
356
357
			/*
358
			 * @codingStandardsIgnoreStart
359
			 *
360
			 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
361
			 */
362
			if ( isset( $details->consumerName ) ) {
363
				$payment->set_consumer_name( $details->consumerName );
364
			}
365
366
			if ( isset( $details->cardHolder ) ) {
367
				$payment->set_consumer_name( $details->cardHolder );
368 11
			}
369
370 11
			if ( isset( $details->consumerAccount ) ) {
371
				$payment->set_consumer_iban( $details->consumerAccount );
372
			}
373 11
374
			if ( isset( $details->consumerBic ) ) {
375 11
				$payment->set_consumer_bic( $details->consumerBic );
376
			}
377
			// @codingStandardsIgnoreEnd
378 11
		}
379 11
	}
380
381
	/**
382 11
	 * Get Mollie customer ID for payment.
383 7
	 *
384
	 * @param Payment $payment Payment.
385 7
	 *
386
	 * @return bool|string
387
	 */
388 11
	public function get_customer_id_for_payment( Payment $payment ) {
389 5
		// Get WordPress user ID from payment customer.
390
		$user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() );
391
392
		// Get Mollie customer ID from user meta.
393
		$customer_id = $this->get_customer_id_by_wp_user_id( $user_id );
394 11
395
		$subscription = $payment->get_subscription();
396
397
		// Get customer ID from subscription meta.
398
		if ( $subscription ) {
399
			$subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' );
400
401
			// Try to get (legacy) customer ID from first payment.
402
			if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) {
403
				$first_payment = $subscription->get_first_payment();
404
405
				$subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' );
406
			}
407 11
408
			if ( ! empty( $subscription_customer_id ) ) {
409
				$customer_id = $subscription_customer_id;
410
			}
411
		}
412 11
413
		// Create new customer if the customer does not exist at Mollie.
414 11
		if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) {
415
			$customer_name = null;
416
417
			if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) {
418
				$customer_name = strval( $payment->get_customer()->get_name() );
419
			}
420
421
			$customer_id = $this->client->create_customer( $payment->get_email(), $customer_name );
422
423
			$this->update_wp_user_customer_id( $user_id, $customer_id );
424 28
		}
425 28
426 11
		// Store customer ID in subscription meta.
427
		if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) {
428
			$subscription->set_meta( 'mollie_customer_id', $customer_id );
429 17
		}
430
431
		// Copy customer ID from subscription to user meta.
432
		$this->copy_customer_id_to_wp_user( $payment );
433
434
		return $customer_id;
435
	}
436
437
	/**
438
	 * Get Mollie customer ID by the specified WordPress user ID.
439
	 *
440
	 * @param int $user_id WordPress user ID.
441
	 *
442
	 * @return string|bool
443
	 */
444
	public function get_customer_id_by_wp_user_id( $user_id ) {
445
		if ( empty( $user_id ) ) {
446
			return false;
447
		}
448
449
		return get_user_meta( $user_id, $this->meta_key_customer_id, true );
450
	}
451
452
	/**
453
	 * Update Mollie customer ID meta for WordPress user.
454
	 *
455
	 * @param int    $user_id     WordPress user ID.
456
	 * @param string $customer_id Mollie Customer ID.
457
	 *
458
	 * @return bool
459 28
	 */
460 28
	private function update_wp_user_customer_id( $user_id, $customer_id ) {
461 1
		if ( empty( $user_id ) || is_bool( $user_id ) ) {
462
			return false;
463
		}
464 27
465
		if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) {
466 27
			return false;
467 27
		}
468
469
		update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id );
470
	}
471
472
	/**
473
	 * Copy Mollie customer ID from subscription meta to WordPress user meta.
474
	 *
475
	 * @param Payment $payment Payment.
476
	 *
477
	 * @return void
478
	 */
479
	public function copy_customer_id_to_wp_user( Payment $payment ) {
480
		if ( $this->config->id !== $payment->config_id ) {
481
			return;
482
		}
483
484
		$subscription = $payment->get_subscription();
485
486
		if ( ! $subscription || empty( $subscription->user_id ) ) {
487
			return;
488
		}
489
490
		// Get customer ID from subscription meta.
491
		$customer_id = $subscription->get_meta( 'mollie_customer_id' );
492
493
		$user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id );
0 ignored issues
show
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

493
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
494
495
		if ( empty( $user_customer_id ) ) {
496
			// Set customer ID as user meta.
497
			$this->update_wp_user_customer_id( $subscription->user_id, $customer_id );
0 ignored issues
show
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

497
			$this->update_wp_user_customer_id( /** @scrutinizer ignore-type */ $subscription->user_id, $customer_id );
Loading history...
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

497
			$this->update_wp_user_customer_id( $subscription->user_id, /** @scrutinizer ignore-type */ $customer_id );
Loading history...
498
		}
499
	}
500
}
501