Gateway::get_output_fields()   F
last analyzed

Complexity

Conditions 17
Paths 1008

Size

Total Lines 133
Code Lines 70

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 5
Bugs 0 Features 0
Metric Value
cc 17
eloc 70
c 5
b 0
f 0
nc 1008
nop 1
dl 0
loc 133
ccs 0
cts 78
cp 0
crap 306
rs 1.0499

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
namespace Pronamic\WordPress\Pay\Gateways\Ingenico\OrderStandard;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\PaymentMethods as Core_PaymentMethods;
7
use Pronamic\WordPress\Pay\Gateways\Ingenico\Brands;
8
use Pronamic\WordPress\Pay\Gateways\Ingenico\DataCustomerHelper;
9
use Pronamic\WordPress\Pay\Gateways\Ingenico\DataGeneralHelper;
10
use Pronamic\WordPress\Pay\Gateways\Ingenico\DataUrlHelper;
11
use Pronamic\WordPress\Pay\Gateways\Ingenico\Parameters;
12
use Pronamic\WordPress\Pay\Gateways\Ingenico\PaymentMethods;
13
use Pronamic\WordPress\Pay\Gateways\Ingenico\Statuses;
14
use Pronamic\WordPress\Pay\Gateways\Ingenico\Util;
15
use Pronamic\WordPress\Pay\Gateways\Ingenico\Security;
16
use Pronamic\WordPress\Pay\Payments\Payment;
17
18
/**
19
 * Title: Ingenico order standard gateway
20
 * Description:
21
 * Copyright: 2005-2021 Pronamic
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.4
26
 * @since   1.0.0
27
 */
