Failed Conditions
Push — develop ( 349acb...3045e9 )
by Reüel
03:04
created

src/Connect/Client.php (9 issues)

1
<?php
2
3
namespace Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect;
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\Connect\XML\DirectTransactionRequestMessage;
8
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\DirectTransactionResponseMessage;
9
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\GatewaysRequestMessage;
10
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\GatewaysResponseMessage;
11
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\IDealIssuersRequestMessage;
12
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\IDealIssuersResponseMessage;
13
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\RedirectTransactionRequestMessage;
14
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\RedirectTransactionResponseMessage;
15
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\XML\StatusRequestMessage;
16
use Pronamic\WordPress\Pay\Gateways\MultiSafepay\Connect\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 3
	public function __construct() {
49 3
		$this->api_url = MultiSafepay::API_PRODUCTION_URL;
50 3
	}
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 3
	private function parse_xml( $xml ) {
69 3
		switch ( $xml->getName() ) {
70 3
			case IDealIssuersRequestMessage::NAME:
71 1
				return IDealIssuersResponseMessage::parse( $xml );
72
73 2
			case GatewaysRequestMessage::NAME:
74 1
				return GatewaysResponseMessage::parse( $xml );
75
76 1
			case DirectTransactionRequestMessage::NAME:
77 1
				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 3
	private function request( $message ) {
97 3
		$return = false;
98
99 3
		$result = Core_Util::remote_get_body(
100 3
			$this->api_url,
101 3
			200,
102
			array(
103 3
				'method' => 'POST',
104 3
				'body'   => (string) $message,
105
			)
106
		);
107
108 3
		if ( is_wp_error( $result ) ) {
109
			$this->error = $result;
110
111
			return false;
112
		}
113
114 3
		$xml = Core_Util::simplexml_load_string( $result );
115
116 3
		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 3
			$return = $this->parse_xml( $xml );
120
121 3
			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 3
		return $return;
128
	}
129
130
	/**
131
	 * Get iDEAL issuers
132
	 *
133
	 * @param Merchant $merchant Merchant.
134
	 *
135
	 * @since 1.2.0
136
	 */
137 1
	public function get_ideal_issuers( $merchant ) {
138 1
		$return = false;
139
140 1
		$request = new IDealIssuersRequestMessage( $merchant );
141
142 1
		$response = $this->request( $request );
143
144 1
		if ( $response ) {
145 1
			$return = $response->issuers;
0 ignored issues
show
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
The property issuers does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean issuer_id?
Loading history...
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
146
		}
147
148 1
		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 1
	public function get_gateways( $merchant, $customer ) {
160 1
		$return = false;
161
162 1
		$request = new GatewaysRequestMessage( $merchant, $customer );
163
164 1
		$response = $this->request( $request );
165
166 1
		if ( $response ) {
167 1
			$return = $response->gateways;
0 ignored issues
show
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
The property gateways does not exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean gateway_info?
Loading history...
168
		}
169
170 1
		return $return;
171
	}
172
173
	/**
174
	 * Start transaction
175
	 *
176
	 * @param array $message Message.
177
	 */
178 1
	public function start_transaction( $message ) {
179 1
		$return = false;
180
181 1
		$response = $this->request( $message );
0 ignored issues
show
$message of type array is incompatible with the type string expected by parameter $message of Pronamic\WordPress\Pay\G...nnect\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 1
		if ( $response ) {
184 1
			$return = $response;
185
		}
186
187 1
		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
$message of type array is incompatible with the type string expected by parameter $message of Pronamic\WordPress\Pay\G...nnect\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