-
Notifications
You must be signed in to change notification settings - Fork 0
/
Coin.php
184 lines (166 loc) · 7.35 KB
/
Coin.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
<?php
/*
CoinPayments.net API Class - v1.1
Copyright 2014-2018 CoinPayments.net. All rights reserved.
License: GPLv2 - http://www.gnu.org/licenses/gpl-2.0.txt
*/
class CoinPaymentsAPI {
private $private_key = '';
private $public_key = '';
private $ch = null;
public function Setup($private_key, $public_key) {
$this->private_key = $private_key;
$this->public_key = $public_key;
$this->ch = null;
}
/**
* Gets the current CoinPayments.net exchange rate. Output includes both crypto and fiat currencies.
* @param short If short == TRUE (the default), the output won't include the currency names and confirms needed to save bandwidth.
*/
public function GetRates($short = TRUE) {
$short = $short ? 1:0;
return $this->api_call('rates', array('short' => $short));
}
/*
*/
public function GetBasicProfile() {
return $this->api_call('get_basic_info', []);
}
/**
* Gets your current coin balances (only includes coins with a balance unless all = TRUE).<br />
* @param all If all = TRUE then it will return all coins, even those with a 0 balance.
*/
public function GetBalances($all = FALSE) {
return $this->api_call('balances', array('all' => $all ? 1:0));
}
/**
* Creates a basic transaction with minimal parameters.<br />
* See CreateTransaction for more advanced features.
* @param amount The amount of the transaction (floating point to 8 decimals).
* @param currency1 The source currency (ie. USD), this is used to calculate the exchange rate for you.
* @param currency2 The cryptocurrency of the transaction. currency1 and currency2 can be the same if you don't want any exchange rate conversion.
* @param buyer_email Set the buyer's email so they can automatically claim refunds if there is an issue with their payment.
* @param address Optionally set the payout address of the transaction. If address is empty then it will follow your payout settings for that coin.
* @param ipn_url Optionally set an IPN handler to receive notices about this transaction. If ipn_url is empty then it will use the default IPN URL in your account.
*/
public function CreateTransactionSimple($amount, $currency1, $currency2, $buyer_email, $address='', $ipn_url='') {
$req = array(
'amount' => $amount,
'currency1' => $currency1,
'currency2' => $currency2,
'buyer_email' => $buyer_email,
'address' => $address,
'ipn_url' => $ipn_url,
);
return $this->api_call('create_transaction', $req);
}
public function CreateTransaction($req) {
// See https://www.coinpayments.net/apidoc-create-transaction for parameters
return $this->api_call('create_transaction', $req);
}
/**
* Creates an address for receiving payments into your CoinPayments Wallet.<br />
* @param currency The cryptocurrency to create a receiving address for.
* @param ipn_url Optionally set an IPN handler to receive notices about this transaction. If ipn_url is empty then it will use the default IPN URL in your account.
*/
public function GetCallbackAddress($currency, $ipn_url = '') {
$req = array(
'currency' => $currency,
'ipn_url' => $ipn_url,
);
return $this->api_call('get_callback_address', $req);
}
/**
* Creates a withdrawal from your account to a specified address.<br />
* @param amount The amount of the transaction (floating point to 8 decimals).
* @param currency The cryptocurrency to withdraw.
* @param address The address to send the coins to.
* @param auto_confirm If auto_confirm is TRUE, then the withdrawal will be performed without an email confirmation.
* @param ipn_url Optionally set an IPN handler to receive notices about this transaction. If ipn_url is empty then it will use the default IPN URL in your account.
*/
public function CreateWithdrawal($amount, $currency, $address, $auto_confirm = FALSE, $ipn_url = '') {
$req = array(
'amount' => $amount,
'currency' => $currency,
'address' => $address,
'auto_confirm' => $auto_confirm ? 1:0,
'ipn_url' => $ipn_url,
);
return $this->api_call('create_withdrawal', $req);
}
/**
* Creates a transfer from your account to a specified merchant.<br />
* @param amount The amount of the transaction (floating point to 8 decimals).
* @param currency The cryptocurrency to withdraw.
* @param merchant The merchant ID to send the coins to.
* @param auto_confirm If auto_confirm is TRUE, then the transfer will be performed without an email confirmation.
*/
public function CreateTransfer($amount, $currency, $merchant, $auto_confirm = FALSE) {
$req = array(
'amount' => $amount,
'currency' => $currency,
'merchant' => $merchant,
'auto_confirm' => $auto_confirm ? 1:0,
);
return $this->api_call('create_transfer', $req);
}
/**
* Creates a transfer from your account to a specified $PayByName tag.<br />
* @param amount The amount of the transaction (floating point to 8 decimals).
* @param currency The cryptocurrency to withdraw.
* @param pbntag The $PayByName tag to send funds to.
* @param auto_confirm If auto_confirm is TRUE, then the transfer will be performed without an email confirmation.
*/
public function SendToPayByName($amount, $currency, $pbntag, $auto_confirm = FALSE) {
$req = array(
'amount' => $amount,
'currency' => $currency,
'pbntag' => $pbntag,
'auto_confirm' => $auto_confirm ? 1:0,
);
return $this->api_call('create_transfer', $req);
}
private function is_setup() {
return (!empty($this->private_key) && !empty($this->public_key));
}
private function api_call($cmd, $req = array()) {
if (!$this->is_setup()) {
return array('error' => 'You have not called the Setup function with your private and public keys!');
}
// Set the API command and required fields
$req['version'] = 1;
$req['cmd'] = $cmd;
$req['key'] = $this->public_key;
$req['format'] = 'json'; //supported values are json and xml
// Generate the query string
$post_data = http_build_query($req, '', '&');
// Calculate the HMAC signature on the POST data
$hmac = hash_hmac('sha512', $post_data, $this->private_key);
// Create cURL handle and initialize (if needed)
if ($this->ch === null) {
$this->ch = curl_init('https://www.coinpayments.net/api.php');
curl_setopt($this->ch, CURLOPT_FAILONERROR, TRUE);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, 0);
}
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('HMAC: '.$hmac));
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($this->ch);
if ($data !== FALSE) {
if (PHP_INT_SIZE < 8 && version_compare(PHP_VERSION, '5.4.0') >= 0) {
// We are on 32-bit PHP, so use the bigint as string option. If you are using any API calls with Satoshis it is highly NOT recommended to use 32-bit PHP
$dec = json_decode($data, TRUE, 512, JSON_BIGINT_AS_STRING);
} else {
$dec = json_decode($data, TRUE);
}
if ($dec !== NULL && count($dec)) {
return $dec;
} else {
// If you are using PHP 5.5.0 or higher you can use json_last_error_msg() for a better error message
return array('error' => 'Unable to parse JSON result ('.json_last_error().')');
}
} else {
return array('error' => 'cURL error: '.curl_error($this->ch));
}
}
};