Failed Conditions
Push — develop ( 2374f0...14a1b4 )
by Reüel
03:47
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
		try {
278
			$due_date = new DateTime( sprintf( '+%s days', $this->config->due_date_days ) );
279
		} catch ( \Exception $e ) {
280
			$due_date = null;
281
		}
282
283
		$request->set_due_date( $due_date );
284
285
		// Create payment.
286
		$result = $this->client->create_payment( $request );
287
288
		if ( ! $result ) {
289
			$this->error = $this->client->get_error();
290
291
			return;
292
		}
293
294
		// Set transaction ID.
295
		if ( isset( $result->id ) ) {
296
			$payment->set_transaction_id( $result->id );
297
		}
298
299
		// Set status.
300
		if ( isset( $result->status ) ) {
301
			$payment->set_status( Statuses::transform( $result->status ) );
302
		}
303
304
		// Set transfer reference.
305
		if ( isset( $result->details->transferReference ) ) {
306
			$payment->set_meta( 'mollie_transfer_reference', $result->details->transferReference );
307
		}
308
309
		// Set action URL.
310
		if ( isset( $result->_links->checkout->href ) ) {
311
			$payment->set_action_url( $result->_links->checkout->href );
312
		}
313
	}
314
315
	/**
316
	 * Update status of the specified payment
317
	 *
318
	 * @param Payment $payment Payment.
319
	 *
320
	 * @return void
321
	 */
322
	public function update_status( Payment $payment ) {
323
		$mollie_payment = $this->client->get_payment( $payment->get_transaction_id() );
324
325
		if ( ! $mollie_payment ) {
326
			$payment->set_status( PaymentStatus::FAILURE );
327
328
			$this->error = $this->client->get_error();
329
330
			return;
331
		}
332
333
		$payment->set_status( Statuses::transform( $mollie_payment->status ) );
334
335
		if ( isset( $mollie_payment->details ) ) {
336
			$details = $mollie_payment->details;
337
338
			/*
339
			 * @codingStandardsIgnoreStart
340
			 *
341
			 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
342
			 */
343
			if ( isset( $details->consumerName ) ) {
344
				$payment->set_consumer_name( $details->consumerName );
345
			}
346
347
			if ( isset( $details->cardHolder ) ) {
348
				$payment->set_consumer_name( $details->cardHolder );
349
			}
350
351
			if ( isset( $details->consumerAccount ) ) {
352
				$payment->set_consumer_iban( $details->consumerAccount );
353
			}
354
355
			if ( isset( $details->consumerBic ) ) {
356
				$payment->set_consumer_bic( $details->consumerBic );
357
			}
358
			// @codingStandardsIgnoreEnd
359
		}
360
	}
361
362
	/**
363
	 * Get Mollie customer ID for payment.
364
	 *
365
	 * @param Payment $payment Payment.
366
	 *
367
	 * @return bool|string
368
	 */
369 11
	public function get_customer_id_for_payment( Payment $payment ) {
370
		// Get WordPress user ID from payment customer.
371 11
		$user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() );
372
373
		// Get Mollie customer ID from user meta.
374 11
		$customer_id = $this->get_customer_id_by_wp_user_id( $user_id );
375
376 11
		$subscription = $payment->get_subscription();
377
378
		// Get customer ID from subscription meta.
379 11
		if ( $subscription ) {
380 11
			$subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' );
381
382
			// Try to get (legacy) customer ID from first payment.
383 11
			if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) {
384 7
				$first_payment = $subscription->get_first_payment();
385
386 7
				$subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' );
387
			}
388
389 11
			if ( ! empty( $subscription_customer_id ) ) {
390 5
				$customer_id = $subscription_customer_id;
391
			}
392
		}
393
394
		// Create new customer if the customer does not exist at Mollie.
395 11
		if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) {
396
			$customer_name = null;
397
398
			if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) {
399
				$customer_name = strval( $payment->get_customer()->get_name() );
400
			}
401
402
			$customer_id = $this->client->create_customer( $payment->get_email(), $customer_name );
403
404
			$this->update_wp_user_customer_id( $user_id, $customer_id );
405
		}
406
407
		// Store customer ID in subscription meta.
408 11
		if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) {
409
			$subscription->set_meta( 'mollie_customer_id', $customer_id );
410
		}
411
412
		// Copy customer ID from subscription to user meta.
413 11
		$this->copy_customer_id_to_wp_user( $payment );
414
415 11
		return $customer_id;
416
	}
417
418
	/**
419
	 * Get Mollie customer ID by the specified WordPress user ID.
420
	 *
421
	 * @param int $user_id WordPress user ID.
422
	 *
423
	 * @return string|bool
424
	 */
425 28
	public function get_customer_id_by_wp_user_id( $user_id ) {
426 28
		if ( empty( $user_id ) ) {
427 11
			return false;
428
		}
429
430 17
		return get_user_meta( $user_id, $this->meta_key_customer_id, true );
431
	}
432
433
	/**
434
	 * Update Mollie customer ID meta for WordPress user.
435
	 *
436
	 * @param int    $user_id     WordPress user ID.
437
	 * @param string $customer_id Mollie Customer ID.
438
	 *
439
	 * @return bool
440
	 */
441
	private function update_wp_user_customer_id( $user_id, $customer_id ) {
442
		if ( empty( $user_id ) || is_bool( $user_id ) ) {
443
			return false;
444
		}
445
446
		if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) {
447
			return false;
448
		}
449
450
		update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id );
451
	}
452
453
	/**
454
	 * Copy Mollie customer ID from subscription meta to WordPress user meta.
455
	 *
456
	 * @param Payment $payment Payment.
457
	 *
458
	 * @return void
459
	 */
460 28
	public function copy_customer_id_to_wp_user( Payment $payment ) {
461 28
		if ( $this->config->id !== $payment->config_id ) {
462 1
			return;
463
		}
464
465 27
		$subscription = $payment->get_subscription();
466
467 27
		if ( ! $subscription || empty( $subscription->user_id ) ) {
468 27
			return;
469
		}
470
471
		// Get customer ID from subscription meta.
472
		$customer_id = $subscription->get_meta( 'mollie_customer_id' );
473
474
		$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

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

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

478
			$this->update_wp_user_customer_id( $subscription->user_id, /** @scrutinizer ignore-type */ $customer_id );
Loading history...
479
		}
480
	}
481
}
482