Failed Conditions
Push — develop ( af8aa2...7450c3 )
by Reüel
17:39
created

src/OrderStandard/Gateway.php (8 issues)

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 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 ) {
0 ignored issues
show
The property alias_enabled does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
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 ) )
0 ignored issues
show
The property order_id does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
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
		$alias = $payment->get_meta( 'ogone_alias' );
120
121
		if ( $this->config->alias_enabled && false !== $alias ) {
0 ignored issues
show
The property alias_enabled does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
122
			$ogone_data_general->set_alias( $alias )
123
								->set_alias_usage( $this->config->alias_usage );
0 ignored issues
show
The property alias_usage does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
124
		}
125
126
		$customer = $payment->get_customer();
127
128
		if ( null !== $customer ) {
129
			// Localised language.
130
			$ogone_data_general->set_language( $customer->get_locale() );
131
		}
132
133
		// Customer.
134
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
135
136
		if ( null !== $customer ) {
137
			$name = $customer->get_name();
138
139
			if ( null !== $name ) {
140
				$ogone_data_customer->set_name( strval( $name ) );
141
			}
142
143
			$ogone_data_customer->set_email( $customer->get_email() );
144
		}
145
146
		$billing_address = $payment->get_billing_address();
147
148
		if ( null !== $billing_address ) {
149
			$ogone_data_customer
150
				->set_address( $billing_address->get_line_1() )
151
				->set_zip( $billing_address->get_postal_code() )
152
				->set_town( $billing_address->get_city() )
153
				->set_country( $billing_address->get_country_code() )
154
				->set_telephone_number( $billing_address->get_phone() );
155
		}
156
157
		/*
158
		 * Payment method.
159
		 *
160
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
161
		 */
162
		switch ( $payment->get_method() ) {
163
			case Core_PaymentMethods::CREDIT_CARD:
164
				/*
165
				 * Set credit card payment method.
166
				 * @since 1.2.3
167
				 */
168
				$ogone_data_general
169
					->set_payment_method( PaymentMethods::CREDIT_CARD );
170
171
				break;
172
			case Core_PaymentMethods::IDEAL:
173
				/*
174
				 * Set iDEAL payment method.
175
				 * @since 1.2.3
176
				 */
177
				$ogone_data_general
178
					->set_brand( Brands::IDEAL )
179
					->set_payment_method( PaymentMethods::IDEAL );
180
181
				break;
182
			case Core_PaymentMethods::BANCONTACT:
183
			case Core_PaymentMethods::MISTER_CASH:
184
				$ogone_data_general
185
					->set_brand( Brands::BCMC )
186
					->set_payment_method( PaymentMethods::CREDIT_CARD );
187
188
				break;
189
		}
190
191
		// Parameter Variable.
192
		$param_var = Util::get_param_var( $this->config->param_var );
0 ignored issues
show
The property param_var does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
193
194
		if ( ! empty( $param_var ) ) {
195
			$ogone_data->set_field( 'PARAMVAR', $param_var );
196
		}
197
198
		// Template Page.
199
		$template_page = $this->config->template_page;
0 ignored issues
show
The property template_page does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
200
201
		if ( ! empty( $template_page ) ) {
202
			$ogone_data->set_field( 'TP', $template_page );
203
		}
204
205
		// URLs.
206
		$ogone_url_helper = new DataUrlHelper( $ogone_data );
207
208
		$ogone_url_helper
209
			->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
210
			->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) )
211
			->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) )
212
			->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) );
213
214
		return $this->client->get_fields();
215
	}
216
217
	/**
218
	 * Update status of the specified payment
219
	 *
220
	 * @param Payment $payment Payment.
221
	 */
222
	public function update_status( Payment $payment ) {
223
		$data = Security::get_request_data();
224
225
		$data = $this->client->verify_request( $data );
226
227
		if ( false !== $data ) {
228
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
229
230
			$payment->set_status( $status );
231
232
			$this->update_status_payment_note( $payment, $data );
0 ignored issues
show
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

232
			$this->update_status_payment_note( $payment, /** @scrutinizer ignore-type */ $data );
Loading history...
233
234
			return;
235
		}
236
237
		// Get order status with direct query.
238
		$order_id = $payment->format_string( $this->config->order_id );
0 ignored issues
show
The property order_id does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
239
240
		try {
241
			$status = $this->client->get_order_status( $order_id );
242
		} catch ( \Exception $e ) {
243
			$payment->add_note( $e->getMessage() );
244
245
			return;
246
		}
247
248
		if ( null !== $status ) {
249
			$payment->set_status( $status );
250
		}
251
	}
252
253
	/**
254
	 * Update status payment note
255
	 *
256
	 * @param Payment $payment Payment.
257
	 * @param array   $data    Data.
258
	 */
259
	private function update_status_payment_note( Payment $payment, $data ) {
260
		$labels = array(
261
			'STATUS'     => __( 'Status', 'pronamic_ideal' ),
262
			'ORDERID'    => __( 'Order ID', 'pronamic_ideal' ),
263
			'CURRENCY'   => __( 'Currency', 'pronamic_ideal' ),
264
			'AMOUNT'     => __( 'Amount', 'pronamic_ideal' ),
265
			'PM'         => __( 'Payment Method', 'pronamic_ideal' ),
266
			'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ),
267
			'CARDNO'     => __( 'Card Number', 'pronamic_ideal' ),
268
			'ED'         => __( 'End Date', 'pronamic_ideal' ),
269
			'CN'         => __( 'Customer Name', 'pronamic_ideal' ),
270
			'TRXDATE'    => __( 'Transaction Date', 'pronamic_ideal' ),
271
			'PAYID'      => __( 'Pay ID', 'pronamic_ideal' ),
272
			'NCERROR'    => __( 'NC Error', 'pronamic_ideal' ),
273
			'BRAND'      => __( 'Brand', 'pronamic_ideal' ),
274
			'IP'         => __( 'IP', 'pronamic_ideal' ),
275
			'SHASIGN'    => __( 'SHA Signature', 'pronamic_ideal' ),
276
		);
277
278
		$note = '';
279
280
		$note .= '<p>';
281
		$note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' );
282
		$note .= '</p>';
283
284
		$note .= '<dl>';
285
286
		foreach ( $labels as $key => $label ) {
287
			if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) {
288
				$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
289
				$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
290
			}
291
		}
292
293
		$note .= '</dl>';
294
295
		$payment->add_note( $note );
296
	}
297
}
298