Wallet/src/js/controllers/buyAmazon.js

213 lines
6.9 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.controllers').controller('buyAmazonController',
2016-06-13 21:50:48 -03:00
function($rootScope, $scope, $ionicModal, $log, $timeout, lodash, profileService, bwsError, configService, walletService, fingerprintService, amazonService, ongoingProcess) {
var self = this;
var client;
var limitsAllowed = {
min: 0.01,
max: 2000
};
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
return cb(walletService.unlock(client, password));
});
};
this.init = function() {
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
amazonService.setCredentials(network);
amazonService.healthCheckRequest();
amazonService.initUuid();
self.allWallets = profileService.getWallets(network, 1);
client = profileService.focusedClient;
if (client && client.credentials.m == 1) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
this.checkLimits = function() {
console.log('[buyAmazon.js:37]'); //TODO
if ($scope.fiat && $scope.fiat >= limitsAllowed.min && $scope.fiat <= limitsAllowed.max)
return true;
else
return false;
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
$scope.type = 'SELL';
$scope.wallets = wallets;
$ionicModal.fromTemplateUrl('views/modals/wallets.html', {
scope: $scope
}).then(function(modal) {
$scope.walletsModal = modal;
$scope.walletsModal.show();
});
$scope.$on('walletSelected', function(ev, obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
client = obj.client;
$scope.$apply();
}, 100);
$scope.walletsModal.hide();
});
};
this.createTx = function() {
self.error = null;
2016-05-19 17:29:24 -03:00
self.errorInfo = null;
2016-05-19 01:18:15 -03:00
var currency_code = configService.getSync().amazon.testnet ? window.amazon_sandbox_currency_code : window.amazon_currency_code;
var dataSrc = {
price: $scope.fiat,
currency: currency_code,
orderId: self.selectedWalletName
};
var outputs = [];
var config = configService.getSync();
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Creating invoice...', true);
$timeout(function() {
amazonService.createBitPayInvoice(dataSrc, function(err, data) {
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Creating invoice...', false);
if (err) {
2016-06-06 12:07:47 -03:00
self.error = bwsError.msg(err);
$scope.$apply();
return;
}
var address, comment, amount;
address = data.data.bitcoinAddress;
amount = parseInt((data.data.btcPrice * 100000000).toFixed(0));
2016-05-19 01:18:15 -03:00
comment = 'Amazon.com Gift Card';
outputs.push({
'toAddress': address,
'amount': amount,
'message': comment
});
var txp = {
toAddress: address,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: null,
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
feeLevel: walletSettings.feeLevel || 'normal'
};
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Creating transaction...', true);
walletService.createTx(client, txp, function(err, createdTxp) {
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Creating transaction...', false);
if (err) {
self.error = bwsError.msg(err);
$scope.$apply();
return;
}
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
if (accept) {
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Sending bitcoin...', true);
self.confirmTx(createdTxp, function(err, tx) {
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Sending bitcoin...', false);
if (err) {
2016-06-13 21:50:48 -03:00
self.error = bwsError.msg(err);
$scope.$apply();
return;
}
var gift = {
amount: dataSrc.price,
currencyCode: dataSrc.currency,
2016-05-16 17:51:26 -03:00
bitpayInvoiceId: data.data.id,
bitpayInvoiceUrl: data.data.url
};
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Creating gift card...', true);
2016-05-19 01:18:15 -03:00
amazonService.createGiftCard(gift, function(err, giftCard) {
2016-06-13 21:50:48 -03:00
ongoingProcess.set('Creating gift card...', false);
if (err) {
2016-06-13 21:50:48 -03:00
self.error = bwsError.msg(err);
2016-05-19 17:29:24 -03:00
self.errorInfo = gift;
2016-06-13 21:50:48 -03:00
$scope.$apply();
return;
}
2016-06-06 12:07:47 -03:00
amazonService.setAmountByDay(dataSrc.price);
self.giftCard = giftCard;
2016-05-19 20:20:06 -03:00
$timeout(function() {
$scope.$digest();
});
});
});
}
});
});
});
}, 100);
};
this.confirmTx = function(txp, cb) {
fingerprintService.check(client, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
handleEncryptedWallet(client, function(err) {
if (err) {
$log.debug(err);
return bwsError.cb(err, null, cb);
}
walletService.publishTx(client, txp, function(err, publishedTxp) {
if (err) {
$log.debug(err);
return bwsError.cb(err, null, cb);
}
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
walletService.lock(client);
if (err) {
$log.debug(err);
walletService.removeTx(client, signedTxp, function(err) {
if (err) $log.debug(err);
});
return bwsError.cb(err, null, cb);
}
walletService.broadcastTx(client, signedTxp, function(err, broadcastedTxp) {
if (err) {
$log.debug(err);
walletService.removeTx(client, broadcastedTxp, function(err) {
if (err) $log.debug(err);
});
return bwsError.cb(err, null, cb);
}
$timeout(function() {
return cb(null, broadcastedTxp);
}, 5000);
});
});
});
});
});
};
});