Failed Conditions
Push — feature/post-pay ( adf651...4d43ec )
by Reüel
04:42
created

Gateway::start()   F

Complexity

Conditions 13
Paths 1152

Size

Total Lines 114
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 182

Importance

Changes 0
Metric Value
cc 13
eloc 62
nc 1152
nop 1
dl 0
loc 114
ccs 0
cts 76
cp 0
crap 182
rs 2.789
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Gateway
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 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\Util as Core_Util;
14
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
15
use Pronamic\WordPress\Pay\Core\PaymentMethods;
16
use Pronamic\WordPress\Pay\Payments\Payment;
17
18
/**
19
 * Gateway
20
 *
21
 * @author  Remco Tolsma
22
 * @version 2.0.2
23
 * @since   1.0.0
24
 */
25
class Gateway extends Core_Gateway {
26
	/**
27
	 * Client.
28
	 *
29
	 * @var Client
30
	 */
31
	private $client;
32
33
	/**
34
	 * Constructs and initializes an OmniKassa 2.0 gateway.
35
	 *
36
	 * @param Config $config Config.
37
	 */
38
	public function __construct( Config $config ) {
39
		parent::__construct( $config );
40
41
		$this->set_method( self::METHOD_HTTP_REDIRECT );
42
43
		// Client.
44
		$this->client = new Client();
45
46
		$url = Client::URL_PRODUCTION;
47
48
		if ( self::MODE_TEST === $config->mode ) {
49
			$url = Client::URL_SANDBOX;
50
		}
51
52
		$this->client->set_url( $url );
53
		$this->client->set_refresh_token( $config->refresh_token );
54
		$this->client->set_signing_key( $config->signing_key );
55
	}
56
57
	/**
58
	 * Get supported payment methods.
59
	 *
60
	 * @see \Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
61
	 * @return array
62
	 */
63
	public function get_supported_payment_methods() {
64
		return array(
65
			PaymentMethods::AFTERPAY,
66
			PaymentMethods::BANCONTACT,
67
			PaymentMethods::CREDIT_CARD,
68
			PaymentMethods::IDEAL,
69
			PaymentMethods::MAESTRO,
70
			PaymentMethods::PAYPAL,
71
		);
72
	}
73
74
	/**
75
	 * Start.
76
	 *
77
	 * @see Core_Gateway::start()
78
	 *
79
	 * @param Payment $payment Payment.
80
	 */
81
	public function start( Payment $payment ) {
82
		// Merchant order ID.
83
		$merchant_order_id = $payment->format_string( $this->config->order_id );
84
85
		$payment->set_meta( 'omnikassa_2_merchant_order_id', $merchant_order_id );
86
87
		// Shipping address.
88
		$shipping_address = $payment->get_shipping_address();
89
90
		if ( null !== $shipping_address ) {
91
			$shipping_detail = new Address();
92
93
			$name = $shipping_address->get_name();
94
95
			if ( null !== $name ) {
96
				$shipping_detail->set_first_name( $name->get_first_name() );
97
				$shipping_detail->set_middle_name( $name->get_middle_name() );
98
				$shipping_detail->set_last_name( $name->get_last_name() );
99
			}
100
101
			$shipping_detail->set_street( $shipping_address->get_street_name() );
102
			$shipping_detail->set_house_number( $shipping_address->get_house_number() );
103
			$shipping_detail->set_house_number_addition( $shipping_address->get_house_number_addition() );
104
			$shipping_detail->set_postal_code( $shipping_address->get_postal_code() );
105
			$shipping_detail->set_city( $shipping_address->get_city() );
106
			$shipping_detail->set_country_code( $shipping_address->get_country_code() );
107
		}
108
109
		// Billing address.
110
		$billing_address = $payment->get_billing_address();
111
112
		if ( null !== $billing_address ) {
113
			$billing_detail = new Address();
114
115
			$name = $billing_address->get_name();
116
117
			if ( null !== $name ) {
118
				$billing_detail->set_first_name( $name->get_first_name() );
119
				$billing_detail->set_middle_name( $name->get_middle_name() );
120
				$billing_detail->set_last_name( $name->get_last_name() );
121
			}
122
123
			$billing_detail->set_street( $billing_address->get_street_name() );
124
			$billing_detail->set_house_number( $billing_address->get_house_number() );
125
			$billing_detail->set_house_number_addition( $billing_address->get_house_number_addition() );
126
			$billing_detail->set_postal_code( $billing_address->get_postal_code() );
127
			$billing_detail->set_city( $billing_address->get_city() );
128
			$billing_detail->set_country_code( $billing_address->get_country_code() );
129
		}
130
131
		// Customer information.
132
		$customer = $payment->get_customer();
133
134
		if ( null !== $customer ) {
135
			$customer_information = new CustomerInformation();
136
137
			$customer_information->set_email_address( $customer->get_email() );
138
			$customer_information->set_telephone_number( $customer->get_phone() );
139
		}
140
141
		// Payment brand.
142
		$payment_brand = PaymentBrands::transform( $payment->get_method() );
143
144
		// New order.
145
		$order = new Order(
146
			$merchant_order_id,
147
			new Money(
148
				$payment->get_currency(),
149
				$payment->get_amount()->get_cents()
0 ignored issues
show
Bug introduced by
The method get_cents() does not exist on Pronamic\WordPress\Money\Money. ( Ignorable by Annotation )

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

149
				$payment->get_amount()->/** @scrutinizer ignore-call */ get_cents()

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
			),
151
			$payment->get_return_url()
152
		);
153
154
		$order->set_description( $payment->get_description() );
155
		$order->set_language( $payment->get_customer()->get_language() );
156
		$order->set_order_items( $payment->get_lines() );
157
158
		if ( isset( $shipping_detail ) ) {
159
			$order->set_shipping_detail( $shipping_detail );
160
		}
161
162
		if ( isset( $billing_detail ) ) {
163
			$order->set_billing_detail( $billing_detail );
164
		}
165
166
		if ( isset( $customer_information ) ) {
167
			$order->set_customer_information( $customer_information );
168
		}
169
170
		$order->set_payment_brand( $payment_brand );
171
172
		if ( null !== $payment_brand ) {
173
			// Payment brand force should only be set if payment brand is not empty.
174
			$order->set_payment_brand_force( PaymentBrandForce::FORCE_ONCE );
175
		}
176
177
		// Maybe update access token.
178
		$this->maybe_update_access_token();
179
180
		// Handle errors.
181
		if ( $this->get_client_error() ) {
182
			return;
183
		}
184
185
		// Announce order.
186
		$result = $this->client->order_announce( $this->config, $order );
187
188
		// Handle errors.
189
		if ( $this->get_client_error() ) {
190
			return;
191
		}
192
193
		if ( $result ) {
194
			$payment->set_action_url( $result->redirectUrl );
195
		}
196
	}
197
198
	/**
199
	 * Update status of the specified payment.
200
	 *
201
	 * @param Payment $payment Payment.
202
	 */
203
	public function update_status( Payment $payment ) {
204
		if ( ! ReturnParameters::contains( $_GET ) ) { // WPCS: CSRF ok.
205
			return;
206
		}
207
208
		$parameters = ReturnParameters::from_array( $_GET ); // WPCS: CSRF ok.
209
210
		// Note.
211
		$note_values = array(
212
			'order_id'  => $parameters->get_order_id(),
213
			'status'    => $parameters->get_status(),
214
			'signature' => $parameters->get_signature(),
215
			'valid'     => $parameters->is_valid( $this->config->signing_key ) ? 'true' : 'false',
216
		);
217
218
		$note = '';
219
220
		$note .= '<p>';
221
		$note .= __( 'OmniKassa 2.0 return URL requested:', 'pronamic_ideal' );
222
		$note .= '</p>';
223
224
		$note .= '<dl>';
225
226
		foreach ( $note_values as $key => $value ) {
227
			$note .= sprintf( '<dt>%s</dt>', esc_html( $key ) );
228
			$note .= sprintf( '<dd>%s</dd>', esc_html( $value ) );
229
		}
230
231
		$note .= '</dl>';
232
233
		$payment->add_note( $note );
234
235
		// Validate.
236
		if ( ! $parameters->is_valid( $this->config->signing_key ) ) {
237
			return;
238
		}
239
240
		// Status.
241
		$payment->set_status( Statuses::transform( $parameters->get_status() ) );
242
	}
243
244
	/**
245
	 * Handle notification.
246
	 *
247
	 * @param Notification $notification Notification.
248
	 *
249
	 * @return void
250
	 */
251
	public function handle_notification( Notification $notification ) {
252
		if ( ! $notification->is_valid( $this->config->signing_key ) ) {
253
			return;
254
		}
255
256
		switch ( $notification->get_event_name() ) {
257
			case 'merchant.order.status.changed':
258
				$this->handle_merchant_order_status_changed( $notification );
259
		}
260
	}
261
262
	/**
263
	 * Handle `merchant.order.status.changed` event.
264
	 *
265
	 * @param Notification $notification Notification.
266
	 *
267
	 * @return void
268
	 */
269
	private function handle_merchant_order_status_changed( Notification $notification ) {
270
		do {
271
			$order_results = $this->client->get_order_results( $notification->get_authentication() );
272
273
			if ( ! $order_results || $order_results->is_valid( $this->config->signing_key ) ) {
274
				return;
275
			}
276
277
			foreach ( $order_results as $order_result ) {
278
				$payment = get_pronamic_payment_by_meta( '_pronamic_payment_omnikassa_2_merchant_order_id', $order_result->get_merchant_order_id() );
279
280
				if ( empty( $payment ) ) {
281
					continue;
282
				}
283
284
				$payment->set_transaction_id( $order_result->get_omnikassa_order_id() );
285
				$payment->set_status( Statuses::transform( $order_result->get_order_status() ) );
286
287
				// Note.
288
				$note = '';
289
290
				$note .= '<p>';
291
				$note .= __( 'OmniKassa 2.0 webhook URL requested:', 'pronamic_ideal' );
292
				$note .= '</p>';
293
				$note .= '<pre>';
294
				$note .= wp_json_encode( $order_result->get_json(), JSON_PRETTY_PRINT );
295
				$note .= '</pre>';
296
297
				$payment->add_note( $note );
298
299
				$payment->save();
300
			}
301
		} while ( $order_results->more_available() );
302
	}
303
304
	/**
305
	 * Maybe update access token.
306
	 *
307
	 * @return void
308
	 */
309
	private function maybe_update_access_token() {
310
		if ( $this->config->is_access_token_valid() ) {
311
			return;
312
		}
313
314
		$data = $this->client->get_access_token_data();
315
316
		if ( ! is_object( $data ) ) {
317
			return;
318
		}
319
320
		$this->config->access_token             = $data->token;
321
		$this->config->access_token_valid_until = $data->validUntil;
322
323
		update_post_meta( $this->config->post_id, '_pronamic_gateway_omnikassa_2_access_token', $data->token );
324
		update_post_meta(
325
			$this->config->post_id,
326
			'_pronamic_gateway_omnikassa_2_access_token_valid_until',
327
			$data->validUntil
328
		);
329
	}
330
331
	/**
332
	 * Get client error.
333
	 *
334
	 * @return \WP_Error|bool
335
	 */
336
	private function get_client_error() {
337
		$error = $this->client->get_error();
338
339
		if ( is_wp_error( $error ) ) {
340
			$this->error = $error;
341
342
			return $error;
343
		}
344
345
		return false;
346
	}
347
}
348