Failed Conditions
Push — develop ( 2621b9...0c37e9 )
by Reüel
03:00
created

Gateway::get_output_fields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
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
		// Alias.
108
		if ( $this->config->alias_enabled ) {
109
			$alias = uniqid();
110
111
			$payment->set_meta( 'ogone_alias', $alias );
112
113
			$ogone_data_general->set_alias( $alias )
114
				->set_alias_usage( $this->config->alias_usage );
115
		}
116
117
		$customer = $payment->get_customer();
118
119
		if ( null !== $customer ) {
120
			// Localised language.
121
			$ogone_data_general->set_language( $customer->get_locale() );
122
		}
123
124
		// Customer.
125
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
126
127
		if ( null !== $customer ) {
128
			$name = $customer->get_name();
129
130
			if ( null !== $name ) {
131
				$ogone_data_customer->set_name( strval( $name ) );
132
			}
133
134
			$ogone_data_customer->set_email( $customer->get_email() );
135
		}
136
137
		$billing_address = $payment->get_billing_address();
138
139
		if ( null !== $billing_address ) {
140
			$ogone_data_customer
141
				->set_address( $billing_address->get_line_1() )
142
				->set_zip( $billing_address->get_postal_code() )
143
				->set_town( $billing_address->get_city() )
144
				->set_country( $billing_address->get_country_code() )
145
				->set_telephone_number( $billing_address->get_phone() );
146
		}
147
148
		/*
149
		 * Payment method.
150
		 *
151
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
152
		 */
153
		switch ( $payment->get_method() ) {
154
			case Core_PaymentMethods::CREDIT_CARD:
155
				/*
156
				 * Set credit card payment method.
157
				 * @since 1.2.3
158
				 */
159
				$ogone_data_general
160
					->set_payment_method( PaymentMethods::CREDIT_CARD );
161
162
				break;
163
			case Core_PaymentMethods::IDEAL:
164
				/*
165
				 * Set iDEAL payment method.
166
				 * @since 1.2.3
167
				 */
168
				$ogone_data_general
169
					->set_brand( Brands::IDEAL )
170
					->set_payment_method( PaymentMethods::IDEAL );
171
172
				break;
173
			case Core_PaymentMethods::BANCONTACT:
174
			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

174
			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...
175
				$ogone_data_general
176
					->set_brand( Brands::BCMC )
177
					->set_payment_method( PaymentMethods::CREDIT_CARD );
178
179
				break;
180
		}
181
182
		// Parameter Variable.
183
		$param_var = Util::get_param_var( $this->config->param_var );
184
185
		if ( ! empty( $param_var ) ) {
186
			$ogone_data->set_field( 'PARAMVAR', $param_var );
187
		}
188
189
		// Template Page.
190
		$template_page = $this->config->param_var;
191
192
		if ( ! empty( $template_page ) ) {
193
			$ogone_data->set_field( 'TP', $template_page );
194
		}
195
196
		// URLs.
197
		$ogone_url_helper = new DataUrlHelper( $ogone_data );
198
199
		$ogone_url_helper
200
			->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
201
			->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) )
202
			->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) )
203
			->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) );
204
	}
205
206
	/**
207
	 * Get output fields
208
	 *
209
	 * @since 1.2.1
210
	 * @see Pronamic_WP_Pay_Gateway::get_output_html()
211
	 */
212
	public function get_output_fields() {
213
		return $this->client->get_fields();
214
	}
215
216
	/**
217
	 * Update status of the specified payment
218
	 *
219
	 * @param Payment $payment Payment.
220
	 */
221
	public function update_status( Payment $payment ) {
222
		$data = Security::get_request_data();
223
224
		$data = $this->client->verify_request( $data );
225
226
		if ( false !== $data ) {
227
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
228
229
			$payment->set_status( $status );
230
231
			$this->update_status_payment_note( $payment, $data );
232
233
			return;
234
		}
235
236
		$order_id = $payment->format_string( $this->config->order_id );
237
238
		$status = $this->client->get_order_status( $order_id );
239
240
		if ( null !== $status ) {
241
			$payment->set_status( $status );
242
		}
243
	}
244
245
	/**
246
	 * Update status payment note
247
	 *
248
	 * @param Payment $payment Payment.
249
	 * @param array   $data    Data.
250
	 */
251
	private function update_status_payment_note( Payment $payment, $data ) {
252
		$labels = array(
253
			'STATUS'     => __( 'Status', 'pronamic_ideal' ),
254
			'ORDERID'    => __( 'Order ID', 'pronamic_ideal' ),
255
			'CURRENCY'   => __( 'Currency', 'pronamic_ideal' ),
256
			'AMOUNT'     => __( 'Amount', 'pronamic_ideal' ),
257
			'PM'         => __( 'Payment Method', 'pronamic_ideal' ),
258
			'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ),
259
			'CARDNO'     => __( 'Card Number', 'pronamic_ideal' ),
260
			'ED'         => __( 'End Date', 'pronamic_ideal' ),
261
			'CN'         => __( 'Customer Name', 'pronamic_ideal' ),
262
			'TRXDATE'    => __( 'Transaction Date', 'pronamic_ideal' ),
263
			'PAYID'      => __( 'Pay ID', 'pronamic_ideal' ),
264
			'NCERROR'    => __( 'NC Error', 'pronamic_ideal' ),
265
			'BRAND'      => __( 'Brand', 'pronamic_ideal' ),
266
			'IP'         => __( 'IP', 'pronamic_ideal' ),
267
			'SHASIGN'    => __( 'SHA Signature', 'pronamic_ideal' ),
268
		);
269
270
		$note = '';
271
272
		$note .= '<p>';
273
		$note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' );
274
		$note .= '</p>';
275
276
		$note .= '<dl>';
277
278
		foreach ( $labels as $key => $label ) {
279
			if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) {
280
				$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
281
				$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
282
			}
283
		}
284
285
		$note .= '</dl>';
286
287
		$payment->add_note( $note );
288
	}
289
}
290