2016-05-13 11:48:48 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-07-13 14:00:21 -03:00
|
|
|
angular.module('copayApp.controllers').controller('buyAmazonController',
|
|
|
|
|
function($rootScope, $scope, $ionicModal, $log, $timeout, lodash, profileService, bwcError, configService, walletService, fingerprintService, amazonService, ongoingProcess) {
|
|
|
|
|
|
2016-05-13 11:48:48 -03:00
|
|
|
var self = this;
|
2016-06-13 15:00:17 -03:00
|
|
|
var client;
|
2016-05-13 11:48:48 -03:00
|
|
|
|
|
|
|
|
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);
|
2016-06-07 21:20:13 -03:00
|
|
|
amazonService.healthCheckRequest();
|
|
|
|
|
amazonService.initUuid();
|
2016-06-13 15:00:17 -03:00
|
|
|
self.allWallets = profileService.getWallets(network, 1);
|
|
|
|
|
client = profileService.focusedClient;
|
2016-07-13 14:00:21 -03:00
|
|
|
if (client && client.credentials.m == 1) {
|
2016-06-13 15:00:17 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
self.selectedWalletId = client.credentials.walletId;
|
|
|
|
|
self.selectedWalletName = client.credentials.walletName;
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
2016-05-13 11:48:48 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$scope.openWalletsModal = function(wallets) {
|
|
|
|
|
self.error = null;
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
$scope.type = 'SELL';
|
|
|
|
|
$scope.wallets = wallets;
|
2016-05-13 11:48:48 -03:00
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
$ionicModal.fromTemplateUrl('views/modals/wallets.html', {
|
|
|
|
|
scope: $scope
|
|
|
|
|
}).then(function(modal) {
|
|
|
|
|
$scope.walletsModal = modal;
|
|
|
|
|
$scope.walletsModal.show();
|
2016-05-13 11:48:48 -03:00
|
|
|
});
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
$scope.$on('walletSelected', function(ev, obj) {
|
2016-05-13 11:48:48 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
self.selectedWalletId = obj.walletId;
|
|
|
|
|
self.selectedWalletName = obj.walletName;
|
2016-06-13 15:00:17 -03:00
|
|
|
client = obj.client;
|
2016-05-13 11:48:48 -03:00
|
|
|
$scope.$apply();
|
|
|
|
|
}, 100);
|
2016-06-13 15:00:17 -03:00
|
|
|
$scope.walletsModal.hide();
|
2016-05-13 11:48:48 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.createTx = function() {
|
|
|
|
|
self.error = null;
|
2016-05-19 17:29:24 -03:00
|
|
|
self.errorInfo = null;
|
2016-05-13 11:48:48 -03:00
|
|
|
|
2016-05-19 01:18:15 -03:00
|
|
|
var currency_code = configService.getSync().amazon.testnet ? window.amazon_sandbox_currency_code : window.amazon_currency_code;
|
2016-07-13 14:00:21 -03:00
|
|
|
var dataSrc = {
|
2016-05-13 11:48:48 -03:00
|
|
|
price: $scope.fiat,
|
2016-06-06 15:51:15 -03:00
|
|
|
currency: currency_code,
|
|
|
|
|
orderId: self.selectedWalletName
|
2016-05-13 11:48:48 -03:00
|
|
|
};
|
|
|
|
|
var outputs = [];
|
|
|
|
|
var config = configService.getSync();
|
|
|
|
|
var configWallet = config.wallet;
|
|
|
|
|
var walletSettings = configWallet.settings;
|
|
|
|
|
|
|
|
|
|
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', true);
|
2016-05-13 11:48:48 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
|
|
|
|
|
amazonService.createBitPayInvoice(dataSrc, function(err, data) {
|
|
|
|
|
if (err) {
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', false);
|
2016-07-13 14:00:21 -03:00
|
|
|
self.error = bwcError.msg(err);
|
2016-06-14 11:44:44 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
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';
|
2016-05-13 11:48:48 -03:00
|
|
|
|
|
|
|
|
outputs.push({
|
|
|
|
|
'toAddress': address,
|
|
|
|
|
'amount': amount,
|
|
|
|
|
'message': comment
|
|
|
|
|
});
|
2016-07-13 14:00:21 -03:00
|
|
|
|
2016-05-13 11:48:48 -03:00
|
|
|
var txp = {
|
|
|
|
|
toAddress: address,
|
|
|
|
|
amount: amount,
|
|
|
|
|
outputs: outputs,
|
|
|
|
|
message: comment,
|
|
|
|
|
payProUrl: null,
|
|
|
|
|
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
|
|
|
|
|
feeLevel: walletSettings.feeLevel || 'normal'
|
|
|
|
|
};
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
walletService.createTx(client, txp, function(err, createdTxp) {
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', false);
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
2016-07-13 14:00:21 -03:00
|
|
|
self.error = bwcError.msg(err);
|
2016-06-14 11:44:44 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
|
2016-07-13 14:00:21 -03:00
|
|
|
if (accept) {
|
2016-05-13 11:48:48 -03:00
|
|
|
self.confirmTx(createdTxp, function(err, tx) {
|
2016-07-13 14:00:21 -03:00
|
|
|
if (err) {
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', false);
|
2016-07-13 14:00:21 -03:00
|
|
|
self.error = bwcError.msg(err);
|
2016-06-14 11:44:44 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
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-05-13 11:48:48 -03:00
|
|
|
};
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', true);
|
2016-05-19 01:18:15 -03:00
|
|
|
amazonService.createGiftCard(gift, function(err, giftCard) {
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', false);
|
2016-07-13 14:00:21 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = bwcError.msg(err);
|
2016-05-19 17:29:24 -03:00
|
|
|
self.errorInfo = gift;
|
2016-06-14 11:44:44 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-06 12:07:47 -03:00
|
|
|
amazonService.setAmountByDay(dataSrc.price);
|
2016-05-13 11:48:48 -03:00
|
|
|
self.giftCard = giftCard;
|
2016-05-19 20:20:06 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
});
|
2016-05-13 11:48:48 -03:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.confirmTx = function(txp, cb) {
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
fingerprintService.check(client, function(err) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
|
|
|
|
return cb(err);
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
handleEncryptedWallet(client, function(err) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
2016-07-13 14:00:21 -03:00
|
|
|
return bwcError.cb(err, null, cb);
|
2016-05-13 11:48:48 -03:00
|
|
|
}
|
|
|
|
|
|
2016-06-14 11:44:44 -03:00
|
|
|
ongoingProcess.set('Processing Transaction...', true);
|
2016-06-13 15:00:17 -03:00
|
|
|
walletService.publishTx(client, txp, function(err, publishedTxp) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
2016-07-13 14:00:21 -03:00
|
|
|
return bwcError.cb(err, null, cb);
|
2016-05-13 11:48:48 -03:00
|
|
|
}
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
|
|
|
|
|
walletService.lock(client);
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
2016-06-13 15:00:17 -03:00
|
|
|
walletService.removeTx(client, signedTxp, function(err) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
});
|
2016-07-13 14:00:21 -03:00
|
|
|
return bwcError.cb(err, null, cb);
|
2016-05-13 11:48:48 -03:00
|
|
|
}
|
|
|
|
|
|
2016-06-13 15:00:17 -03:00
|
|
|
walletService.broadcastTx(client, signedTxp, function(err, broadcastedTxp) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
2016-06-13 15:00:17 -03:00
|
|
|
walletService.removeTx(client, broadcastedTxp, function(err) {
|
2016-05-13 11:48:48 -03:00
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
});
|
2016-07-13 14:00:21 -03:00
|
|
|
return bwcError.cb(err, null, cb);
|
2016-05-13 11:48:48 -03:00
|
|
|
}
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
return cb(null, broadcastedTxp);
|
|
|
|
|
}, 5000);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|