Test Failed
Push — develop ( 7cb291...9cc0c5 )
by Reüel
03:00
created

Gateway::update_status_payment_note()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 27
nc 3
nop 2
dl 0
loc 37
rs 9.488
c 0
b 0
f 0
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-2019 Pronamic
22
 * Company: Pronamic
23
 *
24
 * @author  Remco Tolsma
25
 * @version 2.0.1
26
 * @since   1.0.0
27
 */
28
class Gateway extends Core_Gateway {
29
	/**
30
	 * Slug of this gateway
31
	 *
32
	 * @var string
33
	 */
34
	const SLUG = 'ogone_orderstandard';
35
36
	/**
37
	 * Client.
38
	 *
39
	 * @var Client
40
	 */
41
	protected $client;
42
43
	/**
44
	 * Constructs and initializes an OrderStandard gateway
45
	 *
46
	 * @param Config $config Config.
47
	 */
48
	public function __construct( Config $config ) {
49
		parent::__construct( $config );
50
51
		$this->supports = array(
52
			'payment_status_request',
53
		);
54
55
		$this->set_method( self::METHOD_HTML_FORM );
56
		$this->set_slug( self::SLUG );
57
58
		$this->client = new Client( $this->config->psp_id );
59
60
		$this->client->set_payment_server_url( $config->get_form_action_url() );
61
		$this->client->set_direct_query_url( $config->get_direct_query_url() );
62
		$this->client->set_pass_phrase_in( $config->sha_in_pass_phrase );
63
		$this->client->set_pass_phrase_out( $config->sha_out_pass_phrase );
64
		$this->client->set_user_id( $config->user_id );
65
		$this->client->set_password( $config->password );
66
67
		if ( ! empty( $config->hash_algorithm ) ) {
68
			$this->client->set_hash_algorithm( $config->hash_algorithm );
69
		}
70
	}
71
72
	/**
73
	 * Get supported payment methods
74
	 *
75
	 * @see Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
76
	 */
77
	public function get_supported_payment_methods() {
78
		return array(
79
			Core_PaymentMethods::IDEAL,
80
			Core_PaymentMethods::CREDIT_CARD,
81
			Core_PaymentMethods::BANCONTACT,
82
		);
83
	}
84
85
	/**
86
	 * Start
87
	 *
88
	 * @see Pronamic_WP_Pay_Gateway::start()
89
	 *
90
	 * @param Payment $payment Payment.
91
	 */
92
	public function start( Payment $payment ) {
93
		$payment->set_action_url( $this->client->get_payment_server_url() );
94
95
		$ogone_data = $this->client->get_data();
96
97
		// General.
98
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
99
100
		$ogone_data_general
101
			->set_order_id( $payment->format_string( $this->config->order_id ) )
102
			->set_order_description( $payment->get_description() )
103
			->set_param_plus( 'payment_id=' . $payment->get_id() )
104
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
105
			->set_amount( $payment->get_total_amount()->get_cents() );
106
107
		$customer = $payment->get_customer();
108
109
		if ( null !== $customer ) {
110
			// Localised language.
111
			$ogone_data_general->set_language( $customer->get_locale() );
112
		}
113
114
		// Customer.
115
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
116
117
		if ( null !== $customer ) {
118
			$name = $customer->get_name();
119
120
			if ( null !== $name ) {
121
				$ogone_data_customer->set_name( strval( $name ) );
122
			}
123
124
			$ogone_data_customer->set_email( $customer->get_email() );
125
		}
126
127
		$billing_address = $payment->get_billing_address();
128
129
		if ( null !== $billing_address ) {
130
			$ogone_data_customer
131
				->set_address( $billing_address->get_line_1() )
132
				->set_zip( $billing_address->get_postal_code() )
133
				->set_town( $billing_address->get_city() )
134
				->set_country( $billing_address->get_country_code() )
135
				->set_telephone_number( $billing_address->get_phone() );
136
		}
137
138
		/*
139
		 * Payment method.
140
		 *
141
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
142
		 */
143
		switch ( $payment->get_method() ) {
144
			case Core_PaymentMethods::CREDIT_CARD:
145
				/*
146
				 * Set credit card payment method.
147
				 * @since 1.2.3
148
				 */
149
				$ogone_data_general
150
					->set_payment_method( PaymentMethods::CREDIT_CARD );
151
152
				break;
153
			case Core_PaymentMethods::IDEAL:
154
				/*
155
				 * Set iDEAL payment method.
156
				 * @since 1.2.3
157
				 */
158
				$ogone_data_general
159
					->set_brand( Brands::IDEAL )
160
					->set_payment_method( PaymentMethods::IDEAL );
161
162
				break;
163
			case Core_PaymentMethods::BANCONTACT:
164
			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

164
			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...
165
				$ogone_data_general
166
					->set_brand( Brands::BCMC )
167
					->set_payment_method( PaymentMethods::CREDIT_CARD );
168
169
				break;
170
		}
171
172
		// Parameter Variable.
173
		$param_var = Util::get_param_var( $this->config->param_var );
174
175
		if ( ! empty( $param_var ) ) {
176
			$ogone_data->set_field( 'PARAMVAR', $param_var );
177
		}
178
179
		// Template Page.
180
		$template_page = $this->config->param_var;
181
182
		if ( ! empty( $template_page ) ) {
183
			$ogone_data->set_field( 'TP', $template_page );
184
		}
185
186
		// URLs.
187
		$ogone_url_helper = new DataUrlHelper( $ogone_data );
188
189
		$ogone_url_helper
190
			->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
0 ignored issues
show
Bug introduced by
The function add_query_arg was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

190
			->set_accept_url( /** @scrutinizer ignore-call */ add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
Loading history...
191
			->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) )
192
			->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) )
193
			->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) );
194
	}
195
196
	/**
197
	 * Get output fields
198
	 *
199
	 * @since 1.2.1
200
	 * @see Pronamic_WP_Pay_Gateway::get_output_html()
201
	 */
202
	public function get_output_fields() {
203
		return $this->client->get_fields();
204
	}
205
206
	/**
207
	 * Update status of the specified payment
208
	 *
209
	 * @param Payment $payment Payment.
210
	 */
211
	public function update_status( Payment $payment ) {
212
		$data = Security::get_request_data();
213
214
		$data = $this->client->verify_request( $data );
215
216
		if ( false !== $data ) {
217
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
218
219
			$payment->set_status( $status );
220
221
			$this->update_status_payment_note( $payment, $data );
222
223
			return;
224
		}
225
226
		$order_id = $payment->format_string( $this->config->order_id );
227
228
		$status = $this->client->get_order_status( $order_id );
229
230
		if ( null !== $status ) {
231
			$payment->set_status( $status );
232
		}
233
	}
234
235
	/**
236
	 * Update status payment note
237
	 *
238
	 * @param Payment $payment Payment.
239
	 * @param array   $data    Data.
240
	 */
241
	private function update_status_payment_note( Payment $payment, $data ) {
242
		$labels = array(
243
			'STATUS'     => __( 'Status', 'pronamic_ideal' ),
0 ignored issues
show
Bug introduced by
The function __ was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

243
			'STATUS'     => /** @scrutinizer ignore-call */ __( 'Status', 'pronamic_ideal' ),
Loading history...
244
			'ORDERID'    => __( 'Order ID', 'pronamic_ideal' ),
245
			'CURRENCY'   => __( 'Currency', 'pronamic_ideal' ),
246
			'AMOUNT'     => __( 'Amount', 'pronamic_ideal' ),
247
			'PM'         => __( 'Payment Method', 'pronamic_ideal' ),
248
			'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ),
249
			'CARDNO'     => __( 'Card Number', 'pronamic_ideal' ),
250
			'ED'         => __( 'End Date', 'pronamic_ideal' ),
251
			'CN'         => __( 'Customer Name', 'pronamic_ideal' ),
252
			'TRXDATE'    => __( 'Transaction Date', 'pronamic_ideal' ),
253
			'PAYID'      => __( 'Pay ID', 'pronamic_ideal' ),
254
			'NCERROR'    => __( 'NC Error', 'pronamic_ideal' ),
255
			'BRAND'      => __( 'Brand', 'pronamic_ideal' ),
256
			'IP'         => __( 'IP', 'pronamic_ideal' ),
257
			'SHASIGN'    => __( 'SHA Signature', 'pronamic_ideal' ),
258
		);
259
260
		$note = '';
261
262
		$note .= '<p>';
263
		$note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' );
264
		$note .= '</p>';
265
266
		$note .= '<dl>';
267
268
		foreach ( $labels as $key => $label ) {
269
			if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) {
270
				$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
0 ignored issues
show
Bug introduced by
The function esc_html was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

270
				$note .= sprintf( '<dt>%s</dt>', /** @scrutinizer ignore-call */ esc_html( $label ) );
Loading history...
271
				$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
272
			}
273
		}
274
275
		$note .= '</dl>';
276
277
		$payment->add_note( $note );
278
	}
279
}
280