-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcoinpay.php
238 lines (218 loc) · 6.46 KB
/
coinpay.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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/*
TITLE: Coinpay API class
VERISON: 1.0
AUTHOR: CoinPay
EMAIL: [email protected]
DESCRIPTION: The coinpay class allows you to easily interact with the http://coinpay.in.th API.
Find a full copy of the API documentation at https://coinpay.in.th/docs/
*/
class coinpay
{
var $access_id, $access_key;
var $api_url = 'https://coinpay.in.th/api/';
var $order_id=0;
var $error;
public function init($api_id, $api_key){
if(strlen($api_id) < 12 || strlen($api_key) < 12){
return false;
}
$this->access_id = $api_id;
$this->access_key = $api_key;
return true;
}
public function validate($amount, $currency){
$params = $this->authParam();
$params['amount'] = $amount;
$params['currency'] = $currency;
if($data = $this->apiFetch('validate',$params)){
$this->error = $data->errors;
return $data->success;
}
return false;
}
public function price($amount=0, $currency=''){
$params = $this->authParam();
if($amount > 0){
$params['amount'] = $amount;
$params['currency'] = $currency;
}
if($data = $this->apiFetch('price',$params)){
if(isset($data->success) && $data->success){
return $data;
}else{
$this->error = $data->error;
return false;
}
}
return false;
}
public function getAddress($new = false){
$params = $this->authParam();
if($new){
$params['new'] = true;
}
if($data = $this->apiFetch('address',$params)){
if(isset($data->success) && $data->success){
return $data->address;
}else{
$this->error = $data->error.':'. $data->address;
return false;
}
}
return false;
}
public function checkorder($order_id, $reference_id=''){
$params = $this->authParam();
$params['order_id'] = $order_id;
if($reference_id != ''){
$params['reference_id'] = $reference_id;
}
if($data = $this->apiFetch('checkorder',$params)){
return $data;
}
return false;
}
public function sendReference($order_id, $reference_id){
$params = $this->authParam();
$params['order_id'] = $order_id;
$params['reference_id'] = $reference_id;
if($data = $this->apiFetch('reference',$params)){
return $data;
}
return false;
}
public function balances(){
$params = $this->authParam();
if($data = $this->apiFetch('balance',$params)){
return $data;
}
return false;
}
public function create($data){
$params = $this->authParam();
$params['amount'] = $data['amount'];
$params['currency'] = $data['currency'];
$params['ipn'] = $data['ipn'];
$params['order_id'] = (isset($data['order_id']) ? $data['order_id'] : (int)$this->order_id);
$params['reference_id'] = $data['reference_id'];
if($data = $this->apiFetch('paybox',$params)){
$this->order_id = $data->order_id;
return $data;
}
return false;
}
public function countDown($expire,$selector, $text = 'You must send the bitcoins within the next %s Minutes %s Seconds',$expiremsg = 'Bitcoin payment time has expired, please refresh the page to get a new address'){
return '<p class="bitcoincountdown">'.sprintf($text,'<span id="btcmins">'.$expire.'</span>','<span id="btcsecs">0</span>').'</p><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
if(typeof bitcointhaitimer == \'undefined\'){
var bitcointhaitimer = '.(60*$expire).';
jQuery.noConflict();
jQuery(function($){
function btccountDown(){
bitcointhaitimer -= 1;
var minutes = Math.floor(bitcointhaitimer / 60);
var seconds = bitcointhaitimer - minutes * 60;
$("#btcmins").text(minutes);
$("#btcsecs").text(seconds);
if(bitcointhaitimer <= 0){
$("#btcmins").closest("'.$selector.'").after("<p>'.$expiremsg.'</p>").remove();
}else{
setTimeout(btccountDown,1000);
}
}
setTimeout(btccountDown,1000);
});
}
</script>';
}
public function listOrders($status='',$start_date='',$end_date=''){
$params = $this->authParam();
if(is_array($status)){
$params['status'] = implode(',',$status);
}elseif($status != '' && intval($status) == $status){
$params['status'] = (int)$status;
}
$params['start_date'] = $start_date;
$params['end_date'] = $end_date;
if($data = $this->apiFetch('orders',$params)){
return $data;
}
return false;
}
public function verifyIPN($order_id, $verify){
if($order_id > 0){
$params = $this->authParam();
$params['verify'] = $verify;
$params['order_id'] = $order_id;
if($data = $this->apiFetch('verifyipn',$params)){
return $data->success;
}
}
return false;
}
public function payLink($amount, $currency, $reference_id='',$ipn_url='', $redirect=''){
$params = $this->authParam();
$params['amount'] = $amount;
$params['currency'] = $currency;
$params['reference_id'] = $reference_id;
$params['ipn_url'] = $ipn_url;
$params['redirect'] = $redirect;
if($result = $this->apiFetch('paylink',$params)){
if($result->success){
return $result->link;
}
}
return false;
}
public function createAccount($data){
$params = $this->authParam();
if($result = $this->apiFetch('account/create',array_merge($params, $data))){
return $result;
}
return false;
}
public function listAccount($account_id=''){
$params = $this->authParam();
$params['account_id'] = $account_id;
if($result = $this->apiFetch('account/details',$params)){
return $result;
}
return false;
}
public function setBank($account_id, $bank, $bank_holder, $account_number){
$params = $this->authParam();
$params['account_id'] = $account_id;
$params['branch'] = $bank;
$params['owner'] = $bank_holder;
$params['account'] = $account_number;
if($result = $this->apiFetch('account/bank',$params)){
return $result;
}
return false;
}
private function apiFetch($action,$params){
if($ch = curl_init ()){
curl_setopt ($ch, CURLOPT_URL, $this->api_url.$action.'/');
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, true );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 5 );
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt ($ch, CURLOPT_POST, count($params));
curl_setopt ($ch, CURLOPT_POSTFIELDS,$params);
$str = curl_exec ( $ch );
curl_close ( $ch );
if($data = json_decode($str)){
return $data;
}
}
return false;
}
private function authParam(){
$nonce = time();
$signature = hash('sha256',$this->access_id.$nonce.$this->access_key);
return array('key' => $this->access_id, 'signature' => $signature, 'nonce' => $nonce);
}
}
?>