forked from hostvpn/whmcs-coinbase-bitcoin
/
coinbase.php
183 lines (148 loc) · 7.65 KB
/
coinbase.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
<?php
/*
Copyright (c) 2013 HostVPN.com
This software is released under the MIT License.
*/
function coinbase_config() {
$configarray = array(
"FriendlyName" => array("Type" => "System", "Value"=>"Coinbase (Bitcoin)"),
"whmcs_admin_username" => array("FriendlyName" => "WHMCS Admin User Name", "Type" => "text", "Size" => "20", "Description" => "Need an admin user name to use the local API, usually it's just 'admin'.", ),
"coinbase_api_key" => array("FriendlyName" => "Coinbase API Key", "Type" => "password", "Size" => "70", ),
"coinbase_api_secret" => array("FriendlyName" => "Coinbase API Secret", "Type" => "password", "Size" => "70", ),
"coinbase_type" => array("FriendlyName" => "Coinbase Button Type", "Type" => "dropdown", "Options" => "buy_now,donation,subscription", "Description" => "Subscription type is not yet available.", ),
"coinbase_style" => array("FriendlyName" => "Coinbase Button Style", "Type" => "dropdown", "Options" => "buy_now_small,buy_now_large,donation_small,donation_large,custom_small,custom_large", ),
"coinbase_text" => array("FriendlyName" => "Coinbase Button Text", "Type" => "text", "Size" => "70", "Description" => "<br />This is the text that will appear on your button (e.g. 'Pay with Bitcoin')." ),
"coinbase_ca_path" => array("FriendlyName" => "Coinbase CA Cert Path", "Type" => "text", "Size" => "70", "Description" =>"<br />This should be the full directory path to the ca-coinbase.crt file (e.g. " . dirname(__FILE__) . "/coinbase/ca-coinbase.crt)." ),
"callback_secret" => array("FriendlyName" => "Coinbase Callback Secret", "Type" => "text", "Size" => "70", "Description" => "<br />You will need to append your callback secret to the callback URL you supply to Coinbase. For example, a Callback Secret of 'myKey000', then supply Coinbase with the Callback URL of:<br />https://yourdomain/modules/gateways/callback/coinbase.php?secret=myKey000" ),
);
return $configarray;
}
function coinbase_link($params) {
# Gateway Specific Variables
$api_auth = array($params['coinbase_api_key'], $params['coinbase_api_secret']);
$url = 'https://coinbase.com/api/v1/buttons';
$type = $params['coinbase_type'];
$style = $params['coinbase_style'];
$text = $params['coinbase_text'];
$ca_path = $params['coinbase_ca_path'];
# Invoice Variables
$invoiceid = $params['invoiceid'];
$description = $params["description"];
$amount = $params['amount']; # Format: ##.##
$currency = $params['currency']; # Currency Code
# Client Variables
$firstname = $params['clientdetails']['firstname'];
$lastname = $params['clientdetails']['lastname'];
$email = $params['clientdetails']['email'];
$address1 = $params['clientdetails']['address1'];
$address2 = $params['clientdetails']['address2'];
$city = $params['clientdetails']['city'];
$state = $params['clientdetails']['state'];
$postcode = $params['clientdetails']['postcode'];
$country = $params['clientdetails']['country'];
$phone = $params['clientdetails']['phonenumber'];
# System Variables
$companyname = $params['companyname'];
$systemurl = $params['systemurl'];
$currency = $params['currency'];
$code = coinbase_button_request($invoiceid, $amount, $currency, $description, $type, $style, $text, $url, $ca_path, $api_auth);
return $code;
}
// Are refunds pratical? You would need to ask the user for their wallet address, otherwise you might refund the payment back to an online wallet or some other intermediary service.
/*function coinbase_refund($params) {
# Gateway Specific Variables
$api_key = $params['coinbase_api_key'];
$url = 'https://coinbase.com/api/v1/buttons?api_key=' . $api_key;
# Invoice Variables
$transid = $params['transid'];
$invoiceid = $params['invoiceid'];
$description = $params["description"];
$amount = $params['amount']; # Format: ##.##
$currency = $params['currency']; # Currency Code
# Client Variables
$firstname = $params['clientdetails']['firstname'];
$lastname = $params['clientdetails']['lastname'];
$email = $params['clientdetails']['email'];
$address1 = $params['clientdetails']['address1'];
$address2 = $params['clientdetails']['address2'];
$city = $params['clientdetails']['city'];
$state = $params['clientdetails']['state'];
$postcode = $params['clientdetails']['postcode'];
$country = $params['clientdetails']['country'];
$phone = $params['clientdetails']['phonenumber'];
# Card Details
$cardtype = $params['cardtype'];
$cardnumber = $params['cardnum'];
$cardexpiry = $params['cardexp']; # Format: MMYY
$cardstart = $params['cardstart']; # Format: MMYY
$cardissuenum = $params['cardissuenum'];
# Perform Refund Here & Generate $results Array, eg:
$results = array();
$results["status"] = "success";
$results["transid"] = "12345";
# Return Results
if ($results["status"]=="success") {
return array("status"=>"success","transid"=>$results["transid"],"rawdata"=>$results);
} elseif ($gatewayresult=="declined") {
return array("status"=>"declined","rawdata"=>$results);
} else {
return array("status"=>"error","rawdata"=>$results);
}
}*/
function coinbase_button_request($invoiceid, $amount, $currency, $description, $type, $style, $text, $url, $ca_path, $api_auth) {
$button_data = array(
'button'=>array(
'name'=>'Invoice '.$invoiceid.' Payment',
'price_string'=>$amount,
'price_currency_iso'=>$currency,
'custom'=>$invoiceid,
'description'=>$description,
'type'=>$type,
'style'=>$style),
);
$button_response = coinbase_post_json($url, $button_data, $ca_path, $api_auth);
$button = $button_response['button'];
$button_code = $button['code'];
$type = $button['type'];
$style = $button['style'];
$invoice_id = $button['custom'];
$price = $button['price']['cents'];
$currency = $button['price']['currency_iso'];
$code = '<a class="coinbase-button" data-code="'.$button_code.'" data-button-style="'.$style.'" data-button-text="'.$text.'" data-custom="'.$invoice_id.'" href="https://coinbase.com/checkouts/'.$button_code.'">'.$text.'</a><script src="https://coinbase.com/assets/button.js" type="text/javascript"></script>';
return $code;
}
function coinbase_post_json($url, $button_data, $ca_path, $api_auth) {
if(!function_exists('hash_hmac')) {
die("The hash_hmac() function is unavailable. Please upgrade PHP.");
}
list($api_key, $api_secret) = $api_auth;
// Same method as the official coinbase library for nonce
$microseconds = sprintf('%0.0f',round(microtime(true) * 1000000));
$to_sign = $microseconds . $url . json_encode($button_data);
$signature = hash_hmac("sha256", $to_sign, $api_secret);
$add_headers = array(
'Content-Type: application/json',
'ACCESS_KEY: ' . $api_key,
'ACCESS_SIGNATURE: ' . $signature,
'ACCESS_NONCE: ' . $microseconds,
);
//$ca_coinbase_path = getcwd() . '/coinbase/ca-coinbase.crt';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $add_headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($button_data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, $ca_path);
$response_data = curl_exec($ch);
if (curl_error($ch)) die("Connection Error: ".curl_errno($ch).' - '.curl_error($ch));
curl_close($ch);
$button_response = json_decode($response_data, true);
if ($button_response['success'] == 'true') {
return $button_response;
}
}
?>