2015-08-31 18:12:04 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
angular.module('copayApp.controllers').controller('buyGlideraController',
|
2015-09-08 13:58:24 -03:00
|
|
|
function($scope, $timeout, profileService, addressService, glideraService, gettext, gettextCatalog, bwsError) {
|
2015-09-01 18:53:47 -03:00
|
|
|
|
|
|
|
|
this.addr = {};
|
2015-09-02 19:16:59 -03:00
|
|
|
this.show2faCodeInput = null;
|
|
|
|
|
this.error = null;
|
|
|
|
|
this.success = null;
|
2015-09-08 12:04:27 -03:00
|
|
|
this.loading = null;
|
2015-09-01 18:53:47 -03:00
|
|
|
|
2015-08-31 18:12:04 -03:00
|
|
|
this.getBuyPrice = function(token, price) {
|
|
|
|
|
var self = this;
|
2015-09-02 19:16:59 -03:00
|
|
|
if (!price || (price && !price.qty && !price.fiat)) {
|
|
|
|
|
this.buyPrice = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-09-09 15:24:47 -03:00
|
|
|
this.gettingBuyPrice = true;
|
2015-09-07 13:35:59 -03:00
|
|
|
glideraService.buyPrice(token, price, function(err, buyPrice) {
|
2015-09-09 15:24:47 -03:00
|
|
|
self.gettingBuyPrice = false;
|
2015-09-07 13:35:59 -03:00
|
|
|
if (err) {
|
2015-09-08 13:58:24 -03:00
|
|
|
self.error = gettext('Could not get exchange information. Please, try again.');
|
2015-09-07 13:35:59 -03:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
self.buyPrice = buyPrice;
|
|
|
|
|
}
|
2015-08-31 18:12:04 -03:00
|
|
|
});
|
2015-09-01 18:53:47 -03:00
|
|
|
};
|
|
|
|
|
|
2015-09-02 19:16:59 -03:00
|
|
|
this.get2faCode = function(token) {
|
2015-09-01 18:53:47 -03:00
|
|
|
var self = this;
|
2015-09-07 17:43:55 -03:00
|
|
|
this.loading = gettext('Sending 2FA code...');
|
2015-09-02 19:16:59 -03:00
|
|
|
$timeout(function() {
|
2015-09-07 13:35:59 -03:00
|
|
|
glideraService.get2faCode(token, function(err, sent) {
|
2015-09-07 17:43:55 -03:00
|
|
|
self.loading = null;
|
2015-09-07 13:35:59 -03:00
|
|
|
if (err) {
|
2015-09-08 13:58:24 -03:00
|
|
|
self.error = gettext('Could not send confirmation code to your phone');
|
2015-09-07 13:35:59 -03:00
|
|
|
}
|
|
|
|
|
else {
|
2015-09-07 17:43:55 -03:00
|
|
|
self.error = null;
|
2015-09-07 13:35:59 -03:00
|
|
|
self.show2faCodeInput = sent;
|
|
|
|
|
}
|
2015-09-02 19:16:59 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-09-01 18:53:47 -03:00
|
|
|
};
|
|
|
|
|
|
2015-09-07 13:35:59 -03:00
|
|
|
this.sendRequest = function(token, permissions, twoFaCode) {
|
2015-09-01 18:53:47 -03:00
|
|
|
var fc = profileService.focusedClient;
|
2015-09-02 19:16:59 -03:00
|
|
|
if (!fc) return;
|
2015-09-01 18:53:47 -03:00
|
|
|
var self = this;
|
2015-09-07 13:35:59 -03:00
|
|
|
self.error = null;
|
2015-09-02 19:16:59 -03:00
|
|
|
addressService.getAddress(fc.credentials.walletId, null, function(err, addr) {
|
2015-09-07 13:35:59 -03:00
|
|
|
if (!addr) {
|
2015-09-08 13:58:24 -03:00
|
|
|
self.error = bwsError.msg(err);
|
2015-09-07 13:35:59 -03:00
|
|
|
$scope.$apply();
|
|
|
|
|
}
|
|
|
|
|
else {
|
2015-09-07 17:43:55 -03:00
|
|
|
self.loading = gettext('Buying bitcoin...');
|
2015-09-02 19:16:59 -03:00
|
|
|
var data = {
|
|
|
|
|
destinationAddress: addr,
|
|
|
|
|
qty: self.buyPrice.qty,
|
|
|
|
|
priceUuid: self.buyPrice.priceUuid,
|
|
|
|
|
useCurrentPrice: false,
|
|
|
|
|
ip: null
|
|
|
|
|
};
|
2015-09-07 13:35:59 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
glideraService.buy(token, twoFaCode, data, function(err, data) {
|
2015-09-07 17:43:55 -03:00
|
|
|
self.loading = null;
|
2015-09-07 13:35:59 -03:00
|
|
|
if (err) {
|
2015-09-08 10:17:59 -03:00
|
|
|
self.error = err;
|
2015-09-07 13:35:59 -03:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
self.success = data;
|
2015-09-09 10:39:35 -03:00
|
|
|
$scope.$emit('Local/GlideraTx');
|
2015-09-07 13:35:59 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-09-02 19:16:59 -03:00
|
|
|
}
|
|
|
|
|
});
|
2015-09-01 18:53:47 -03:00
|
|
|
};
|
2015-08-31 18:12:04 -03:00
|
|
|
|
|
|
|
|
});
|