Failed Conditions
Push — develop ( ba56c1...8ad4eb )
by Reüel
07:31
created

src/DirectLink/Gateway.php (7 issues)

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-2020 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 Pronamic_WP_Pay_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
$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 ) )
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_cents() );
75
76
		// Alias.
77
		if ( $this->config->alias_enabled ) {
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
		if ( null !== $customer ) {
88
			// Localised language.
89
			$ogone_data_general->set_language( $customer->get_locale() );
90
		}
91
92
		// Customer.
93
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
94
95
		if ( null !== $customer ) {
96
			$name = $customer->get_name();
97
98
			if ( null !== $name ) {
99
				$ogone_data_customer->set_name( strval( $name ) );
100
			}
101
102
			$ogone_data_customer->set_email( $customer->get_email() );
103
		}
104
105
		$billing_address = $payment->get_billing_address();
106
107
		if ( null !== $billing_address ) {
108
			$ogone_data_customer
109
				->set_address( $billing_address->get_line_1() )
110
				->set_zip( $billing_address->get_postal_code() )
111
				->set_town( $billing_address->get_city() )
112
				->set_country( $billing_address->get_country_code() )
113
				->set_telephone_number( $billing_address->get_phone() );
114
		}
115
116
		// DirectLink.
117
		$ogone_data_directlink = new DataHelper( $ogone_data );
118
119
		$ogone_data_directlink
120
			->set_user_id( $this->client->user_id )
121
			->set_password( $this->client->password );
122
123
		// Credit card.
124
		$ogone_data_credit_card = new DataCreditCardHelper( $ogone_data );
125
126
		$credit_card = $payment->get_credit_card();
127
128
		if ( $credit_card ) {
129
			$ogone_data_credit_card
130
				->set_number( $credit_card->get_number() )
0 ignored issues
show
$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

130
				->set_number( /** @scrutinizer ignore-type */ $credit_card->get_number() )
Loading history...
131
				->set_expiration_date( $credit_card->get_expiration_date() )
0 ignored issues
show
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

131
				->set_expiration_date( /** @scrutinizer ignore-type */ $credit_card->get_expiration_date() )
Loading history...
132
				->set_security_code( $credit_card->get_security_code() );
133
		}
134
135
		$ogone_data->set_field( 'OPERATION', 'SAL' );
136
137
		// 3-D Secure
138
		if ( $this->config->enabled_3d_secure ) {
139
			$secure_data_helper = new SecureDataHelper( $ogone_data );
140
141
			$secure_data_helper
142
				->set_3d_secure_flag( true )
0 ignored issues
show
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

142
				->set_3d_secure_flag( /** @scrutinizer ignore-type */ true )
Loading history...
143
				->set_http_accept( Server::get( 'HTTP_ACCEPT' ) )
144
				->set_http_user_agent( Server::get( 'HTTP_USER_AGENT' ) )
145
				->set_window( 'MAINW' );
146
147
			$ogone_data->set_field( 'ACCEPTURL', $payment->get_return_url() );
148
			$ogone_data->set_field( 'DECLINEURL', $payment->get_return_url() );
149
			$ogone_data->set_field( 'EXCEPTIONURL', $payment->get_return_url() );
150
			$ogone_data->set_field( 'COMPLUS', '' );
151
		}
152
153
		// Signature.
154
		$calculation_fields = Security::get_calculations_parameters_in();
155
156
		$fields = Security::get_calculation_fields( $calculation_fields, $ogone_data->get_fields() );
0 ignored issues
show
It seems like $calculation_fields can also be of type false; however, parameter $calculation_fields of Pronamic\WordPress\Pay\G...et_calculation_fields() 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

156
		$fields = Security::get_calculation_fields( /** @scrutinizer ignore-type */ $calculation_fields, $ogone_data->get_fields() );
Loading history...
157
158
		$signature = Security::get_signature( $fields, $this->config->sha_in_pass_phrase, $this->config->hash_algorithm );
159
160
		$ogone_data->set_field( 'SHASIGN', $signature );
161
162
		// Order.
163
		$result = $this->client->order_direct( $ogone_data->get_fields() );
164
165
		$payment->set_transaction_id( $result->pay_id );
166
		$payment->set_action_url( $payment->get_return_url() );
167
		$payment->set_status( Statuses::transform( $result->status ) );
168
169
		if ( ! empty( $result->html_answer ) ) {
170
			$payment->set_meta( 'ogone_directlink_html_answer', $result->html_answer );
0 ignored issues
show
The property html_answer does not seem to exist on Pronamic\WordPress\Pay\G...irectLink\OrderResponse.
Loading history...
171
			$payment->set_action_url( $payment->get_pay_redirect_url() );
172
		}
173
	}
174
175
	/**
176
	 * Payment redirect.
177
	 *
178
	 * @param Payment $payment Payment.
179
	 *
180
	 * @return void
181
	 */
182
	public function payment_redirect( Payment $payment ) {
183
		$html_answer = $payment->get_meta( 'ogone_directlink_html_answer' );
184
185
		if ( ! empty( $html_answer ) ) {
186
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
187
			echo $html_answer;
188
189
			exit;
190
		}
191
	}
192
193
	/**
194
	 * Update status of the specified payment
195
	 *
196
	 * @param Payment $payment Payment.
197
	 */
198
	public function update_status( Payment $payment ) {
199
		$data = Security::get_request_data();
200
201
		$data = array_change_key_case( $data, CASE_UPPER );
202
203
		$calculation_fields = Security::get_calculations_parameters_out();
204
205
		$fields = Security::get_calculation_fields( $calculation_fields, $data );
0 ignored issues
show
It seems like $calculation_fields can also be of type false; however, parameter $calculation_fields of Pronamic\WordPress\Pay\G...et_calculation_fields() 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

205
		$fields = Security::get_calculation_fields( /** @scrutinizer ignore-type */ $calculation_fields, $data );
Loading history...
206
207
		$signature     = $data['SHASIGN'];
208
		$signature_out = Security::get_signature( $fields, $this->config->sha_out_pass_phrase, $this->config->hash_algorithm );
209
210
		if ( 0 === strcasecmp( $signature, $signature_out ) ) {
211
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
212
213
			$payment->set_status( $status );
214
		}
215
	}
216
}
217