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

src/DirectLink/Gateway.php (2 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.2
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
	 * @see Pronamic_WP_Pay_Gateway::start()
57
	 *
58
	 * @param Payment $payment Payment.
59
	 */
60
	public function start( Payment $payment ) {
61
		$ogone_data = new Data();
62
63
		// General.
64
		$ogone_data_general = new DataGeneralHelper( $ogone_data );
65
66
		$ogone_data_general
67
			->set_psp_id( $this->client->psp_id )
68
			->set_order_id( $payment->format_string( $this->config->order_id ) )
69
			->set_order_description( $payment->get_description() )
70
			->set_param_plus( 'payment_id=' . $payment->get_id() )
71
			->set_currency( $payment->get_total_amount()->get_currency()->get_alphabetic_code() )
72
			->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

72
			->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...
73
74
		// Alias.
75
		if ( $this->config->alias_enabled ) {
76
			$alias = uniqid();
77
78
			$payment->set_meta( 'ogone_alias', $alias );
79
80
			$ogone_data_general->set_alias( $alias );
81
		}
82
83
		$customer = $payment->get_customer();
84
85
		if ( null !== $customer ) {
86
			// Localised language.
87
			$ogone_data_general->set_language( $customer->get_locale() );
88
		}
89
90
		// Customer.
91
		$ogone_data_customer = new DataCustomerHelper( $ogone_data );
92
93
		if ( null !== $customer ) {
94
			$name = $customer->get_name();
95
96
			if ( null !== $name ) {
97
				$ogone_data_customer->set_name( strval( $name ) );
98
			}
99
100
			$ogone_data_customer->set_email( $customer->get_email() );
101
		}
102
103
		$billing_address = $payment->get_billing_address();
104
105
		if ( null !== $billing_address ) {
106
			$ogone_data_customer
107
				->set_address( $billing_address->get_line_1() )
108
				->set_zip( $billing_address->get_postal_code() )
109
				->set_town( $billing_address->get_city() )
110
				->set_country( $billing_address->get_country_code() )
111
				->set_telephone_number( $billing_address->get_phone() );
112
		}
113
114
		// DirectLink.
115
		$ogone_data_directlink = new DataHelper( $ogone_data );
116
117
		$ogone_data_directlink
118
			->set_user_id( $this->client->user_id )
119
			->set_password( $this->client->password );
120
121
		// Credit card.
122
		$ogone_data_credit_card = new DataCreditCardHelper( $ogone_data );
123
124
		$credit_card = $payment->get_credit_card();
125
126
		if ( $credit_card ) {
127
			$ogone_data_credit_card
128
				->set_number( $credit_card->get_number() )
129
				->set_expiration_date( $credit_card->get_expiration_date() )
130
				->set_security_code( $credit_card->get_security_code() );
131
		}
132
133
		$ogone_data->set_field( 'OPERATION', 'SAL' );
134
135
		// 3-D Secure
136
		if ( $this->config->enabled_3d_secure ) {
137
			$secure_data_helper = new SecureDataHelper( $ogone_data );
138
139
			$secure_data_helper
140
				->set_3d_secure_flag( true )
141
				->set_http_accept( Server::get( 'HTTP_ACCEPT' ) )
142
				->set_http_user_agent( Server::get( 'HTTP_USER_AGENT' ) )
143
				->set_window( 'MAINW' );
144
145
			$ogone_data->set_field( 'ACCEPTURL', $payment->get_return_url() );
146
			$ogone_data->set_field( 'DECLINEURL', $payment->get_return_url() );
147
			$ogone_data->set_field( 'EXCEPTIONURL', $payment->get_return_url() );
148
			$ogone_data->set_field( 'COMPLUS', '' );
149
		}
150
151
		// Signature.
152
		$calculation_fields = Security::get_calculations_parameters_in();
153
154
		$fields = Security::get_calculation_fields( $calculation_fields, $ogone_data->get_fields() );
155
156
		$signature = Security::get_signature( $fields, $this->config->sha_in_pass_phrase, $this->config->hash_algorithm );
157
158
		$ogone_data->set_field( 'SHASIGN', $signature );
159
160
		// Order.
161
		$result = $this->client->order_direct( $ogone_data->get_fields() );
162
163
		$error = $this->client->get_error();
164
165
		if ( is_wp_error( $error ) ) {
166
			$this->error = $error;
167
		} else {
168
			$payment->set_transaction_id( $result->pay_id );
169
			$payment->set_action_url( $payment->get_return_url() );
170
			$payment->set_status( Statuses::transform( $result->status ) );
171
172
			if ( ! empty( $result->html_answer ) ) {
173
				$payment->set_meta( 'ogone_directlink_html_answer', $result->html_answer );
174
				$payment->set_action_url( $payment->get_pay_redirect_url() );
175
			}
176
		}
177
	}
178
179
	/**
180
	 * Payment redirect.
181
	 *
182
	 * @param Payment $payment Payment.
183
	 *
184
	 * @return void
185
	 */
186
	public function payment_redirect( Payment $payment ) {
187
		$html_answer = $payment->get_meta( 'ogone_directlink_html_answer' );
188
189
		if ( ! empty( $html_answer ) ) {
190
			// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
191
			echo $html_answer;
192
193
			exit;
0 ignored issues
show
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...
194
		}
195
	}
196
197
	/**
198
	 * Update status of the specified payment
199
	 *
200
	 * @param Payment $payment Payment.
201
	 */
202
	public function update_status( Payment $payment ) {
203
		$data = Security::get_request_data();
204
205
		$data = array_change_key_case( $data, CASE_UPPER );
206
207
		$calculation_fields = Security::get_calculations_parameters_out();
208
209
		$fields = Security::get_calculation_fields( $calculation_fields, $data );
210
211
		$signature     = $data['SHASIGN'];
212
		$signature_out = Security::get_signature( $fields, $this->config->sha_out_pass_phrase, $this->config->hash_algorithm );
213
214
		if ( 0 === strcasecmp( $signature, $signature_out ) ) {
215
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
216
217
			$payment->set_status( $status );
218
		}
219
	}
220
}
221