Failed Conditions
Push — develop ( 59650a...3d7b28 )
by Reüel
07:06
created

Gateway::update_status()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 5
nop 1
dl 0
loc 34
ccs 0
cts 21
cp 0
crap 30
rs 9.3888
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, '', '' ) );
0 ignored issues
show
Bug introduced by
The method format() does not exist on integer. ( Ignorable by Annotation )

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

118
			->set_amount( $payment->get_total_amount()->get_minor_units()->/** @scrutinizer ignore-call */ format( 0, '', '' ) );

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...
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
		if ( null !== $customer ) {
131
			// Localised language.
132
			$ogone_data_general->set_language( $customer->get_locale() );
133
		}
134
135
		// Customer.
136
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
137
138
		if ( null !== $customer ) {
139
			$name = $customer->get_name();
140
141
			if ( null !== $name ) {
142
				$ogone_data_customer->set_name( strval( $name ) );
143
			}
144
145
			$ogone_data_customer->set_email( $customer->get_email() );
146
		}
147
148
		$billing_address = $payment->get_billing_address();
149
150
		if ( null !== $billing_address ) {
151
			$ogone_data_customer
152
				->set_address( $billing_address->get_line_1() )
153
				->set_zip( $billing_address->get_postal_code() )
154
				->set_town( $billing_address->get_city() )
155
				->set_country( $billing_address->get_country_code() )
156
				->set_telephone_number( $billing_address->get_phone() );
157
		}
158
159
		/*
160
		 * Payment method.
161
		 *
162
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
163
		 */
164
		switch ( $payment->get_method() ) {
165
			case Core_PaymentMethods::BANK_TRANSFER:
166
				/*
167
				 * Set bank transfer payment method.
168
				 * @since 2.2.0
169
				 */
170
				$ogone_data_general
171
					->set_payment_method( PaymentMethods::BANK_TRANSFER );
172
173
				break;
174
			case Core_PaymentMethods::CREDIT_CARD:
175
				/*
176
				 * Set credit card payment method.
177
				 * @since 1.2.3
178
				 */
179
				$ogone_data_general
180
					->set_payment_method( PaymentMethods::CREDIT_CARD );
181
182
				break;
183
			case Core_PaymentMethods::IDEAL:
184
				/*
185
				 * Set iDEAL payment method.
186
				 * @since 1.2.3
187
				 */
188
				$ogone_data_general
189
					->set_brand( Brands::IDEAL )
190
					->set_payment_method( PaymentMethods::IDEAL );
191
192
				break;
193
			case Core_PaymentMethods::BANCONTACT:
194
			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

194
			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...
195
				$ogone_data_general
196
					->set_brand( Brands::BCMC )
197
					->set_payment_method( PaymentMethods::CREDIT_CARD );
198
199
				break;
200
			case Core_PaymentMethods::PAYPAL:
201
				$ogone_data_general
202
					->set_payment_method( PaymentMethods::PAYPAL );
203
204
				break;
205
		}
206
207
		// Parameter Variable.
208
		$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...
209
210
		if ( ! empty( $param_var ) ) {
211
			$ogone_data->set_field( 'PARAMVAR', $param_var );
212
		}
213
214
		// Template Page.
215
		$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...
216
217
		if ( ! empty( $template_page ) ) {
218
			$ogone_data->set_field( 'TP', $template_page );
219
		}
220
221
		// URLs.
222
		$ogone_url_helper = new DataUrlHelper( $ogone_data );
223
224
		$ogone_url_helper
225
			->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
226
			->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) )
227
			->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) )
228
			->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) );
229
230
		return $this->client->get_fields();
231
	}
232
233
	/**
234
	 * Update status of the specified payment
235
	 *
236
	 * @param Payment $payment Payment.
237
	 */
238
	public function update_status( Payment $payment ) {
239
		$data = Security::get_request_data();
240
241
		$data = $this->client->verify_request( $data );
242
243
		if ( false !== $data ) {
244
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
245
246
			$payment->set_status( $status );
247
248
			// Update transaction ID.
249
			if ( \array_key_exists( Parameters::PAY_ID, $data ) ) {
250
				$payment->set_transaction_id( $data[ Parameters::PAY_ID ] );
251
			}
252
253
			// Add payment note.
254
			$this->update_status_payment_note( $payment, $data );
0 ignored issues
show
Bug introduced by
It seems like $data can also be of type null; however, parameter $data of Pronamic\WordPress\Pay\G...e_status_payment_note() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

254
			$this->update_status_payment_note( $payment, /** @scrutinizer ignore-type */ $data );
Loading history...
255
256
			return;
257
		}
258
259
		// Get order status with direct query.
260
		$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...
261
262
		try {
263
			$status = $this->client->get_order_status( $order_id );
264
		} catch ( \Exception $e ) {
265
			$payment->add_note( $e->getMessage() );
266
267
			return;
268
		}
269
270
		if ( null !== $status ) {
271
			$payment->set_status( $status );
272
		}
273
	}
274
275
	/**
276
	 * Update status payment note
277
	 *
278
	 * @param Payment $payment Payment.
279
	 * @param array   $data    Data.
280
	 */
281
	private function update_status_payment_note( Payment $payment, $data ) {
282
		$labels = array(
283
			'STATUS'     => __( 'Status', 'pronamic_ideal' ),
284
			'ORDERID'    => __( 'Order ID', 'pronamic_ideal' ),
285
			'CURRENCY'   => __( 'Currency', 'pronamic_ideal' ),
286
			'AMOUNT'     => __( 'Amount', 'pronamic_ideal' ),
287
			'PM'         => __( 'Payment Method', 'pronamic_ideal' ),
288
			'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ),
289
			'CARDNO'     => __( 'Card Number', 'pronamic_ideal' ),
290
			'ED'         => __( 'End Date', 'pronamic_ideal' ),
291
			'CN'         => __( 'Customer Name', 'pronamic_ideal' ),
292
			'TRXDATE'    => __( 'Transaction Date', 'pronamic_ideal' ),
293
			'PAYID'      => __( 'Pay ID', 'pronamic_ideal' ),
294
			'NCERROR'    => __( 'NC Error', 'pronamic_ideal' ),
295
			'BRAND'      => __( 'Brand', 'pronamic_ideal' ),
296
			'IP'         => __( 'IP', 'pronamic_ideal' ),
297
			'SHASIGN'    => __( 'SHA Signature', 'pronamic_ideal' ),
298
		);
299
300
		$note = '';
301
302
		$note .= '<p>';
303
		$note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' );
304
		$note .= '</p>';
305
306
		$note .= '<dl>';
307
308
		foreach ( $labels as $key => $label ) {
309
			if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) {
310
				$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
311
				$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
312
			}
313
		}
314
315
		$note .= '</dl>';
316
317
		$payment->add_note( $note );
318
	}
319
}
320