-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthScript.js
More file actions
106 lines (94 loc) · 3.17 KB
/
Copy pathAuthScript.js
File metadata and controls
106 lines (94 loc) · 3.17 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
let relysiaAuthWin;
// eslint-disable-next-line no-unused-vars
function RelysiaOAuth (onSuccess, onError, clientKey, clientSecret) {
if (!onSuccess || !onError || !clientKey || !clientSecret) {
throw new Error('RelysiaOAuth : missing required params');
}
const serverUrl = 'https://relysia-pevyuqi8i-kohze.vercel.app';
const onAuthResp = async (resp) => {
if (resp.data.error) {
onError(resp.data);
} else {
try {
const details = await fetchData(resp.data.accessCode);
onSuccess(details);
} catch (error) {
onError(error);
}
}
relysiaAuthWin.close();
};
const fetchData = async (accessCode) => {
const oAuthData = {
clientKey,
code: accessCode,
clientSecret,
};
const oAuthRes = await fetch(
`https://wallet.vaionex.com/v1/oauth/token`,
{
method: "POST",
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
},
body: JSON.stringify(oAuthData),
}
);
const jsonOauthRes = await oAuthRes.json();
return jsonOauthRes;
}
const signIn = () => {
const ssoUrl = `${serverUrl}/auth/login?clientKey=${clientKey}`;
const SSO_WINDOW_NAME = "auth_window";
const left = (window.screen.width/2)-(800/2);
const top = (window.screen.height/2)-(800/2);
relysiaAuthWin = window.open(ssoUrl, SSO_WINDOW_NAME, `toolbar=yes,scrollbars=yes,resizable=yes,top=${top},left=${left},width=800,height=800`);
if (window.addEventListener) {
window.addEventListener("message", onAuthResp, false);
}
};
signIn();
}
const ApiHost = `https://wallet.vaionex.com`;
var getWallets = async (opts) => {
if (!opts.oauthToken) throw new Error('Oauth Token is missing');
const resp = await fetch(`${ApiHost}/v1/wallets`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"oauth": `${opts.oauthToken}`
}
});
const getAllWallet = await resp.json();
return getAllWallet;
}
var pay = async (opts) => {
if(!opts.paymailAddress) throw new Error('Paymail Address is missing');
if(!opts.amount) throw new Error('Pay amount is missing');
if(!opts.oauthToken) throw new Error('Oauth Token is missing');
if(!opts.walletid) throw new Error('Wallet Id is missing');
const getURI = await fetch(`${ApiHost}/v1/URI`, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"uri": `payto:${opts.paymailAddress}?amount=${opts.amount}`,
}
});
const toJsonGetURI = await getURI.json();
// console.log(toJsonGetURI);
// paying
const payResponse = await fetch(`${ApiHost}/v1/pay`, {
method: 'POST',
headers: {
"Content-Type": "application/json",
"Access-Control-Allow-Origin": "*",
"walletid": `${opts.walletid}`,
"oauth": `${opts.oauthToken}`
},
body: JSON.stringify(toJsonGetURI.data.data),
});
return payResponse;
}