First integration. BitPay invoice generation is ready. Fake responses from amazon
This commit is contained in:
parent
2c442d3642
commit
2c89ff7f66
17 changed files with 734 additions and 2 deletions
53
src/js/controllers/amazon.js
Normal file
53
src/js/controllers/amazon.js
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('amazonController',
|
||||
function($rootScope, $scope, $timeout, $modal, profileService, configService, storageService, amazonService, isChromeApp, animationService, lodash, nodeWebkit) {
|
||||
|
||||
window.ignoreMobilePause = true;
|
||||
|
||||
this.init = function() {
|
||||
var self = this;
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
amazonService.setCredentials(network);
|
||||
amazonService.getGiftCards(function(err, gcds) {
|
||||
if (err) {
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
self.giftCards = gcds;
|
||||
|
||||
});
|
||||
};
|
||||
|
||||
this.openCardModal = function(card) {
|
||||
$rootScope.modalOpened = true;
|
||||
var self = this;
|
||||
var fc = profileService.focusedClient;
|
||||
var ModalInstanceCtrl = function($scope, $modalInstance) {
|
||||
$scope.card = card;
|
||||
|
||||
$scope.cancel = lodash.debounce(function() {
|
||||
$modalInstance.dismiss('cancel');
|
||||
}, 0, 1000);
|
||||
|
||||
};
|
||||
|
||||
var modalInstance = $modal.open({
|
||||
templateUrl: 'views/modals/amazon-card-details.html',
|
||||
windowClass: animationService.modalAnimated.slideRight,
|
||||
controller: ModalInstanceCtrl,
|
||||
});
|
||||
|
||||
var disableCloseModal = $rootScope.$on('closeModal', function() {
|
||||
modalInstance.dismiss('cancel');
|
||||
});
|
||||
|
||||
modalInstance.result.finally(function() {
|
||||
$rootScope.modalOpened = false;
|
||||
disableCloseModal();
|
||||
var m = angular.element(document.getElementsByClassName('reveal-modal'));
|
||||
m.addClass(animationService.modalAnimated.slideOutRight);
|
||||
});
|
||||
};
|
||||
|
||||
});
|
||||
239
src/js/controllers/buyAmazon.js
Normal file
239
src/js/controllers/buyAmazon.js
Normal file
|
|
@ -0,0 +1,239 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('buyAmazonController',
|
||||
function($rootScope, $scope, $modal, $log, $timeout, lodash, profileService, animationService, bwsError, configService, walletService, fingerprintService, amazonService) {
|
||||
|
||||
window.ignoreMobilePause = true;
|
||||
var self = this;
|
||||
var fc;
|
||||
var minimumAmount = 1;
|
||||
var stepAmount = 1;
|
||||
var multiplierAmount = 2;
|
||||
var maximumAmount = 10;
|
||||
|
||||
var otherWallets = function(network) {
|
||||
return lodash.filter(profileService.getWallets(network), function(w) {
|
||||
return w.network == network && w.m == 1;
|
||||
});
|
||||
};
|
||||
|
||||
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() {
|
||||
$scope.fiat = minimumAmount * multiplierAmount;
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
amazonService.setCredentials(network);
|
||||
self.otherWallets = otherWallets(network);
|
||||
// Choose focused wallet
|
||||
try {
|
||||
var currentWalletId = profileService.focusedClient.credentials.walletId;
|
||||
lodash.find(self.otherWallets, function(w) {
|
||||
if (w.id == currentWalletId) {
|
||||
$timeout(function() {
|
||||
self.selectedWalletId = w.id;
|
||||
self.selectedWalletName = w.name;
|
||||
fc = profileService.getClient(w.id);
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
}
|
||||
});
|
||||
} catch (e) {
|
||||
$log.debug(e);
|
||||
};
|
||||
};
|
||||
|
||||
$scope.openWalletsModal = function(wallets) {
|
||||
self.error = null;
|
||||
var ModalInstanceCtrl = function($scope, $modalInstance) {
|
||||
$scope.type = 'SELL';
|
||||
$scope.wallets = wallets;
|
||||
$scope.noColor = true;
|
||||
$scope.cancel = function() {
|
||||
$modalInstance.dismiss('cancel');
|
||||
};
|
||||
|
||||
$scope.selectWallet = function(walletId, walletName) {
|
||||
if (!profileService.getClient(walletId).isComplete()) {
|
||||
self.error = bwsError.msg('WALLET_NOT_COMPLETE');
|
||||
$modalInstance.dismiss('cancel');
|
||||
return;
|
||||
}
|
||||
$modalInstance.close({
|
||||
'walletId': walletId,
|
||||
'walletName': walletName,
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
var modalInstance = $modal.open({
|
||||
templateUrl: 'views/modals/wallets.html',
|
||||
windowClass: animationService.modalAnimated.slideUp,
|
||||
controller: ModalInstanceCtrl,
|
||||
});
|
||||
|
||||
modalInstance.result.finally(function() {
|
||||
var m = angular.element(document.getElementsByClassName('reveal-modal'));
|
||||
m.addClass(animationService.modalAnimated.slideOutDown);
|
||||
});
|
||||
|
||||
modalInstance.result.then(function(obj) {
|
||||
$timeout(function() {
|
||||
self.selectedWalletId = obj.walletId;
|
||||
self.selectedWalletName = obj.walletName;
|
||||
fc = profileService.getClient(obj.walletId);
|
||||
$scope.$apply();
|
||||
}, 100);
|
||||
});
|
||||
};
|
||||
|
||||
this.setAmount = function(plus) {
|
||||
if (plus && $scope.fiat < maximumAmount ) {
|
||||
stepAmount = stepAmount + 1;
|
||||
$scope.fiat = stepAmount * multiplierAmount;
|
||||
} else if (!plus && $scope.fiat > minimumAmount * multiplierAmount) {
|
||||
stepAmount = stepAmount - 1;
|
||||
$scope.fiat = stepAmount * multiplierAmount;
|
||||
}
|
||||
};
|
||||
|
||||
this.createTx = function() {
|
||||
self.error = null;
|
||||
|
||||
var dataSrc = {
|
||||
price: $scope.fiat,
|
||||
currency: 'USD'
|
||||
};
|
||||
var outputs = [];
|
||||
var config = configService.getSync();
|
||||
var configWallet = config.wallet;
|
||||
var walletSettings = configWallet.settings;
|
||||
|
||||
|
||||
self.loading = 'Creating invoice...';
|
||||
$timeout(function() {
|
||||
|
||||
amazonService.createBitPayInvoice(dataSrc, function(err, data) {
|
||||
if (err) {
|
||||
self.loading = null;
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
|
||||
var address, comment, amount;
|
||||
|
||||
address = data.data.bitcoinAddress;
|
||||
amount = parseInt((data.data.btcPrice * 100000000).toFixed(0));
|
||||
comment = 'Buy Amazon 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'
|
||||
};
|
||||
|
||||
self.loading = 'Creating transaction...';
|
||||
walletService.createTx(fc, txp, function(err, createdTxp) {
|
||||
self.loading = null;
|
||||
if (err) {
|
||||
self.loading = null;
|
||||
$log.debug(err);
|
||||
self.error = bwsError.msg(err);
|
||||
$scope.$apply();
|
||||
return;
|
||||
}
|
||||
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
|
||||
if (accept) {
|
||||
self.loading = 'Sending bitcoin...';
|
||||
self.confirmTx(createdTxp, function(err, tx) {
|
||||
if (err) {
|
||||
self.loading = null;
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
var gift = {
|
||||
amount: dataSrc.price,
|
||||
currencyCode: dataSrc.currency,
|
||||
bitpayInvoiceId: data.data.id
|
||||
};
|
||||
self.loading = 'Buying gift card...';
|
||||
amazonService.buyGiftCard(gift, function(err, giftCard) {
|
||||
self.loading = null;
|
||||
if (err) {
|
||||
self.error = err;
|
||||
return;
|
||||
}
|
||||
self.giftCard = giftCard;
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 100);
|
||||
};
|
||||
|
||||
this.confirmTx = function(txp, cb) {
|
||||
|
||||
fingerprintService.check(fc, function(err) {
|
||||
if (err) {
|
||||
$log.debug(err);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
handleEncryptedWallet(fc, function(err) {
|
||||
if (err) {
|
||||
$log.debug(err);
|
||||
return bwsError.cb(err, null, cb);
|
||||
}
|
||||
|
||||
walletService.publishTx(fc, txp, function(err, publishedTxp) {
|
||||
if (err) {
|
||||
$log.debug(err);
|
||||
return bwsError.cb(err, null, cb);
|
||||
}
|
||||
|
||||
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
|
||||
walletService.lock(fc);
|
||||
if (err) {
|
||||
$log.debug(err);
|
||||
walletService.removeTx(fc, signedTxp, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
return bwsError.cb(err, null, cb);
|
||||
}
|
||||
|
||||
walletService.broadcastTx(fc, signedTxp, function(err, broadcastedTxp) {
|
||||
if (err) {
|
||||
$log.debug(err);
|
||||
walletService.removeTx(fc, broadcastedTxp, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
return bwsError.cb(err, null, cb);
|
||||
}
|
||||
$timeout(function() {
|
||||
return cb(null, broadcastedTxp);
|
||||
}, 5000);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
});
|
||||
|
|
@ -160,6 +160,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
|
|||
|
||||
self.initGlidera();
|
||||
self.initCoinbase();
|
||||
self.initAmazon();
|
||||
|
||||
self.hideBalance();
|
||||
|
||||
|
|
@ -1040,6 +1041,10 @@ 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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue