Wallet/src/js/controllers/buyGlidera.js

78 lines
2.2 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('buyGlideraController',
2015-09-07 13:35:59 -03:00
function($scope, $timeout, profileService, addressService, glideraService, gettext) {
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-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-07 13:35:59 -03:00
glideraService.buyPrice(token, price, function(err, buyPrice) {
if (err) {
self.error = gettext('Glidera could not get pricing to buy bitcoin');
}
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 13:35:59 -03:00
this.loading = true;
2015-09-02 19:16:59 -03:00
$timeout(function() {
2015-09-07 13:35:59 -03:00
glideraService.get2faCode(token, function(err, sent) {
self.loading = false;
if (err) {
self.error = gettext('Glidera could not the 2FA code to your phone');
}
else {
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) {
self.error = gettext('Could not get the bitcoin address');
$scope.$apply();
}
else {
self.loading = true;
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) {
self.loading = false;
if (err) {
self.error = gettext('Could not buy bitcoin');
}
else {
self.success = data;
$scope.$emit('Local/GlideraUpdated', token, permissions);
}
});
}, 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
});