1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Payvision client |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2021 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
|
|
|
use Pronamic\WordPress\Pay\Facades\Http; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Payvision client |
17
|
|
|
* |
18
|
|
|
* @link https://github.com/payvisionpayments/php/blob/master/generatepaymentform.php |
19
|
|
|
* @author Remco Tolsma |
20
|
|
|
* @version 1.1.0 |
21
|
|
|
* @since 1.0.0 |
22
|
|
|
*/ |
23
|
|
|
class Client { |
24
|
|
|
/** |
25
|
|
|
* Config. |
26
|
|
|
* |
27
|
|
|
* @var Config |
28
|
|
|
*/ |
29
|
|
|
private $config; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructs and initializes an Payvision client object. |
33
|
|
|
* |
34
|
|
|
* @param Config $config Payvision config. |
35
|
|
|
*/ |
36
|
8 |
|
public function __construct( Config $config ) { |
37
|
8 |
|
$this->config = $config; |
38
|
8 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Send request with the specified action and parameters |
42
|
|
|
* |
43
|
|
|
* @param string $method Payvision API method. |
44
|
|
|
* @param string $path Path. |
45
|
|
|
* @param object|string[]|string|false $request Request object. |
46
|
|
|
* @return object |
47
|
|
|
* @throws \Exception Throws exception when error occurs. |
48
|
|
|
*/ |
49
|
6 |
|
public function send_request( $method, $path, $request = null ) { |
50
|
|
|
// Request. |
51
|
6 |
|
$authorization = 'Basic ' . \base64_encode( $this->config->get_username() . ':' . $this->config->get_password() ); |
52
|
|
|
|
53
|
6 |
|
$response = Http::request( |
54
|
6 |
|
$this->config->get_endpoint_url( $path ), |
55
|
|
|
array( |
56
|
6 |
|
'method' => $method, |
57
|
|
|
'headers' => array( |
58
|
6 |
|
'Authorization' => $authorization, |
59
|
6 |
|
'Content-Type' => 'application/json', |
60
|
|
|
), |
61
|
6 |
|
'body' => $request, |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
|
65
|
5 |
|
$data = $response->json(); |
66
|
|
|
|
67
|
|
|
// Object. |
68
|
3 |
|
if ( ! \is_object( $data ) ) { |
69
|
1 |
|
$response_code = $response->status(); |
70
|
|
|
|
71
|
1 |
|
throw new \Exception( |
72
|
1 |
|
\sprintf( |
73
|
1 |
|
'Could not JSON decode Payvision response to an object, HTTP response: "%s %s", HTTP body: "%s".', |
74
|
1 |
|
$response_code, |
75
|
1 |
|
$response->message(), |
76
|
1 |
|
$response->body() |
77
|
|
|
), |
78
|
1 |
|
\intval( $response_code ) |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return $data; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|