Gateway   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 91
c 3
b 0
f 0
dl 0
loc 196
ccs 0
cts 103
cp 0
rs 10
wmc 16

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A update_status() 0 16 2
C start() 0 119 11
A payment_redirect() 0 8 2
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\Ingenico\DirectLink;
4
5
use Pronamic\WordPress\Pay\Core\Gateway as Core_Gateway;
6
use Pronamic\WordPress\Pay\Core\Server;
7
use Pronamic\WordPress\Pay\Gateways\Ingenico\Data;
8
use Pronamic\WordPress\Pay\Gateways\Ingenico\DataCreditCardHelper;
9
use Pronamic\WordPress\Pay\Gateways\Ingenico\DataCustomerHelper;
10
use Pronamic\WordPress\Pay\Gateways\Ingenico\DataGeneralHelper;
11
use Pronamic\WordPress\Pay\Gateways\Ingenico\Parameters;
12
use Pronamic\WordPress\Pay\Gateways\Ingenico\SecureDataHelper;
13
use Pronamic\WordPress\Pay\Gateways\Ingenico\Statuses;
14
use Pronamic\WordPress\Pay\Gateways\Ingenico\Security;
15
use Pronamic\WordPress\Pay\Payments\Payment;
16
17
/**
18
 * Title: Ingenico DirectLink gateway
19
 * Description:
20
 * Copyright: 2005-2021 Pronamic
21
 * Company: Pronamic
22
 *
23
 * @author  Remco Tolsma
24
 * @version 2.0.4
25
 * @since   1.0.0
26
 */
27
class Gateway extends Core_Gateway {
28
	/**
29
	 * Client.
30
	 *
31
	 * @var Client
32
	 */
33
	protected $client;
34
35
	/**
36
	 * Constructs and initializes an Ogone DirectLink gateway
37
	 *
38
	 * @param Config $config Config.
39
	 */
40
	public function __construct( Config $config ) {
41
		parent::__construct( $config );
42
43
		$this->set_method( self::METHOD_HTTP_REDIRECT );
44
45
		$this->client           = new Client();
46
		$this->client->psp_id   = $config->psp_id;
47
		$this->client->sha_in   = $config->sha_in_pass_phrase;
48
		$this->client->user_id  = $config->user_id;
49
		$this->client->password = $config->password;
50
		$this->client->api_url  = $config->api_url;
51
	}
52
53
	/**
54
	 * Start
55
	 *
56
	 * @param Payment $payment Payment.
57
	 *
58
	 * @see Core_Gateway::start()
59
	 *
60
	 * @throws \Exception Throws exception if DirectLink request fails.
61
	 */
62
	public function start( Payment $payment ) {
63
		$ogone_data = new Data();
64
65
		// General.
66
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
67
68
		$ogone_data_general
69
			->set_psp_id( $this->client->psp_id )
0 ignored issues
show
Bug introduced by
$this->client->psp_id of type string is incompatible with the type integer expected by parameter $number of Pronamic\WordPress\Pay\G...ralHelper::set_psp_id(). ( Ignorable by Annotation )

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

69
			->set_psp_id( /** @scrutinizer ignore-type */ $this->client->psp_id )
Loading history...
70
			->set_order_id( $payment->format_string( $this->config->order_id ) )
0 ignored issues
show
Bug introduced by
The property order_id does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
71
			->set_order_description( $payment->get_description() )
72
			->set_param_plus( 'payment_id=' . $payment->get_id() )
73
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
74
			->set_amount( $payment->get_total_amount()->get_minor_units()->format( 0, '', '' ) );
75
76
		// Alias.
77
		if ( $this->config->alias_enabled ) {
0 ignored issues
show
Bug introduced by
The property alias_enabled does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
78
			$alias = uniqid();
79
80
			$payment->set_meta( 'ogone_alias', $alias );
81
82
			$ogone_data_general->set_alias( $alias );
83
		}
84
85
		$customer = $payment->get_customer();
86
87
		// Language.
88
		$locale = \get_locale();
89
90
		if ( null !== $customer ) {
91
			$customer_locale = $customer->get_locale();
92
93
			// Locale not always contains `_`, e.g. "Nederlands" in Firefox.
94
			if ( null !== $customer_locale && false !== \strpos( $customer_locale, '_' ) ) {
95
				$locale = $customer_locale;
96
			}
97
		}
98
99
		$ogone_data_general->set_language( $locale );
100
101
		// Customer.
102
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
103
104
		if ( null !== $customer ) {
105
			$name = $customer->get_name();
106
107
			if ( null !== $name ) {
108
				$ogone_data_customer->set_name( strval( $name ) );
109
			}
110
111
			$ogone_data_customer->set_email( $customer->get_email() );
112
		}
113
114
		$billing_address = $payment->get_billing_address();
115
116
		if ( null !== $billing_address ) {
117
			$ogone_data_customer
118
				->set_address( $billing_address->get_line_1() )
119
				->set_zip( $billing_address->get_postal_code() )
120
				->set_town( $billing_address->get_city() )
121
				->set_country( $billing_address->get_country_code() )
122
				->set_telephone_number( $billing_address->get_phone() );
123
		}
124
125
		// DirectLink.
126
		$ogone_data_directlink = new DataHelper( $ogone_data );
127
128
		$ogone_data_directlink
129
			->set_user_id( $this->client->user_id )
130
			->set_password( $this->client->password );
131
132
		// Credit card.
133
		$ogone_data_credit_card = new DataCreditCardHelper( $ogone_data );
134
135
		$credit_card = $payment->get_credit_card();
136
137
		if ( $credit_card ) {
138
			$ogone_data_credit_card
139
				->set_number( $credit_card->get_number() )
0 ignored issues
show
Bug introduced by
$credit_card->get_number() of type string is incompatible with the type integer expected by parameter $number of Pronamic\WordPress\Pay\G...ardHelper::set_number(). ( Ignorable by Annotation )

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

139
				->set_number( /** @scrutinizer ignore-type */ $credit_card->get_number() )
Loading history...
140
				->set_expiration_date( $credit_card->get_expiration_date() )
0 ignored issues
show
Bug introduced by
It seems like $credit_card->get_expiration_date() can also be of type null; however, parameter $date of Pronamic\WordPress\Pay\G...::set_expiration_date() does only seem to accept DateTime, 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

140
				->set_expiration_date( /** @scrutinizer ignore-type */ $credit_card->get_expiration_date() )
Loading history...
141
				->set_security_code( $credit_card->get_security_code() );
142
		}
143
144
		$ogone_data->set_field( 'OPERATION', 'SAL' );
145
146
		// 3-D Secure
147
		if ( $this->config->enabled_3d_secure ) {
0 ignored issues
show
Bug introduced by
The property enabled_3d_secure does not seem to exist on Pronamic\WordPress\Pay\Core\GatewayConfig.
Loading history...
148
			$secure_data_helper = new SecureDataHelper( $ogone_data );
149
150
			$secure_data_helper
151
				->set_3d_secure_flag( true )
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $flag of Pronamic\WordPress\Pay\G...r::set_3d_secure_flag(). ( Ignorable by Annotation )

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

151
				->set_3d_secure_flag( /** @scrutinizer ignore-type */ true )
Loading history...
152
				->set_http_accept( Server::get( 'HTTP_ACCEPT' ) )
153
				->set_http_user_agent( Server::get( 'HTTP_USER_AGENT' ) )
154
				->set_window( 'MAINW' );
155
156
			$ogone_data->set_field( 'ACCEPTURL', $payment->get_return_url() );
157
			$ogone_data->set_field( 'DECLINEURL', $payment->get_return_url() );
158
			$ogone_data->set_field( 'EXCEPTIONURL', $payment->get_return_url() );
159
			$ogone_data->set_field( 'COMPLUS', '' );
160
		}
161
162
		// Signature.
163
		$calculation_fields = Security::get_calculations_parameters_in();
164
165
		$fields = Security::get_calculation_fields( $calculation_fields, $ogone_data->get_fields() );
166
167
		$signature = Security::get_signature( $fields, $this->config->sha_in_pass_phrase, $this->config->hash_algorithm );
168
169
		$ogone_data->set_field( 'SHASIGN', $signature );
170
171
		// Order.
172
		$result = $this->client->order_direct( $ogone_data->get_fields() );
173
174
		$payment->set_transaction_id( $result->pay_id );
175
		$payment->set_action_url( $payment->get_return_url() );
176
		$payment->set_status( Statuses::transform( $result->status ) );
177
178
		if ( ! empty( $result->html_answer ) ) {
179
			$payment->set_meta( 'ogone_directlink_html_answer', $result->html_answer );
0 ignored issues
show
Bug introduced by
The property html_answer does not seem to exist on Pronamic\WordPress\Pay\G...irectLink\OrderResponse.
Loading history...
180
			$payment->set_action_url( $payment->get_pay_redirect_url() );
181
		}
182
	}
183
184
	/**
185
	 * Payment redirect.
186
	 *
187
	 * @param Payment $payment Payment.
188
	 *
189
	 * @return void
190
	 */
191
	public function payment_redirect( Payment $payment ) {
192
		$html_answer = $payment->get_meta( 'ogone_directlink_html_answer' );
193
194
		if ( ! empty( $html_answer ) ) {
195
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
196
			echo $html_answer;
197
198
			exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
199
		}
200
	}
201
202
	/**
203
	 * Update status of the specified payment
204
	 *
205
	 * @param Payment $payment Payment.
206
	 */
207
	public function update_status( Payment $payment ) {
208
		$data = Security::get_request_data();
209
210
		$data = array_change_key_case( $data, CASE_UPPER );
211
212
		$calculation_fields = Security::get_calculations_parameters_out();
213
214
		$fields = Security::get_calculation_fields( $calculation_fields, $data );
215
216
		$signature     = $data['SHASIGN'];
217
		$signature_out = Security::get_signature( $fields, $this->config->sha_out_pass_phrase, $this->config->hash_algorithm );
218
219
		if ( 0 === strcasecmp( $signature, $signature_out ) ) {
220
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
221
222
			$payment->set_status( $status );
223
		}
224
	}
225
}
226