Wallet/src/js/controllers/buyAmazon.js

220 lines
7.6 KiB
JavaScript
Raw Normal View History

'use strict';
2016-07-13 14:00:21 -03:00
angular.module('copayApp.controllers').controller('buyAmazonController',
2016-09-05 14:59:11 -03:00
function($scope, $log, $timeout, $state, lodash, profileService, bwcError, gettextCatalog, configService, walletService, amazonService, ongoingProcess, platformInfo, externalLinkService, popupService) {
2016-07-13 14:00:21 -03:00
var self = this;
2016-09-23 13:05:50 -03:00
var network = amazonService.getEnvironment();
var wallet;
$scope.$on('Wallet/Changed', function(event, w) {
if (lodash.isEmpty(w)) {
$log.debug('No wallet provided');
return;
}
wallet = w;
$log.debug('Wallet changed: ' + w.name);
});
$scope.openExternalLink = function(url, target) {
2016-09-05 14:59:11 -03:00
externalLinkService.open(url, target);
};
this.confirm = function() {
2016-09-23 13:05:50 -03:00
var message = gettextCatalog.getString('Amazon.com Gift Card purchase for ${{amount}} USD', {amount: $scope.formData.fiat});
var ok = gettextCatalog.getString('Buy');
popupService.showConfirm(null, message, ok, null, function(res) {
if (res) self.createTx();
});
};
this.createTx = function() {
2016-05-19 17:29:24 -03:00
self.errorInfo = null;
if (lodash.isEmpty(wallet)) return;
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
$log.info('No signing proposal: No private key');
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg('MISSING_PRIVATE_KEY'));
return;
}
2016-07-13 14:00:21 -03:00
var dataSrc = {
2016-07-29 15:30:38 -03:00
currency: 'USD',
2016-09-23 13:05:50 -03:00
amount: $scope.formData.fiat,
uuid: wallet.id
};
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);
$timeout(function() {
2016-07-21 11:18:48 -03:00
amazonService.createBitPayInvoice(dataSrc, function(err, dataInvoice) {
if (err) {
2016-06-14 11:44:44 -03:00
ongoingProcess.set('Processing Transaction...', false);
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
return;
}
2016-07-21 11:18:48 -03:00
amazonService.getBitPayInvoice(dataInvoice.invoiceId, function(err, invoice) {
if (err) {
2016-07-21 11:18:48 -03:00
ongoingProcess.set('Processing Transaction...', false);
2016-08-30 20:47:55 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
return;
}
2016-07-21 11:18:48 -03:00
2016-08-03 10:05:20 -03:00
$log.debug('Fetch PayPro Request...', invoice.paymentUrls.BIP73);
2016-07-21 11:18:48 -03:00
wallet.fetchPayPro({
2016-08-03 10:05:20 -03:00
payProUrl: invoice.paymentUrls.BIP73,
}, function(err, paypro) {
2016-07-21 11:18:48 -03:00
if (err) {
2016-08-03 15:48:49 -03:00
ongoingProcess.set('Processing Transaction...', false);
2016-08-03 10:05:20 -03:00
$log.warn('Could not fetch payment request:', err);
var msg = err.toString();
if (msg.match('HTTP')) {
2016-08-30 20:47:55 -03:00
msg = gettextCatalog.getString('Could not fetch payment information');
2016-08-03 10:05:20 -03:00
}
2016-08-30 20:47:55 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), msg);
2016-08-03 10:05:20 -03:00
return;
}
if (!paypro.verified) {
2016-08-03 15:48:49 -03:00
ongoingProcess.set('Processing Transaction...', false);
2016-08-03 10:05:20 -03:00
$log.warn('Failed to verify payment protocol signatures');
2016-08-30 20:47:55 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Payment Protocol Invalid'));
2016-07-21 11:18:48 -03:00
$timeout(function() {
$scope.$digest();
});
return;
}
2016-08-03 10:05:20 -03:00
var address, comment, amount, url;
address = paypro.toAddress;
amount = paypro.amount;
url = paypro.url;
comment = 'Amazon.com Gift Card';
outputs.push({
'toAddress': address,
'amount': amount,
'message': comment
});
var txp = {
toAddress: address,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: url,
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
feeLevel: walletSettings.feeLevel || 'normal'
};
walletService.createTx(wallet, txp, function(err, createdTxp) {
2016-08-03 10:05:20 -03:00
ongoingProcess.set('Processing Transaction...', false);
if (err) {
2016-08-30 20:47:55 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
2016-08-03 10:05:20 -03:00
return;
2016-07-21 11:18:48 -03:00
}
2016-08-29 18:54:14 -03:00
walletService.publishAndSign(wallet, createdTxp, function(err, tx) {
2016-08-19 18:03:25 -03:00
if (err) {
ongoingProcess.set('Processing Transaction...', false);
2016-08-30 20:47:55 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
2016-08-29 18:54:14 -03:00
walletService.removeTx(wallet, tx, function(err) {
if (err) $log.debug(err);
});
2016-08-19 18:03:25 -03:00
$timeout(function() {
$scope.$digest();
2016-08-03 10:05:20 -03:00
});
2016-08-19 18:03:25 -03:00
return;
2016-08-03 10:05:20 -03:00
}
2016-08-19 18:03:25 -03:00
var count = 0;
ongoingProcess.set('Processing Transaction...', true);
dataSrc.accessKey = dataInvoice.accessKey;
dataSrc.invoiceId = invoice.id;
dataSrc.invoiceUrl = invoice.url;
dataSrc.invoiceTime = invoice.invoiceTime;
self.debounceCreate(count, dataSrc);
2016-08-03 10:05:20 -03:00
});
2016-07-21 11:18:48 -03:00
});
});
});
});
}, 100);
};
2016-07-21 11:18:48 -03:00
self.debounceCreate = lodash.throttle(function(count, dataSrc) {
self.debounceCreateGiftCard(count, dataSrc);
}, 8000, {
'leading': true
});
self.debounceCreateGiftCard = function(count, dataSrc) {
amazonService.createGiftCard(dataSrc, function(err, giftCard) {
$log.debug("creating gift card " + count);
if (err) {
2016-08-04 13:21:14 -03:00
giftCard = {};
giftCard.status = 'FAILURE';
2016-07-21 11:18:48 -03:00
ongoingProcess.set('Processing Transaction...', false);
2016-08-30 20:47:55 -03:00
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err));
2016-07-21 11:18:48 -03:00
self.errorInfo = dataSrc;
$timeout(function() {
$scope.$digest();
});
}
2016-08-04 13:21:14 -03:00
2016-07-21 11:18:48 -03:00
if (giftCard.status == 'PENDING' && count < 3) {
$log.debug("pending gift card not available yet");
self.debounceCreate(count + 1, dataSrc, dataSrc);
return;
}
var now = moment().unix() * 1000;
2016-07-21 11:18:48 -03:00
var newData = giftCard;
newData['invoiceId'] = dataSrc.invoiceId;
newData['accessKey'] = dataSrc.accessKey;
newData['invoiceUrl'] = dataSrc.invoiceUrl;
newData['amount'] = dataSrc.amount;
newData['date'] = dataSrc.invoiceTime || now;
2016-07-29 12:46:41 -03:00
newData['uuid'] = dataSrc.uuid;
2016-07-21 11:18:48 -03:00
if (newData.status == 'expired') {
amazonService.savePendingGiftCard(newData, {
remove: true
}, function(err) {
return;
});
}
amazonService.savePendingGiftCard(newData, null, function(err) {
ongoingProcess.set('Processing Transaction...', false);
$log.debug("Saving new gift card with status: " + newData.status);
self.giftCard = newData;
2016-09-16 16:25:45 -03:00
if (newData.status == 'PENDING') $state.transitionTo('tabs.giftcards.amazon');
2016-07-21 11:18:48 -03:00
$timeout(function() {
$scope.$digest();
});
});
});
};
2016-07-21 11:18:48 -03:00
$scope.$on("$ionicView.enter", function(event, data){
2016-09-23 13:05:50 -03:00
$scope.formData = { fiat: null };
$scope.wallets = profileService.getWallets({
network: network,
onlyComplete: true
});
});
});