Failed Conditions
Push — develop ( 0669ed...0eccc5 )
by Reüel
05:00
created

src/Client.php (9 issues)

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
19
/**
20
 * Title: MultiSafepay Connect client
21
 * Description:
22
 * Copyright: 2005-2021 Pronamic
23
 * Company: Pronamic
24
 *
25
 * @author  Remco Tolsma
26
 * @version 2.0.5
27
 * @since   1.0.0
28
 */
29
class Client {
30
	/**
31
	 * API URL
32
	 *
33
	 * @var string
34
	 */
35
	public $api_url;
36
37
	/**
38
	 * Constructs and initializes an MultiSafepay Connect client
39
	 */
40 3
	public function __construct() {
41 3
		$this->api_url = MultiSafepay::API_PRODUCTION_URL;
42 3
	}
43
44
	/**
45
	 * Parse XML.
46
	 *
47
	 * @param SimpleXMLElement $xml XML to parse.
48
	 *
49
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
50
	 */
51 3
	private function parse_xml( $xml ) {
52 3
		switch ( $xml->getName() ) {
53 3
			case IDealIssuersRequestMessage::NAME:
54 1
				return IDealIssuersResponseMessage::parse( $xml );
55
56 2
			case GatewaysRequestMessage::NAME:
57 1
				return GatewaysResponseMessage::parse( $xml );
58
59 1
			case DirectTransactionRequestMessage::NAME:
60 1
				return DirectTransactionResponseMessage::parse( $xml );
61
62
			case RedirectTransactionRequestMessage::NAME:
63
				return RedirectTransactionResponseMessage::parse( $xml );
64
65
			case StatusRequestMessage::NAME:
66
				return StatusResponseMessage::parse( $xml );
67
		}
68
69
		return false;
70
	}
71
72
	/**
73
	 * Request.
74
	 *
75
	 * @param string $message Message.
76
	 *
77
	 * @return bool|DirectTransactionResponseMessage|RedirectTransactionResponseMessage|StatusResponseMessage
78
	 */
79 3
	private function request( $message ) {
80 3
		$result = Core_Util::remote_get_body(
81 3
			$this->api_url,
82 3
			200,
83
			array(
84 3
				'method' => 'POST',
85 3
				'body'   => (string) $message,
86
			)
87
		);
88
89 3
		if ( is_wp_error( $result ) ) {
90
			throw new \Exception( $result->get_error_message() );
91
		}
92
93 3
		$xml = Core_Util::simplexml_load_string( $result );
0 ignored issues
show
It seems like $result can also be of type WP_Error; however, parameter $string of Pronamic\WordPress\Pay\C...simplexml_load_string() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

93
		$xml = Core_Util::simplexml_load_string( /** @scrutinizer ignore-type */ $result );
Loading history...
94
95 3
		$return = $this->parse_xml( $xml );
96
97 3
		if ( is_object( $return ) && isset( $return->result ) && 'error' === $return->result ) {
98
			throw new \Exception( $xml->error->description );
99
		}
100
101 3
		return $return;
102
	}
103
104
	/**
105
	 * Get iDEAL issuers
106
	 *
107
	 * @param Merchant $merchant Merchant.
108
	 *
109
	 * @since 1.2.0
110
	 */
111 1
	public function get_ideal_issuers( $merchant ) {
112 1
		$return = false;
113
114 1
		$request = new IDealIssuersRequestMessage( $merchant );
115
116 1
		$response = $this->request( $request );
117
118 1
		if ( $response ) {
119 1
			$return = $response->issuers;
0 ignored issues
show
The property issuers does not seem to exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage.
Loading history...
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...
120
		}
121
122 1
		return $return;
123
	}
124
125
	/**
126
	 * Get gateways.
127
	 *
128
	 * @param Merchant $merchant Merchant.
129
	 * @param Customer $customer Customer.
130
	 *
131
	 * @since 1.2.0
132
	 */
133 1
	public function get_gateways( $merchant, $customer ) {
134 1
		$return = false;
135
136 1
		$request = new GatewaysRequestMessage( $merchant, $customer );
137
138 1
		$response = $this->request( $request );
139
140 1
		if ( $response ) {
141 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 exist on Pronamic\WordPress\Pay\G...nsactionResponseMessage. Did you mean gateway_info?
Loading history...
The property gateways does not seem to exist on Pronamic\WordPress\Pay\G...L\StatusResponseMessage.
Loading history...
142
		}
143
144 1
		return $return;
145
	}
146
147
	/**
148
	 * Start transaction
149
	 *
150
	 * @param array $message Message.
151
	 */
152 1
	public function start_transaction( $message ) {
153 1
		$return = false;
154
155 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...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

155
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
156
157 1
		if ( $response ) {
158 1
			$return = $response;
159
		}
160
161 1
		return $return;
162
	}
163
164
	/**
165
	 * Get status
166
	 *
167
	 * @param array $message Message.
168
	 */
169
	public function get_status( $message ) {
170
		$return = false;
171
172
		$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...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

172
		$response = $this->request( /** @scrutinizer ignore-type */ $message );
Loading history...
173
174
		if ( $response ) {
175
			$return = $response;
176
		}
177
178
		return $return;
179
	}
180
}
181