Failed Conditions
Push — feature/webhook-status ( e55647 )
by Reüel
05:56
created

Gateway::get_supported_features()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

458
		$user_customer_id = $this->get_customer_id_by_wp_user_id( /** @scrutinizer ignore-type */ $subscription->user_id );
Loading history...
459
460
		if ( empty( $user_customer_id ) ) {
461
			// Set customer ID as user meta.
462
			$this->update_wp_user_customer_id( $subscription->user_id, $customer_id );
0 ignored issues
show
Bug introduced by
$subscription->user_id of type string is incompatible with the type integer expected by parameter $user_id of Pronamic\WordPress\Pay\G...e_wp_user_customer_id(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

462
			$this->update_wp_user_customer_id( /** @scrutinizer ignore-type */ $subscription->user_id, $customer_id );
Loading history...
463
		}
464
	}
465
}
466