|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Payvision client |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2020 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\Payvision |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Payvision; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Payvision client |
|
15
|
|
|
* |
|
16
|
|
|
* @link https://github.com/payvisionpayments/php/blob/master/generatepaymentform.php |
|
17
|
|
|
* @author Remco Tolsma |
|
18
|
|
|
* @version 1.0.5 |
|
19
|
|
|
* @since 1.0.0 |
|
20
|
|
|
*/ |
|
21
|
|
|
class Client { |
|
22
|
|
|
/** |
|
23
|
|
|
* Config. |
|
24
|
|
|
* |
|
25
|
|
|
* @var Config |
|
26
|
|
|
*/ |
|
27
|
|
|
private $config; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Constructs and initializes an Payvision client object. |
|
31
|
|
|
* |
|
32
|
|
|
* @param Config $config Payvision config. |
|
33
|
|
|
*/ |
|
34
|
9 |
|
public function __construct( Config $config ) { |
|
35
|
9 |
|
$this->config = $config; |
|
36
|
9 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Send request with the specified action and parameters |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $method Payvision API method. |
|
42
|
|
|
* @param string $path Path. |
|
43
|
|
|
* @param object|string[]|string|false $request Request object. |
|
44
|
|
|
* @return object |
|
45
|
|
|
* @throws \Exception Throws exception when error occurs. |
|
46
|
|
|
*/ |
|
47
|
6 |
|
public function send_request( $method, $path, $request = null ) { |
|
48
|
|
|
// Request. |
|
49
|
6 |
|
$authorization = 'Basic ' . \base64_encode( $this->config->get_username() . ':' . $this->config->get_password() ); |
|
50
|
|
|
|
|
51
|
6 |
|
$response = \wp_remote_request( |
|
52
|
6 |
|
$this->config->get_endpoint_url( $path ), |
|
53
|
|
|
array( |
|
54
|
6 |
|
'method' => $method, |
|
55
|
|
|
'headers' => array( |
|
56
|
6 |
|
'Authorization' => $authorization, |
|
57
|
6 |
|
'Content-Type' => 'application/json', |
|
58
|
|
|
), |
|
59
|
6 |
|
'body' => $request, |
|
60
|
|
|
) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
6 |
|
if ( $response instanceof \WP_Error ) { |
|
64
|
1 |
|
throw new \Exception( $response->get_error_message() ); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
// Body. |
|
68
|
5 |
|
$body = \wp_remote_retrieve_body( $response ); |
|
69
|
|
|
|
|
70
|
|
|
// Response. |
|
71
|
5 |
|
$response_code = \wp_remote_retrieve_response_code( $response ); |
|
72
|
|
|
|
|
73
|
5 |
|
$response_message = \wp_remote_retrieve_response_message( $response ); |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* On PHP 7 or higher the `json_decode` function will return `null` and |
|
77
|
|
|
* `json_last_error` will return `4` (Syntax error). On PHP 5.6 or lower |
|
78
|
|
|
* the `json_decode` will also return `null`, but json_last_error` will |
|
79
|
|
|
* return `0` (No error). Therefore we check if the HTTP response body |
|
80
|
|
|
* is an empty string. |
|
81
|
|
|
* |
|
82
|
|
|
* @link https://3v4l.org/ |
|
83
|
|
|
*/ |
|
84
|
5 |
|
if ( '' === $body ) { |
|
85
|
1 |
|
throw new \Exception( |
|
86
|
1 |
|
\sprintf( |
|
87
|
1 |
|
'Payvision response is empty, HTTP response: "%s %s".', |
|
88
|
1 |
|
$response_code, |
|
89
|
1 |
|
$response_message |
|
90
|
|
|
) |
|
91
|
|
|
); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// JSON. |
|
95
|
4 |
|
$data = \json_decode( $body ); |
|
96
|
|
|
|
|
97
|
|
|
// JSON error. |
|
98
|
4 |
|
$json_error = \json_last_error(); |
|
99
|
|
|
|
|
100
|
4 |
|
if ( \JSON_ERROR_NONE !== $json_error ) { |
|
101
|
1 |
|
throw new \Exception( |
|
102
|
1 |
|
\sprintf( |
|
103
|
1 |
|
'Could not JSON decode Payvision response, HTTP response: "%s %s", HTTP body length: "%d", JSON error: "%s".', |
|
104
|
1 |
|
$response_code, |
|
105
|
1 |
|
$response_message, |
|
106
|
1 |
|
\strlen( $body ), |
|
107
|
1 |
|
\json_last_error_msg() |
|
108
|
|
|
), |
|
109
|
|
|
$json_error |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Object. |
|
114
|
3 |
|
if ( ! \is_object( $data ) ) { |
|
115
|
1 |
|
throw new \Exception( |
|
116
|
1 |
|
\sprintf( |
|
117
|
1 |
|
'Could not JSON decode Payvision response to an object, HTTP response: "%s %s", HTTP body: "%s".', |
|
118
|
1 |
|
$response_code, |
|
119
|
1 |
|
$response_message, |
|
120
|
1 |
|
$body |
|
121
|
|
|
), |
|
122
|
1 |
|
\intval( $response_code ) |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
return $data; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|