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");
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,35 +3,20 @@
|
|||
angular.module('copayApp.services').factory('amazonService', function($http, $log, lodash, moment, storageService, configService, platformInfo) {
|
||||
var root = {};
|
||||
var credentials = {};
|
||||
var DAILYLIMIT = 500;
|
||||
|
||||
root.setCredentials = function(network) {
|
||||
credentials.AMAZON_SANDBOX = network == 'testnet' ? true : false;
|
||||
credentials.AMAZON_SERVICE_NAME = 'AGCODService';
|
||||
|
||||
if (network == 'testnet') {
|
||||
credentials.BITPAY_API_URL = window.amazon_sandbox_bitpay_api_url;
|
||||
credentials.BITPAY_API_TOKEN = window.amazon_sandbox_bitpay_api_token;
|
||||
credentials.AMAZON_ACCESS_KEY = window.amazon_sandbox_access_key;
|
||||
credentials.AMAZON_SECRET_KEY = window.amazon_sandbox_secret_key;
|
||||
credentials.AMAZON_PARTNER_ID = window.amazon_sandbox_partner_id;
|
||||
credentials.AMAZON_REGION = window.amazon_sandbox_region;
|
||||
credentials.AMAZON_ENDPOINT = window.amazon_sandbox_endpoint;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
credentials.BITPAY_API_URL = window.amazon_bitpay_api_url;
|
||||
credentials.BITPAY_API_TOKEN = window.amazon_bitpay_api_token;
|
||||
credentials.AMAZON_ACCESS_KEY = window.amazon_access_key;
|
||||
credentials.AMAZON_SECRET_KEY = window.amazon_secret_key;
|
||||
credentials.AMAZON_PARTNER_ID = window.amazon_partner_id;
|
||||
credentials.AMAZON_REGION = window.amazon_region;
|
||||
credentials.AMAZON_ENDPOINT = window.amazon_endpoint;
|
||||
};
|
||||
};
|
||||
|
||||
var _getUuid = function(cb) {
|
||||
var isCordova = platformInfo.isCordova;
|
||||
|
||||
|
||||
if (isCordova) {
|
||||
window.plugins.uniqueDeviceID.get(function(uuid) {
|
||||
return cb(uuid);
|
||||
|
|
@ -44,154 +29,6 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
|
|||
}
|
||||
};
|
||||
|
||||
var _checkLimits = function(amount, cb) {
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
var dateStamp = moment.utc().format('YYYY-MM-DD');
|
||||
|
||||
storageService.getAmazon(network, function(err, amazon) {
|
||||
if (err) $log.error(err);
|
||||
|
||||
if (lodash.isEmpty(amazon)) return cb('CAN_NOT_GET_DATA_FROM_STORAGE');
|
||||
|
||||
if (lodash.isString(amazon)) {
|
||||
amazon = JSON.parse(amazon);
|
||||
}
|
||||
|
||||
if (amazon.date == dateStamp && (amazon.amount + amount) > DAILYLIMIT)
|
||||
return cb('EXCEEDED_DAILY_LIMIT');
|
||||
|
||||
return cb();
|
||||
});
|
||||
};
|
||||
|
||||
root.healthCheckRequest = function() {
|
||||
$http({
|
||||
method: 'GET',
|
||||
url: credentials.AMAZON_ENDPOINT + '/sping',
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
}).then(function(data) {
|
||||
$log.info('Amazon Health Check: SUCCESS');
|
||||
}, function(data) {
|
||||
$log.error('Amazon Health Check: ERROR ' + data.data.error);
|
||||
});
|
||||
};
|
||||
|
||||
root.initUuid = function() {
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
var dateStamp = moment.utc().format('YYYY-MM-DD');
|
||||
_getUuid(function(uuid) {
|
||||
storageService.getAmazon(network, function(err, amazon) {
|
||||
if (err) $log.error(err);
|
||||
if (lodash.isEmpty(amazon))
|
||||
amazon = {
|
||||
uuid: uuid,
|
||||
date: dateStamp,
|
||||
amount: 0
|
||||
};
|
||||
|
||||
if (lodash.isString(amazon)) {
|
||||
amazon = JSON.parse(amazon);
|
||||
}
|
||||
|
||||
amazon.uuid = uuid;
|
||||
if (amazon.date != dateStamp) {
|
||||
amazon.date = dateStamp;
|
||||
amazon.amount = 0;
|
||||
}
|
||||
amazon = JSON.stringify(amazon);
|
||||
storageService.setAmazon(network, amazon, function(err) {
|
||||
if (err) $log.error(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.setAmountByDay = function(amount) {
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
var dateStamp = moment.utc().format('YYYY-MM-DD');
|
||||
storageService.getAmazon(network, function(err, amazon) {
|
||||
if (err) $log.error(err);
|
||||
|
||||
if (lodash.isString(amazon)) {
|
||||
amazon = JSON.parse(amazon);
|
||||
}
|
||||
|
||||
if (amazon.date == dateStamp) {
|
||||
amazon.amount = amazon.amount + amount;
|
||||
} else {
|
||||
amazon.date = dateStamp;
|
||||
amazon.amount = amount;
|
||||
}
|
||||
amazon = JSON.stringify(amazon);
|
||||
storageService.setAmazon(network, amazon, function(err) {
|
||||
if (err) $log.error(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var _getSignatureKey = function() {
|
||||
|
||||
var key = credentials.AMAZON_SECRET_KEY;
|
||||
var dateStamp = moment.utc().format('YYYYMMDD');
|
||||
var regionName = credentials.AMAZON_REGION;
|
||||
var serviceName = credentials.AMAZON_SERVICE_NAME;
|
||||
|
||||
var kDate= CryptoJS.HmacSHA256(dateStamp, "AWS4" + key, { asBytes: true});
|
||||
var kRegion= CryptoJS.HmacSHA256(regionName, kDate, { asBytes: true });
|
||||
var kService=CryptoJS.HmacSHA256(serviceName, kRegion, { asBytes: true });
|
||||
var kSigning= CryptoJS.HmacSHA256("aws4_request", kService, { asBytes: true });
|
||||
|
||||
return kSigning;
|
||||
}
|
||||
|
||||
|
||||
var _getHeaders = function(data, method, endpoint, amz_target) {
|
||||
var content_type = 'application/json';
|
||||
var accept = 'application/json';
|
||||
var amz_date = moment.utc().format('YYYYMMDD[T]HHmmss[Z]');
|
||||
var date_stamp = moment.utc().format('YYYYMMDD');
|
||||
var canonical_querystring = '';
|
||||
|
||||
/************* TASK 1: CREATE A CANONICAL REQUEST *************/
|
||||
|
||||
var canonical_headers =
|
||||
'accept:' + accept + '\n' +
|
||||
'content-type:' + content_type + '\n' +
|
||||
'host:' + credentials.AMAZON_ENDPOINT.replace('https://', '') + '\n' +
|
||||
'x-amz-date:' + amz_date + '\n' +
|
||||
'x-amz-target:' + amz_target + '\n';
|
||||
|
||||
var signed_headers = 'accept;content-type;host;x-amz-date;x-amz-target';
|
||||
data = JSON.stringify(data);
|
||||
var payload_hash = CryptoJS.SHA256(data).toString(CryptoJS.enc.Hex);
|
||||
var canonical_request = method + '\n' + endpoint + '\n' + canonical_querystring + '\n' + canonical_headers + '\n' + signed_headers + '\n' + payload_hash;
|
||||
|
||||
/************* TASK 2: CREATE THE STRING TO SIGN *************/
|
||||
|
||||
var algorithm = 'AWS4-HMAC-SHA256';
|
||||
var credential_scope = date_stamp + '/' + credentials.AMAZON_REGION + '/' + credentials.AMAZON_SERVICE_NAME + '/' + 'aws4_request';
|
||||
var hashed_canonical_request = CryptoJS.SHA256(canonical_request).toString(CryptoJS.enc.Hex);
|
||||
var string_to_sign = algorithm + '\n' + amz_date + '\n' + credential_scope + '\n' + hashed_canonical_request;
|
||||
|
||||
/************* TASK 3: CALCULATE THE SIGNATURE *************/
|
||||
|
||||
var signing_key = _getSignatureKey();
|
||||
var signature = CryptoJS.HmacSHA256(string_to_sign, signing_key).toString(CryptoJS.enc.Hex)
|
||||
var authorization_header = algorithm + ' ' + 'Credential=' + credentials.AMAZON_ACCESS_KEY + '/' + credential_scope + ', ' + 'SignedHeaders=' + signed_headers + ', ' + 'Signature=' + signature;
|
||||
|
||||
/************* TASK 4: ADD SIGNING INFORMATION TO THE REQUEST *************/
|
||||
|
||||
return {
|
||||
'Content-Type': content_type,
|
||||
'Accept': accept,
|
||||
'X-Amz-Date': amz_date,
|
||||
'X-Amz-Target': amz_target,
|
||||
'Authorization': authorization_header
|
||||
};
|
||||
};
|
||||
|
||||
var _getBitPay = function(endpoint) {
|
||||
return {
|
||||
method: 'GET',
|
||||
|
|
@ -212,41 +49,9 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
|
|||
},
|
||||
data: data
|
||||
};
|
||||
};
|
||||
|
||||
root.createBitPayInvoice = function(data, cb) {
|
||||
_getUuid(function(uuid) {
|
||||
if (lodash.isEmpty(uuid)) return cb('CAN_NOT_GET_UUID');
|
||||
var dataSrc = {
|
||||
price: data.price,
|
||||
currency: data.currency,
|
||||
orderId: data.orderId,
|
||||
posData: '{uuid:' + uuid + '}'
|
||||
};
|
||||
_checkLimits(data.price, function(err) {
|
||||
if (err) return cb(err);
|
||||
$http(_postBitPay('/invoices', dataSrc)).then(function(data) {
|
||||
$log.info('BitPay Create Invoice: SUCCESS');
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('BitPay Create Invoice: ERROR ' + data.data.error);
|
||||
return cb(data.data.error);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.getBitPayInvoice = function(id, cb) {
|
||||
$http(_getBitPay('/invoices/' + id)).then(function(data) {
|
||||
$log.info('BitPay Get Invoice: SUCCESS');
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('BitPay Get Invoice: ERROR ' + data.data.error);
|
||||
return cb(data.data.error);
|
||||
});
|
||||
};
|
||||
|
||||
root.saveGiftCard = function(gc, opts, cb) {
|
||||
root.savePendingGiftCard = function(gc, opts, cb) {
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
storageService.getAmazonGiftCards(network, function(err, oldGiftCards) {
|
||||
if (lodash.isString(oldGiftCards)) {
|
||||
|
|
@ -256,12 +61,12 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
|
|||
gc = JSON.parse(gc);
|
||||
}
|
||||
var inv = oldGiftCards || {};
|
||||
inv[gc.gcId] = gc;
|
||||
inv[gc.invoiceId] = gc;
|
||||
if (opts && (opts.error || opts.status)) {
|
||||
inv[gc.gcId] = lodash.assign(inv[gc.gcId], opts);
|
||||
inv[gc.invoiceId] = lodash.assign(inv[gc.invoiceId], opts);
|
||||
}
|
||||
if (opts && opts.remove) {
|
||||
delete(inv[gc.gcId]);
|
||||
delete(inv[gc.invoiceId]);
|
||||
}
|
||||
inv = JSON.stringify(inv);
|
||||
|
||||
|
|
@ -271,81 +76,64 @@ angular.module('copayApp.services').factory('amazonService', function($http, $lo
|
|||
});
|
||||
};
|
||||
|
||||
root.getGiftCards = function(cb) {
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
root.getPendingGiftCards = function(cb) {
|
||||
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
|
||||
storageService.getAmazonGiftCards(network, function(err, giftCards) {
|
||||
var _gcds = giftCards ? JSON.parse(giftCards) : null;
|
||||
return cb(err, _gcds);
|
||||
});
|
||||
};
|
||||
|
||||
root.createGiftCard = function(dataSrc, cb) {
|
||||
var environment = credentials.AMAZON_SANDBOX ? 'T' : 'P'; // T: test - P: production
|
||||
var now = moment().unix();
|
||||
var requestId = dataSrc.creationRequestId || credentials.AMAZON_PARTNER_ID + environment + now;
|
||||
|
||||
var data = {
|
||||
'creationRequestId': requestId,
|
||||
'partnerId': credentials.AMAZON_PARTNER_ID,
|
||||
'value': {
|
||||
'currencyCode': dataSrc.currencyCode,
|
||||
'amount': dataSrc.amount
|
||||
}
|
||||
};
|
||||
root.createBitPayInvoice = function(data, cb) {
|
||||
_getUuid(function(uuid) {
|
||||
if (lodash.isEmpty(uuid)) return cb('CAN_NOT_GET_UUID');
|
||||
var dataSrc = {
|
||||
currency: data.currency,
|
||||
amount: data.amount,
|
||||
clientId: uuid
|
||||
};
|
||||
|
||||
var method = 'POST';
|
||||
var endpoint = '/CreateGiftCard';
|
||||
var amz_target = 'com.amazonaws.agcod.AGCODService.CreateGiftCard';
|
||||
|
||||
var headers = _getHeaders(data, method, endpoint, amz_target);
|
||||
|
||||
$http({
|
||||
'method': method,
|
||||
'url': credentials.AMAZON_ENDPOINT + endpoint,
|
||||
'data': JSON.stringify(data),
|
||||
'headers': headers
|
||||
}).then(function(data) {
|
||||
$log.info('Amazon.com Gift Card Create/Update: SUCCESS');
|
||||
var newData = data.data;
|
||||
newData['bitpayInvoiceId'] = dataSrc.bitpayInvoiceId;
|
||||
newData['bitpayInvoiceUrl'] = dataSrc.bitpayInvoiceUrl;
|
||||
newData['date'] = dataSrc.date || now;
|
||||
root.saveGiftCard(newData, null, function(err) {
|
||||
return cb(null, newData);
|
||||
$http(_postBitPay('/amazon-gift/pay', dataSrc)).then(function(data) {
|
||||
$log.info('BitPay Create Invoice: SUCCESS');
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('BitPay Create Invoice: ERROR ' + data.data.message);
|
||||
return cb(data.data);
|
||||
});
|
||||
}, function(data) {
|
||||
$log.error('Amazon.com Gift Card Create/Update: ERROR ' + data.statusText);
|
||||
return cb(data.statusText);
|
||||
});
|
||||
};
|
||||
|
||||
root.cancelGiftCard = function(dataSrc, cb) {
|
||||
var data = {
|
||||
'creationRequestId': dataSrc.creationRequestId,
|
||||
'partnerId': credentials.AMAZON_PARTNER_ID,
|
||||
'gcId': dataSrc.gcId,
|
||||
};
|
||||
|
||||
var method = 'POST';
|
||||
var endpoint = '/CancelGiftCard';
|
||||
var amz_target = 'com.amazonaws.agcod.AGCODService.CancelGiftCard';
|
||||
|
||||
var headers = _getHeaders(data, method, endpoint, amz_target);
|
||||
|
||||
$http({
|
||||
'method': method,
|
||||
'url': credentials.AMAZON_ENDPOINT + endpoint,
|
||||
'data': JSON.stringify(data),
|
||||
'headers': headers
|
||||
}).then(function(data) {
|
||||
$log.info('Amazon.com Gift Card Cancel: SUCCESS');
|
||||
return cb(null, data.data);
|
||||
root.getBitPayInvoice = function(id, cb) {
|
||||
$http(_getBitPay('/invoices/' + id)).then(function(data) {
|
||||
$log.info('BitPay Get Invoice: SUCCESS');
|
||||
return cb(null, data.data.data);
|
||||
}, function(data) {
|
||||
$log.error('Amazon.com Gift Card Cancel: ERROR ' + data.statusText);
|
||||
return cb(data.statusText);
|
||||
$log.error('BitPay Get Invoice: ERROR ' + data.data.error);
|
||||
return cb(data.data.error);
|
||||
});
|
||||
};
|
||||
|
||||
root.createGiftCard = function(dataInvoice, cb) {
|
||||
_getUuid(function(uuid) {
|
||||
var dataSrc = {
|
||||
"clientId": uuid,
|
||||
"invoiceId": dataInvoice.invoiceId,
|
||||
"accessKey": dataInvoice.accessKey
|
||||
};
|
||||
|
||||
$http(_postBitPay('/amazon-gift/redeem', dataSrc)).then(function(data) {
|
||||
var status = data.data.status == ('new' || 'paid') ? 'PENDING' : data.data.status;
|
||||
data.data.status = status;
|
||||
data.data.clientId = uuid;
|
||||
$log.info('Amazon.com Gift Card Create/Update: ' + status);
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('Amazon.com Gift Card Create/Update: ' + data.data.message);
|
||||
return cb(data.data);
|
||||
});
|
||||
})
|
||||
|
||||
};
|
||||
return root;
|
||||
|
||||
});
|
||||
|
|
|
|||
|
|
@ -317,7 +317,7 @@ angular.module('copayApp.services')
|
|||
});
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
root.setAmazonGiftCards = function(network, gcs, cb) {
|
||||
storage.set('amazonGiftCards-' + network, gcs, cb);
|
||||
};
|
||||
|
|
@ -330,17 +330,5 @@ angular.module('copayApp.services')
|
|||
storage.remove('amazonGiftCards-' + network, cb);
|
||||
};
|
||||
|
||||
root.setAmazon = function(network, data, cb) {
|
||||
storage.set('amazon-' + network, data, cb);
|
||||
};
|
||||
|
||||
root.getAmazon = function(network, cb) {
|
||||
storage.get('amazon-' + network, cb);
|
||||
};
|
||||
|
||||
root.removeAmazon = function(network, cb) {
|
||||
storage.remove('amazon-' + network, cb);
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -729,6 +729,10 @@ ul.manage li {
|
|||
padding: 20px;
|
||||
}
|
||||
|
||||
.p15t {
|
||||
padding-top: 15px;
|
||||
}
|
||||
|
||||
.p20t {
|
||||
padding-top: 20px;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue