Failed Conditions
Push — develop ( 0c37e9...a75740 )
by Reüel
05:30
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-2019 Pronamic
21
 * Company: Pronamic
22
 *
23
 * @author  Remco Tolsma
24
 * @version 2.0.1
25
 * @since   1.0.0
26
 */
27
class Gateway extends Core_Gateway {
28
	/**
29
	 * Slug of this gateway
30
	 *
31
	 * @var string
32
	 */
33
	const SLUG = 'ogone-directlink';
34
35
	/**
36
	 * Client.
37
	 *
38
	 * @var Client
39
	 */
40
	protected $client;
41
42
	/**
43
	 * Constructs and initializes an Ogone DirectLink gateway
44
	 *
45
	 * @param Config $config Config.
46
	 */
47
	public function __construct( Config $config ) {
48
		parent::__construct( $config );
49
50
		$this->set_method( self::METHOD_HTTP_REDIRECT );
51
		$this->set_slug( self::SLUG );
52
53
		$this->client           = new Client();
54
		$this->client->psp_id   = $config->psp_id;
55
		$this->client->sha_in   = $config->sha_in_pass_phrase;
56
		$this->client->user_id  = $config->user_id;
57
		$this->client->password = $config->password;
58
		$this->client->api_url  = $config->api_url;
59
	}
60
61
	/**
62
	 * Start
63
	 *
64
	 * @see Pronamic_WP_Pay_Gateway::start()
65
	 *
66
	 * @param Payment $payment Payment.
67
	 */
68
	public function start( Payment $payment ) {
69
		$ogone_data = new Data();
70
71
		// General.
72
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
73
74
		$ogone_data_general
75
			->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

75
			->set_psp_id( /** @scrutinizer ignore-type */ $this->client->psp_id )
Loading history...
76
			->set_order_id( $payment->format_string( $this->config->order_id ) )
77
			->set_order_description( $payment->get_description() )
78
			->set_param_plus( 'payment_id=' . $payment->get_id() )
79
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
80
			->set_amount( $payment->get_total_amount()->get_cents() );
81
82
		// Alias.
83
		if ( $this->config->alias_enabled ) {
84
			$alias = uniqid();
85
86
			$payment->set_meta( 'ogone_alias', $alias );
87
88
			$ogone_data_general->set_alias( $alias );
89
		}
90
91
		$customer = $payment->get_customer();
92
93
		if ( null !== $customer ) {
94
			// Localised language.
95
			$ogone_data_general->set_language( $customer->get_locale() );
96
		}
97
98
		// Customer.
99
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
100
101
		if ( null !== $customer ) {
102
			$name = $customer->get_name();
103
104
			if ( null !== $name ) {
105
				$ogone_data_customer->set_name( strval( $name ) );
106
			}
107
108
			$ogone_data_customer->set_email( $customer->get_email() );
109
		}
110
111
		$billing_address = $payment->get_billing_address();
112
113
		if ( null !== $billing_address ) {
114
			$ogone_data_customer
115
				->set_address( $billing_address->get_line_1() )
116
				->set_zip( $billing_address->get_postal_code() )
117
				->set_town( $billing_address->get_city() )
118
				->set_country( $billing_address->get_country_code() )
119
				->set_telephone_number( $billing_address->get_phone() );
120
		}
121
122
		// DirectLink.
123
		$ogone_data_directlink = new DataHelper( $ogone_data );
124
125
		$ogone_data_directlink
126
			->set_user_id( $this->client->user_id )
127
			->set_password( $this->client->password );
128
129
		// Credit card.
130
		$ogone_data_credit_card = new DataCreditCardHelper( $ogone_data );
131
132
		$credit_card = $payment->get_credit_card();
133
134
		if ( $credit_card ) {
135
			$ogone_data_credit_card
136
				->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

136
				->set_number( /** @scrutinizer ignore-type */ $credit_card->get_number() )
Loading history...
137
				->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

137
				->set_expiration_date( /** @scrutinizer ignore-type */ $credit_card->get_expiration_date() )
Loading history...
138
				->set_security_code( $credit_card->get_security_code() );
139
		}
140
141
		$ogone_data->set_field( 'OPERATION', 'SAL' );
142
143
		// 3-D Secure
144
		if ( $this->config->enabled_3d_secure ) {
145
			$secure_data_helper = new SecureDataHelper( $ogone_data );
146
147
			$secure_data_helper
148
				->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

148
				->set_3d_secure_flag( /** @scrutinizer ignore-type */ true )
Loading history...
149
				->set_http_accept( Server::get( 'HTTP_ACCEPT' ) )
150
				->set_http_user_agent( Server::get( 'HTTP_USER_AGENT' ) )
151
				->set_window( 'MAINW' );
152
153
			$ogone_data->set_field( 'ACCEPTURL', $payment->get_return_url() );
154
			$ogone_data->set_field( 'DECLINEURL', $payment->get_return_url() );
155
			$ogone_data->set_field( 'EXCEPTIONURL', $payment->get_return_url() );
156
			$ogone_data->set_field( 'COMPLUS', '' );
157
		}
158
159
		// Signature.
160
		$calculation_fields = Security::get_calculations_parameters_in();
161
162
		$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

162
		$fields = Security::get_calculation_fields( /** @scrutinizer ignore-type */ $calculation_fields, $ogone_data->get_fields() );
Loading history...
163
164
		$signature = Security::get_signature( $fields, $this->config->sha_in_pass_phrase, $this->config->hash_algorithm );
165
166
		$ogone_data->set_field( 'SHASIGN', $signature );
167
168
		// Order.
169
		$result = $this->client->order_direct( $ogone_data->get_fields() );
170
171
		$error = $this->client->get_error();
172
173
		if ( is_wp_error( $error ) ) {
174
			$this->error = $error;
175
		} else {
176
			$payment->set_transaction_id( $result->pay_id );
177
			$payment->set_action_url( $payment->get_return_url() );
178
			$payment->set_status( Statuses::transform( $result->status ) );
179
180
			if ( ! empty( $result->html_answer ) ) {
181
				$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...
182
				$payment->set_action_url( $payment->get_pay_redirect_url() );
183
			}
184
		}
185
	}
186
187
	/**
188
	 * Update status of the specified payment
189
	 *
190
	 * @param Payment $payment Payment.
191
	 */
192
	public function update_status( Payment $payment ) {
193
		$data = Security::get_request_data();
194
195
		$data = array_change_key_case( $data, CASE_UPPER );
196
197
		$calculation_fields = Security::get_calculations_parameters_out();
198
199
		$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

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