Test Failed
Push — master ( 272cf6...4fb2a3 )
by Remco
21:31 queued 14:28
created

Gateway::next_payment_delivery_date()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

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

455
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
456
457 16
		if ( empty( $user_customer_id ) ) {
458
			// Set customer ID as user meta.
459 16
			$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

459
			$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

459
			$this->update_wp_user_customer_id( /** @scrutinizer ignore-type */ $subscription->user_id, $customer_id );
Loading history...
460
		}
461
	}
462
463
	/**
464
	 * Next payment delivery date.
465
	 *
466
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
467
	 * @param Payment   $payment                    Payment.
468
	 *
469
	 * @return \DateTime
470
	 */
471
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
472
		// Check gateway.
473
		$gateway_id = get_post_meta( $payment->get_config_id(), '_pronamic_gateway_id', true );
474
475
		if ( 'mollie' !== $gateway_id ) {
476
			return $next_payment_delivery_date;
477
		}
478
479
		// Check direct debit payment method.
480
		if ( ! PaymentMethods::is_direct_debit_method( $payment->get_method() ) ) {
481
			return $next_payment_delivery_date;
482
		}
483
484
		// Textual representation of the day of the week, Sunday through Saturday.
485
		$day_of_week = $next_payment_delivery_date->format( 'l' );
486
487
		switch ( $day_of_week ) {
488
			case 'Monday':
489
				$next_payment_delivery_date->sub( new DateInterval( 'P3D' ) );
490
				break;
491
492
			case 'Saturday':
493
				$next_payment_delivery_date->sub( new DateInterval( 'P2D' ) );
494
				break;
495
496
			case 'Sunday':
497
				$next_payment_delivery_date->sub( new DateInterval( 'P3D' ) );
498
				break;
499
500
			default:
501
				$next_payment_delivery_date->sub( new DateInterval( 'P1D' ) );
502
				break;
503
		}
504
505
		$next_payment_delivery_date->setTime( 0, 0, 0 );
506
507
		return $next_payment_delivery_date;
508
	}
509
}
510