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

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

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

458
			$this->update_wp_user_customer_id( /** @scrutinizer ignore-type */ $subscription->user_id, $customer_id );
Loading history...
459
		}
460
	}
461
}
462