Test Failed
Push — develop ( fd928b...40893b )
by Remco
04:14
created

Client::get_ideal_issuers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay;
4
5
use Pronamic\WordPress\Pay\Core\Util as Core_Util;
6
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\MultiSafepay;
7
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionRequestMessage;
8
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\DirectTransactionResponseMessage;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\GatewaysRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\GatewaysResponseMessage;
11
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\IDealIssuersRequestMessage;
12
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\IDealIssuersResponseMessage;
13
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionRequestMessage;
14
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\RedirectTransactionResponseMessage;
15
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusRequestMessage;
16
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\XML\StatusResponseMessage;
17
use SimpleXMLElement;
18
use WP_Error;
19
20
/**
21
 * Title: MultiSafepay Connect client
22
 * Description:
23
 * Copyright: 2005-2019 Pronamic
24
 * Company: Pronamic
25
 *
26
 * @author  Remco Tolsma
27
 * @version 2.0.2
28
 * @since   1.0.0
29
 */
30
class Client {
31
	/**
32
	 * Error
33
	 *
34
	 * @var WP_Error
35
	 */
36
	private $error;
37
38
	/**
39
	 * API URL
40
	 *
41
	 * @var string
42
	 */
43
	public $api_url;
44
45
	/**
46
	 * Constructs and initializes an MultiSafepay Connect client
47
	 */
48
	public function __construct() {
49
		$this->api_url = MultiSafepay::API_PRODUCTION_URL;
50
	}
51
52
	/**
53
	 * Get error
54
	 *
55
	 * @return WP_Error
56
	 */
57
	public function get_error() {
58
		return $this->error;
59
	}
60
61
	/**
62
	 * Parse XML.
63
	 *
64
	 * @param SimpleXMLElement $xml XML to parse.
65
	 *
66
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
67
	 */
68
	private function parse_xml( $xml ) {
69
		switch ( $xml->getName() ) {
70
			case IDealIssuersRequestMessage::NAME:
71
				return IDealIssuersResponseMessage::parse( $xml );
72
73
			case GatewaysRequestMessage::NAME:
74
				return GatewaysResponseMessage::parse( $xml );
75
76
			case DirectTransactionRequestMessage::NAME:
77
				return DirectTransactionResponseMessage::parse( $xml );
78
79
			case RedirectTransactionRequestMessage::NAME:
80
				return RedirectTransactionResponseMessage::parse( $xml );
81
82
			case StatusRequestMessage::NAME:
83
				return StatusResponseMessage::parse( $xml );
84
		}
85
86
		return false;
87
	}
88
89
	/**
90
	 * Request.
91
	 *
92
	 * @param string $message Message.
93
	 *
94
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
95
	 */
96
	private function request( $message ) {
97
		$return = false;
98
99
		$result = Core_Util::remote_get_body(
100
			$this->api_url,
101
			200,
102
			array(
103
				'method' => 'POST',
104
				'body'   => (string) $message,
105
			)
106
		);
107
108
		if ( is_wp_error( $result ) ) {
109
			$this->error = $result;
110
111
			return false;
112
		}
113
114
		$xml = Core_Util::simplexml_load_string( $result );
115
116
		if ( is_wp_error( $xml ) ) {
117
			$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...
118
		} else {
119
			$return = $this->parse_xml( $xml );
120
121
			if ( is_object( $return ) && isset( $return->result ) && 'error' === $return->result ) {
122
				$this->error = new WP_Error( 'multisafepay_error', $xml->error->description, $xml->error );
123
				$return      = false;
124
			}
125
		}
126
127
		return $return;
128
	}
129
130
	/**
131
	 * Get iDEAL issuers
132
	 *
133
	 * @param Merchant $merchant Merchant.
134
	 *
135
	 * @since 1.2.0
136
	 */
137
	public function get_ideal_issuers( $merchant ) {
138
		$return = false;
139
140
		$request = new IDealIssuersRequestMessage( $merchant );
141
142
		$response = $this->request( $request );
143
144
		if ( $response ) {
145
			$return = $response->issuers;
0 ignored issues
show
Bug introduced by
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
The property issuers does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean issuer_id?
Loading history...
Bug introduced by
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
146
		}
147
148
		return $return;
149
	}
150
151
	/**
152
	 * Get gateways.
153
	 *
154
	 * @param Merchant $merchant Merchant.
155
	 * @param Customer $customer Customer.
156
	 *
157
	 * @since 1.2.0
158
	 */
159
	public function get_gateways( $merchant, $customer ) {
160
		$return = false;
161
162
		$request = new GatewaysRequestMessage( $merchant, $customer );
163
164
		$response = $this->request( $request );
165
166
		if ( $response ) {
167
			$return = $response->gateways;
0 ignored issues
show
Bug introduced by
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
Bug introduced by
The property gateways does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean gateway_info?
Loading history...
Bug introduced by
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
168
		}
169
170
		return $return;
171
	}
172
173
	/**
174
	 * Start transaction
175
	 *
176
	 * @param array $message Message.
177
	 */
178
	public function start_transaction( $message ) {
179
		$return = false;
180
181
		$response = $this->request( $message );
0 ignored issues
show
Bug introduced by
$message of type array is incompatible with the type string expected by parameter $message of Pronamic\WordPress\Pay\G...fepay\Client::request(). ( Ignorable by Annotation )

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

181
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
182
183
		if ( $response ) {
184
			$return = $response;
185
		}
186
187
		return $return;
188
	}
189
190
	/**
191
	 * Get status
192
	 *
193
	 * @param array $message Message.
194
	 */
195
	public function get_status( $message ) {
196
		$return = false;
197
198
		$response = $this->request( $message );
0 ignored issues
show
Bug introduced by
$message of type array is incompatible with the type string expected by parameter $message of Pronamic\WordPress\Pay\G...fepay\Client::request(). ( Ignorable by Annotation )

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

198
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
199
200
		if ( $response ) {
201
			$return = $response;
202
		}
203
204
		return $return;
205
	}
206
}
207