Failed Conditions
Push — develop ( 17ba57...2374f0 )
by Reüel
03:28
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( $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 Pronamic_WP_Pay_Gateway::get_issuers()
81
	 */
82 2
	public function get_issuers() {
83 2
		$groups = array();
84
85 2
		$result = $this->client->get_issuers();
86
87 2
		if ( ! $result ) {
88 2
			$this->error = $this->client->get_error();
89
90 2
			return $groups;
91
		}
92
93
		$groups[] = array(
94
			'options' => $result,
95
		);
96
97
		return $groups;
98
	}
99
100
	/**
101
	 * Get available payment methods.
102
	 *
103
	 * @see Core_Gateway::get_available_payment_methods()
104
	 */
105 1
	public function get_available_payment_methods() {
106 1
		$payment_methods = array();
107
108
		// Set sequence types to get payment methods for.
109 1
		$sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST );
110
111 1
		$results = array();
112
113 1
		foreach ( $sequence_types as $sequence_type ) {
114
			// Get active payment methods for Mollie account.
115 1
			$result = $this->client->get_payment_methods( $sequence_type );
116
117 1
			if ( ! $result ) {
118 1
				$this->error = $this->client->get_error();
119
120 1
				break;
121
			}
122
123
			if ( Sequence::FIRST === $sequence_type ) {
124
				foreach ( $result as $method => $title ) {
125
					unset( $result[ $method ] );
126
127
					// Get WordPress payment method for direct debit method.
128
					$method         = Methods::transform_gateway_method( $method );
129
					$payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true );
130
131
					if ( $payment_method ) {
132
						$results[ $payment_method ] = $title;
133
					}
134
				}
135
			}
136
137
			if ( is_array( $result ) ) {
138
				$results = array_merge( $results, $result );
139
			}
140
		}
141
142
		// Transform to WordPress payment methods.
143 1
		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
			}
149
150
			if ( $payment_method ) {
151
				$payment_methods[] = $payment_method;
152
			}
153
		}
154
155 1
		$payment_methods = array_unique( $payment_methods );
156
157 1
		return $payment_methods;
158
	}
159
160
	/**
161
	 * Get supported payment methods
162
	 *
163
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
164
	 */
165 2
	public function get_supported_payment_methods() {
166
		return array(
167 2
			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
			PaymentMethods::SOFORT,
182
		);
183
	}
184
185
	/**
186
	 * Get webhook URL for Mollie.
187
	 *
188
	 * @return string
189
	 */
190 4
	public function get_webhook_url() {
191 4
		$url = home_url( '/' );
192
193 4
		$host = wp_parse_url( $url, PHP_URL_HOST );
194
195 4
		if ( is_array( $host ) ) {
196
			// Parsing failure.
197
			$host = '';
198
		}
199
200 4
		if ( 'localhost' === $host ) {
201
			// Mollie doesn't allow localhost.
202 1
			return null;
203 3
		} elseif ( '.dev' === substr( $host, -4 ) ) {
204
			// Mollie doesn't allow the .dev TLD.
205 1
			return null;
206 2
		} elseif ( '.local' === substr( $host, -6 ) ) {
207
			// Mollie doesn't allow the .local TLD.
208 1
			return null;
209 1
		} elseif ( '.test' === substr( $host, -5 ) ) {
210
			// Mollie doesn't allow the .test TLD.
211
			return null;
212
		}
213
214 1
		$url = add_query_arg( 'mollie_webhook', '', $url );
215
216 1
		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
		// Recurring payment method.
249
		$is_recurring_method = ( $payment->get_subscription() && PaymentMethods::is_recurring_method( $payment_method ) );
250
251
		if ( false === $is_recurring_method ) {
252
			// Always use 'direct debit mandate via iDEAL/Bancontact/Sofort' payment methods as recurring method.
253
			$is_recurring_method = PaymentMethods::is_direct_debit_method( $payment_method );
254
		}
255
256
		if ( $is_recurring_method ) {
257
			$request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST;
258
259
			if ( Sequence::FIRST === $request->sequence_type ) {
260
				$payment_method = PaymentMethods::get_first_payment_method( $payment_method );
261
			}
262
263
			if ( Sequence::RECURRING === $request->sequence_type ) {
264
				$payment->set_action_url( $payment->get_return_url() );
265
			}
266
		}
267
268
		// Leap of faith if the WordPress payment method could not transform to a Mollie method?
269
		$request->method = Methods::transform( $payment_method, $payment_method );
270
271
		// Issuer.
272
		if ( Methods::IDEAL === $request->method ) {
273
			$request->issuer = $payment->get_issuer();
274
		}
275
276
		// Due date.
277
		$due_date_days = \filter_var( $this->config->due_date_days, \FILTER_SANITIZE_NUMBER_INT );
278
279
		if ( Methods::BANKTRANSFER === $request->method && ! empty( $due_date_days ) ) {
280
			$date = new DateTime();
281
282
			$interval_spec = sprintf( 'P%dD', $due_date_days );
283
284
			$date->add( new \DateInterval( $interval_spec ) );
285
286
			$request->set_due_date( $date->format( 'Y-m-d' ) );
287
		}
288
289
		// Create payment.
290
		$result = $this->client->create_payment( $request );
291
292
		if ( ! $result ) {
293
			$this->error = $this->client->get_error();
294
295
			return;
296
		}
297
298
		// Set transaction ID.
299
		if ( isset( $result->id ) ) {
300
			$payment->set_transaction_id( $result->id );
301
		}
302
303
		// Set status.
304
		if ( isset( $result->status ) ) {
305
			$payment->set_status( Statuses::transform( $result->status ) );
306
		}
307
308
		// Set transfer reference.
309
		if ( isset( $result->details->transferReference ) ) {
310
			$payment->set_meta( 'mollie_transfer_reference', $result->details->transferReference );
311
		}
312
313
		// Set action URL.
314
		if ( isset( $result->_links->checkout->href ) ) {
315
			$payment->set_action_url( $result->_links->checkout->href );
316
		}
317
	}
