-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcryptopay.php
More file actions
141 lines (115 loc) · 3.06 KB
/
cryptopay.php
File metadata and controls
141 lines (115 loc) · 3.06 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
/* Class */
class Cryptopay
{
const URL = 'https://cryptopay.me/api/v1';
const VERSION = '1.0';
private $api_key;
function __construct($key)
{
$this->api_key = $key;
}
private function request ($method, $url, $data = array())
{
if (!empty($data))
{
if (isset($data['callback_params']))
{
foreach($data['callback_params'] AS $name => $value)
{
$data["callback_params[$name]"] = $value;
}
unset($data['callback_params']);
}
$data['api_key'] = $this->api_key;
$data = http_build_query($data);
}
try
{
if (!$curl = curl_init())
{
throw new Exception('Module CURL is not initialized');
}
curl_setopt($curl, CURLOPT_URL, self::URL . $url);
if ($method === 'POST')
{
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'Cryptopay PHP API Client '. self::VERSION);
if (!$out = curl_exec($curl))
{
$error = json_encode(curl_error($curl) .' ('. curl_errno($curl) .')');
throw new Exception($error);
}
$response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if (!($response_code >= 200 && $response_code < 300))
{
$errors['error_code'] = $response_code;
if (is_object(json_decode($out)) && !empty($out))
{
foreach (json_decode($out) AS $error_name => $error_mess)
{
$errors[$error_name] = ($error_name != 'error') ? $error_mess[0] : $error_mess;
}
}
throw new Exception(json_encode($errors));
}
curl_close($curl);
}
catch (Exception $e)
{
$out = $e->getMessage();
}
return json_decode($out, true);
}
public function invoice ($data)
{
return $this->request('POST', '/invoices', $data);
}
public function button ($data, $name = '')
{
$button_data = $this->request('POST', '/buttons', $data);
if($name == '') $name = '<img src="https://cryptopay.me/assets/pay-btns/btn-sm-green.png">';
$button = '<a data-cryptopay-token="'. $button_data['token'] .'" href="#">'. $name .'</a><script src="https://cryptopay.me/assets/button.js"></script>';
return $button;
}
public function hosted ($data)
{
$hosted = $this->request('POST', '/hosted', $data);
return $hosted['url'];
}
public function invoices ()
{
return $this->request('GET', '/invoices?api_key='. $this->api_key);
}
public function rate ()
{
return $this->request('GET', '/cryptopay_rate');
}
public function validate_hash ($data)
{
$required_parameters = array('uuid', 'price', 'currency');
try
{
$errors = array();
foreach($required_parameters AS $parameter)
{
if(!array_key_exists($parameter, $data))
{
$errors[] = 'Not passed as parameter: '. $parameter;
}
}
if (!empty($errors)) throw new Exception(implode("\n", $errors));
$price_cents = $data['price'] * 100;
$validate_str = "{$this->api_key}_{$data['uuid']}_$price_cents{$data['currency']}";
$result = sha1($validate_str);
}
catch (Exception $e)
{
$result = $e->getMessage();
}
return $result;
}
}