Failed Conditions
Push — develop ( 9d2c86...0e2663 )
by Remco
10:32 queued 02:27
created

Gateway::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 6
b 0
f 0
nc 2
nop 1
dl 0
loc 22
ccs 0
cts 14
cp 0
crap 6
rs 9.9
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2021 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Gateways\OmniKassa2
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2;
12
13
use Pronamic\WordPress\Money\Money;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Pronamic\WordPress\Pay\Gateways\OmniKassa2\Money. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Pronamic\WordPress\Money\TaxedMoney;
15
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
16
use Pronamic\WordPress\Pay\Core\PaymentMethods;
17
use Pronamic\WordPress\Pay\Payments\Payment;
18
19
/**
20
 * Gateway
21
 *
22
 * @author  Remco Tolsma
23
 * @version 2.3.4
24
 * @since   1.0.0
25
 */
26
class Gateway extends Core_Gateway {
27
	/**
28
	 * Client.
29
	 *
30
	 * @var Client
31
	 */
32
	private $client;
33
34
	/**
35
	 * Constructs and initializes an OmniKassa 2.0 gateway.
36
	 *
37
	 * @param Config $config Config.
38
	 */
39
	public function __construct( Config $config ) {
40
		parent::__construct( $config );
41
42
		$this->set_method( self::METHOD_HTTP_REDIRECT );
43
44
		// Supported features.
45
		$this->supports = array(
46
			'webhook_log',
47
		);
48
49
		// Client.
50
		$this->client = new Client();
51
52
		$url = Client::URL_PRODUCTION;
53
54
		if ( self::MODE_TEST === $config->mode ) {
55
			$url = Client::URL_SANDBOX;
56
		}
57
58
		$this->client->set_url( $url );
59
		$this->client->set_refresh_token( $config->refresh_token );
60
		$this->client->set_signing_key( $config->signing_key );
61
	}
62
63
	/**
64
	 * Get supported payment methods.
65
	 *
66
	 * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
67
	 * @return array<string>
68
	 */
69
	public function get_supported_payment_methods() {
70
		return array(
71
			PaymentMethods::AFTERPAY,
72
			PaymentMethods::BANCONTACT,
73
			PaymentMethods::CREDIT_CARD,
74
			PaymentMethods::IDEAL,
75
			PaymentMethods::MAESTRO,
76
			PaymentMethods::PAYPAL,
77
		);
78
	}
79
80
	/**
81
	 * Start.
82
	 *
83
	 * @param Payment $payment Payment.
84
	 * @return void
85
	 * @throws \Exception Throws exception when payment could not start at Rabobank OmniKassa 2.0.
86
	 * @see Core_Gateway::start()
87
	 */
88
	public function start( Payment $payment ) {
89
		// Merchant order ID.
90
		$merchant_order_id = $payment->format_string( $this->config->order_id );
91
92
		$payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id );
93
94
		// New order.
95
		$merchant_return_url = $payment->get_return_url();
96
97
		/**
98
		 * Filters the OmniKassa 2.0 merchant return URL.
99
		 *
100
		 * OmniKassa 2 requires for each order announcement a merchant return URL.
101
		 * OmniKassa 2 does not allow all merchant return URL's. An order
102
		 * announcement with a merchant return URL's with the TLD `.test` will
103
		 * for example result in the following error:
104
		 *
105
		 * > merchantReturnURL is not a valid web address
106
		 *
107
		 * This can be very inconvenient for testing OmniKassa 2, therefor we
108
		 * introduced this filter.
109
		 *
110
		 * @link https://github.com/wp-pay-gateways/omnikassa-2#pronamic_pay_omnikassa_2_merchant_return_url
111
		 * @link https://github.com/wp-pay-gateways/omnikassa-2/tree/master/documentation#merchantreturnurl-is-not-a-valid-web-address
112
		 * @param string $merchant_return_url Merchant return URL.
113
		 */
114
		$merchant_return_url = \apply_filters( 'pronamic_pay_omnikassa_2_merchant_return_url', $merchant_return_url );
115
116
		$order = new Order(
117
			$merchant_order_id,
118
			MoneyTransformer::transform( $payment->get_total_amount() ),
119
			$merchant_return_url
120
		);
121
122
		// Shipping address.
123
		$order->set_shipping_detail( AddressTransformer::transform( $payment->get_shipping_address() ) );
124
125
		// Billing address.
126
		$order->set_billing_detail( AddressTransformer::transform( $payment->get_billing_address() ) );
127
128
		// Customer information.
129
		$customer = $payment->get_customer();
130
131
		if ( null !== $customer ) {
132
			// Language.
133
			$language = $customer->get_language();
134
135
			if ( null !== $language ) {
136
				$order->set_language( \strtoupper( $language ) );
137
			}
138
139
			// Customer information.
140
			$customer_information = new CustomerInformation();
141
142
			$customer_information->set_email_address( $customer->get_email() );
143
			$customer_information->set_date_of_birth( $customer->get_birth_date() );
144
			$customer_information->set_gender( Gender::transform( $customer->get_gender() ) );
145
			$customer_information->set_telephone_number( $customer->get_phone() );
146
147
			$name = $customer->get_name();
148
149
			if ( null !== $name ) {
150
				$customer_information->set_initials( $name->get_initials() );
151
			}
152
153
			$order->set_customer_information( $customer_information );
154
		}
155
156
		// Payment brand.
157
		$payment_brand = PaymentBrands::transform( $payment->get_method() );
158
159
		$order->set_payment_brand( $payment_brand );
160
161
		if ( null !== $payment_brand ) {
162
			// Payment brand force should only be set if payment brand is not empty.
163
			$order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE );
164
		}
165
166
		// Description.
167
		$description = $payment->get_description();
168
169
		if ( null !== $description ) {
170
			$order->set_description( DataHelper::sanitize_an( $description, 35 ) );
171
		}
172
173
		// Lines.
174
		$lines = $payment->get_lines();
175
176
		if ( null !== $lines ) {
177
			$order_items = $order->new_items();
178
179
			$i = 1;
180
181
			foreach ( $lines as $line ) {
182
				$name = \sprintf(
183
					/* translators: %s: item index */
184
					\__( 'Item %s', 'pronamic_ideal' ),
185
					$i++
186
				);
187
188
				$line_name = $line->get_name();
189
190
				if ( null !== $line_name && '' !== $line_name ) {
191
					$name = $line_name;
192
				}
193
194
				$unit_price = $line->get_unit_price();
195
196
				if ( null === $unit_price ) {
197
					$unit_price = new Money();
198
				}
199
200
				$item = $order_items->new_item(
201
					DataHelper::sanitize_an( $name, 50 ),
202
					(int) $line->get_quantity(),
203
					// The amount in cents, including VAT, of the item each, see below for more details.
204
					MoneyTransformer::transform( $unit_price ),
205
					ProductCategories::transform( $line->get_type() )
206
				);
207
208
				$item->set_id( $line->get_id() );
209
210
				// Description.
211
				$description = $line->get_description();
212
213
				if ( empty( $description ) && PaymentBrands::AFTERPAY === $payment_brand ) {
214
					/*
215
					 * The `OrderItem.description` field is documented as `0..1` (optional),
216
					 * but for AfterPay payments it is required.
217
					 *
218
					 * @link https://github.com/wp-pay-gateways/omnikassa-2/tree/feature/post-pay/documentation#error-5024
219
					 */
220
					$description = $name;
221
				}
222
223
				if ( null !== $description ) {
224
					$description = DataHelper::sanitize_an( $description, 100 );
225
				}
226
227
				$item->set_description( $description );
228
229
				if ( $unit_price instanceof TaxedMoney ) {
230
					$tax_amount = $unit_price->get_tax_amount();
231
232
					if ( null !== $tax_amount ) {
233
						// The VAT of the item each, see below for more details.
234
						$item->set_tax( MoneyTransformer::transform( $tax_amount ) );
235
					}
236
				}
237
			}
238
		}
239
240
		// Maybe update access token.
241
		$this->maybe_update_access_token();
242
243
		// Announce order.
244
		$response = $this->client->order_announce( $this->config, $order );
245
246
		$payment->set_transaction_id( $response->get_omnikassa_order_id() );
247
		$payment->set_action_url( $response->get_redirect_url() );
248
	}
249
250
	/**
251
	 * Update status of the specified payment.
252
	 *
253
	 * @param Payment $payment Payment.
254
	 * @return void
255
	 */
256
	public function update_status( Payment $payment ) {
257
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended, SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
258
		if ( ! ReturnParameters::contains( $_GET ) ) {
259
			return;
260
		}
261
262
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended, SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable.DisallowedSuperGlobalVariable
263
		$parameters = ReturnParameters::from_array( $_GET );
264
265
		// Note.
266
		$note_values = array(
267
			'order_id'  => $parameters->get_order_id(),
268
			'status'    => $parameters->get_status(),
269
			'signature' => (string) $parameters->get_signature(),
270
			'valid'     => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false',
271
		);
272
273
		$note = '';
274
275
		$note .= '<p>';
276
		$note .= \__( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' );
277
		$note .= '</p>';
278
279
		$note .= '<dl>';
280
281
		foreach ( $note_values as $key => $value ) {
282
			$note .= \sprintf( '<dt>%s</dt>', \esc_html( $key ) );
283
			$note .= \sprintf( '<dd>%s</dd>', \esc_html( $value ) );
284
		}
285
286
		$note .= '</dl>';
287
288
		$payment->add_note( $note );
289
290
		// Validate.
291
		if ( ! $parameters->is_valid( $this->config->signing_key ) ) {
292
			return;
293
		}
294
295
		// Status.
296
		$pronamic_status = Statuses::transform( $parameters->get_status() );
297
298
		if ( null !== $pronamic_status ) {
299
			$payment->set_status( $pronamic_status );
300
		}
301
	}
302
303
	/**
304
	 * Handle notification.
305
	 *
306
	 * @param Notification $notification Notification.
307
	 * @return void
308
	 * @throws \Pronamic\WordPress\Pay\Gateways\OmniKassa2\InvalidSignatureException Throws invalid signature exception when notification message does not match gateway configuration signature.
309
	 */
310
	public function handle_notification( Notification $notification ) {
311
		if ( ! $notification->is_valid( $this->config->signing_key ) ) {
312
			throw new \Pronamic\WordPress\Pay\Gateways\OmniKassa2\InvalidSignatureException(
313
				\sprintf(
314
					'Signature on notification message does not match gateway configuration signature (%s).',
315
					\substr( $this->config->signing_key, 0, 7 )
316
				)
317
			);
318
		}
319
320
		switch ( $notification->get_event_name() ) {
321
			case 'merchant.order.status.changed':
322
				$this->handle_merchant_order_status_changed( $notification );
323
		}
324
	}
325
326
	/**
327
	 * Handle `merchant.order.status.changed` event.
328
	 *
329
	 * @param Notification $notification Notification.
330
	 * @return void
331
	 * @throws \Pronamic\WordPress\Pay\Gateways\OmniKassa2\InvalidSignatureException Throws invalid signature exception when order results message does not match gateway configuration signature.
332
	 * @throws \Pronamic\WordPress\Pay\Gateways\OmniKassa2\UnknownOrderIdsException Throws unknow order IDs exception when no payment could be find for on ore more OmniKassa order IDs.
333
	 */
334
	private function handle_merchant_order_status_changed( Notification $notification ) {
335
		$unknown_order_ids = array();
336
337
		do {
338
			$order_results = $this->client->get_order_results( $notification->get_authentication() );
339
340
			if ( ! $order_results->is_valid( $this->config->signing_key ) ) {
341
				throw new \Pronamic\WordPress\Pay\Gateways\OmniKassa2\InvalidSignatureException(
342
					\sprintf(
343
						'Signature on order results message does not match gateway configuration signature (%s).',
344
						\substr( $this->config->signing_key, 0, 7 )
345
					)
346
				);
347
			}
348
349
			foreach ( $order_results as $order_result ) {
350
				$omnikassa_order_id = $order_result->get_omnikassa_order_id();
351
352
				$payment = \get_pronamic_payment_by_transaction_id( $omnikassa_order_id );
353
354
				if ( empty( $payment ) ) {
355
					/**
356
					 * If there is no payment found with the OmniKassa order ID
357
					 * we will continue to check the other order results. It is
358
					 * possible that the payment has been deleted and can
359
					 * therefore no longer be updated. We keep track of this
360
					 * exception and throw it at the end of this function.
361
					 */
362
					$unknown_order_ids[] = $omnikassa_order_id;
363
364
					continue;
365
				}
366
367
				/**
368
				 * Webhook log payment.
369
				 *
370
				 * The `pronamic_pay_webhook_log_payment` action is triggered so the
371
				 * `wp-pay/core` library can hook into this and register the webhook
372
				 * call.
373
				 *
374
				 * @param Payment $payment Payment to log.
375
				 */
376
				\do_action( 'pronamic_pay_webhook_log_payment', $payment );
377
378
				$pronamic_status = Statuses::transform( $order_result->get_order_status() );
379
380
				if ( null !== $pronamic_status ) {
381
					$payment->set_status( $pronamic_status );
382
				}
383
384
				// Note.
385
				$note = \sprintf(
386
					'<p>%s</p><pre>%s</pre>',
387
					\__( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' ),
388
					(string) \wp_json_encode( $order_result, \JSON_PRETTY_PRINT )
389
				);
390
391
				$payment->add_note( $note );
392
393
				$payment->save();
394
			}
395
		} while ( $order_results->more_available() );
396
397
		if ( \count( $unknown_order_ids ) > 0 ) {
398
			throw new \Pronamic\WordPress\Pay\Gateways\OmniKassa2\UnknownOrderIdsException(
399
				\sprintf(
400
					'Could not find payments for the following OmniKassa order IDs: %s.',
401
					\implode( ', ', $unknown_order_ids )
402
				)
403
			);
404
		}
405
	}
406
407
	/**
408
	 * Maybe update access token.
409
	 *
410
	 * @return void
411
	 */
412
	private function maybe_update_access_token() {
413
		if ( $this->config->is_access_token_valid() ) {
414
			return;
415
		}
416
417
		$data = $this->client->get_access_token_data();
418
419
		if ( isset( $data->token ) ) {
420
			$this->config->access_token = $data->token;
421
422
			\update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token );
423
		}
424
425
		/*
426
		 * @codingStandardsIgnoreStart
427
		 *
428
		 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
429
		 */
430
		if ( isset( $data->validUntil ) ) {
431
			$this->config->access_token_valid_until = $data->validUntil;
432
433
			\update_post_meta(
434
				$this->config->post_id,
435
				'_pronamic_gateway_omnikassa_2_access_token_valid_until',
436
				$data->validUntil
437
			);
438
		}
439
		// @codingStandardsIgnoreEnd
440
	}
441
}
442