28
class Gateway extends Core_Gateway {
29
	/**
30
	 * Client.
31
	 *
32
	 * @var Client
33
	 */
34
	protected $client;
35
36
	/**
37
	 * Constructs and initializes an OrderStandard gateway
38
	 *
39
	 * @param Config $config Config.
40
	 */
41
	public function __construct( Config $config ) {
42
		parent::__construct( $config );
43
44
		$this->set_method( self::METHOD_HTML_FORM );
45
46
		// Supported features.
47
		$this->supports = array(
48
			'payment_status_request',
49
		);
50
51
		// Client.
52
		$this->client = new Client( $this->config->psp_id );
53
54
		$this->client->set_payment_server_url( $config->get_form_action_url() );
55
		$this->client->set_direct_query_url( $config->get_direct_query_url() );
56
		$this->client->set_pass_phrase_in( $config->sha_in_pass_phrase );
57
		$this->client->set_pass_phrase_out( $config->sha_out_pass_phrase );
58
		$this->client->set_user_id( $config->user_id );
59
		$this->client->set_password( $config->password );
60
61
		if ( ! empty( $config->hash_algorithm ) ) {
62
			$this->client->set_hash_algorithm( $config->hash_algorithm );
63
		}
64
	}
65
	/**
66
	 * Get supported payment methods
67
	 *
68
	 * @see Core_Gateway::get_supported_payment_methods()
69
	 */
70
	public function get_supported_payment_methods() {
71
		return array(
72
			Core_PaymentMethods::BANK_TRANSFER,
73
			Core_PaymentMethods::IDEAL,
74
			Core_PaymentMethods::CREDIT_CARD,
75
			Core_PaymentMethods::BANCONTACT,
76
			Core_PaymentMethods::PAYPAL,
77
		);
78
	}
79
80
	/**
81
	 * Start
82
	 *
83
	 * @see Core_Gateway::start()
84
	 *
85
	 * @param Payment $payment Payment.
86
	 */
87
	public function start( Payment $payment ) {
88
		$payment->set_action_url( $this->client->get_payment_server_url() );
89
90
		if ( $this->config->alias_enabled ) {
0 ignored issues
show
Bug introduced by
The property alias_enabled does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
91
			$alias = uniqid();
92
93
			$payment->set_meta( 'ogone_alias', $alias );
94
		}
95
	}
96
97
	/**
98
	 * Get output fields
99
	 *
100
	 * @param Payment $payment Payment.
101
	 *
102
	 * @return array
103
	 * @since   1.2.1
104
	 * @version 2.0.4
105
	 * @see     Core_Gateway::get_output_html()
106
	 */
107
	public function get_output_fields( Payment $payment ) {
108
		$ogone_data = $this->client->get_data();
109
110
		// General.
111
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
112
113
		$ogone_data_general
114
			->set_order_id( $payment->format_string( $this->config->order_id ) )
0 ignored issues
show
Bug introduced by
The property order_id does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
115
			->set_order_description( $payment->get_description() )
116
			->set_param_plus( 'payment_id=' . $payment->get_id() )
117
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
118
			->set_amount( $payment->get_total_amount()->get_minor_units()->format( 0, '', '' ) );
119
120
		// Alias.
121
		$alias = $payment->get_meta( 'ogone_alias' );
122
123
		if ( $this->config->alias_enabled && false !== $alias ) {
0 ignored issues
show
Bug introduced by
The property alias_enabled does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
124
			$ogone_data_general->set_alias( $alias )
125
								->set_alias_usage( $this->config->alias_usage );
0 ignored issues
show
Bug introduced by
The property alias_usage does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
126
		}
127
128
		$customer = $payment->get_customer();
129
130
		// Language.
131
		$locale = \get_locale();
132
133
		if ( null !== $customer ) {
134
			$customer_locale = $customer->get_locale();
135
136
			// Locale not always contains `_`, e.g. "Nederlands" in Firefox.
137
			if ( null !== $customer_locale && false !== \strpos( $customer_locale, '_' ) ) {
138
				$locale = $customer_locale;
139
			}
140
		}
141
142
		$ogone_data_general->set_language( $locale );
143
144
		// Customer.
145
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
146
147
		if ( null !== $customer ) {
148
			$name = $customer->get_name();
149
150
			if ( null !== $name ) {
151
				$ogone_data_customer->set_name( strval( $name ) );
152
			}
153
154
			$ogone_data_customer->set_email( $customer->get_email() );
155
		}
156
157
		$billing_address = $payment->get_billing_address();
158
159
		if ( null !== $billing_address ) {
160
			$ogone_data_customer
161
				->set_address( $billing_address->get_line_1() )
162
				->set_zip( $billing_address->get_postal_code() )
163
				->set_town( $billing_address->get_city() )
164
				->set_country( $billing_address->get_country_code() )
165
				->set_telephone_number( $billing_address->get_phone() );
166
		}
167
168
		/*
169
		 * Payment method.
170
		 *
171
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
172
		 */
173
		switch ( $payment->get_payment_method() ) {
0 ignored issues
show
Bug introduced by
The method get_payment_method() does not exist on Pronamic\WordPress\Pay\Payments\Payment. ( Ignorable by Annotation )

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

173
		switch ( $payment->/** @scrutinizer ignore-call */ get_payment_method() ) {

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...
174
			case Core_PaymentMethods::BANK_TRANSFER:
175
				/*
176
				 * Set bank transfer payment method.
177
				 * @since 2.2.0
178
				 */
179
				$ogone_data_general
180
					->set_payment_method( PaymentMethods::BANK_TRANSFER );
181
182
				break;
183
			case Core_PaymentMethods::CREDIT_CARD:
184
				/*
185
				 * Set credit card payment method.
186
				 * @since 1.2.3
187
				 */
188
				$ogone_data_general
189
					->set_payment_method( PaymentMethods::CREDIT_CARD );
190
191
				break;
192
			case Core_PaymentMethods::IDEAL:
193
				/*
194
				 * Set iDEAL payment method.
195
				 * @since 1.2.3
196
				 */
197
				$ogone_data_general
198
					->set_brand( Brands::IDEAL )
199
					->set_payment_method( PaymentMethods::IDEAL );
200
201
				break;
202
			case Core_PaymentMethods::BANCONTACT:
203
			case Core_PaymentMethods::MISTER_CASH:
0 ignored issues
show
Deprecated Code introduced by
The constant Pronamic\WordPress\Pay\C...entMethods::MISTER_CASH has been deprecated: "Bancontact/Mister Cash" was renamed to just "Bancontact". ( Ignorable by Annotation )

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

203
			case /** @scrutinizer ignore-deprecated */ Core_PaymentMethods::MISTER_CASH:

This class constant has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the constant will be removed from the class and what other constant to use instead.

Loading history...
204
				$ogone_data_general
205
					->set_brand( Brands::BCMC )
206
					->set_payment_method( PaymentMethods::CREDIT_CARD );
207
208
				break;
209
			case Core_PaymentMethods::PAYPAL:
210
				$ogone_data_general
211
					->set_payment_method( PaymentMethods::PAYPAL );
212
213
				break;
214
		}
215
216
		// Parameter Variable.
217
		$param_var = Util::get_param_var( $this->config->param_var );
0 ignored issues
show
Bug introduced by
The property param_var does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
218
219
		if ( ! empty( $param_var ) ) {
220
			$ogone_data->set_field( 'PARAMVAR', $param_var );
221
		}
222
223
		// Template Page.
224
		$template_page = $this->config->template_page;
0 ignored issues
show
Bug introduced by
The property template_page does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
225
226
		if ( ! empty( $template_page ) ) {
227
			$ogone_data->set_field( 'TP', $template_page );
228
		}
229
230
		// URLs.
231
		$ogone_url_helper = new DataUrlHelper( $ogone_data );
232
233
		$ogone_url_helper
234
			->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
235
			->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) )
236
			->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) )
237
			->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) );
238
239
		return $this->client->get_fields();
240
	}
241
242
	/**
243
	 * Update status of the specified payment
244
	 *
245
	 * @param Payment $payment Payment.
246
	 */
247
	public function update_status( Payment $payment ) {
248
		$data = Security::get_request_data();
249
250
		$data = $this->client->verify_request( $data );
251
252
		if ( false !== $data ) {
253
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
254
255
			$payment->set_status( $status );
256
257
			// Update transaction ID.
258
			if ( \array_key_exists( Parameters::PAY_ID, $data ) ) {
259
				$payment->set_transaction_id( $data[ Parameters::PAY_ID ] );
260
			}
261
262
			// Add payment note.
263
			$this->update_status_payment_note( $payment, $data );
264
265
			return;
266
		}
267
268
		// Get order status with direct query.
269
		$order_id = $payment->format_string( $this->config->order_id );
0 ignored issues
show
Bug introduced by
The property order_id does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
270
271
		try {
272
			$status = $this->client->get_order_status( $order_id );
273
		} catch ( \Exception $e ) {
274
			$payment->add_note( $e->getMessage() );
275
276
			return;
277
		}
278
279
		if ( null !== $status ) {
280
			$payment->set_status( $status );
281
		}
282
	}
283
284
	/**
285
	 * Update status payment note
286
	 *
287
	 * @param Payment $payment Payment.
288
	 * @param array   $data    Data.
289
	 */
290
	private function update_status_payment_note( Payment $payment, $data ) {
291
		$labels = array(
292
			'STATUS'     => __( 'Status', 'pronamic_ideal' ),
293
			'ORDERID'    => __( 'Order ID', 'pronamic_ideal' ),
294
			'CURRENCY'   => __( 'Currency', 'pronamic_ideal' ),
295
			'AMOUNT'     => __( 'Amount', 'pronamic_ideal' ),
296
			'PM'         => __( 'Payment Method', 'pronamic_ideal' ),
297
			'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ),
298
			'CARDNO'     => __( 'Card Number', 'pronamic_ideal' ),
299
			'ED'         => __( 'End Date', 'pronamic_ideal' ),
300
			'CN'         => __( 'Customer Name', 'pronamic_ideal' ),
301
			'TRXDATE'    => __( 'Transaction Date', 'pronamic_ideal' ),
302
			'PAYID'      => __( 'Pay ID', 'pronamic_ideal' ),
303
			'NCERROR'    => __( 'NC Error', 'pronamic_ideal' ),
304
			'BRAND'      => __( 'Brand', 'pronamic_ideal' ),
305
			'IP'         => __( 'IP', 'pronamic_ideal' ),
306
			'SHASIGN'    => __( 'SHA Signature', 'pronamic_ideal' ),
307
		);
308
309
		$note = '';
310
311
		$note .= '<p>';
312
		$note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' );
313
		$note .= '</p>';
314
315
		$note .= '<dl>';
316
317
		foreach ( $labels as $key => $label ) {
318
			if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) {
319
				$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
320
				$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
321
			}
322
		}
323
324
		$note .= '</dl>';
325
326
		$payment->add_note( $note );
327
	}
328
}
329