2015-08-28 18:23:24 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2015-09-09 14:15:08 -03:00
|
|
|
angular.module('copayApp.services').factory('glideraService', function($http, $log, isCordova) {
|
2015-08-28 18:23:24 -03:00
|
|
|
var root = {};
|
2015-09-07 19:45:03 -03:00
|
|
|
var credentials = {};
|
2015-09-07 18:48:47 -03:00
|
|
|
|
2015-09-08 12:04:27 -03:00
|
|
|
root.setCredentials = function(network) {
|
2015-09-07 18:48:47 -03:00
|
|
|
if (network == 'testnet') {
|
2015-09-07 19:45:03 -03:00
|
|
|
credentials.HOST = 'https://sandbox.glidera.io';
|
2015-09-09 14:15:08 -03:00
|
|
|
if (isCordova) {
|
2016-04-29 10:33:59 -03:00
|
|
|
credentials.REDIRECT_URI = 'copay://glidera';
|
2015-09-09 14:15:08 -03:00
|
|
|
credentials.CLIENT_ID = 'dfc56e4336e32bb8ba46dde34f3d7d6d';
|
|
|
|
|
credentials.CLIENT_SECRET = '5eb679058f6c7eb81123162323d4fba5';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
credentials.REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
|
|
|
|
|
credentials.CLIENT_ID = '9915b6ffa6dc3baffb87135ed3873d49';
|
|
|
|
|
credentials.CLIENT_SECRET = 'd74eda05b9c6a228fd5c85cfbd0eb7eb';
|
|
|
|
|
}
|
2015-09-07 18:48:47 -03:00
|
|
|
}
|
|
|
|
|
else {
|
2015-09-07 19:45:03 -03:00
|
|
|
credentials.HOST = 'https://glidera.io';
|
2015-09-22 14:11:54 -03:00
|
|
|
if (isCordova) {
|
2016-04-29 10:33:59 -03:00
|
|
|
credentials.REDIRECT_URI = 'copay://glidera';
|
2015-09-22 14:11:54 -03:00
|
|
|
credentials.CLIENT_ID = '9c8023f0ac0128235b7b27a6f2610c83';
|
|
|
|
|
credentials.CLIENT_SECRET = '30431511407b47f25a83bffd72881d55';
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
credentials.REDIRECT_URI = 'urn:ietf:wg:oauth:2.0:oob';
|
|
|
|
|
credentials.CLIENT_ID = '8a9e8a9cf155db430c1ea6c7889afed1';
|
|
|
|
|
credentials.CLIENT_SECRET = '24ddec578f38d5488bfe13601933c05f';
|
|
|
|
|
}
|
2015-09-07 19:45:03 -03:00
|
|
|
};
|
2015-09-07 18:48:47 -03:00
|
|
|
};
|
2015-08-28 18:23:24 -03:00
|
|
|
|
|
|
|
|
root.getOauthCodeUrl = function() {
|
2015-09-07 19:45:03 -03:00
|
|
|
return credentials.HOST
|
2015-08-28 18:23:24 -03:00
|
|
|
+ '/oauth2/auth?response_type=code&client_id='
|
2015-09-07 19:45:03 -03:00
|
|
|
+ credentials.CLIENT_ID
|
2015-08-28 18:23:24 -03:00
|
|
|
+ '&redirect_uri='
|
2015-09-07 19:45:03 -03:00
|
|
|
+ credentials.REDIRECT_URI;
|
2015-08-28 18:23:24 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getToken = function(code, cb) {
|
|
|
|
|
var req = {
|
|
|
|
|
method: 'POST',
|
2015-09-07 19:45:03 -03:00
|
|
|
url: credentials.HOST + '/api/v1/oauth/token',
|
2015-08-28 18:23:24 -03:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json'
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
grant_type : 'authorization_code',
|
|
|
|
|
code: code,
|
2015-09-07 19:45:03 -03:00
|
|
|
client_id : credentials.CLIENT_ID,
|
|
|
|
|
client_secret: credentials.CLIENT_SECRET,
|
|
|
|
|
redirect_uri: credentials.REDIRECT_URI
|
2015-08-28 18:23:24 -03:00
|
|
|
}
|
2015-08-31 18:12:04 -03:00
|
|
|
};
|
2015-08-28 18:23:24 -03:00
|
|
|
|
|
|
|
|
$http(req).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Authorization Access Token: SUCCESS');
|
2015-09-02 16:02:40 -03:00
|
|
|
return cb(null, data.data);
|
2015-08-28 18:23:24 -03:00
|
|
|
}, function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.error('Glidera Authorization Access Token: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Authorization Access Token: ERROR ' + data.statusText);
|
2015-08-28 18:23:24 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var _get = function(endpoint, token) {
|
|
|
|
|
return {
|
|
|
|
|
method: 'GET',
|
2015-09-07 19:45:03 -03:00
|
|
|
url: credentials.HOST + '/api/v1' + endpoint,
|
2015-08-28 18:23:24 -03:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
2015-08-31 18:12:04 -03:00
|
|
|
'Accept': 'application/json',
|
|
|
|
|
'Authorization': 'Bearer ' + token
|
2015-08-28 18:23:24 -03:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-05 14:30:02 -03:00
|
|
|
root.getAccessTokenPermissions = function(token, cb) {
|
2015-08-28 18:23:24 -03:00
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/oauth/token', token)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Access Token Permissions: SUCCESS');
|
2015-08-28 18:23:24 -03:00
|
|
|
return cb(null, data.data);
|
|
|
|
|
}, function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.error('Glidera Access Token Permissions: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Access Token Permissions: ERROR ' + data.statusText);
|
2015-08-28 18:23:24 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getEmail = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/user/email', token)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Get Email: SUCCESS');
|
2015-08-28 18:23:24 -03:00
|
|
|
return cb(null, data.data);
|
|
|
|
|
}, function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.error('Glidera Get Email: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Get Email: ERROR ' + data.statusText);
|
2015-08-28 18:23:24 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getPersonalInfo = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/user/personalinfo', token)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Get Personal Info: SUCCESS');
|
2015-08-28 18:23:24 -03:00
|
|
|
return cb(null, data.data);
|
|
|
|
|
}, function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.error('Glidera Get Personal Info: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Get Personal Info: ERROR ' + data.statusText);
|
2015-08-28 18:23:24 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getStatus = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/user/status', token)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera User Status: SUCCESS');
|
2015-08-28 18:23:24 -03:00
|
|
|
return cb(null, data.data);
|
|
|
|
|
}, function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.error('Glidera User Status: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera User Status: ERROR ' + data.statusText);
|
2015-08-28 18:23:24 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getLimits = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/user/limits', token)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Transaction Limits: SUCCESS');
|
|
|
|
|
return cb(null, data.data);
|
|
|
|
|
}, function(data) {
|
|
|
|
|
$log.error('Glidera Transaction Limits: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Transaction Limits: ERROR ' + data.statusText);
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getTransactions = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/transaction', token)).then(function(data) {
|
2015-09-02 16:02:40 -03:00
|
|
|
$log.info('Glidera Transactions: SUCCESS');
|
2015-09-01 18:53:47 -03:00
|
|
|
return cb(null, data.data.transactions);
|
2015-09-02 16:02:40 -03:00
|
|
|
}, function(data) {
|
|
|
|
|
$log.error('Glidera Transactions: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Transactions: ERROR ' + data.statusText);
|
2015-09-02 16:02:40 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getTransaction = function(token, txid, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
if (!txid) return cb('TxId required');
|
|
|
|
|
$http(_get('/transaction/' + txid, token)).then(function(data) {
|
|
|
|
|
$log.info('Glidera Transaction: SUCCESS');
|
|
|
|
|
return cb(null, data.data);
|
2015-08-28 18:23:24 -03:00
|
|
|
}, function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.error('Glidera Transaction: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Transaction: ERROR ' + data.statusText);
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.getSellAddress = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/user/create_sell_address', token)).then(function(data) {
|
|
|
|
|
$log.info('Glidera Create Sell Address: SUCCESS');
|
2015-09-03 02:50:59 -03:00
|
|
|
return cb(null, data.data.sellAddress);
|
2015-08-31 18:12:04 -03:00
|
|
|
}, function(data) {
|
|
|
|
|
$log.error('Glidera Create Sell Address: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Create Sell Address: ERROR ' + data.statusText);
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-01 18:53:47 -03:00
|
|
|
root.get2faCode = function(token, cb) {
|
|
|
|
|
if (!token) return cb('Invalid Token');
|
|
|
|
|
$http(_get('/authentication/get2faCode', token)).then(function(data) {
|
|
|
|
|
$log.info('Glidera Sent 2FA code by SMS: SUCCESS');
|
|
|
|
|
return cb(null, data.status == 200 ? true : false);
|
|
|
|
|
}, function(data) {
|
|
|
|
|
$log.error('Glidera Sent 2FA code by SMS: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Sent 2FA code by SMS: ERROR ' + data.statusText);
|
2015-09-01 18:53:47 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var _post = function(endpoint, token, twoFaCode, data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
return {
|
|
|
|
|
method: 'POST',
|
2015-09-07 19:45:03 -03:00
|
|
|
url: credentials.HOST + '/api/v1' + endpoint,
|
2015-08-31 18:12:04 -03:00
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
'Accept': 'application/json',
|
2015-09-01 18:53:47 -03:00
|
|
|
'Authorization': 'Bearer ' + token,
|
|
|
|
|
'2FA_CODE': twoFaCode
|
2015-08-31 18:12:04 -03:00
|
|
|
},
|
|
|
|
|
data: data
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.sellPrice = function(token, price, cb) {
|
|
|
|
|
var data = {
|
|
|
|
|
qty: price.qty,
|
|
|
|
|
fiat: price.fiat
|
|
|
|
|
};
|
2015-09-01 18:53:47 -03:00
|
|
|
$http(_post('/prices/sell', token, null, data)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Sell Price: SUCCESS');
|
2015-09-01 18:53:47 -03:00
|
|
|
return cb(null, data.data);
|
2015-08-31 18:12:04 -03:00
|
|
|
}, function(data) {
|
|
|
|
|
$log.error('Glidera Sell Price: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Sell Price: ERROR ' + data.statusText);
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-01 18:53:47 -03:00
|
|
|
root.sell = function(token, twoFaCode, data, cb) {
|
2015-08-31 18:12:04 -03:00
|
|
|
var data = {
|
|
|
|
|
refundAddress: data.refundAddress,
|
|
|
|
|
signedTransaction: data.signedTransaction,
|
|
|
|
|
priceUuid: data.priceUuid,
|
|
|
|
|
useCurrentPrice: data.useCurrentPrice,
|
|
|
|
|
ip: data.ip
|
|
|
|
|
};
|
2015-09-01 18:53:47 -03:00
|
|
|
$http(_post('/sell', token, twoFaCode, data)).then(function(data) {
|
|
|
|
|
$log.info('Glidera Sell: SUCCESS');
|
2015-09-03 02:50:59 -03:00
|
|
|
return cb(null, data.data);
|
2015-08-31 18:12:04 -03:00
|
|
|
}, function(data) {
|
2015-09-08 10:17:59 -03:00
|
|
|
$log.error('Glidera Sell Request: ERROR ' + data.statusText);
|
|
|
|
|
return cb('Glidera Sell Request: ERROR ' + data.statusText);
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
root.buyPrice = function(token, price, cb) {
|
|
|
|
|
var data = {
|
|
|
|
|
qty: price.qty,
|
|
|
|
|
fiat: price.fiat
|
|
|
|
|
};
|
2015-09-01 18:53:47 -03:00
|
|
|
$http(_post('/prices/buy', token, null, data)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Buy Price: SUCCESS');
|
2015-09-01 18:53:47 -03:00
|
|
|
return cb(null, data.data);
|
2015-08-31 18:12:04 -03:00
|
|
|
}, function(data) {
|
|
|
|
|
$log.error('Glidera Buy Price: ERROR ' + data.statusText);
|
2015-09-08 10:17:59 -03:00
|
|
|
return cb('Glidera Buy Price: ERROR ' + data.statusText);
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-01 18:53:47 -03:00
|
|
|
root.buy = function(token, twoFaCode, data, cb) {
|
2015-08-31 18:12:04 -03:00
|
|
|
var data = {
|
|
|
|
|
destinationAddress: data.destinationAddress,
|
|
|
|
|
qty: data.qty,
|
|
|
|
|
priceUuid: data.priceUuid,
|
|
|
|
|
useCurrentPrice: data.useCurrentPrice,
|
|
|
|
|
ip: data.ip
|
|
|
|
|
};
|
2015-09-01 18:53:47 -03:00
|
|
|
$http(_post('/buy', token, twoFaCode, data)).then(function(data) {
|
2015-08-31 18:12:04 -03:00
|
|
|
$log.info('Glidera Buy: SUCCESS');
|
2015-09-02 19:16:59 -03:00
|
|
|
return cb(null, data.data);
|
2015-08-31 18:12:04 -03:00
|
|
|
}, function(data) {
|
2015-09-08 10:17:59 -03:00
|
|
|
$log.error('Glidera Buy Request: ERROR ' + data.statusText);
|
|
|
|
|
return cb('Glidera Buy Request: ERROR ' + data.statusText);
|
2015-08-28 18:23:24 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
|
|
});
|