amazon integration refactor
This commit is contained in:
parent
42a8176d7c
commit
6dff2840c8
12 changed files with 336 additions and 454 deletions
|
|
@ -1,24 +1,70 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('amazonController',
|
||||
function($scope, $timeout, $ionicModal, lodash, configService, amazonService) {
|
||||
angular.module('copayApp.controllers').controller('amazonController',
|
||||
function($scope, $timeout, $ionicModal, $log, lodash, bwcError, configService, amazonService) {
|
||||
|
||||
this.init = function() {
|
||||
var self = this;
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
self.sandbox = network == 'testnet' ? true : false;
|
||||
amazonService.setCredentials(network);
|
||||
amazonService.getGiftCards(function(err, gcds) {
|
||||
amazonService.getPendingGiftCards(function(err, gcds) {
|
||||
if (err) {
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
self.giftCards = lodash.isEmpty(gcds) ? null : gcds;
|
||||
$scope.giftCards = lodash.isEmpty(gcds) ? null : gcds;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
};
|
||||
this.updatePendingGiftCards();
|
||||
}
|
||||
|
||||
this.updatePendingGiftCards = lodash.debounce(function() {
|
||||
|
||||
amazonService.getPendingGiftCards(function(err, gcds) {
|
||||
lodash.forEach(gcds, function(dataFromStorage) {
|
||||
if (dataFromStorage.status == 'PENDING') {
|
||||
$log.debug("creating gift card");
|
||||
amazonService.createGiftCard(dataFromStorage, function(err, giftCard) {
|
||||
if (err) {
|
||||
$log.debug(bwcError.msg(err));
|
||||
return;
|
||||
}
|
||||
if (giftCard.status != 'PENDING') {
|
||||
var newData = {};
|
||||
|
||||
lodash.merge(newData, dataFromStorage, giftCard);
|
||||
|
||||
if (newData.status == 'expired') {
|
||||
amazonService.savePendingGiftCard(newData, {
|
||||
remove: true
|
||||
}, function(err) {
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
amazonService.savePendingGiftCard(newData, null, function(err) {
|
||||
$log.debug("Saving new gift card");
|
||||
amazonService.getPendingGiftCards(function(err, gcds) {
|
||||
if (err) {
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
$scope.giftCards = gcds;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
});
|
||||
} else $log.debug("pending gift card not available yet");
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}, 1000);
|
||||
|
||||
this.openCardModal = function(card) {
|
||||
var self = this;
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('buyAmazonController',
|
||||
function($rootScope, $scope, $ionicModal, $log, $timeout, lodash, profileService, bwcError, configService, walletService, fingerprintService, amazonService, ongoingProcess) {
|
||||
function($rootScope, $scope, $ionicModal, $log, $timeout, $state, lodash, profileService, bwcError, configService, walletService, fingerprintService, amazonService, ongoingProcess) {
|
||||
|
||||
var self = this;
|
||||
var client;
|
||||
|
|
@ -17,11 +17,9 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
|
|||
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) {
|
||||
if (client && client.credentials.m == 1 && client.credentials.network == network) {
|
||||
$timeout(function() {
|
||||
self.selectedWalletId = client.credentials.walletId;
|
||||
self.selectedWalletName = client.credentials.walletName;
|
||||
|
|
@ -62,9 +60,8 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
|
|||
|
||||
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
|
||||
amount: $scope.fiat
|
||||
};
|
||||
var outputs = [];
|
||||
var config = configService.getSync();
|
||||
|
|
@ -74,8 +71,7 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
|
|||
|
||||
ongoingProcess.set('Processing Transaction...', true);
|
||||
$timeout(function() {
|
||||
|
||||
amazonService.createBitPayInvoice(dataSrc, function(err, data) {
|
||||
amazonService.createBitPayInvoice(dataSrc, function(err, dataInvoice) {
|
||||
if (err) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
self.error = bwcError.msg(err);
|
||||
|
|
@ -85,79 +81,131 @@ angular.module('copayApp.controllers').controller('buyAmazonController',
|
|||
return;
|
||||
}
|
||||
|
||||
var address, comment, amount;
|
||||
|
||||
address = data.data.bitcoinAddress;
|
||||
amount = parseInt((data.data.btcPrice * 100000000).toFixed(0));
|
||||
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'
|
||||
};
|
||||
|
||||
walletService.createTx(client, txp, function(err, createdTxp) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
amazonService.getBitPayInvoice(dataInvoice.invoiceId, function(err, invoice) {
|
||||
if (err) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
self.error = bwcError.msg(err);
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
return;
|
||||
}
|
||||
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
|
||||
if (accept) {
|
||||
self.confirmTx(createdTxp, function(err, tx) {
|
||||
if (err) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
self.error = bwcError.msg(err);
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
return;
|
||||
}
|
||||
var gift = {
|
||||
amount: dataSrc.price,
|
||||
currencyCode: dataSrc.currency,
|
||||
bitpayInvoiceId: data.data.id,
|
||||
bitpayInvoiceUrl: data.data.url
|
||||
};
|
||||
ongoingProcess.set('Processing Transaction...', true);
|
||||
amazonService.createGiftCard(gift, function(err, giftCard) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
|
||||
var address, comment, amount;
|
||||
|
||||
address = invoice.bitcoinAddress;
|
||||
amount = parseInt((invoice.btcPrice * 100000000).toFixed(0));
|
||||
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'
|
||||
};
|
||||
|
||||
walletService.createTx(client, txp, function(err, createdTxp) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
if (err) {
|
||||
self.error = bwcError.msg(err);
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
return;
|
||||
}
|
||||
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
|
||||
if (accept) {
|
||||
self.confirmTx(createdTxp, function(err, tx) {
|
||||
if (err) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
self.error = bwcError.msg(err);
|
||||
self.errorInfo = gift;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
return;
|
||||
}
|
||||
amazonService.setAmountByDay(dataSrc.price);
|
||||
self.giftCard = giftCard;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 100);
|
||||
};
|
||||
|
||||
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) {
|
||||
ongoingProcess.set('Processing Transaction...', false);
|
||||
self.error = bwcError.msg(err);
|
||||
self.errorInfo = dataSrc;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
return;
|
||||
}
|
||||
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();
|
||||
|
||||
var newData = giftCard;
|
||||
newData['invoiceId'] = dataSrc.invoiceId;
|
||||
newData['accessKey'] = dataSrc.accessKey;
|
||||
newData['invoiceUrl'] = dataSrc.invoiceUrl;
|
||||
newData['amount'] = dataSrc.amount;
|
||||
newData['date'] = dataSrc.invoiceTime || now;
|
||||
|
||||
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;
|
||||
if (newData.status == 'PENDING') $state.transitionTo('amazon');
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
this.confirmTx = function(txp, cb) {
|
||||
|
||||
fingerprintService.check(client, function(err) {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, $ionicScrollDelegate, $ionicPopup, $ionicSideMenuDelegate, latestReleaseService, feeService, bwcService, pushNotificationsService, lodash, go, profileService, configService, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, addonManager, bwcError, txFormatService, uxLanguage, glideraService, coinbaseService, platformInfo, addressbookService, openURLService, ongoingProcess) {
|
||||
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, $ionicScrollDelegate, $ionicPopup, $ionicSideMenuDelegate, $httpBackend, latestReleaseService, feeService, bwcService, pushNotificationsService, lodash, go, profileService, configService, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, addonManager, bwcError, txFormatService, uxLanguage, glideraService, coinbaseService, amazonService, platformInfo, addressbookService, openURLService, ongoingProcess) {
|
||||
|
||||
var self = this;
|
||||
var SOFT_CONFIRMATION_LIMIT = 12;
|
||||
var errors = bwcService.getErrors();
|
||||
|
|
@ -1041,10 +1042,6 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
});
|
||||
};
|
||||
|
||||
self.initAmazon = function() {
|
||||
self.amazonEnabled = configService.getSync().amazon.enabled;
|
||||
};
|
||||
|
||||
self.initGlidera = function(accessToken) {
|
||||
self.glideraEnabled = configService.getSync().glidera.enabled;
|
||||
self.glideraTestnet = configService.getSync().glidera.testnet;
|
||||
|
|
@ -1394,6 +1391,15 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
});
|
||||
};
|
||||
|
||||
|
||||
self.initAmazon = function() {
|
||||
self.amazonEnabled = configService.getSync().amazon.enabled;
|
||||
self.amazonTestnet = configService.getSync().amazon.testnet;
|
||||
var network = self.amazonTestnet ? 'testnet' : 'livenet';
|
||||
if (!self.amazonEnabled) return;
|
||||
amazonService.setCredentials(network);
|
||||
};
|
||||
|
||||
self.isInFocus = function(walletId) {
|
||||
var fc = profileService.focusedClient;
|
||||
return fc && fc.credentials.walletId == walletId;
|
||||
|
|
|
|||
|
|
@ -1,53 +1,45 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('amazonCardDetailsController', function($scope, $timeout, amazonService, ongoingProcess) {
|
||||
|
||||
$scope.cancelGiftCard = function() {
|
||||
var dataSrc = {
|
||||
creationRequestId: $scope.card.creationRequestId,
|
||||
gcId: $scope.card.gcId,
|
||||
bitpayInvoiceId: $scope.card.bitpayInvoiceId,
|
||||
bitpayInvoiceUrl: $scope.card.bitpayInvoiceUrl,
|
||||
date: $scope.card.date
|
||||
};
|
||||
ongoingProcess.set('Canceling gift card...', true);
|
||||
amazonService.cancelGiftCard(dataSrc, function(err, data) {
|
||||
ongoingProcess.set('Canceling gift card...', false);
|
||||
if (err || data.status != 'SUCCESS') {
|
||||
$scope.error = err || data.status;
|
||||
return;
|
||||
}
|
||||
$scope.refreshGiftCard();
|
||||
});
|
||||
};
|
||||
angular.module('copayApp.controllers').controller('amazonCardDetailsController', function($scope, $log, $timeout, bwcError, amazonService, lodash, ongoingProcess) {
|
||||
|
||||
$scope.remove = function() {
|
||||
amazonService.saveGiftCard($scope.card, {remove: true}, function(err) {
|
||||
amazonService.savePendingGiftCard($scope.card, {
|
||||
remove: true
|
||||
}, function(err) {
|
||||
$scope.$emit('UpdateAmazonList');
|
||||
$scope.cancel();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.refreshGiftCard = function() {
|
||||
var dataSrc = {
|
||||
creationRequestId: $scope.card.creationRequestId,
|
||||
amount: $scope.card.cardInfo.value.amount,
|
||||
currencyCode: $scope.card.cardInfo.value.currencyCode,
|
||||
bitpayInvoiceId: $scope.card.bitpayInvoiceId,
|
||||
bitpayInvoiceUrl: $scope.card.bitpayInvoiceUrl,
|
||||
date: $scope.card.date
|
||||
};
|
||||
ongoingProcess.set('Updating gift card...', true);
|
||||
amazonService.createGiftCard(dataSrc, function(err, data) {
|
||||
ongoingProcess.set('Updating gift card...', false);
|
||||
amazonService.getPendingGiftCards(function(err, gcds) {
|
||||
if (err) {
|
||||
$scope.error = err;
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
$scope.$emit('UpdateAmazonList');
|
||||
$scope.card = data;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
lodash.forEach(gcds, function(dataFromStorage) {
|
||||
if (dataFromStorage.status == 'PENDING' && dataFromStorage.invoiceId == $scope.card.invoiceId) {
|
||||
$log.debug("creating gift card");
|
||||
amazonService.createGiftCard(dataFromStorage, function(err, giftCard) {
|
||||
if (err) {
|
||||
self.error = bwcError.msg(err);
|
||||
$log.debug(bwcError.msg(err));
|
||||
return;
|
||||
}
|
||||
if (!lodash.isEmpty(giftCard)) {
|
||||
var newData = {};
|
||||
lodash.merge(newData, dataFromStorage, giftCard);
|
||||
amazonService.savePendingGiftCard(newData, null, function(err) {
|
||||
$log.debug("Saving new gift card");
|
||||
$scope.card = newData;
|
||||
$scope.$emit('UpdateAmazonList');
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
} else $log.debug("pending gift card not available yet");
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue