Failed Conditions
Push — master ( e22298...bcbd11 )
by Reüel
10:06 queued 11s
created

Gateway::get_client_error()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 2
b 0
f 0
nc 2
nop 0
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 6
rs 10
1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 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\Pay\Core\Gateway as Core_Gateway;
14
use Pronamic\WordPress\Pay\Core\PaymentMethods;
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
17
/**
18
 * Gateway
19
 *
20
 * @author  Remco Tolsma
21
 * @version 2.1.10
22
 * @since   1.0.0
23
 */
24
class Gateway extends Core_Gateway {
25
	/**
26
	 * Client.
27
	 *
28
	 * @var Client
29
	 */
30
	private $client;
31
32
	/**
33
	 * Constructs and initializes an OmniKassa 2.0 gateway.
34
	 *
35
	 * @param Config $config Config.
36
	 */
37
	public function __construct( Config $config ) {
38
		parent::__construct( $config );
39
40
		$this->set_method( self::METHOD_HTTP_REDIRECT );
41
42
		// Supported features.
43
		$this->supports = array(
44
			'webhook_log',
45
		);
46
47
		// Client.
48
		$this->client = new Client();
49
50
		$url = Client::URL_PRODUCTION;
51
52
		if ( self::MODE_TEST === $config->mode ) {
53
			$url = Client::URL_SANDBOX;
54
		}
55
56
		$this->client->set_url( $url );
57
		$this->client->set_refresh_token( $config->refresh_token );
58
		$this->client->set_signing_key( $config->signing_key );
59
	}
60
61
	/**
62
	 * Get supported payment methods.
63
	 *
64
	 * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
65
	 * @return array<string>
66
	 */
67
	public function get_supported_payment_methods() {
68
		return array(
69
			PaymentMethods::AFTERPAY,
70
			PaymentMethods::BANCONTACT,
71
			PaymentMethods::CREDIT_CARD,
72
			PaymentMethods::IDEAL,
73
			PaymentMethods::MAESTRO,
74
			PaymentMethods::PAYPAL,
75
		);
76
	}
77
78
	/**
79
	 * Start.
80
	 *
81
	 * @see Core_Gateway::start()
82
	 * @param Payment $payment Payment.
83
	 * @throws \Exception Throws exception when payment could not start at Rabobank OmniKassa 2.0.
84
	 */
85
	public function start( Payment $payment ) {
86
		// Merchant order ID.
87
		$merchant_order_id = $payment->format_string( $this->config->order_id );
88
89
		$payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id );
90
91
		// New order.
92
		$merchant_return_url = $payment->get_return_url();
93
		$merchant_return_url = \apply_filters( 'pronamic_pay_omnikassa_2_merchant_return_url', $merchant_return_url );
94
95
		$order = new Order(
96
			$merchant_order_id,
97
			MoneyTransformer::transform( $payment->get_total_amount() ),
98
			$merchant_return_url
99
		);
100
101
		// Shipping address.
102
		$order->set_shipping_detail( AddressTransformer::transform( $payment->get_shipping_address() ) );
103
104
		// Billing address.
105
		$order->set_billing_detail( AddressTransformer::transform( $payment->get_billing_address() ) );
106
107
		// Customer information.
108
		$customer = $payment->get_customer();
109
110
		if ( null !== $customer ) {
111
			// Language.
112
			$language = $customer->get_language();
113
114
			if ( null !== $language ) {
115
				$order->set_language( \strtoupper( $language ) );
116
			}
117
118
			// Customer information.
119
			$customer_information = new CustomerInformation();
120
121
			$customer_information->set_email_address( $customer->get_email() );
122
			$customer_information->set_date_of_birth( $customer->get_birth_date() );
123
			$customer_information->set_gender( Gender::transform( $customer->get_gender() ) );
124
			$customer_information->set_telephone_number( $customer->get_phone() );
125
126
			$name = $customer->get_name();
127
128
			if ( null !== $name ) {
129
				$customer_information->set_initials( $name->get_initials() );
130
			}
131
132
			$order->set_customer_information( $customer_information );
133
		}
134
135
		// Payment brand.
136
		$payment_brand = PaymentBrands::transform( $payment->get_method() );
137
138
		$order->set_payment_brand( $payment_brand );
139
140
		if ( null !== $payment_brand ) {
141
			// Payment brand force should only be set if payment brand is not empty.
142
			$order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE );
143
		}
144
145
		// Description.
146
		$description = $payment->get_description();
147
148
		if ( null !== $description ) {
149
			$order->set_description( DataHelper::sanitize_an( $description, 35 ) );
150
		}
151
152
		// Lines.
153
		$lines = $payment->get_lines();
154
155
		if ( null !== $lines ) {
156
			$order_items = $order->new_items();
157
158
			$i = 1;
159
160
			foreach ( $lines as $line ) {
161
				$name = \sprintf(
162
					/* translators: %s: item index */
163
					\__( 'Item %s', 'pronamic_ideal' ),
164
					$i++
165
				);
166
167
				if ( null !== $line->get_name() && '' !== $line->get_name() ) {
168
					$name = $line->get_name();
169
				}
170
171
				$item = $order_items->new_item(
172
					DataHelper::sanitize_an( $name, 50 ),
173
					$line->get_quantity(),
174
					// The amount in cents, including VAT, of the item each, see below for more details.
175
					MoneyTransformer::transform( $line->get_unit_price() ),
176
					ProductCategories::transform( $line->get_type() )
177
				);
178
179
				$item->set_id( $line->get_id() );
180
181
				// Description.
182
				$description = $line->get_description();
183
184
				if ( empty( $description ) && PaymentBrands::AFTERPAY === $payment_brand ) {
185
					/*
186
					 * The `OrderItem.description` field is documentated as `0..1` (optional),
187
					 * but for AfterPay payments it is required.
188
					 *
189
					 * @link https://github.com/wp-pay-gateways/omnikassa-2/tree/feature/post-pay/documentation#error-5024
190
					 */
191
					$description = $name;
192
				}
193
194
				if ( null !== $description ) {
195
					$description = DataHelper::sanitize_an( $description, 100 );
196
				}
197
198
				$item->set_description( $description );
199
200
				$tax_amount = $line->get_unit_price()->get_tax_amount();
201
202
				if ( null !== $tax_amount ) {
203
					// The VAT of the item each, see below for more details.
204
					$item->set_tax( MoneyTransformer::transform( $tax_amount ) );
205
				}
206
			}
207
		}
208
209
		// Maybe update access token.
210
		$this->maybe_update_access_token();
211
212
		// Announce order.
213
		$response = $this->client->order_announce( $this->config, $order );
214
215
		// Validate.
216
		if ( ! $response->is_valid( $this->config->signing_key ) ) {
217
			throw new \Exception( 'Could not validate OmniKassa 2.0 response signature with signing key.' );
218
		}
219
220
		$payment->set_action_url( $response->get_redirect_url() );
221
	}
222
223
	/**
224
	 * Update status of the specified payment.
225
	 *
226
	 * @param Payment $payment Payment.
227
	 */
228
	public function update_status( Payment $payment ) {
229
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
230
		if ( ! ReturnParameters::contains( $_GET ) ) {
231
			return;
232
		}
233
234
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
235
		$parameters = ReturnParameters::from_array( $_GET );
236
237
		// Note.
238
		$note_values = array(
239
			'order_id'  => $parameters->get_order_id(),
240
			'status'    => $parameters->get_status(),
241
			'signature' => $parameters->get_signature(),
242
			'valid'     => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false',
243
		);
244
245
		$note = '';
246
247
		$note .= '<p>';
248
		$note .= \__( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' );
249
		$note .= '</p>';
250
251
		$note .= '<dl>';
252
253
		foreach ( $note_values as $key => $value ) {
254
			$note .= \sprintf( '<dt>%s</dt>', \esc_html( $key ) );
255
			$note .= \sprintf( '<dd>%s</dd>', \esc_html( $value ) );
256
		}
257
258
		$note .= '</dl>';
259
260
		$payment->add_note( $note );
261
262
		// Validate.
263
		if ( ! $parameters->is_valid( $this->config->signing_key ) ) {
264
			return;
265
		}
266
267
		// Status.
268
		$pronamic_status = Statuses::transform( $parameters->get_status() );
269
270
		if ( null !== $pronamic_status ) {
271
			$payment->set_status( $pronamic_status );
272
		}
273
	}
274
275
	/**
276
	 * Handle notification.
277
	 *
278
	 * @param Notification $notification Notification.
279
	 * @return void
280
	 */
281
	public function handle_notification( Notification $notification ) {
282
		if ( ! $notification->is_valid( $this->config->signing_key ) ) {
283
			return;
284
		}
285
286
		switch ( $notification->get_event_name() ) {
287
			case 'merchant.order.status.changed':
288
				$this->handle_merchant_order_status_changed( $notification );
289
		}
290
	}
291
292
	/**
293
	 * Handle `merchant.order.status.changed` event.
294
	 *
295
	 * @param Notification $notification Notification.
296
	 * @return void
297
	 */
298
	private function handle_merchant_order_status_changed( Notification $notification ) {
299
		do {
300
			$order_results = $this->client->get_order_results( $notification->get_authentication() );
301
302
			if ( ! $order_results->is_valid( $this->config->signing_key ) ) {
303
				return;
304
			}
305
306
			foreach ( $order_results as $order_result ) {
307
				$payment = \get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() );
308
309
				// Log webhook request.
310
				\do_action( 'pronamic_pay_webhook_log_payment', $payment );
311
312
				if ( empty( $payment ) ) {
313
					continue;
314
				}
315
316
				$payment->set_transaction_id( $order_result->get_omnikassa_order_id() );
317
318
				$pronamic_status = Statuses::transform( $order_result->get_order_status() );
319
320
				if ( null !== $pronamic_status ) {
321
					$payment->set_status( $pronamic_status );
322
				}
323
324
				// Note.
325
				$note = '';
326
327
				$note .= '<p>';
328
				$note .= \__( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' );
329
				$note .= '</p>';
330
				$note .= '<pre>';
331
				$note .= \wp_json_encode( $order_result->get_json(), \JSON_PRETTY_PRINT );
332
				$note .= '</pre>';
333
334
				$payment->add_note( $note );
335
336
				$payment->save();
337
			}
338
		} while ( $order_results->more_available() );
339
	}
340
341
	/**
342
	 * Maybe update access token.
343
	 *
344
	 * @return void
345
	 */
346
	private function maybe_update_access_token() {
347
		if ( $this->config->is_access_token_valid() ) {
348
			return;
349
		}
350
351
		$data = $this->client->get_access_token_data();
352
353
		if ( isset( $data->token ) ) {
354
			$this->config->access_token = $data->token;
355
356
			\update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token );
357
		}
358
359
		/*
360
		 * @codingStandardsIgnoreStart
361
		 *
362
		 * Ignore coding standards because of sniff WordPress.NamingConventions.ValidVariableName.NotSnakeCaseMemberVar
363
		 */
364
		if ( isset( $data->validUntil ) ) {
365
			$this->config->access_token_valid_until = $data->validUntil;
366
367
			\update_post_meta(
368
				$this->config->post_id,
369
				'_pronamic_gateway_omnikassa_2_access_token_valid_until',
370
				$data->validUntil
371
			);
372
		}
373
		// @codingStandardsIgnoreEnd
374
	}
375
}
376