Failed Conditions
Push — develop ( 9cc0c5...5590ec )
by Reüel
03:35
created

Gateway::update_status()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 1
dl 0
loc 16
ccs 0
cts 10
cp 0
crap 6
rs 9.9666
c 0
b 0
f 0
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
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

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

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

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

139
				->set_3d_secure_flag( /** @scrutinizer ignore-type */ true )
Loading history...
140
				->set_http_accept( Server::get( 'HTTP_ACCEPT' ) )
141
				->set_http_user_agent( Server::get( 'HTTP_USER_AGENT' ) )
142
				->set_window( 'MAINW' );
143
144
			$ogone_data->set_field( 'ACCEPTURL', $payment->get_return_url() );
145
			$ogone_data->set_field( 'DECLINEURL', $payment->get_return_url() );
146
			$ogone_data->set_field( 'EXCEPTIONURL', $payment->get_return_url() );
147
			$ogone_data->set_field( 'COMPLUS', '' );
148
		}
149
150
		// Signature.
151
		$calculation_fields = Security::get_calculations_parameters_in();
152
153
		$fields = Security::get_calculation_fields( $calculation_fields, $ogone_data->get_fields() );
154
155
		$signature = Security::get_signature( $fields, $this->config->sha_in_pass_phrase, $this->config->hash_algorithm );
156
157
		$ogone_data->set_field( 'SHASIGN', $signature );
158
159
		// Order.
160
		$result = $this->client->order_direct( $ogone_data->get_fields() );
161
162
		$error = $this->client->get_error();
163
164
		if ( is_wp_error( $error ) ) {
165
			$this->error = $error;
166
		} else {
167
			$payment->set_transaction_id( $result->pay_id );
168
			$payment->set_action_url( $payment->get_return_url() );
169
			$payment->set_status( Statuses::transform( $result->status ) );
170
171
			if ( ! empty( $result->html_answer ) ) {
172
				$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...
173
				$payment->set_action_url( add_query_arg( 'payment_redirect', $payment->get_id(), home_url( '/' ) ) );
174
			}
175
		}
176
	}
177
178
	/**
179
	 * Update status of the specified payment
180
	 *
181
	 * @param Payment $payment Payment.
182
	 */
183
	public function update_status( Payment $payment ) {
184
		$data = Security::get_request_data();
185
186
		$data = array_change_key_case( $data, CASE_UPPER );
187
188
		$calculation_fields = Security::get_calculations_parameters_out();
189
190
		$fields = Security::get_calculation_fields( $calculation_fields, $data );
191
192
		$signature     = $data['SHASIGN'];
193
		$signature_out = Security::get_signature( $fields, $this->config->sha_out_pass_phrase, $this->config->hash_algorithm );
194
195
		if ( 0 === strcasecmp( $signature, $signature_out ) ) {
196
			$status = Statuses::transform( $data[ Parameters::STATUS ] );
197
198
			$payment->set_status( $status );
199
		}
200
	}
201
}
202