Failed Conditions
Push — develop ( 1dfc49...e4f983 )
by Remco
04:49
created

src/Client.php (6 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
9
/**
10
 * Title: TargetPay gateway
11
 * Description:
12
 * Copyright: 2005-2021 Pronamic
13
 * Company: Pronamic
14
 *
15
 * @author  Remco Tolsma
16
 * @version 2.0.3
17
 * @since   1.0.0
18
 */
19
class Client {
20
	/**
21
	 * URL for issuers in Dutch language
22
	 *
23
	 * @var string
24
	 */
25
	const URL_ISSUERS_NL = 'https://www.targetpay.com/ideal/issuers-nl.js';
26
27
	/**
28
	 * URL for issuers in English language
29
	 *
30
	 * @var string
31
	 */
32
	const URL_ISSUERS_EN = 'https://www.targetpay.com/ideal/issuers-en.js';
33
34
	/**
35
	 * URL for retrieving issuers in HTL format
36
	 *
37
	 * @var string
38
	 */
39
	const URL_ISSUERS_HTML = 'https://www.targetpay.com/ideal/getissuers.php?format=html';
40
41
	/**
42
	 * URL for retrieving issuers in XML format
43
	 *
44
	 * @var string
45
	 */
46
	const URL_ISSUERS_XML = 'https://www.targetpay.com/ideal/getissuers.php?format=xml';
47
48
	/**
49
	 * URL to start an transaction
50
	 *
51
	 * @var string
52
	 */
53
	const URL_START_TRANSACTION = 'https://www.targetpay.com/ideal/start';
54
55
	/**
56
	 * URL to check an transaction
57
	 *
58
	 * @var string
59
	 */
60
	const URL_CHECK_TRANSACTION = 'https://www.targetpay.com/ideal/check';
61
62
	/**
63
	 * Token used by TargetPay to separate some values
64
	 *
65
	 * @var string
66
	 */
67
	const TOKEN = ' |';
68
69
	/**
70
	 * Status indicator for 'Ok'
71
	 *
72
	 * @var string
73
	 */
74
	const STATUS_OK = '000000';
75
76
	/**
77
	 * Status indicator for 'No layout code'
78
	 *
79
	 * @var string
80
	 */
81
	const STATUS_NO_LAYOUT_CODE = 'TP0001';
82
83
	/**
84
	 * Constructs and initializes an TargetPay client object
85
	 */
86
	public function __construct() {
87
88
	}
89
90
	/**
91
	 * Remote get.
92
	 *
93
	 * @param string $url URL for GET request.
94
	 *
95
	 * @return string
96
	 */
97
	private function remote_get( $url ) {
98
		return Util::remote_get_body( $url, 200 );
0 ignored issues
show
Bug Best Practice introduced by
The expression return Pronamic\WordPres...ote_get_body($url, 200) also could return the type WP_Error which is incompatible with the documented return type string.
Loading history...
99
	}
100
101
	/**
102
	 * Start transaction
103
	 *
104
	 * @param IDealStartParameters $parameters Start parameters.
105
	 *
106
	 * @return stdClass
107
	 */
108
	public function start_transaction( IDealStartParameters $parameters ) {
109
		$url = Util::build_url( self::URL_START_TRANSACTION, $parameters->get_array() );
110
111
		$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

111
		/** @scrutinizer ignore-call */ 
112
  $data = self::remote_get( $url );
Loading history...
112
113
		if ( false !== $data ) {
114
			$status = strtok( $data, self::TOKEN );
115
116
			if ( self::STATUS_OK === $status ) {
117
				$result = new stdClass();
118
119
				$result->status         = $status;
120
				$result->transaction_id = strtok( self::TOKEN );
121
				$result->url            = strtok( self::TOKEN );
122
123
				return $result;
124
			} else {
125
				$code        = $status;
126
				$description = substr( $data, 7 );
127
128
				$error = new Error( $code, $description );
129
130
				throw new \Exception( 'targetpay', (string) $error );
0 ignored issues
show
(string)$error of type string is incompatible with the type integer expected by parameter $code of Exception::__construct(). ( Ignorable by Annotation )

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

130
				throw new \Exception( 'targetpay', /** @scrutinizer ignore-type */ (string) $error );
Loading history...
131
			}
132
		}
133
	}
134
135
	/**
136
	 * Check status
137
	 *
138
	 * @param string $rtlo
139
	 * @param string $transaction_id
140
	 * @param string $once
141
	 * @param string $test
142
	 *
143
	 * @return null|Status
144
	 */
145
	public function check_status( $rtlo, $transaction_id, $once, $test ) {
146
		$result = null;
147
148
		$url = Util::build_url(
149
			self::URL_CHECK_TRANSACTION,
150
			array(
151
				'rtlo'  => $rtlo,
152
				'trxid' => $transaction_id,
153
				'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

153
				'once'  => Util::boolean_to_numeric( /** @scrutinizer ignore-type */ $once ),
Loading history...
154
				'test'  => Util::boolean_to_numeric( $test ),
155
			)
156
		);
157
158
		$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

158
		/** @scrutinizer ignore-call */ 
159
  $data = self::remote_get( $url );
Loading history...
159
160
		if ( false !== $data ) {
161
			$result = StatusStringParser::parse( $data );
162
		}
163
164
		return $result;
165
	}
166
167
	/**
168
	 * Get issuers
169
	 *
170
	 * @return array
171
	 */
172
	public function get_issuers() {
173
		$issuers = false;
174
175
		$url = self::URL_ISSUERS_XML;
176
177
		$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

177
		/** @scrutinizer ignore-call */ 
178
  $data = self::remote_get( $url );
Loading history...
178
179
		if ( false !== $data ) {
180
			$xml = Util::simplexml_load_string( $data );
181
182
			$issuers = array();
183
184
			foreach ( $xml->issuer as $xml_issuer ) {
185
				$id   = Security::filter( $xml_issuer['id'] );
186
				$name = Security::filter( $xml_issuer );
187
188
				$issuers[ $id ] = $name;
189
			}
190
		}
191
192
		return $issuers;
193
	}
194
}
195