Mercado Libre: First steps
This commit is contained in:
parent
67612e547c
commit
9abd852f4b
18 changed files with 3648 additions and 0 deletions
271
src/js/controllers/buyMercadoLibre.js
Normal file
271
src/js/controllers/buyMercadoLibre.js
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('buyMercadoLibreController', function($scope, $log, $state, $timeout, $filter, $ionicHistory, lodash, mercadoLibreService, popupService, profileService, ongoingProcess, configService, walletService, payproService, bwcError, externalLinkService, platformInfo) {
|
||||
|
||||
var amount;
|
||||
var currency;
|
||||
$scope.isCordova = platformInfo.isCordova;
|
||||
|
||||
$scope.openExternalLink = function(url) {
|
||||
externalLinkService.open(url);
|
||||
};
|
||||
|
||||
var showErrorAndBack = function(msg, err) {
|
||||
$scope.sendStatus = '';
|
||||
err = err && err.errors ? err.errors[0].message : err;
|
||||
popupService.showAlert(msg, err, function() {
|
||||
$ionicHistory.goBack();
|
||||
});
|
||||
};
|
||||
|
||||
var showError = function(msg, err) {
|
||||
$scope.sendStatus = '';
|
||||
err = err && err.errors ? err.errors[0].message : (err || '');
|
||||
popupService.showAlert(msg, err);
|
||||
};
|
||||
|
||||
var publishAndSign = function(wallet, txp, onSendStatusChange, cb) {
|
||||
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
|
||||
var err = 'No signing proposal: No private key';
|
||||
$log.info(err);
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
walletService.publishAndSign(wallet, txp, function(err, txp) {
|
||||
if (err) return cb(err);
|
||||
return cb(null, txp);
|
||||
}, onSendStatusChange);
|
||||
};
|
||||
|
||||
var statusChangeHandler = function(processName, showName, isOn) {
|
||||
$log.debug('statusChangeHandler: ', processName, showName, isOn);
|
||||
if (processName == 'buyingGiftCard' && !isOn) {
|
||||
$scope.sendStatus = 'success';
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
}, 100);
|
||||
} else if (showName) {
|
||||
$scope.sendStatus = showName;
|
||||
}
|
||||
};
|
||||
|
||||
var checkTransaction = lodash.throttle(function(count, dataSrc) {
|
||||
mercadoLibreService.createGiftCard(dataSrc, function(err, giftCard) {
|
||||
console.log('[buyMercadoLibre.js:53]',giftCard); //TODO
|
||||
$log.debug("creating gift card " + count);
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
giftCard = {};
|
||||
giftCard.status = 'FAILURE';
|
||||
showError('Error creating gift card', err);
|
||||
}
|
||||
|
||||
if (giftCard.status == 'PENDING' && count < 3) {
|
||||
$log.debug("Waiting for payment confirmation");
|
||||
checkTransaction(count + 1, dataSrc);
|
||||
return;
|
||||
}
|
||||
|
||||
var now = moment().unix() * 1000;
|
||||
|
||||
var newData = giftCard;
|
||||
newData['invoiceId'] = dataSrc.invoiceId;
|
||||
newData['accessKey'] = dataSrc.accessKey;
|
||||
newData['invoiceUrl'] = dataSrc.invoiceUrl;
|
||||
newData['amount'] = dataSrc.amount;
|
||||
newData['currency'] = dataSrc.currency;
|
||||
newData['date'] = dataSrc.invoiceTime || now;
|
||||
newData['uuid'] = dataSrc.uuid;
|
||||
|
||||
if (newData.status == 'expired') {
|
||||
mercadoLibreService.savePendingGiftCard(newData, {
|
||||
remove: true
|
||||
}, function(err) {
|
||||
$log.error(err);
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('Invoice expired');
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
mercadoLibreService.savePendingGiftCard(newData, null, function(err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
$log.debug("Saving new gift card with status: " + newData.status);
|
||||
$scope.mlGiftCard = newData;
|
||||
});
|
||||
});
|
||||
}, 8000, {
|
||||
'leading': true
|
||||
});
|
||||
|
||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||
amount = data.stateParams.amount;
|
||||
currency = data.stateParams.currency;
|
||||
|
||||
/* TODO
|
||||
if (amount > 2000 || amount < 50) {
|
||||
showErrorAndBack('Purchase amount must be a value between 50 and 2000');
|
||||
return;
|
||||
}
|
||||
*/
|
||||
|
||||
$scope.amountUnitStr = $filter('formatFiatAmount')(amount) + ' ' + currency;
|
||||
|
||||
$scope.network = mercadoLibreService.getNetwork();
|
||||
$scope.wallets = profileService.getWallets({
|
||||
onlyComplete: true,
|
||||
network: $scope.network,
|
||||
hasFunds: true
|
||||
});
|
||||
if (lodash.isEmpty($scope.wallets)) {
|
||||
showErrorAndBack('No wallets with funds');
|
||||
return;
|
||||
}
|
||||
$scope.wallet = $scope.wallets[0]; // Default first wallet
|
||||
});
|
||||
|
||||
$scope.buyConfirm = function() {
|
||||
|
||||
var message = 'Buy gift card for ' + amount + ' ' + currency;
|
||||
var okText = 'Confirm';
|
||||
var cancelText = 'Cancel';
|
||||
popupService.showConfirm(null, message, okText, cancelText, function(ok) {
|
||||
if (!ok) return;
|
||||
|
||||
var config = configService.getSync();
|
||||
var configWallet = config.wallet;
|
||||
var walletSettings = configWallet.settings;
|
||||
// Selected WalletID as UUID
|
||||
var uuid = $scope.wallet.id;
|
||||
var dataSrc = {
|
||||
currency: currency,
|
||||
amount: amount,
|
||||
uuid: uuid
|
||||
};
|
||||
|
||||
ongoingProcess.set('buyingGiftCard', true, statusChangeHandler);
|
||||
mercadoLibreService.createBitPayInvoice(dataSrc, function(err, dataInvoice) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
|
||||
if (err && err.message && err.message.match(/suspended/i)) {
|
||||
showError('Service not available', 'Mercadolibre Gift Card Service is not available at this moment. Please try back later.');
|
||||
} else {
|
||||
showError('Could not access Gift Card Service', err);
|
||||
};
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
var accessKey = dataInvoice ? dataInvoice.accessKey : null;
|
||||
|
||||
if (!accessKey) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('No access key defined');
|
||||
return;
|
||||
}
|
||||
|
||||
mercadoLibreService.getBitPayInvoice(dataInvoice.invoiceId, function(err, invoice) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('Error getting BitPay invoice', err);
|
||||
return;
|
||||
}
|
||||
|
||||
var payProUrl = (invoice && invoice.paymentUrls) ? invoice.paymentUrls.BIP73 : null;
|
||||
|
||||
if (!payProUrl) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('Error fetching invoice');
|
||||
return;
|
||||
}
|
||||
|
||||
payproService.getPayProDetails(payProUrl, function(err, payProDetails) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('Error fetching payment info', bwcError.msg(err));
|
||||
return;
|
||||
}
|
||||
|
||||
var outputs = [];
|
||||
var toAddress = payProDetails.toAddress;
|
||||
var amountSat = payProDetails.amount;
|
||||
var comment = amount + ' ' + currency + ' Mercadolibre Gift Card';
|
||||
|
||||
outputs.push({
|
||||
'toAddress': toAddress,
|
||||
'amount': amountSat,
|
||||
'message': comment
|
||||
});
|
||||
|
||||
var txp = {
|
||||
toAddress: toAddress,
|
||||
amount: amountSat,
|
||||
outputs: outputs,
|
||||
message: comment,
|
||||
payProUrl: payProUrl,
|
||||
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
|
||||
feeLevel: walletSettings.feeLevel || 'normal'
|
||||
};
|
||||
|
||||
walletService.createTx($scope.wallet, txp, function(err, ctxp) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('Could not create transaction', bwcError.msg(err));
|
||||
return;
|
||||
}
|
||||
publishAndSign($scope.wallet, ctxp, function() {}, function(err, txSent) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingGiftCard', false, statusChangeHandler);
|
||||
showError('Could not send transaction', err);
|
||||
return;
|
||||
}
|
||||
$log.debug('Transaction broadcasted. Waiting for confirmation...');
|
||||
var invoiceId = JSON.parse(payProDetails.merchant_data).invoiceId;
|
||||
var dataSrc = {
|
||||
currency: currency,
|
||||
amount: amount,
|
||||
uuid: uuid,
|
||||
accessKey: accessKey,
|
||||
invoiceId: invoice.id,
|
||||
invoiceUrl: payProUrl,
|
||||
invoiceTime: invoice.invoiceTime
|
||||
};
|
||||
checkTransaction(1, dataSrc);
|
||||
});
|
||||
});
|
||||
}, true); // Disable loader
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.showWalletSelector = function() {
|
||||
$scope.walletSelectorTitle = 'Buy from';
|
||||
$scope.showWallets = true;
|
||||
};
|
||||
|
||||
$scope.onWalletSelect = function(wallet) {
|
||||
$scope.wallet = wallet;
|
||||
};
|
||||
|
||||
$scope.goBackHome = function() {
|
||||
$scope.sendStatus = '';
|
||||
$ionicHistory.nextViewOptions({
|
||||
disableAnimate: true,
|
||||
historyRoot: true
|
||||
});
|
||||
$ionicHistory.clearHistory();
|
||||
var claimCode = $scope.mlGiftCard ? $scope.mlGiftCard.pin : null;
|
||||
$state.go('tabs.home').then(function() {
|
||||
$ionicHistory.nextViewOptions({
|
||||
disableAnimate: true
|
||||
});
|
||||
$state.transitionTo('tabs.giftcards.mercadoLibre').then(function() {
|
||||
$state.transitionTo('tabs.giftcards.mercadoLibre.cards', {
|
||||
cardClaimCode: claimCode
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
24
src/js/controllers/mercadoLibre.js
Normal file
24
src/js/controllers/mercadoLibre.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('mercadoLibreController',
|
||||
function($scope, $timeout, $log, mercadoLibreService, externalLinkService, popupService) {
|
||||
|
||||
$scope.openExternalLink = function(url) {
|
||||
externalLinkService.open(url);
|
||||
};
|
||||
|
||||
var init = function() {
|
||||
mercadoLibreService.getPendingGiftCards(function(err, gcds) {
|
||||
if (err) $log.error(err);
|
||||
$scope.giftCards = gcds;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||
$scope.network = mercadoLibreService.getNetwork();
|
||||
init();
|
||||
});
|
||||
});
|
||||
106
src/js/controllers/mercadoLibreCards.js
Normal file
106
src/js/controllers/mercadoLibreCards.js
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('mercadoLibreCardsController',
|
||||
function($scope, $timeout, $ionicModal, $log, $ionicScrollDelegate, lodash, mercadoLibreService, platformInfo, externalLinkService, popupService, ongoingProcess) {
|
||||
|
||||
$scope.openExternalLink = function(url) {
|
||||
externalLinkService.open(url);
|
||||
};
|
||||
|
||||
var updateGiftCards = function(cb) {
|
||||
mercadoLibreService.getPendingGiftCards(function(err, gcds) {
|
||||
if (err) {
|
||||
popupService.showAlert('Could not get gift cards', err);
|
||||
if (cb) return cb();
|
||||
else return;
|
||||
}
|
||||
$scope.giftCards = gcds;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
$ionicScrollDelegate.resize();
|
||||
if (cb) return cb();
|
||||
}, 100);
|
||||
});
|
||||
};
|
||||
|
||||
$scope.updatePendingGiftCards = lodash.debounce(function() {
|
||||
$scope.updatingPending = {};
|
||||
updateGiftCards(function() {
|
||||
var index = 0;
|
||||
var gcds = $scope.giftCards;
|
||||
lodash.forEach(gcds, function(dataFromStorage) {
|
||||
if (dataFromStorage.status == 'PENDING' || dataFromStorage.status == 'invalid') {
|
||||
$log.debug("Creating / Updating gift card");
|
||||
$scope.updatingPending[dataFromStorage.invoiceId] = true;
|
||||
|
||||
mercadoLibreService.createGiftCard(dataFromStorage, function(err, giftCard) {
|
||||
|
||||
$scope.updatingPending[dataFromStorage.invoiceId] = false;
|
||||
if (err) {
|
||||
popupService.showAlert('Error creating gift card', err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (giftCard.status != 'PENDING') {
|
||||
var newData = {};
|
||||
|
||||
lodash.merge(newData, dataFromStorage, giftCard);
|
||||
|
||||
if (newData.status == 'expired') {
|
||||
mercadoLibreService.savePendingGiftCard(newData, {
|
||||
remove: true
|
||||
}, function(err) {
|
||||
updateGiftCards();
|
||||
return;
|
||||
});
|
||||
}
|
||||
|
||||
mercadoLibreService.savePendingGiftCard(newData, null, function(err) {
|
||||
$log.debug("Saving new gift card");
|
||||
updateGiftCards();
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}, 1000, {
|
||||
'leading': true
|
||||
});
|
||||
|
||||
$scope.openCardModal = function(card) {
|
||||
$scope.card = card;
|
||||
|
||||
$ionicModal.fromTemplateUrl('views/modals/mercadolibre-card-details.html', {
|
||||
scope: $scope
|
||||
}).then(function(modal) {
|
||||
$scope.mercadoLibreCardDetailsModal = modal;
|
||||
$scope.mercadoLibreCardDetailsModal.show();
|
||||
});
|
||||
|
||||
$scope.$on('modal.hidden', function() {
|
||||
$scope.updatePendingGiftCards();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||
$scope.cardClaimCode = data.stateParams.cardClaimCode;
|
||||
updateGiftCards(function() {
|
||||
if ($scope.cardClaimCode) {
|
||||
var card = lodash.find($scope.giftCards, {
|
||||
claimCode: $scope.cardClaimCode
|
||||
});
|
||||
if (lodash.isEmpty(card)) {
|
||||
popupService.showAlert(null, 'Card not found');
|
||||
return;
|
||||
}
|
||||
$scope.openCardModal(card);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
$scope.$on("$ionicView.afterEnter", function(event, data) {
|
||||
$scope.updatePendingGiftCards();
|
||||
});
|
||||
});
|
||||
83
src/js/controllers/modals/mercadoLibreCardDetails.js
Normal file
83
src/js/controllers/modals/mercadoLibreCardDetails.js
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
'use strict';
|
||||
|
||||
angular.module('copayApp.controllers').controller('mercadoLibreCardDetailsController', function($scope, $log, $timeout, $ionicScrollDelegate, bwcError, mercadoLibreService, lodash, ongoingProcess, popupService, externalLinkService) {
|
||||
|
||||
$scope.cancelGiftCard = function() {
|
||||
ongoingProcess.set('cancelingGiftCard', true);
|
||||
mercadoLibreService.cancelGiftCard($scope.card, function(err, data) {
|
||||
ongoingProcess.set('cancelingGiftCard', false);
|
||||
if (err) {
|
||||
popupService.showAlert('Error canceling gift card', bwcError.msg(err));
|
||||
return;
|
||||
}
|
||||
$scope.card.cardStatus = data.cardStatus;
|
||||
$timeout(function() {
|
||||
$ionicScrollDelegate.resize();
|
||||
$ionicScrollDelegate.scrollTop();
|
||||
}, 10);
|
||||
mercadoLibreService.savePendingGiftCard($scope.card, null, function(err) {
|
||||
$scope.refreshGiftCard();
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.remove = function() {
|
||||
mercadoLibreService.savePendingGiftCard($scope.card, {
|
||||
remove: true
|
||||
}, function(err) {
|
||||
$scope.cancel();
|
||||
});
|
||||
};
|
||||
|
||||
$scope.refreshGiftCard = function() {
|
||||
ongoingProcess.set('updatingGiftCard', true);
|
||||
mercadoLibreService.getPendingGiftCards(function(err, gcds) {
|
||||
if (lodash.isEmpty(gcds)) {
|
||||
$timeout(function() {
|
||||
ongoingProcess.set('updatingGiftCard', false);
|
||||
}, 1000);
|
||||
}
|
||||
if (err) {
|
||||
popupService.showAlert('Error', err);
|
||||
return;
|
||||
}
|
||||
var index = 0;
|
||||
lodash.forEach(gcds, function(dataFromStorage) {
|
||||
if (++index == Object.keys(gcds).length) {
|
||||
$timeout(function() {
|
||||
ongoingProcess.set('updatingGiftCard', false);
|
||||
}, 1000);
|
||||
}
|
||||
if (dataFromStorage.status == 'PENDING' && dataFromStorage.invoiceId == $scope.card.invoiceId) {
|
||||
$log.debug("creating gift card");
|
||||
mercadoLibreService.createGiftCard(dataFromStorage, function(err, giftCard) {
|
||||
if (err) {
|
||||
popupService.showAlert('Error', bwcError.msg(err));
|
||||
return;
|
||||
}
|
||||
if (!lodash.isEmpty(giftCard)) {
|
||||
var newData = {};
|
||||
lodash.merge(newData, dataFromStorage, giftCard);
|
||||
mercadoLibreService.savePendingGiftCard(newData, null, function(err) {
|
||||
$log.debug("Saving new gift card");
|
||||
$scope.card = newData;
|
||||
$timeout(function() {
|
||||
$scope.$digest();
|
||||
});
|
||||
});
|
||||
} else $log.debug("pending gift card not available yet");
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
$scope.cancel = function() {
|
||||
$scope.mercadoLibreCardDetailsModal.hide();
|
||||
};
|
||||
|
||||
$scope.openExternalLink = function(url) {
|
||||
externalLinkService.open(url);
|
||||
};
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue