Test Failed
Push — master ( 12f600...60caf1 )
by Remco
20:03 queued 11:56
created

src/OrderStandard/Gateway.php (1 issue)

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
		$ogone_data = $this->client->get_data();
89
90
		// General.
91
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
92
93
		$ogone_data_general
94
			->set_order_id( $payment->format_string( $this->config->order_id ) )
95
			->set_order_description( $payment->get_description() )
96
			->set_param_plus( 'payment_id=' . $payment->get_id() )
97
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
98
			->set_amount( $payment->get_total_amount()->get_cents() );
0 ignored issues
show
Deprecated Code introduced by
The function Pronamic\WordPress\Money\Money::get_cents() has been deprecated: 1.2.2 Use `Money::get_minor_units()` instead. ( Ignorable by Annotation )

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

98
			->set_amount( /** @scrutinizer ignore-deprecated */ $payment->get_total_amount()->get_cents() );

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
99
100
		// Alias.
101
		if ( $this->config->alias_enabled ) {
102
			$alias = uniqid();
103
104
			$payment->set_meta( 'ogone_alias', $alias );
105
106
			$ogone_data_general->set_alias( $alias )
107
				->set_alias_usage( $this->config->alias_usage );
108
		}
109
110
		$customer = $payment->get_customer();
111
112
		if ( null !== $customer ) {
113
			// Localised language.
114
			$ogone_data_general->set_language( $customer->get_locale() );
115
		}
116
117
		// Customer.
118
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
119
120
		if ( null !== $customer ) {
121
			$name = $customer->get_name();
122
123
			if ( null !== $name ) {
124
				$ogone_data_customer->set_name( strval( $name ) );
125
			}
126
127
			$ogone_data_customer->set_email( $customer->get_email() );
128
		}
129
130
		$billing_address = $payment->get_billing_address();
131
132
		if ( null !== $billing_address ) {
133
			$ogone_data_customer
134
				->set_address( $billing_address->get_line_1() )
135
				->set_zip( $billing_address->get_postal_code() )
136
				->set_town( $billing_address->get_city() )
137
				->set_country( $billing_address->get_country_code() )
138
				->set_telephone_number( $billing_address->get_phone() );
139
		}
140
141
		/*
142
		 * Payment method.
143
		 *
144
		 * @link https://github.com/wp-pay-gateways/ogone/wiki/Brands
145
		 */
146
		switch ( $payment->get_method() ) {
147
			case Core_PaymentMethods::CREDIT_CARD:
148
				/*
149
				 * Set credit card payment method.
150
				 * @since 1.2.3
151
				 */
152
				$ogone_data_general
153
					->set_payment_method( PaymentMethods::CREDIT_CARD );
154
155
				break;
156
			case Core_PaymentMethods::IDEAL:
157
				/*
158
				 * Set iDEAL payment method.
159
				 * @since 1.2.3
160
				 */
161
				$ogone_data_general
162
					->set_brand( Brands::IDEAL )
163
					->set_payment_method( PaymentMethods::IDEAL );
164
165
				break;
166
			case Core_PaymentMethods::BANCONTACT:
167
			case Core_PaymentMethods::MISTER_CASH:
168
				$ogone_data_general
169
					->set_brand( Brands::BCMC )
170
					->set_payment_method( PaymentMethods::CREDIT_CARD );
171
172
				break;
173
		}
174
175
		// Parameter Variable.
176
		$param_var = Util::get_param_var( $this->config->param_var );
177
178
		if ( ! empty( $param_var ) ) {
179
			$ogone_data->set_field( 'PARAMVAR', $param_var );
180
		}
181
182
		// Template Page.
183
		$template_page = $this->config->template_page;
184
185
		if ( ! empty( $template_page ) ) {
186
			$ogone_data->set_field( 'TP', $template_page );
187
		}
188
189
		// URLs.
190
		$ogone_url_helper = new DataUrlHelper( $ogone_data );
191
192
		$ogone_url_helper
193
			->set_accept_url( add_query_arg( 'status', 'accept', $payment->get_return_url() ) )
194
			->set_cancel_url( add_query_arg( 'status', 'cancel', $payment->get_return_url() ) )
195
			->set_decline_url( add_query_arg( 'status', 'decline', $payment->get_return_url() ) )
196
			->set_exception_url( add_query_arg( 'status', 'exception', $payment->get_return_url() ) );
197
	}
198
199
	/**
200
	 * Get output fields
201
	 *
202
	 * @since 1.2.1
203
	 * @see Pronamic_WP_Pay_Gateway::get_output_html()
204
	 */
205
	public function get_output_fields() {
206
		return $this->client->get_fields();
207
	}
208
209
	/**
210
	 * Update status of the specified payment
211
	 *
212
	 * @param Payment $payment Payment.
213
	 */
214
	public function update_status( Payment $payment ) {
215
		$data = Security::get_request_data();
216
217
		$data = $this->client->verify_request( $data );
218
219
		if ( false !== $data ) {
220
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
221
222
			$payment->set_status( $status );
223
224
			$this->update_status_payment_note( $payment, $data );
225
226
			return;
227
		}
228
229
		$order_id = $payment->format_string( $this->config->order_id );
230
231
		$status = $this->client->get_order_status( $order_id );
232
233
		if ( null !== $status ) {
234
			$payment->set_status( $status );
235
		}
236
	}
237
238
	/**
239
	 * Update status payment note
240
	 *
241
	 * @param Payment $payment Payment.
242
	 * @param array   $data    Data.
243
	 */
244
	private function update_status_payment_note( Payment $payment, $data ) {
245
		$labels = array(
246
			'STATUS'     => __( 'Status', 'pronamic_ideal' ),
247
			'ORDERID'    => __( 'Order ID', 'pronamic_ideal' ),
248
			'CURRENCY'   => __( 'Currency', 'pronamic_ideal' ),
249
			'AMOUNT'     => __( 'Amount', 'pronamic_ideal' ),
250
			'PM'         => __( 'Payment Method', 'pronamic_ideal' ),
251
			'ACCEPTANCE' => __( 'Acceptance', 'pronamic_ideal' ),
252
			'CARDNO'     => __( 'Card Number', 'pronamic_ideal' ),
253
			'ED'         => __( 'End Date', 'pronamic_ideal' ),
254
			'CN'         => __( 'Customer Name', 'pronamic_ideal' ),
255
			'TRXDATE'    => __( 'Transaction Date', 'pronamic_ideal' ),
256
			'PAYID'      => __( 'Pay ID', 'pronamic_ideal' ),
257
			'NCERROR'    => __( 'NC Error', 'pronamic_ideal' ),
258
			'BRAND'      => __( 'Brand', 'pronamic_ideal' ),
259
			'IP'         => __( 'IP', 'pronamic_ideal' ),
260
			'SHASIGN'    => __( 'SHA Signature', 'pronamic_ideal' ),
261
		);
262
263
		$note = '';
264
265
		$note .= '<p>';
266
		$note .= __( 'Ogone transaction data in response message:', 'pronamic_ideal' );
267
		$note .= '</p>';
268
269
		$note .= '<dl>';
270
271
		foreach ( $labels as $key => $label ) {
272
			if ( isset( $data[ $key ] ) && '' !== $data[ $key ] ) {
273
				$note .= sprintf( '<dt>%s</dt>', esc_html( $label ) );
274
				$note .= sprintf( '<dd>%s</dd>', esc_html( $data[ $key ] ) );
275
			}
276
		}
277
278
		$note .= '</dl>';
279
280
		$payment->add_note( $note );
281
	}
282
}
283