Failed Conditions
Push — develop ( aa4863...9d10aa )
by Reüel
05:05
created

src/Client.php (10 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\TargetPay;
4
5
use Pronamic\WordPress\Pay\Core\Util;
6
use Pronamic\WordPress\Pay\Core\XML\Security;
7
use stdClass;
8
use WP_Error;
9
10
/**
11
 * Title: TargetPay gateway
12
 * Description:
13
 * Copyright: 2005-2019 Pronamic
14
 * Company: Pronamic
15
 *
16
 * @author  Remco Tolsma
17
 * @version 2.0.0
18
 * @since   1.0.0
19
 */
20
class Client {
21
	/**
22
	 * URL for issuers in Dutch language
23
	 *
24
	 * @var string
25
	 */
26
	const URL_ISSUERS_NL = 'https://www.targetpay.com/ideal/issuers-nl.js';
27
28
	/**
29
	 * URL for issuers in English language
30
	 *
31
	 * @var string
32
	 */
33
	const URL_ISSUERS_EN = 'https://www.targetpay.com/ideal/issuers-en.js';
34
35
	/**
36
	 * URL for retrieving issuers in HTL format
37
	 *
38
	 * @var string
39
	 */
40
	const URL_ISSUERS_HTML = 'https://www.targetpay.com/ideal/getissuers.php?format=html';
41
42
	/**
43
	 * URL for retrieving issuers in XML format
44
	 *
45
	 * @var string
46
	 */
47
	const URL_ISSUERS_XML = 'https://www.targetpay.com/ideal/getissuers.php?format=xml';
48
49
	/**
50
	 * URL to start an transaction
51
	 *
52
	 * @var string
53
	 */
54
	const URL_START_TRANSACTION = 'https://www.targetpay.com/ideal/start';
55
56
	/**
57
	 * URL to check an transaction
58
	 *
59
	 * @var string
60
	 */
61
	const URL_CHECK_TRANSACTION = 'https://www.targetpay.com/ideal/check';
62
63
	/**
64
	 * Token used by TargetPay to separate some values
65
	 *
66
	 * @var string
67
	 */
68
	const TOKEN = ' |';
69
70
	/**
71
	 * Status indicator for 'Ok'
72
	 *
73
	 * @var string
74
	 */
75
	const STATUS_OK = '000000';
76
77
	/**
78
	 * Status indicator for 'No layout code'
79
	 *
80
	 * @var string
81
	 */
82
	const STATUS_NO_LAYOUT_CODE = 'TP0001';
83
84
	/**
85
	 * Error
86
	 *
87
	 * @var WP_Error
88
	 */
89
	private $error;
90
91
	/**
92
	 * Constructs and initializes an TargetPay client object
93
	 */
94
	public function __construct() {
95
96
	}
97
98
	/**
99
	 * Get error.
100
	 *
101
	 * @return WP_Error
102
	 */
103
	public function get_error() {
104
		return $this->error;
105
	}
106
107
	/**
108
	 * Remote get.
109
	 *
110
	 * @param string $url URL for GET request.
111
	 *
112
	 * @return string|WP_Error
113
	 */
114
	private function remote_get( $url ) {
115
		return Util::remote_get_body( $url, 200 );
116
	}
117
118
	/**
119
	 * Start transaction
120
	 *
121
	 * @param IDealStartParameters $parameters Start parameters.
122
	 *
123
	 * @return stdClass
124
	 */
125
	public function start_transaction( IDealStartParameters $parameters ) {
126
		$url = Util::build_url( self::URL_START_TRANSACTION, $parameters->get_array() );
127
128
		$data = self::remote_get( $url );
0 ignored issues
show
Bug Best Practice introduced by
The method Pronamic\WordPress\Pay\G...ay\Client::remote_get() is not static, but was called statically. ( Ignorable by Annotation )

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

128
		/** @scrutinizer ignore-call */ 
129
  $data = self::remote_get( $url );
Loading history...
129
130
		if ( false !== $data ) {
131
			$status = strtok( $data, self::TOKEN );
0 ignored issues
show
$data of type WP_Error is incompatible with the type string expected by parameter $str of strtok(). ( Ignorable by Annotation )

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

131
			$status = strtok( /** @scrutinizer ignore-type */ $data, self::TOKEN );
Loading history...
132
133
			if ( self::STATUS_OK === $status ) {
134
				$result = new stdClass();
135
136
				$result->status         = $status;
137
				$result->transaction_id = strtok( self::TOKEN );
138
				$result->url            = strtok( self::TOKEN );
139
140
				return $result;
141
			} else {
142
				$code        = $status;
143
				$description = substr( $data, 7 );
0 ignored issues
show
$data of type WP_Error is incompatible with the type string expected by parameter $string of substr(). ( Ignorable by Annotation )

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

143
				$description = substr( /** @scrutinizer ignore-type */ $data, 7 );
Loading history...
144
145
				$error = new Error( $code, $description );
146
147
				$this->error = new WP_Error( 'targetpay_error', (string) $error, $error );
148
			}
149
		}
150
	}
151
152
	/**
153
	 * Check status
154
	 *
155
	 * @param string $rtlo
156
	 * @param string $transaction_id
157
	 * @param string $once
158
	 * @param string $test
159
	 *
160
	 * @return null|Status
161
	 */
162
	public function check_status( $rtlo, $transaction_id, $once, $test ) {
163
		$result = null;
164
165
		$url = Util::build_url(
166
			self::URL_CHECK_TRANSACTION,
167
			array(
168
				'rtlo'  => $rtlo,
169
				'trxid' => $transaction_id,
170
				'once'  => Util::boolean_to_numeric( $once ),
0 ignored issues
show
$once of type string is incompatible with the type boolean expected by parameter $boolean of Pronamic\WordPress\Pay\C...l::boolean_to_numeric(). ( Ignorable by Annotation )

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

170
				'once'  => Util::boolean_to_numeric( /** @scrutinizer ignore-type */ $once ),
Loading history...
171
				'test'  => Util::boolean_to_numeric( $test ),
172
			)
173
		);
174
175
		$data = self::remote_get( $url );
0 ignored issues
show
Bug Best Practice introduced by
The method Pronamic\WordPress\Pay\G...ay\Client::remote_get() is not static, but was called statically. ( Ignorable by Annotation )

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

175
		/** @scrutinizer ignore-call */ 
176
  $data = self::remote_get( $url );
Loading history...
176
177
		if ( false !== $data ) {
178
			$result = StatusStringParser::parse( $data );
0 ignored issues
show
$data of type WP_Error is incompatible with the type string expected by parameter $string of Pronamic\WordPress\Pay\G...usStringParser::parse(). ( Ignorable by Annotation )

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

178
			$result = StatusStringParser::parse( /** @scrutinizer ignore-type */ $data );
Loading history...
179
		}
180
181
		return $result;
182
	}
183
184
	/**
185
	 * Get issuers
186
	 *
187
	 * @return array
188
	 */
189
	public function get_issuers() {
190
		$issuers = false;
191
192
		$url = self::URL_ISSUERS_XML;
193
194
		$data = self::remote_get( $url );
0 ignored issues
show
Bug Best Practice introduced by
The method Pronamic\WordPress\Pay\G...ay\Client::remote_get() is not static, but was called statically. ( Ignorable by Annotation )

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

194
		/** @scrutinizer ignore-call */ 
195
  $data = self::remote_get( $url );
Loading history...
195
196
		if ( false !== $data ) {
197
			$xml = Util::simplexml_load_string( $data );
0 ignored issues
show
$data of type WP_Error is incompatible with the type string expected by parameter $string of Pronamic\WordPress\Pay\C...simplexml_load_string(). ( Ignorable by Annotation )

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

197
			$xml = Util::simplexml_load_string( /** @scrutinizer ignore-type */ $data );
Loading history...
198
199
			if ( is_wp_error( $xml ) ) {
200
				$this->error = $xml;
0 ignored issues
show
Documentation Bug introduced by
It seems like $xml of type SimpleXMLElement is incompatible with the declared type WP_Error of property $error.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
201
			} else {
202
				$issuers = array();
203
204
				foreach ( $xml->issuer as $xml_issuer ) {
205
					$id   = Security::filter( $xml_issuer['id'] );
206
					$name = Security::filter( $xml_issuer );
207
208
					$issuers[ $id ] = $name;
209
				}
210
			}
211
		}
212
213
		return $issuers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $issuers could also return false which is incompatible with the documented return type array. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
214
	}
215
}
216