Failed Conditions
Push — develop ( 384879...89d365 )
by Reüel
05:41
created

Gateway::start()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 6
rs 10
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
	 * 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 Pronamic_WP_Pay_Gateway::get_supported_payment_methods()
69
	 */
70
	public function get_supported_payment_methods() {
71
		return array(
72
			Core_PaymentMethods::IDEAL,
73
			Core_PaymentMethods::CREDIT_CARD,
74
			Core_PaymentMethods::BANCONTACT,
75
		);
76
	}
77
78
	/**
79
	 * Start
80
	 *
81
	 * @see Pronamic_WP_Pay_Gateway::start()
82
	 *
83
	 * @param Payment $payment Payment.
84
	 */
85
	public function start( Payment $payment ) {
86
		$payment->set_action_url( $this->client->get_payment_server_url() );
87
88
		if ( $this->config->alias_enabled ) {
89
			$alias = uniqid();
90
91
			$payment->set_meta( 'ogone_alias', $alias );
92
		}
93
	}
94
95
	/**
96
	 * Get output fields
97
	 *
98
	 * @param Payment $payment Payment.
99
	 *
100
	 * @return array
101
	 * @since   1.2.1
102
	 * @version 2.0.4
103
	 * @see     Core_Gateway::get_output_html()
104
	 */
105
	public function get_output_fields( Payment $payment ) {
106
		$ogone_data = $this->client->get_data();
107
108
		// General.
109
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
110
111
		$ogone_data_general
112
			->set_order_id( $payment->format_string( $this->config->order_id ) )
113
			->set_order_description( $payment->get_description() )
114
			->set_param_plus( 'payment_id=' . $payment->get_id() )
115
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
116
			->set_amount( $payment->get_total_amount()->get_minor_units() );
117
118
		// Alias.
119
		if ( $this->config->alias_enabled ) {
120
			$ogone_data_general->set_alias( $payment->get_meta( 'ogone_alias' ) )
0 ignored issues
show
Bug introduced by
It seems like $payment->get_meta('ogone_alias') can also be of type false; however, parameter $alias of Pronamic\WordPress\Pay\G...eralHelper::set_alias() does only seem to accept string, 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

120
			$ogone_data_general->set_alias( /** @scrutinizer ignore-type */ $payment->get_meta( 'ogone_alias' ) )
Loading history...
121
			                   ->set_alias_usage( $this->config->alias_usage );
122
		}
123
124
		$customer = $payment->get_customer();
125
126
		if ( null !== $customer ) {
127
			// Localised language.
128
			$ogone_data_general->set_language( $customer->get_locale() );
129
		}
130
131
		// Customer.
132
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
133
134
		if ( null !== $customer ) {
135
			$name = $customer->get_name();
136
137
			if ( null !== $name ) {
138
				$ogone_data_customer->set_name( strval( $name ) );
139
			}
140
141
			$ogone_data_customer->set_email( $customer->get_email() );
142
		}
143
144
		$billing_address = $payment->get_billing_address();
145
146
		if ( null !== $billing_address ) {
147
			$ogone_data_customer
148
				->set_address( $billing_address->get_line_1() )
149
				->set_zip( $billing_address->get_postal_code() )
150
				->set_town( $billing_address->get_city() )
151
				->set_country( $billing_address->get_country_code() )
152
				->set_telephone_number( $billing_address->get_phone() );
153
		}
154
155
		/*
156
		 * Payment method.
157
		 *
158
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
159
		 */
160
		switch ( $payment->get_method() ) {
161
			case Core_PaymentMethods::CREDIT_CARD:
162
				/*
163
				 * Set credit card payment method.
164
				 * @since 1.2.3
165
				 */
166
				$ogone_data_general
167
					->set_payment_method( PaymentMethods::CREDIT_CARD );
168
169
				break;
170
			case Core_PaymentMethods::IDEAL:
171
				/*
172
				 * Set iDEAL payment method.
173
				 * @since 1.2.3
174
				 */
175
				$ogone_data_general
176
					->set_brand( Brands::IDEAL )
177
					->set_payment_method( PaymentMethods::IDEAL );
178
179
				break;
180
			case Core_PaymentMethods::BANCONTACT:
181
			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

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