-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
137 lines (130 loc) · 5.89 KB
/
api.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
<?php
require_once("simplex_api.php");
// TODO test functions
$route = isset($_REQUEST['ROUTE']) ? $_REQUEST['ROUTE'] : null;
// $route = explode('/',$_SERVER["REQUEST_URI"]);
// $route = array_key_exists(2, $route) ? $route[2] : $route[1];
// $route = strpos($route, '?') ? substr($route, 0, strpos($route, '?')) : $route;
// TODO error handle if no path given
$response = null;
$error = false;
$redirect = false;
// TODO refactor all $_REQUEST handling into here
// Parameter-handling duplicated here as troubleshooting for an issue where I wasn't able to read any POSTed params ... would like to refactor all param handling into one place
switch($route) {
case 'supported_cryptos':
try {
$_PUBLIC_KEY = isset($_REQUEST['PUBLIC_KEY']) ? $_REQUEST['PUBLIC_KEY'] : $PUBLIC_KEY;
$cryptos = supported_cryptos($_PUBLIC_KEY);
$response = $cryptos;
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
case 'supported_fiats':
try {
$_PUBLIC_KEY = isset($_REQUEST['PUBLIC_KEY']) ? $_REQUEST['PUBLIC_KEY'] : $PUBLIC_KEY;
$fiats = supported_fiats($_PUBLIC_KEY);
$response = $fiats;
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
case 'quote':
try {
$_FIAT_TICKER = isset($_REQUEST['FIAT_TICKER']) ? $_REQUEST['FIAT_TICKER'] : $FIAT_TICKER;
$_CRYPTO_TICKER = isset($_REQUEST['CRYPTO_TICKER']) ? $_REQUEST['CRYPTO_TICKER'] : $CRYPTO_TICKER;
$_REQUESTED_TICKER = isset($_REQUEST['REQUESTED_TICKER']) ? $_REQUEST['REQUESTED_TICKER'] : $REQUESTED_TICKER;
$_REQUESTED_AMOUNT = isset($_REQUEST['REQUESTED_AMOUNT']) ? $_REQUEST['REQUESTED_AMOUNT'] : $REQUESTED_AMOUNT;
$_USER_ID = isset($_REQUEST['USER_ID']) ? $_REQUEST['USER_ID'] : $USER_ID;
$_WALLET_ID = isset($_REQUEST['WALLET_ID']) ? $_REQUEST['WALLET_ID'] : $WALLET_ID;
$_API_KEY = isset($_REQUEST['API_KEY']) ? $_REQUEST['API_KEY'] : $API_KEY;
$_REFERRAL_IP = isset($_REQUEST['REFERRAL_IP']) ? $_REQUEST['REFERRAL_IP'] : getUserIP();
$quote = get_quote($_FIAT_TICKER, $_CRYPTO_TICKER, $_REQUESTED_TICKER, $_REQUESTED_AMOUNT, $_USER_ID, $_WALLET_ID, $_API_KEY, $_REFERRAL_IP);
$response = $quote;
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
case 'order':
try {
$_QUOTE_ID = isset($_REQUEST['QUOTE_ID']) ? $_REQUEST['QUOTE_ID'] : $QUOTE_ID;
if (!$_QUOTE_ID) {
throw new Exception('Quote ID not provided');
}
$_ADDRESS = isset($_REQUEST['ADDRESS']) ? $_REQUEST['ADDRESS'] : $ADDRESS;
$_CRYPTO_TICKER = isset($_REQUEST['CRYPTO_TICKER']) ? $_REQUEST['CRYPTO_TICKER'] : $CRYPTO_TICKER;
$_USER_ID = isset($_REQUEST['USER_ID']) ? $_REQUEST['USER_ID'] : $USER_ID;
$_SIGNUP_TIMESTAMP = isset($_REQUEST['SIGNUP_TIMESTAMP']) ? $_REQUEST['SIGNUP_TIMESTAMP'] : $SIGNUP_TIMESTAMP;
// TODO sanitize $_REQUEST inputs above
// $quote = get_quote();
$order = place_order($_QUOTE_ID, $_ADDRESS, $_CRYPTO_TICKER, $_USER_ID, $_SIGNUP_TIMESTAMP);
// $order = json_decode($order);
// $order->quote = json_decode($quote);
// $order = json_encode($order);
$response = $order;
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
case 'redirect':
try {
if (!isset($_REQUEST['PAYMENT_ID'])) {
$response = json_encode(array('error' => 'true', 'message' => 'Payment ID needed to redirect'));
break;
}
$_PAYMENT_ID = isset($_REQUEST['PAYMENT_ID']) ? $_REQUEST['PAYMENT_ID'] : $PAYMENT_ID;
// TODO sanitize $_REQUEST input
$response = redirect($_PAYMENT_ID);
$redirect = true;
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
case 'success':
try {
$response = success();
$redirect = true;
if (!gettype($response) === 'string') {
if (!array_key_exists('error', $response)) { // TODO correct error detection and handling here
if ($response['error']) {
$redirect = false;
}
}
}
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
case 'failure':
try {
$response = failure();
$redirect = true;
if (!gettype($response) === 'string') {
if (!array_key_exists('error', $response)) { // TODO correct error detection and handling here
if ($response['error']) {
$redirect = false;
}
}
}
} catch (Exception $e) {
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => $e->getMessage()));
}
break;
default:
// $error = true;
$response = json_encode(array('error' => 'true', 'message' => 'No route provided'));
break;
}
if ((!$error && !$redirect) || ($response === FALSE || is_null($response))) {
header('Content-Type: application/json; charset=utf-8');
}
echo $response;
?>