318
319
	/**
320
	 * Update status of the specified payment
321
	 *
322
	 * @param Payment $payment Payment.
323
	 *
324
	 * @return void
325
	 */
326
	public function update_status( Payment $payment ) {
327
		$mollie_payment = $this->client->get_payment( $payment->get_transaction_id() );
328
329
		if ( ! $mollie_payment ) {
330
			$payment->set_status( PaymentStatus::FAILURE );
331
332
			$this->error = $this->client->get_error();
333
334
			return;
335
		}
336
337
		$payment->set_status( Statuses::transform( $mollie_payment->status ) );
338
339
		if ( isset( $mollie_payment->details ) ) {
340
			$details = $mollie_payment->details;
341
342
			/*
343
			 * @codingStandardsIgnoreStart
344
			 *
345
			 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
346
			 */
347
			if ( isset( $details->consumerName ) ) {
348
				$payment->set_consumer_name( $details->consumerName );
349
			}
350
351
			if ( isset( $details->cardHolder ) ) {
352
				$payment->set_consumer_name( $details->cardHolder );
353
			}
354
355
			if ( isset( $details->consumerAccount ) ) {
356
				$payment->set_consumer_iban( $details->consumerAccount );
357
			}
358
359
			if ( isset( $details->consumerBic ) ) {
360
				$payment->set_consumer_bic( $details->consumerBic );
361
			}
362
			// @codingStandardsIgnoreEnd
363
		}
364
	}
365
366
	/**
367
	 * Get Mollie customer ID for payment.
368
	 *
369
	 * @param Payment $payment Payment.
370
	 *
371
	 * @return bool|string
372
	 */
373 11
	public function get_customer_id_for_payment( Payment $payment ) {
374
		// Get WordPress user ID from payment customer.
375 11
		$user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() );
376
377
		// Get Mollie customer ID from user meta.
378 11
		$customer_id = $this->get_customer_id_by_wp_user_id( $user_id );
379
380 11
		$subscription = $payment->get_subscription();
381
382
		// Get customer ID from subscription meta.
383 11
		if ( $subscription ) {
384 11
			$subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' );
385
386
			// Try to get (legacy) customer ID from first payment.
387 11
			if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) {
388 7
				$first_payment = $subscription->get_first_payment();
389
390 7
				$subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' );
391
			}
392
393 11
			if ( ! empty( $subscription_customer_id ) ) {
394 5
				$customer_id = $subscription_customer_id;
395
			}
396
		}
397
398
		// Create new customer if the customer does not exist at Mollie.
399 11
		if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) {
400
			$customer_name = null;
401
402
			if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) {
403
				$customer_name = strval( $payment->get_customer()->get_name() );
404
			}
405
406
			$customer_id = $this->client->create_customer( $payment->get_email(), $customer_name );
407
408
			$this->update_wp_user_customer_id( $user_id, $customer_id );
409
		}
410
411
		// Store customer ID in subscription meta.
412 11
		if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) {
413
			$subscription->set_meta( 'mollie_customer_id', $customer_id );
414
		}
415
416
		// Copy customer ID from subscription to user meta.
417 11
		$this->copy_customer_id_to_wp_user( $payment );
418
419 11
		return $customer_id;
420
	}
421
422
	/**
423
	 * Get Mollie customer ID by the specified WordPress user ID.
424
	 *
425
	 * @param int $user_id WordPress user ID.
426
	 *
427
	 * @return string|bool
428
	 */
429 28
	public function get_customer_id_by_wp_user_id( $user_id ) {
430 28
		if ( empty( $user_id ) ) {
431 11
			return false;
432
		}
433
434 17
		return get_user_meta( $user_id, $this->meta_key_customer_id, true );
435
	}
436
437
	/**
438
	 * Update Mollie customer ID meta for WordPress user.
439
	 *
440
	 * @param int    $user_id     WordPress user ID.
441
	 * @param string $customer_id Mollie Customer ID.
442
	 *
443
	 * @return bool
444
	 */
445
	private function update_wp_user_customer_id( $user_id, $customer_id ) {
446
		if ( empty( $user_id ) || is_bool( $user_id ) ) {
447
			return false;
448
		}
449
450
		if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) {
451
			return false;
452
		}
453
454
		update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id );
455
	}
456
457
	/**
458
	 * Copy Mollie customer ID from subscription meta to WordPress user meta.
459
	 *
460
	 * @param Payment $payment Payment.
461
	 *
462
	 * @return void
463
	 */
464 28
	public function copy_customer_id_to_wp_user( Payment $payment ) {
465 28
		if ( $this->config->id !== $payment->config_id ) {
466 1
			return;
467
		}
468
469 27
		$subscription = $payment->get_subscription();
470
471 27
		if ( ! $subscription || empty( $subscription->user_id ) ) {
472 27
			return;
473
		}
474
475
		// Get customer ID from subscription meta.
476
		$customer_id = $subscription->get_meta( 'mollie_customer_id' );
477
478
		$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

478
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
479
480
		if ( empty( $user_customer_id ) ) {
481
			// Set customer ID as user meta.
482
			$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

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

482
			$this->update_wp_user_customer_id( $subscription->user_id, /** @scrutinizer ignore-type */ $customer_id );
Loading history...
483
		}
484
	}
485
}
486