Failed Conditions
Push — develop ( 755a8b...f93ab4 )
by Reüel
08:34
created

src/Client.php (2 issues)

1
<?php
2
/**
3
 * Client
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2019 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Payments
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Sisow;
12
13
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
14
use Pronamic\WordPress\Pay\Gateways\Sisow\XML\ErrorParser;
15
use Pronamic\WordPress\Pay\Gateways\Sisow\XML\InvoiceParser;
16
use Pronamic\WordPress\Pay\Gateways\Sisow\XML\MerchantParser;
17
use Pronamic\WordPress\Pay\Gateways\Sisow\XML\ReservationParser;
18
use Pronamic\WordPress\Pay\Gateways\Sisow\XML\TransactionParser;
19
use SimpleXMLElement;
20
21
/**
22
 * Title: Sisow
23
 * Description:
24
 * Copyright: 2005-2019 Pronamic
25
 * Company: Pronamic
26
 *
27
 * @author  Remco Tolsma
28
 * @version 2.0.0
29
 * @since   1.0.0
30
 */
31
class Client {
32
	/**
33
	 * Sisow REST API endpoint URL.
34
	 *
35
	 * @var string
36
	 */
37
	const API_URL = 'https://www.sisow.nl/Sisow/iDeal/RestHandler.ashx';
38
39
	/**
40
	 * Sisow merchant ID.
41
	 *
42
	 * @var string
43
	 */
44
	private $merchant_id;
45
46
	/**
47
	 * Sisow merchant key.
48
	 *
49
	 * @var string
50
	 */
51
	private $merchant_key;
52
53
	/**
54
	 * Indicator to use test mode or not.
55
	 *
56
	 * @var boolean
57
	 */
58
	private $test_mode;
59
60
	/**
61
	 * Constructs and initializes a Sisow client object.
62
	 *
63
	 * @param string $merchant_id  Merchant ID.
64
	 * @param string $merchant_key Merchant key.
65
	 */
66
	public function __construct( $merchant_id, $merchant_key ) {
67
		$this->merchant_id  = $merchant_id;
68
		$this->merchant_key = $merchant_key;
69
	}
70
71
	/**
72
	 * Set test mode.
73
	 *
74
	 * @param boolean $test_mode True if test mode, false otherwise.
75
	 */
76
	public function set_test_mode( $test_mode ) {
77
		$this->test_mode = $test_mode;
78
	}
79
80
	/**
81
	 * Send request with the specified action and parameters.
82
	 *
83
	 * @param string       $method  Method.
84
	 * @param Request|null $request Request.
85
	 *
86
	 * @return false|SimpleXMLElement
87
	 */
88
	private function send_request( $method, Request $request = null ) {
89
		$url = self::API_URL . '/' . $method;
90
91
		if ( null !== $request ) {
92
			$request->sign( $this->merchant_key );
93
		}
94
95
		$result = Core_Util::remote_get_body(
96
			$url,
97
			200,
98
			array(
99
				'method' => 'POST',
100
				'body'   => ( null === $request ) ? null : $request->get_parameters(),
101
			)
102
		);
103
104
		if ( ! is_string( $result ) ) {
105
			return false;
106
		}
107
108
		// XML.
109
		$xml = Core_Util::simplexml_load_string( $result );
110
111
		return $xml;
112
	}
113
114
	/**
115
	 * Parse the specified document and return parsed result.
116
	 *
117
	 * @param SimpleXMLElement $document Document.
118
	 *
119
	 * @return Invoice|Merchant|Reservation|Transaction|Error
120
	 */
121
	private function parse_document( SimpleXMLElement $document ) {
122
		$this->error = null;
0 ignored issues
show
Bug Best Practice introduced by
The property error does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
123
124
		$name = $document->getName();
125
126
		switch ( $name ) {
127
			case 'cancelreservationresponse':
128
				$reservation = ReservationParser::parse( $document->reservation );
129
130
				return $reservation;
131
			case 'checkmerchantresponse':
132
				$merchant = MerchantParser::parse( $document->merchant );
133
134
				return $merchant;
135
			case 'errorresponse':
136
				$sisow_error = ErrorParser::parse( $document->error );
137
138
				throw new \Pronamic\WordPress\Pay\GatewayException( 'sisow', $sisow_error->message, $sisow_error );
0 ignored issues
show
The type Pronamic\WordPress\Pay\GatewayException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
139
140
				return $sisow_error;
141
			case 'invoiceresponse':
142
				$invoice = InvoiceParser::parse( $document->invoice );
143
144
				return $invoice;
145
			case 'transactionrequest':
146
				$transaction = TransactionParser::parse( $document->transaction );
147
148
				return $transaction;
149
			case 'statusresponse':
150
				$transaction = TransactionParser::parse( $document->transaction );
151
152
				return $transaction;
153
			default:
154
				throw new \Pronamic\WordPress\Pay\GatewayException(
155
					'sisow',
156
					/* translators: %s: XML document element name */
157
					sprintf( __( 'Unknwon Sisow message (%s)', 'pronamic_ideal' ), $name )
158
				);
159
		}
160
	}
161
162
	/**
163
	 * Get directory.
164
	 *
165
	 * @return array|false
166
	 */
167
	public function get_directory() {
168
		if ( $this->test_mode ) {
169
			return array(
170
				'99' => __( 'Sisow Bank (test)', 'pronamic_ideal' ),
171
			);
172
		}
173
174
		// Request.
175
		$result = $this->send_request( RequestMethods::DIRECTORY_REQUEST );
176
177
		if ( false === $result ) {
178
			return false;
179
		}
180
181
		// Parse.
182
		$directory = array();
183
184
		foreach ( $result->directory->issuer as $issuer ) {
185
			$id   = (string) $issuer->issuerid;
186
			$name = (string) $issuer->issuername;
187
188
			$directory[ $id ] = $name;
189
		}
190
191
		return $directory;
192
	}
193
194
	/**
195
	 * Get merchant.
196
	 *
197
	 * @param MerchantRequest $merchant_request Merchant request.
198
	 *
199
	 * @return Merchant|bool
200
	 */
201
	public function get_merchant( MerchantRequest $merchant_request ) {
202
		// Request.
203
		$response = $this->send_request( RequestMethods::CHECK_MERCHANT_REQUEST, $merchant_request );
204
205
		if ( false === $response ) {
206
			return false;
207
		}
208
209
		// Parse.
210
		$message = $this->parse_document( $response );
211
212
		if ( $message instanceof Merchant ) {
213
			return $message;
214
		}
215
216
		return false;
217
	}
218
219
	/**
220
	 * Create an transaction with the specified parameters.
221
	 *
222
	 * @param TransactionRequest $request Transaction request.
223
	 *
224
	 * @return Transaction|false
225
	 */
226
	public function create_transaction( TransactionRequest $request ) {
227
		// Request.
228
		$response = $this->send_request( RequestMethods::TRANSACTION_REQUEST, $request );
229
230
		if ( false === $response ) {
231
			return false;
232
		}
233
234
		// Parse.
235
		$message = $this->parse_document( $response );
236
237
		if ( $message instanceof Transaction ) {
238
			return $message;
239
		}
240
241
		return false;
242
	}
243
244
	/**
245
	 * Create invoice for reservation payment.
246
	 *
247
	 * @param InvoiceRequest $request Invoice request.
248
	 *
249
	 * @return Invoice|false
250
	 */
251
	public function create_invoice( InvoiceRequest $request ) {
252
		// Request.
253
		$response = $this->send_request( RequestMethods::INVOICE_REQUEST, $request );
254
255
		if ( false === $response ) {
256
			return false;
257
		}
258
259
		// Parse.
260
		$message = $this->parse_document( $response );
261
262
		if ( $message instanceof Invoice ) {
263
			return $message;
264
		}
265
266
		return false;
267
	}
268
269
	/**
270
	 * Cancel reservation payment.
271
	 *
272
	 * @param CancelReservationRequest $request Reservation cancellation request.
273
	 *
274
	 * @return Reservation|false
275
	 */
276
	public function cancel_reservation( CancelReservationRequest $request ) {
277
		$request->set_parameter( 'shopid', null );
278
279
		// Request.
280
		$response = $this->send_request( RequestMethods::CANCEL_RESERVATION_REQUEST, $request );
281
282
		if ( false === $response ) {
283
			return false;
284
		}
285
286
		// Parse.
287
		$message = $this->parse_document( $response );
288
289
		if ( $message instanceof Reservation ) {
290
			return $message;
291
		}
292
293
		return false;
294
	}
295
296
	/**
297
	 * Get the status of the specified transaction ID.
298
	 *
299
	 * @param StatusRequest $request Status request object.
300
	 *
301
	 * @return Transaction|false
302
	 */
303
	public function get_status( StatusRequest $request ) {
304
		// Request.
305
		$response = $this->send_request( RequestMethods::STATUS_REQUEST, $request );
306
307
		if ( false === $response ) {
308
			return false;
309
		}
310
311
		// Parse.
312
		$message = $this->parse_document( $response );
313
314
		if ( $message instanceof Transaction ) {
315
			return $message;
316
		}
317
318
		return false;
319
	}
320
}
321