-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrequestsOfficial.js
More file actions
239 lines (179 loc) · 6.85 KB
/
requestsOfficial.js
File metadata and controls
239 lines (179 loc) · 6.85 KB
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
var firebase = require("firebase/app");
// Add the Firebase products that you want to use
require("firebase/auth");
require("firebase/database")
// Your web app's Firebase configuration
let firebaseConfig = {
apiKey: "AIzaSyAyQX877wOO217haEL0yRY4GREhXFFuT00",
authDomain: "hackillinois2020.firebaseapp.com",
databaseURL: "https://hackillinois2020.firebaseio.com",
projectId: "hackillinois2020",
storageBucket: "hackillinois2020.appspot.com",
messagingSenderId: "70394633571",
appId: "1:70394633571:web:9f8c54485d5240ad024756",
measurementId: "G-WM1X40BVL1"
};
// Initialize Firebase
try {firebase.initializeApp(firebaseConfig);} catch{}
// key is the qr_id in this case and the child would whatever value you wanted
// for example, key could be 12345 and child could be either email, price, or product
// alternatively, if you're looking for a secret key, key would be the user id (some long string)
// and child would be "secret_key" or "email" if you were trying to get an email from a user id
function read(key, child, callback) {
firebase.database().goOnline();
var rootRef = firebase.database().ref().child(key);
//window.alert("test");
// window.alert(rootRef)
// console.log(rootRef)
// console.log(key)
rootRef.on("value", snap => {
var target = snap.child(child).val();
// console.log(target);
firebase.database().goOffline();
callback(target);
});
// firebase.database().goOffline();
}
function writeUserData(userId, email, secret_key) {
firebase.database().goOnline();
// console.log("ATTEMPT DATA WRITE")
// console.log(userId)
firebase.database().ref(userId).set({
email: email,
secret_key: secret_key
}).then(() => {firebase.database().goOffline();});;
}
const fetch = require("node-fetch");
const global_auth = "f7a6f17719994b80237fd372ca7735d1:839f1e2235496fd7f0fc266fb34a04e4";
async function sendCheckToUser(sender, recieverEmail, amount) {
try {
// console.log('line', sender, recieverEmail, amount);
//first need to get the right info from firebase
//the below will probably go in the firebase callback
var data = `{\"recipient\":\"${recieverEmail}\",\"name\":\"John Mayer\",\"amount\":${amount},\"description\":\"Test Check\"}`;
// console.log(data)
let response = await fetch("https://sandbox.checkbook.io/v3/check/digital", {
method: 'POST',
headers: {
'accept': 'application/json',
'content-Type': 'application/json',
'authorization': `${sender}`
},
body: data,
});
let responseJson = await response.json();
// console.log(responseJson)
return responseJson;
} catch (error) {
console.error(error);
}
}
// console.log(sendCheckToUser('8ebfd5cc92a84fee92680ce94fa7d2bc:r63LL14nVScEADTMkXVYsjINt0qiV7', 'msp@gitconnect.io', 100))
// console.log(emailToSecret("jmayor@mayor.com"))
// sendCheckToUser('23d1092da427469085d72cb201674e63:wKtFwrKpLvAlJeRpnyJGWkkP6DTgVA', 'labalawren@gitconnect.io')
async function verifyMicroDeposit(secret_key) {
try {
//first need to get the right info from firebase
//the below will probably go in the firebase callback
var data = "{\"amount_1\":0.07,\"amount_2\":0.15}";
let response = await fetch("https://sandbox.checkbook.io/v3/bank/verify", {
method: 'POST',
headers: {
'accept': 'application/json',
'content-Type': 'application/json',
'authorization': `${secret_key}`
},
body: data,
});
} catch (error) {
console.error(error);
}
}
//add bank account
async function addBankAccount(secret_key, accountNum , accountType) {
try {
// console.log(accountNum)
var data = `{\"routing\":\"021000021\",\"account\":\"${accountNum}\",\"type\":\"${accountType}\"}`;
// console.log(data)
// console.log(secret_key)
let response = await fetch("https://sandbox.checkbook.io/v3/bank", {
method: 'POST',
headers: {
'accept': 'application/json',
'content-Type': 'application/json',
'authorization': `${secret_key}`
},
body: data,
}).then( () => {
//here call verficiation
verifyMicroDeposit(secret_key);
});
let responseJson = await response
// console.log(responseJson)
return responseJson;
} catch (error) {
console.error(error);
}
}
async function createNewUser(name, email) {
try {
//before you do this add a check in the get all dictionary for this email
var data = `{\"user_id\":\"${email}\",\"name\":\"${name}\"}`;
let response = await fetch("https://sandbox.checkbook.io/v3/user", {
method: 'POST',
headers: {
'accept': 'application/json',
'content-Type': 'application/json',
'authorization': global_auth
},
body: data,
}).then(
);
let responseJson = await response.json();
// console.log(responseJson)
// console.log(typeof responseJson)
writeUserData(`${responseJson.id}`, `${email}`, `${responseJson.key}:${responseJson.secret}`);
addBankAccount(`${responseJson.key}:${responseJson.secret}`, `12340000`, 'CHECKING')
return responseJson;
} catch (error) {
console.error(error);
}
}
// createNewUser("Barack Obama", "barackobama@whitehouse.gov")
async function emailToSecret(email, callback) {
//before you do this add a check in the get all dictionary for this email
let response = await fetch("https://sandbox.checkbook.io/v3/user/list", {
method: 'GET',
headers: {
'accept': 'application/json',
'content-Type': 'application/json',
'authorization': global_auth
},
}).then(
);
let responseJson = await response.json();
// console.log(responseJson)
// console.log(typeof responseJson)
//now get the email
for (i = 0; i < responseJson.users.length; i++) {
// console.log(responseJson.users[i].user_id);
if (responseJson.users[i].user_id == email) {
// console.log(responseJson.users[i].id);
// console.log(read('c5b37975a6584413bce649c3911aa9c9', 'secret_key'))
read(`${responseJson.users[i].id}`, 'secret_key', callback);
return;
}
}
throw 'No email found'
// return responseJson;
}
var RESTfunct = {
emailToSecret: emailToSecret,
getProduct: {
name: (qr_id, callback) => {read(qr_id, 'product', callback);},
vendor_email: (qr_id, callback) => {read(qr_id, 'email', callback);},
price: (qr_id, callback) => {read(qr_id, 'price', callback);}
},
sendTransaction: sendCheckToUser
};
export default RESTfunct;