Failed Conditions
Push — master ( 25af54...272cf6 )
by Reüel
09:37 queued 03:46
created

src/Gateway.php (2 issues)

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

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

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