Failed Conditions
Push — develop ( 3861b4...1ec521 )
by Reüel
07:46
created

src/Gateway.php (4 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods;
7
use Pronamic\WordPress\Pay\Core\Recurring as Core_Recurring;
8
use Pronamic\WordPress\Pay\Core\Statuses as Core_Statuses;
9
use Pronamic\WordPress\Pay\Payments\Payment;
10
11
/**
12
 * Title: Mollie
13
 * Description:
14
 * Copyright: 2005-2019 Pronamic
15
 * Company: Pronamic
16
 *
17
 * @author  Remco Tolsma
18
 * @version 2.1.0
19
 * @since   1.1.0
20
 */
21
class Gateway extends Core_Gateway {
22
	/**
23
	 * Slug of this gateway
24
	 *
25
	 * @var string
26
	 */
27
	const SLUG = 'mollie';
28
29
	/**
30
	 * Client.
31
	 *
32
	 * @var Client
33
	 */
34
	protected $client;
35
36
	/**
37
	 * Meta key for customer ID.
38
	 *
39
	 * @var string
40
	 */
41
	private $meta_key_customer_id = '_pronamic_pay_mollie_customer_id';
42
43
	/**
44
	 * Constructs and initializes an Mollie gateway
45
	 *
46
	 * @param Config $config Config.
47
	 */
48 40
	public function __construct( Config $config ) {
49 40
		parent::__construct( $config );
50
51 40
		$this->supports = array(
52
			'payment_status_request',
53
			'recurring_direct_debit',
54
			'recurring_credit_card',
55
			'recurring',
56
		);
57
58 40
		$this->set_method( self::METHOD_HTTP_REDIRECT );
59 40
		$this->set_slug( self::SLUG );
60
61 40
		$this->client = new Client( $config->api_key );
62 40
		$this->client->set_mode( $config->mode );
63
64 40
		if ( self::MODE_TEST === $config->mode ) {
65 40
			$this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test';
66
		}
67
68
		// Actions.
69 40
		add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 );
70 40
	}
71
72
	/**
73
	 * Get issuers
74
	 *
75
	 * @see Pronamic_WP_Pay_Gateway::get_issuers()
76
	 */
77 3
	public function get_issuers() {
78 3
		$groups = array();
79
80 3
		$result = $this->client->get_issuers();
81
82 3
		if ( ! $result ) {
83 3
			$this->error = $this->client->get_error();
84
85 3
			return $groups;
86
		}
87
88
		$groups[] = array(
89
			'options' => $result,
90
		);
91
92
		return $groups;
93
	}
94
95
	/**
96
	 * Get available payment methods.
97
	 *
98
	 * @see Core_Gateway::get_available_payment_methods()
99
	 */
100 2
	public function get_available_payment_methods() {
101 2
		$payment_methods = array();
102
103
		// Set sequence types to get payment methods for.
104 2
		$sequence_types = array( Sequence::ONE_OFF, Sequence::RECURRING, Sequence::FIRST );
105
106 2
		$results = array();
107
108 2
		foreach ( $sequence_types as $sequence_type ) {
109
			// Get active payment methods for Mollie account.
110 2
			$result = $this->client->get_payment_methods( $sequence_type );
111
112 2
			if ( ! $result ) {
113 2
				$this->error = $this->client->get_error();
114
115 2
				break;
116
			}
117
118
			if ( Sequence::FIRST === $sequence_type ) {
119
				foreach ( $result as $method => $title ) {
120
					unset( $result[ $method ] );
121
122
					// Get WordPress payment method for direct debit method.
123
					$method         = Methods::transform_gateway_method( $method );
124
					$payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true );
125
126
					if ( $payment_method ) {
127
						$results[ $payment_method ] = $title;
128
					}
129
				}
130
			}
131
132
			$results = array_merge( $results, $result );
133
		}
134
135
		// Transform to WordPress payment methods.
136 2
		foreach ( $results as $method => $title ) {
137
			if ( PaymentMethods::is_recurring_method( $method ) ) {
138
				$payment_method = $method;
139
			} else {
140
				$payment_method = Methods::transform_gateway_method( $method );
141
			}
142
143
			if ( $payment_method ) {
144
				$payment_methods[] = $payment_method;
145
			}
146
		}
147
148 2
		$payment_methods = array_unique( $payment_methods );
149
150 2
		return $payment_methods;
151
	}
152
153
	/**
154
	 * Get supported payment methods
155
	 *
156
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
157
	 */
158 2
	public function get_supported_payment_methods() {
159
		return array(
160 2
			PaymentMethods::BANCONTACT,
161
			PaymentMethods::BANK_TRANSFER,
162
			PaymentMethods::BELFIUS,
163
			PaymentMethods::BITCOIN,
164
			PaymentMethods::CREDIT_CARD,
165
			PaymentMethods::DIRECT_DEBIT,
166
			PaymentMethods::DIRECT_DEBIT_BANCONTACT,
167
			PaymentMethods::DIRECT_DEBIT_IDEAL,
168
			PaymentMethods::DIRECT_DEBIT_SOFORT,
169
			PaymentMethods::IDEAL,
170
			PaymentMethods::KBC,
171
			PaymentMethods::PAYPAL,
172
			PaymentMethods::SOFORT,
173
		);
174
	}
