forked from DarrenWestwood/virtuemart-plugin
-
Notifications
You must be signed in to change notification settings - Fork 3
/
test-setup.php
295 lines (261 loc) · 10.7 KB
/
test-setup.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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
error_reporting(0);
$existing_api = $_POST['api'];
$existing_secret = $_POST['secret'];
$existing_url = $_POST['url'];
$base_url = $_POST['base_url'];
if( isset($_POST['alt_payments']) )
{
$alt_payments = $_POST['alt_payments'];
}else{
$alt_payments = 0;
}
$timer = $_POST['timer'];
class Blockonomics
{
const BASE_URL = 'https://www.blockonomics.co';
const NEW_ADDRESS_URL = 'https://www.blockonomics.co/api/new_address';
const PRICE_URL = 'https://www.blockonomics.co/api/price';
const ADDRESS_URL = 'https://www.blockonomics.co/api/address?&no_balance=true&only_xpub=true&get_callback=true';
const SET_CALLBACK_URL = 'https://www.blockonomics.co/api/update_callback';
public function __construct()
{
}
public function new_address($api_key, $secret, $reset=false)
{
$options = array(
'http' => array(
'header' => 'Authorization: Bearer ' . $api_key. "\r\n" ."Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => '',
'ignore_errors' => true
)
);
if($reset)
{
$get_params = "?match_callback=$secret&reset=1";
}
else
{
$get_params = "?match_callback=$secret";
}
$context = stream_context_create($options);
$contents = file_get_contents(Blockonomics::NEW_ADDRESS_URL.$get_params, false, $context);
$responseObj = json_decode($contents);
//Create response object if it does not exist
if (!isset($responseObj)) $responseObj = new stdClass();
$responseObj->{'response_code'} = $http_response_header[0];
return $responseObj;
}
public function get_price($currency)
{
$options = array( 'http' => array( 'method' => 'GET') );
$context = stream_context_create($options);
$contents = file_get_contents(Blockonomics::PRICE_URL. "?currency=$currency", false, $context);
$price = json_decode($contents);
return $price->price;
}
public function get_xpubs($api_key)
{
$options = array(
'http' => array(
'header' => 'Authorization: Bearer ' . $api_key. "\r\n" ."Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'GET',
'content' => '',
'ignore_errors' => true
)
);
$context = stream_context_create($options);
$contents = file_get_contents(Blockonomics::ADDRESS_URL, false, $context);
$responseObj = json_decode($contents);
return $responseObj;
}
public function update_callback($api_key, $callback_url, $xpub)
{
$options = array(
'http' => array(
'header' => 'Authorization: Bearer ' . $api_key. "\r\n" ."Content-Type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => '{"callback": "'.$callback_url.'", "xpub": "'.$xpub.'"}',
'ignore_errors' => true
)
);
$context = stream_context_create($options);
$contents = file_get_contents(Blockonomics::SET_CALLBACK_URL, false, $context);
$responseObj = json_decode($contents);
return $responseObj;
}
}
if($existing_api != "" || $existing_api != 0){
function prepareForSave($db_api,$db_secret,$db_url,$alt_payments,$db_timer){
return 'merchant_apikey="'.$db_api.'"|merchant_secret="'.$db_secret.'"|merchant_callback="'.$db_url.'"|alt_payments='.$alt_payments.'|timer="'.$db_timer.'"|';
}
define( '_JEXEC', 1 );
define( 'JPATH_BASE', realpath(dirname(__FILE__).'/../../..' ));
require_once ( JPATH_BASE .'/includes/defines.php' );
require_once ( JPATH_BASE .'/includes/framework.php' );
require_once ( JPATH_BASE .'/configuration.php' );
$mainframe = JFactory::getApplication('site');
// Get a db connection.
$db = JFactory::getDbo();
// Create a new query object.
$query = $db->getQuery(true);
$pmIDquery = 'SELECT `virtuemart_paymentmethod_id` FROM `#__virtuemart_paymentmethods` WHERE `payment_element`="' . "blockonomics" . '" ';
$dbs = JFactory::getDBO();
$dbs->setQuery($pmIDquery);
$pmIDcontent = $dbs->loadResult();
// Create an object for the record we are going to update.
$object = new stdClass();
// Must be a valid primary key value.
$object->virtuemart_paymentmethod_id = $pmIDcontent;
$object->payment_params = prepareForSave($existing_api,$existing_secret,$existing_url,$alt_payments,$timer);
// Update their details in the users table using id as the primary key.
$result = JFactory::getDbo()->updateObject('#__virtuemart_paymentmethods', $object, 'virtuemart_paymentmethod_id');
// $payment_params = prepareForSave($existing_api,$existing_secret,$existing_url,$alt_payments,$timer);
// // echo $payment_params;
// $q = 'SELECT `payment_params` FROM `#__virtuemart_paymentmethods` WHERE `payment_element`=' . 'blockonomics';
// $db->setQuery($q);
// $payment_params = $db->loadResult();
// // var_dump($result);
//echo "Settings saved<br>";
function update_callback_url($callback_url, $xPub, $blockonomics,$existing_api)
{
$blockonomics->update_callback(
$existing_api,
$callback_url,
$xPub
);
}
/**
* Check the status of callback urls
* If no xPubs set, return
* If one xPub is set without callback url, set the url
* If more than one xPubs are set, give instructions on integrating to multiple sites
* @return Strin Count of found xPubs
*/
function check_callback_urls($existing_api, $existing_secret, $existing_url, $base_url)
{
$blockonomics = new Blockonomics;
//$virtuemart_order_id = $result
$responseObj = $blockonomics->get_xpubs($existing_api);
// No xPubs set
if (count($responseObj) == 0)
{
return "0";
}
// One xPub set
if (count($responseObj) == 1)
{
// Existing Callback
$callback_secret = $existing_secret;
$callback_url = $existing_url;
// No Callback URL set, set one
if(!$responseObj[0]->callback || $responseObj[0]->callback == null)
{
update_callback_url($callback_url, $responseObj[0]->address, $blockonomics, $existing_api);
return "1";
}
// One xPub with one Callback URL
else
{
if($responseObj[0]->callback == $callback_url)
{
return "1";
}
if($responseObj[0]->callback == $callback_url)
{
return "1";
}
// Check if only secret differs
if(strpos($responseObj[0]->callback, $base_url) !== false) // $responseObj[0]->callback is in $responseCallback
{
//Looks like the user regenrated callback by mistake
//Just force Update_callback on server
update_callback_url($callback_url, $responseObj[0]->address, $blockonomics, $existing_api);
return "1";
}else{ // $responseObj[0]->callback not in $responseCallback
return "2";
}
}
}
if (count($responseObj) > 1)
{
$callback_secret = $existing_secret;
$callback_url = $existing_url;
// Check if callback url is set
foreach ($responseObj as $resObj) {
if($resObj->callback == $callback_url)
{
return "1";
}
}
return "2";
}
}
function testSetup($existing_api, $existing_secret)
{
$blockonomics = new Blockonomics;
$responseObj = $blockonomics->new_address($existing_api, $existing_secret, true);
if(!ini_get('allow_url_fopen')) {
$error_str = 'allow_url_fopen is not enabled, please enable this in php.ini';
} elseif(!isset($responseObj->response_code)) {
$error_str = 'Your web host is blocking outgoing HTTPS connections. Blockonomics requires an outgoing HTTPS POST (port 443) to generate a new address. Check with your web hosting provider to allow this.';
} else {
switch ($responseObj->response_code) {
case 'HTTP/1.1 200 OK':
break;
case 'HTTP/1.1 401 Unauthorized': {
$error_str = 'API Key is incorrect. Make sure that your API key is set. ';
break;
}
case 'HTTP/1.1 500 Internal Server Error': {
if(isset($responseObj->message)) {
$error_code = $responseObj->message;
switch ($error_code) {
case "Could not find matching xpub":
$error_str = 'There is a problem in the Callback URL. Make sure that you have set your Callback URL from the admin Blockonomics module configuration to your Merchants > Settings.';
break;
case "This require you to add an xpub in your wallet watcher":
$error_str = 'There is a problem in the XPUB. Make sure that the you have added an address to Wallet Watcher > Address Watcher. If you have added an address make sure that it is an XPUB address and not a Bitcoin address.';
break;
default:
$error_str = $responseObj->message;
}
break;
} else {
$error_str = $responseObj->response_code;
break;
}
}
default:
$error_str = $responseObj->response_code;
break;
}
}
if(isset($error_str)) {
return $error_str;
}
return false;
}
$urls_count = check_callback_urls($existing_api, $existing_secret, $existing_url, $base_url);
if($urls_count == '2')
{
$message = "Seems that you have set multiple xPubs or you already have a Callback URL set. <a href='https://blockonomics.freshdesk.com/support/solutions/articles/33000209399-merchants-integrating-multiple-websites' target='_blank'>Here is a guide</a> to setup multiple websites.";
echo $message;
}else {
$setup_errors = testSetup($existing_api, $existing_secret);
if($setup_errors)
{
$message = $setup_errors . 'For more information, please consult <a href="https://blockonomics.freshdesk.com/support/solutions/articles/33000215104-troubleshooting-unable-to-generate-new-address" target="_blank">this troubleshooting article</a>';
echo $message;
}
else
{
$message = 'Congrats! Setup is all done';
echo $message;
}
}
}else{
$message = 'Please enter your API key and try again';
echo $message;
}