Completed
Push — develop ( 5d58bc...871d97 )
by Reüel
05:02
created
src/Gateway.php 1 patch
Spacing   +92 added lines, -92 removed lines patch added patch discarded remove patch
@@ -38,8 +38,8 @@  discard block
 block discarded – undo
38 38
 	 *
39 39
 	 * @param Config $config
40 40
 	 */
41
-	public function __construct( Config $config ) {
42
-		parent::__construct( $config );
41
+	public function __construct(Config $config) {
42
+		parent::__construct($config);
43 43
 
44 44
 		$this->supports = array(
45 45
 			'payment_status_request',
@@ -48,18 +48,18 @@  discard block
 block discarded – undo
48 48
 			'recurring',
49 49
 		);
50 50
 
51
-		$this->set_method( Core_Gateway::METHOD_HTTP_REDIRECT );
52
-		$this->set_slug( self::SLUG );
51
+		$this->set_method(Core_Gateway::METHOD_HTTP_REDIRECT);
52
+		$this->set_slug(self::SLUG);
53 53
 
54
-		$this->client = new Client( $config->api_key );
55
-		$this->client->set_mode( $config->mode );
54
+		$this->client = new Client($config->api_key);
55
+		$this->client->set_mode($config->mode);
56 56
 
57
-		if ( 'test' === $config->mode ) {
57
+		if ('test' === $config->mode) {
58 58
 			$this->meta_key_customer_id = '_pronamic_pay_mollie_customer_id_test';
59 59
 		}
60 60
 
61 61
 		// Actions.
62
-		add_action( 'pronamic_payment_status_update', array( $this, 'copy_customer_id_to_wp_user' ), 99, 1 );
62
+		add_action('pronamic_payment_status_update', array($this, 'copy_customer_id_to_wp_user'), 99, 1);
63 63
 	}
64 64
 
65 65
 	/**
@@ -72,7 +72,7 @@  discard block
 block discarded – undo
72 72
 
73 73
 		$result = $this->client->get_issuers();
74 74
 
75
-		if ( ! $result ) {
75
+		if ( ! $result) {
76 76
 			$this->error = $this->client->get_error();
77 77
 
78 78
 			return $groups;
@@ -94,51 +94,51 @@  discard block
 block discarded – undo
94 94
 		$payment_methods = array();
95 95
 
96 96
 		// Set recurring types to get payment methods for.
97
-		$recurring_types = array( null, Recurring::RECURRING, Recurring::FIRST );
97
+		$recurring_types = array(null, Recurring::RECURRING, Recurring::FIRST);
98 98
 
99 99
 		$results = array();
100 100
 
101
-		foreach ( $recurring_types as $recurring_type ) {
101
+		foreach ($recurring_types as $recurring_type) {
102 102
 			// Get active payment methods for Mollie account.
103
-			$result = $this->client->get_payment_methods( $recurring_type );
103
+			$result = $this->client->get_payment_methods($recurring_type);
104 104
 
105
-			if ( ! $result ) {
105
+			if ( ! $result) {
106 106
 				$this->error = $this->client->get_error();
107 107
 
108 108
 				break;
109 109
 			}
110 110
 
111
-			if ( Recurring::FIRST === $recurring_type ) {
112
-				foreach ( $result as $method => $title ) {
113
-					unset( $result[ $method ] );
111
+			if (Recurring::FIRST === $recurring_type) {
112
+				foreach ($result as $method => $title) {
113
+					unset($result[$method]);
114 114
 
115 115
 					// Get WordPress payment method for direct debit method.
116
-					$method         = Methods::transform_gateway_method( $method );
117
-					$payment_method = array_search( $method, PaymentMethods::get_recurring_methods(), true );
116
+					$method         = Methods::transform_gateway_method($method);
117
+					$payment_method = array_search($method, PaymentMethods::get_recurring_methods(), true);
118 118
 
119
-					if ( $payment_method ) {
120
-						$results[ $payment_method ] = $title;
119
+					if ($payment_method) {
120
+						$results[$payment_method] = $title;
121 121
 					}
122 122
 				}
123 123
 			}
124 124
 
125
-			$results = array_merge( $results, $result );
125
+			$results = array_merge($results, $result);
126 126
 		}
127 127
 
128 128
 		// Transform to WordPress payment methods.
129
-		foreach ( $results as $method => $title ) {
130
-			if ( PaymentMethods::is_recurring_method( $method ) ) {
129
+		foreach ($results as $method => $title) {
130
+			if (PaymentMethods::is_recurring_method($method)) {
131 131
 				$payment_method = $method;
132 132
 			} else {
133
-				$payment_method = Methods::transform_gateway_method( $method );
133
+				$payment_method = Methods::transform_gateway_method($method);
134 134
 			}
135 135
 
136
-			if ( $payment_method ) {
136
+			if ($payment_method) {
137 137
 				$payment_methods[] = $payment_method;
138 138
 			}
139 139
 		}
140 140
 
141
-		$payment_methods = array_unique( $payment_methods );
141
+		$payment_methods = array_unique($payment_methods);
142 142
 
143 143
 		return $payment_methods;
144 144
 	}
@@ -172,22 +172,22 @@  discard block
 block discarded – undo
172 172
 	 * @return string
173 173
 	 */
174 174
 	public function get_webhook_url() {
175
-		$url = home_url( '/' );
175
+		$url = home_url('/');
176 176
 
177
-		$host = wp_parse_url( $url, PHP_URL_HOST );
177
+		$host = wp_parse_url($url, PHP_URL_HOST);
178 178
 
179
-		if ( 'localhost' === $host ) {
179
+		if ('localhost' === $host) {
180 180
 			// Mollie doesn't allow localhost.
181 181
 			return null;
182
-		} elseif ( '.dev' === substr( $host, -4 ) ) {
182
+		} elseif ('.dev' === substr($host, -4)) {
183 183
 			// Mollie doesn't allow the .dev TLD.
184 184
 			return null;
185
-		} elseif ( '.local' === substr( $host, -6 ) ) {
185
+		} elseif ('.local' === substr($host, -6)) {
186 186
 			// Mollie doesn't allow the .local TLD.
187 187
 			return null;
188 188
 		}
189 189
 
190
-		$url = add_query_arg( 'mollie_webhook', '', $url );
190
+		$url = add_query_arg('mollie_webhook', '', $url);
191 191
 
192 192
 		return $url;
193 193
 	}
@@ -197,19 +197,19 @@  discard block
 block discarded – undo
197 197
 	 *
198 198
 	 * @see Pronamic_WP_Pay_Gateway::start()
199 199
 	 */
200
-	public function start( Payment $payment ) {
200
+	public function start(Payment $payment) {
201 201
 		$request = new PaymentRequest();
202 202
 
203 203
 		$request->amount       = $payment->get_amount()->get_amount();
204 204
 		$request->description  = $payment->get_description();
205 205
 		$request->redirect_url = $payment->get_return_url();
206 206
 		$request->webhook_url  = $this->get_webhook_url();
207
-		$request->locale       = LocaleHelper::transform( $payment->get_language() );
207
+		$request->locale       = LocaleHelper::transform($payment->get_language());
208 208
 
209 209
 		// Customer ID.
210
-		$customer_id = $this->get_customer_id_for_payment( $payment );
210
+		$customer_id = $this->get_customer_id_for_payment($payment);
211 211
 
212
-		if ( ! empty( $customer_id ) ) {
212
+		if ( ! empty($customer_id)) {
213 213
 			$request->customer_id = $customer_id;
214 214
 		}
215 215
 
@@ -219,49 +219,49 @@  discard block
 block discarded – undo
219 219
 		// Subscription.
220 220
 		$subscription = $payment->get_subscription();
221 221
 
222
-		if ( $subscription && PaymentMethods::is_recurring_method( $payment_method ) ) {
222
+		if ($subscription && PaymentMethods::is_recurring_method($payment_method)) {
223 223
 			$request->recurring_type = $payment->get_recurring() ? Recurring::RECURRING : Recurring::FIRST;
224 224
 
225
-			if ( Recurring::FIRST === $request->recurring_type ) {
226
-				$payment_method = PaymentMethods::get_first_payment_method( $payment_method );
225
+			if (Recurring::FIRST === $request->recurring_type) {
226
+				$payment_method = PaymentMethods::get_first_payment_method($payment_method);
227 227
 			}
228 228
 
229
-			if ( Recurring::RECURRING === $request->recurring_type ) {
230
-				$payment->set_action_url( $payment->get_return_url() );
229
+			if (Recurring::RECURRING === $request->recurring_type) {
230
+				$payment->set_action_url($payment->get_return_url());
231 231
 			}
232 232
 		}
233 233
 
234 234
 		// Leap of faith if the WordPress payment method could not transform to a Mollie method?
235
-		$request->method = Methods::transform( $payment_method, $payment_method );
235
+		$request->method = Methods::transform($payment_method, $payment_method);
236 236
 
237 237
 		// Issuer.
238
-		if ( Methods::IDEAL === $request->method ) {
238
+		if (Methods::IDEAL === $request->method) {
239 239
 			// If payment method is iDEAL we set the user chosen issuer ID.
240 240
 			$request->issuer = $payment->get_issuer();
241 241
 		}
242 242
 
243 243
 		// Create payment.
244
-		$result = $this->client->create_payment( $request );
244
+		$result = $this->client->create_payment($request);
245 245
 
246
-		if ( ! $result ) {
246
+		if ( ! $result) {
247 247
 			$this->error = $this->client->get_error();
248 248
 
249 249
 			return false;
250 250
 		}
251 251
 
252 252
 		// Set transaction ID.
253
-		if ( isset( $result->id ) ) {
254
-			$payment->set_transaction_id( $result->id );
253
+		if (isset($result->id)) {
254
+			$payment->set_transaction_id($result->id);
255 255
 		}
256 256
 
257 257
 		// Set status
258
-		if ( isset( $result->status ) ) {
259
-			$payment->set_status( Statuses::transform( $result->status ) );
258
+		if (isset($result->status)) {
259
+			$payment->set_status(Statuses::transform($result->status));
260 260
 		}
261 261
 
262 262
 		// Set action URL.
263
-		if ( isset( $result->links, $result->links->paymentUrl ) ) {
264
-			$payment->set_action_url( $result->links->paymentUrl );
263
+		if (isset($result->links, $result->links->paymentUrl)) {
264
+			$payment->set_action_url($result->links->paymentUrl);
265 265
 		}
266 266
 	}
267 267
 
@@ -270,36 +270,36 @@  discard block
 block discarded – undo
270 270
 	 *
271 271
 	 * @param Payment $payment
272 272
 	 */
273
-	public function update_status( Payment $payment ) {
274
-		$mollie_payment = $this->client->get_payment( $payment->get_transaction_id() );
273
+	public function update_status(Payment $payment) {
274
+		$mollie_payment = $this->client->get_payment($payment->get_transaction_id());
275 275
 
276
-		if ( ! $mollie_payment ) {
277
-			$payment->set_status( Core_Statuses::FAILURE );
276
+		if ( ! $mollie_payment) {
277
+			$payment->set_status(Core_Statuses::FAILURE);
278 278
 
279 279
 			$this->error = $this->client->get_error();
280 280
 
281 281
 			return;
282 282
 		}
283 283
 
284
-		$payment->set_status( Statuses::transform( $mollie_payment->status ) );
284
+		$payment->set_status(Statuses::transform($mollie_payment->status));
285 285
 
286
-		if ( isset( $mollie_payment->details ) ) {
286
+		if (isset($mollie_payment->details)) {
287 287
 			$details = $mollie_payment->details;
288 288
 
289
-			if ( isset( $details->consumerName ) ) {
290
-				$payment->set_consumer_name( $details->consumerName );
289
+			if (isset($details->consumerName)) {
290
+				$payment->set_consumer_name($details->consumerName);
291 291
 			}
292 292
 
293
-			if ( isset( $details->cardHolder ) ) {
294
-				$payment->set_consumer_name( $details->cardHolder );
293
+			if (isset($details->cardHolder)) {
294
+				$payment->set_consumer_name($details->cardHolder);
295 295
 			}
296 296
 
297
-			if ( isset( $details->consumerAccount ) ) {
298
-				$payment->set_consumer_iban( $details->consumerAccount );
297
+			if (isset($details->consumerAccount)) {
298
+				$payment->set_consumer_iban($details->consumerAccount);
299 299
 			}
300 300
 
301
-			if ( isset( $details->consumerBic ) ) {
302
-				$payment->set_consumer_bic( $details->consumerBic );
301
+			if (isset($details->consumerBic)) {
302
+				$payment->set_consumer_bic($details->consumerBic);
303 303
 			}
304 304
 		}
305 305
 	}
@@ -311,42 +311,42 @@  discard block
 block discarded – undo
311 311
 	 *
312 312
 	 * @return bool|string
313 313
 	 */
314
-	public function get_customer_id_for_payment( Payment $payment ) {
314
+	public function get_customer_id_for_payment(Payment $payment) {
315 315
 		// Get Mollie customer ID from user meta.
316
-		$customer_id = $this->get_customer_id_by_wp_user_id( $payment->user_id );
316
+		$customer_id = $this->get_customer_id_by_wp_user_id($payment->user_id);
317 317
 
318 318
 		$subscription = $payment->get_subscription();
319 319
 
320 320
 		// Get customer ID from subscription meta.
321
-		if ( $subscription ) {
322
-			$subscription_customer_id = $subscription->get_meta( 'mollie_customer_id' );
321
+		if ($subscription) {
322
+			$subscription_customer_id = $subscription->get_meta('mollie_customer_id');
323 323
 
324 324
 			// Try to get (legacy) customer ID from first payment.
325
-			if ( empty( $subscription_customer_id ) && $subscription->get_first_payment() ) {
325
+			if (empty($subscription_customer_id) && $subscription->get_first_payment()) {
326 326
 				$first_payment = $subscription->get_first_payment();
327 327
 
328
-				$subscription_customer_id = $first_payment->get_meta( 'mollie_customer_id' );
328
+				$subscription_customer_id = $first_payment->get_meta('mollie_customer_id');
329 329
 			}
330 330
 
331
-			if ( ! empty( $subscription_customer_id ) ) {
331
+			if ( ! empty($subscription_customer_id)) {
332 332
 				$customer_id = $subscription_customer_id;
333 333
 			}
334 334
 		}
335 335
 
336 336
 		// Create new customer if the customer does not exist at Mollie.
337
-		if ( ( empty( $customer_id ) || ! $this->client->get_customer( $customer_id ) ) && Core_Recurring::RECURRING !== $payment->recurring_type ) {
338
-			$customer_id = $this->client->create_customer( $payment->get_email(), $payment->get_customer_name() );
337
+		if ((empty($customer_id) || ! $this->client->get_customer($customer_id)) && Core_Recurring::RECURRING !== $payment->recurring_type) {
338
+			$customer_id = $this->client->create_customer($payment->get_email(), $payment->get_customer_name());
339 339
 
340
-			$this->update_wp_user_customer_id( $payment->user_id, $customer_id );
340
+			$this->update_wp_user_customer_id($payment->user_id, $customer_id);
341 341
 		}
342 342
 
343 343
 		// Store customer ID in subscription meta.
344
-		if ( $subscription && empty( $subscription_customer_id ) && ! empty( $customer_id ) ) {
345
-			$subscription->set_meta( 'mollie_customer_id', $customer_id );
344
+		if ($subscription && empty($subscription_customer_id) && ! empty($customer_id)) {
345
+			$subscription->set_meta('mollie_customer_id', $customer_id);
346 346
 		}
347 347
 
348 348
 		// Copy customer ID from subscription to user meta.
349
-		$this->copy_customer_id_to_wp_user( $payment );
349
+		$this->copy_customer_id_to_wp_user($payment);
350 350
 
351 351
 		return $customer_id;
352 352
 	}
@@ -358,12 +358,12 @@  discard block
 block discarded – undo
358 358
 	 *
359 359
 	 * @return string
360 360
 	 */
361
-	public function get_customer_id_by_wp_user_id( $user_id ) {
362
-		if ( empty( $user_id ) ) {
361
+	public function get_customer_id_by_wp_user_id($user_id) {
362
+		if (empty($user_id)) {
363 363
 			return false;
364 364
 		}
365 365
 
366
-		return get_user_meta( $user_id, $this->meta_key_customer_id, true );
366
+		return get_user_meta($user_id, $this->meta_key_customer_id, true);
367 367
 	}
368 368
 
369 369
 	/**
@@ -374,16 +374,16 @@  discard block
 block discarded – undo
374 374
 	 *
375 375
 	 * @return bool
376 376
 	 */
377
-	private function update_wp_user_customer_id( $user_id, $customer_id ) {
378
-		if ( empty( $user_id ) || is_bool( $user_id ) ) {
377
+	private function update_wp_user_customer_id($user_id, $customer_id) {
378
+		if (empty($user_id) || is_bool($user_id)) {
379 379
 			return false;
380 380
 		}
381 381
 
382
-		if ( ! is_string( $customer_id ) || empty( $customer_id ) || 1 === strlen( $customer_id ) ) {
382
+		if ( ! is_string($customer_id) || empty($customer_id) || 1 === strlen($customer_id)) {
383 383
 			return false;
384 384
 		}
385 385
 
386
-		update_user_meta( $user_id, $this->meta_key_customer_id, $customer_id );
386
+		update_user_meta($user_id, $this->meta_key_customer_id, $customer_id);
387 387
 	}
388 388
 
389 389
 	/**
@@ -393,25 +393,25 @@  discard block
 block discarded – undo
393 393
 	 *
394 394
 	 * @return void
395 395
 	 */
396
-	public function copy_customer_id_to_wp_user( Payment $payment ) {
397
-		if ( $this->config->id !== $payment->config_id ) {
396
+	public function copy_customer_id_to_wp_user(Payment $payment) {
397
+		if ($this->config->id !== $payment->config_id) {
398 398
 			return;
399 399
 		}
400 400
 
401 401
 		$subscription = $payment->get_subscription();
402 402
 
403
-		if ( ! $subscription || empty( $subscription->user_id ) ) {
403
+		if ( ! $subscription || empty($subscription->user_id)) {
404 404
 			return;
405 405
 		}
406 406
 
407 407
 		// Get customer ID from subscription meta.
408
-		$customer_id = $subscription->get_meta( 'mollie_customer_id' );
408
+		$customer_id = $subscription->get_meta('mollie_customer_id');
409 409
 
410
-		$user_customer_id = $this->get_customer_id_by_wp_user_id( $subscription->user_id );
410
+		$user_customer_id = $this->get_customer_id_by_wp_user_id($subscription->user_id);
411 411
 
412
-		if ( empty( $user_customer_id ) ) {
412
+		if (empty($user_customer_id)) {
413 413
 			// Set customer ID as user meta.
414
-			$this->update_wp_user_customer_id( $subscription->user_id, $customer_id );
414
+			$this->update_wp_user_customer_id($subscription->user_id, $customer_id);
415 415
 		}
416 416
 	}
417 417
 }
Please login to merge, or discard this patch.