Test Failed
Push — develop ( 4673e9...fbb7c8 )
by Reüel
03:47
created

Gateway::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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

435
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
436
437
		if ( empty( $user_customer_id ) ) {
438
			// Set customer ID as user meta.
439
			$this->update_wp_user_customer_id( $subscription->user_id, $customer_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...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

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

439
			$this->update_wp_user_customer_id( $subscription->user_id, /** @scrutinizer ignore-type */ $customer_id );
Loading history...
440
		}
441 28
	}
442 28
443 1
	/**
444
	 * Next payment delivery date.
445
	 *
446 27
	 * @param \DateTime $next_payment_delivery_date Next payment delivery date.
447
	 * @param Payment   $payment                    Payment.
448 27
	 *
449 27
	 * @return \DateTime
450
	 */
451
	public function next_payment_delivery_date( \DateTime $next_payment_delivery_date, Payment $payment ) {
452
		// Check gateway.
453
		$gateway_id = get_post_meta( $payment->get_config_id(), '_pronamic_gateway_id', true );
454
455
		if ( 'mollie' !== $gateway_id ) {
456
			return $next_payment_delivery_date;
457
		}
458
459
		// Check direct debit payment method.
460
		if ( ! PaymentMethods::is_direct_debit_method( $payment->get_method() ) ) {
461
			return $next_payment_delivery_date;
462
		}
463
464
		// Textual representation of the day of the week, Sunday through Saturday.
465
		$day_of_week = $next_payment_delivery_date->format( 'l' );
466
467
		switch ( $day_of_week ) {
468
			case 'Monday':
469
				$next_payment_delivery_date->sub( new DateInterval( 'P3D' ) );
470
				break;
471 11
472
			case 'Saturday':
473 11
				$next_payment_delivery_date->sub( new DateInterval( 'P2D' ) );
474
				break;
475 11
476 11
			case 'Sunday':
477
				$next_payment_delivery_date->sub( new DateInterval( 'P3D' ) );
478
				break;
479
480
			default:
481
				$next_payment_delivery_date->sub( new DateInterval( 'P1D' ) );
482
				break;
483
		}
484
485
		$next_payment_delivery_date->setTime( 0, 0, 0 );
486
487
		return $next_payment_delivery_date;
488
	}
489
}
490