-
Notifications
You must be signed in to change notification settings - Fork 0
/
Client.php
82 lines (74 loc) · 2.37 KB
/
Client.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
namespace Plugin\Smartpay;
use Exception;
class Client
{
private $secretKey;
private $publicKey;
private $apiPrefix;
function __construct($secretKey = null, $publicKey = null, $apiPrefix = null) {
$this->secretKey = $secretKey;
$this->publicKey = $publicKey;
$this->apiPrefix = $apiPrefix;
if (!$secretKey) {
$this->secretKey = getenv('SMARTPAY_SECRET_KEY');
}
if (!$publicKey) {
$this->publicKey = getenv('SMARTPAY_PUBLIC_KEY');
}
if (!$apiPrefix) {
$this->apiPrefix = "https://api.smartpay.co/v1";
}
}
/**
* @throws Exception
*/
public function httpGet($url)
{;
$curl = $this->curlInit($url);
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
if ($httpCode != 200) {
log_error("[Smartpay] GET {$url} {$httpCode}", array(
'response' => $response
));
throw new Exception("システム管理者に連絡してください");
}
return json_decode($response, true);
}
/**
* @throws Exception
*/
public function httpPost($url, $data)
{
$curl = $this->curlInit($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_RESPONSE_CODE);
curl_close($curl);
if ($httpCode != 200) {
log_error("[Smartpay] POST {$url} {$httpCode}", array(
'payload' => $data,
'response' => $response
));
throw new Exception("システム管理者に連絡してください");
}
return json_decode($response, true);
}
private function curlInit($url)
{
$url = $this->apiPrefix . $url;
$curl = curl_init($url);
$authorization = "Authorization: Basic {$this->secretKey}";
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
$authorization
));
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate,sdch');
return $curl;
}
}