Wallet/src/js/services/amazonService.js

139 lines
4.1 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.services').factory('amazonService', function($http, $log, lodash, moment, storageService, configService, platformInfo) {
var root = {};
var credentials = {};
root.setCredentials = function(network) {
if (network == 'testnet') {
2016-05-19 01:18:15 -03:00
credentials.BITPAY_API_URL = window.amazon_sandbox_bitpay_api_url;
credentials.BITPAY_API_TOKEN = window.amazon_sandbox_bitpay_api_token;
2016-07-21 11:18:48 -03:00
} else {
2016-05-19 01:18:15 -03:00
credentials.BITPAY_API_URL = window.amazon_bitpay_api_url;
credentials.BITPAY_API_TOKEN = window.amazon_bitpay_api_token;
};
2016-06-07 10:42:54 -03:00
};
2016-06-07 15:34:11 -03:00
var _getUuid = function(cb) {
var isCordova = platformInfo.isCordova;
2016-07-21 11:18:48 -03:00
if (isCordova) {
window.plugins.uniqueDeviceID.get(function(uuid) {
return cb(uuid);
}, function(err) {
2016-06-07 15:34:11 -03:00
$log.error(err);
return cb();
});
} else {
return cb('XXX'); // Test purpose
}
};
2016-05-16 17:51:26 -03:00
var _getBitPay = function(endpoint) {
return {
method: 'GET',
2016-05-19 01:18:15 -03:00
url: credentials.BITPAY_API_URL + endpoint,
headers: {
'content-type': 'application/json'
}
};
};
var _postBitPay = function(endpoint, data) {
data.token = credentials.BITPAY_API_TOKEN;
return {
method: 'POST',
2016-05-19 01:18:15 -03:00
url: credentials.BITPAY_API_URL + endpoint,
headers: {
'content-type': 'application/json'
},
data: data
};
};
2016-07-21 11:18:48 -03:00
root.savePendingGiftCard = function(gc, opts, cb) {
var network = configService.getSync().amazon.testnet ? 'testnet' : 'livenet';
storageService.getAmazonGiftCards(network, function(err, oldGiftCards) {
if (lodash.isString(oldGiftCards)) {
oldGiftCards = JSON.parse(oldGiftCards);
}
if (lodash.isString(gc)) {
gc = JSON.parse(gc);
}
var inv = oldGiftCards || {};
2016-07-21 11:18:48 -03:00
inv[gc.invoiceId] = gc;
if (opts && (opts.error || opts.status)) {
2016-07-21 11:18:48 -03:00
inv[gc.invoiceId] = lodash.assign(inv[gc.invoiceId], opts);
}
if (opts && opts.remove) {
2016-07-21 11:18:48 -03:00
delete(inv[gc.invoiceId]);
}
inv = JSON.stringify(inv);
storageService.setAmazonGiftCards(network, inv, function(err) {
return cb(err);
});
});
};
2016-07-21 11:18:48 -03:00
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);
});
};
2016-07-21 11:18:48 -03:00
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
};
2016-05-19 01:18:15 -03:00
2016-07-21 11:18:48 -03:00
$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);
2016-05-19 01:18:15 -03:00
});
2016-05-19 17:29:24 -03:00
});
};
2016-07-21 11:18:48 -03:00
root.getBitPayInvoice = function(id, cb) {
$http(_getBitPay('/invoices/' + id)).then(function(data) {
$log.info('BitPay Get Invoice: SUCCESS');
return cb(null, data.data.data);
2016-05-19 17:29:24 -03:00
}, function(data) {
2016-07-21 11:18:48 -03:00
$log.error('BitPay Get Invoice: ERROR ' + data.data.error);
return cb(data.data.error);
});
};
2016-07-21 11:18:48 -03:00
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;
});