175
176
	/**
177
	 * Get webhook URL for Mollie.
178
	 *
179
	 * @return string
180
	 */
181 4
	public function get_webhook_url() {
182 4
		$url = home_url( '/' );
183
184 4
		$host = wp_parse_url( $url, PHP_URL_HOST );
185
186 4
		if ( is_array( $host ) ) {
187
			// Parsing failure.
188
			$host = '';
189
		}
190
191 4
		if ( 'localhost' === $host ) {
192
			// Mollie doesn't allow localhost.
193 1
			return null;
194 3
		} elseif ( '.dev' === substr( $host, -4 ) ) {
195
			// Mollie doesn't allow the .dev TLD.
196 1
			return null;
197 2
		} elseif ( '.local' === substr( $host, -6 ) ) {
198
			// Mollie doesn't allow the .local TLD.
199 1
			return null;
200 1
		} elseif ( '.test' === substr( $host, -5 ) ) {
201
			// Mollie doesn't allow the .test TLD.
202
			return null;
203
		}
204
205 1
		$url = add_query_arg( 'mollie_webhook', '', $url );
206
207 1
		return $url;
208
	}
209
210
	/**
211
	 * Start
212
	 *
213
	 * @see Pronamic_WP_Pay_Gateway::start()
214
	 *
215
	 * @param Payment $payment Payment.
216
	 */
217
	public function start( Payment $payment ) {
218
		$request               = new PaymentRequest();
219
		$request->amount       = AmountTransformer::transform( $payment->get_total_amount() );
0 ignored issues
show
Documentation Bug introduced by
It seems like Pronamic\WordPress\Pay\G...nt->get_total_amount()) of type Pronamic\WordPress\Money\Money is incompatible with the declared type Pronamic\WordPress\Pay\Gateways\Mollie\Amount of property $amount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
220
		$request->description  = $payment->get_description();
221
		$request->redirect_url = $payment->get_return_url();
222
		$request->webhook_url  = $this->get_webhook_url();
223
224
		// Locale.
225
		if ( null !== $payment->get_customer() ) {
226
			$request->locale = LocaleHelper::transform( $payment->get_customer()->get_locale() );
227
		}
228
229
		// Customer ID.
230
		$customer_id = $this->get_customer_id_for_payment( $payment );
231
232
		if ( is_string( $customer_id ) && ! empty( $customer_id ) ) {
233
			$request->customer_id = $customer_id;
234
		}
235
236
		// Payment method.
237
		$payment_method = $payment->get_method();
238
239
		// Subscription.
240
		$subscription = $payment->get_subscription();
241
242
		if ( $subscription && PaymentMethods::is_recurring_method( $payment_method ) ) {
243
			$request->sequence_type = $payment->get_recurring() ? Sequence::RECURRING : Sequence::FIRST;
244
245
			if ( Sequence::FIRST === $request->sequence_type ) {
246
				$payment_method = PaymentMethods::get_first_payment_method( $payment_method );
247
			}
248
249
			if ( Sequence::RECURRING === $request->sequence_type ) {
250
				$payment->set_action_url( $payment->get_return_url() );
251
			}
252
		}
253
254
		// Leap of faith if the WordPress payment method could not transform to a Mollie method?
255
		$request->method = Methods::transform( $payment_method, $payment_method );
256
257
		// Issuer.
258
		if ( Methods::IDEAL === $request->method ) {
259
			$request->issuer = $payment->get_issuer();
260
		}
261
262
		// Create payment.
263
		$result = $this->client->create_payment( $request );
264
265
		if ( ! $result ) {
266
			$this->error = $this->client->get_error();
267
268
			return;
269
		}
270
271
		// Set transaction ID.
272
		if ( isset( $result->id ) ) {
273
			$payment->set_transaction_id( $result->id );
274
		}
275
276
		// Set status.
277
		if ( isset( $result->status ) ) {
278
			$payment->set_status( Statuses::transform( $result->status ) );
279
		}
280
281
		// Set action URL.
282
		if ( isset( $result->_links->checkout->href ) ) {
283
			$payment->set_action_url( $result->_links->checkout->href );
284
		}
285
	}
286
287
	/**
288
	 * Update status of the specified payment
289
	 *
290
	 * @param Payment $payment Payment.
291
	 *
292
	 * @return void
293
	 */
294
	public function update_status( Payment $payment ) {
295
		$mollie_payment = $this->client->get_payment( $payment->get_transaction_id() );
296
297
		if ( ! $mollie_payment ) {
298
			$payment->set_status( Core_Statuses::FAILURE );
299
300
			$this->error = $this->client->get_error();
301
302
			return;
303
		}
304
305
		$payment->set_status( Statuses::transform( $mollie_payment->status ) );
306
307
		if ( isset( $mollie_payment->details ) ) {
308
			$details = $mollie_payment->details;
309
310
			/*
311
			 * @codingStandardsIgnoreStart
312
			 *
313
			 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
314
			 */
315
			if ( isset( $details->consumerName ) ) {
316
				$payment->set_consumer_name( $details->consumerName );
317
			}
318
319
			if ( isset( $details->cardHolder ) ) {
320
				$payment->set_consumer_name( $details->cardHolder );
321
			}
322
323
			if ( isset( $details->consumerAccount ) ) {
324
				$payment->set_consumer_iban( $details->consumerAccount );
325
			}
326
327
			if ( isset( $details->consumerBic ) ) {
328
				$payment->set_consumer_bic( $details->consumerBic );
329
			}
330
			// @codingStandardsIgnoreEnd
331
		}
332
	}
333
334
	/**
335
	 * Get Mollie customer ID for payment.
336
	 *
337
	 * @param Payment $payment Payment.
338
	 *
339
	 * @return bool|string
340
	 */
341 11
	public function get_customer_id_for_payment( Payment $payment ) {
342
		// Get WordPress user ID from payment customer.
343 11
		$user_id = ( null === $payment->get_customer() ? null : $payment->get_customer()->get_user_id() );
344
345
		// Get Mollie customer ID from user meta.
346 11
		$customer_id = $this->get_customer_id_by_wp_user_id( $user_id );
347
348 11
		$subscription = $payment->get_subscription();
349
350
		// Get customer ID from subscription meta.
351 11
		if ( $subscription ) {
352 11
			$subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' );
353
354
			// Try to get (legacy) customer ID from first payment.
355 11
			if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) {
356 7
				$first_payment = $subscription->get_first_payment();
357
358 7
				$subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' );
359
			}
360
361 11
			if ( ! empty( $subscription_customer_id ) ) {
362 5
				$customer_id = $subscription_customer_id;
363
			}
364
		}
365
366
		// Create new customer if the customer does not exist at Mollie.
367 11
		if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) {
368
			$customer_name = null;
369
370
			if ( null !== $payment->get_customer() && null !== $payment->get_customer()->get_name() ) {
371
				$customer_name = strval( $payment->get_customer()->get_name() );
372
			}
373
374
			$customer_id = $this->client->create_customer( $payment->get_email(), $customer_name );
375
376
			$this->update_wp_user_customer_id( $user_id, $customer_id );
377
		}
378
379
		// Store customer ID in subscription meta.
380 11
		if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) {
381
			$subscription->set_meta( 'mollie_customer_id', $customer_id );
382
		}
383
384
		// Copy customer ID from subscription to user meta.
385 11
		$this->copy_customer_id_to_wp_user( $payment );
386
387 11
		return $customer_id;
388
	}
389
390
	/**
391
	 * Get Mollie customer ID by the specified WordPress user ID.
392
	 *
393
	 * @param int $user_id WordPress user ID.
394
	 *
395
	 * @return string|bool
396
	 */
397 28
	public function get_customer_id_by_wp_user_id( $user_id ) {
398 28
		if ( empty( $user_id ) ) {
399 11
			return false;
400
		}
401
402 17
		return get_user_meta( $user_id, $this->meta_key_customer_id, true );
403
	}
404
405
	/**
406
	 * Update Mollie customer ID meta for WordPress user.
407
	 *
408
	 * @param int    $user_id     WordPress user ID.
409
	 * @param string $customer_id Mollie Customer ID.
410
	 *
411
	 * @return bool
412
	 */
413
	private function update_wp_user_customer_id( $user_id, $customer_id ) {
414
		if ( empty( $user_id ) || is_bool( $user_id ) ) {
415
			return false;
416
		}
417
418
		if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) {
419
			return false;
420
		}
421
422
		update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id );
423
	}
424
425
	/**
426
	 * Copy Mollie customer ID from subscription meta to WordPress user meta.
427
	 *
428
	 * @param Payment $payment Payment.
429
	 *
430
	 * @return void
431
	 */
432 28
	public function copy_customer_id_to_wp_user( Payment $payment ) {
433 28
		if ( $this->config->id !== $payment->config_id ) {
434 1
			return;
435
		}
436
437 27
		$subscription = $payment->get_subscription();
438
439 27
		if ( ! $subscription || empty( $subscription->user_id ) ) {
440 27
			return;
441
		}
442
443
		// Get customer ID from subscription meta.
444
		$customer_id = $subscription->get_meta( 'mollie_customer_id' );
445
446
		$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

446
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
447
448
		if ( empty( $user_customer_id ) ) {
449
			// Set customer ID as user meta.
450
			$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

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

450
			$this->update_wp_user_customer_id( /** @scrutinizer ignore-type */ $subscription->user_id, $customer_id );
Loading history...
451
		}
452
	}
453
}
454