Mercado Libre: First steps
This commit is contained in:
parent
67612e547c
commit
9abd852f4b
18 changed files with 3648 additions and 0 deletions
196
src/js/services/mercadoLibreService.js
Normal file
196
src/js/services/mercadoLibreService.js
Normal file
|
|
@ -0,0 +1,196 @@
|
|||
'use strict';
|
||||
angular.module('copayApp.services').factory('mercadoLibreService', function($http, $log, lodash, moment, storageService, configService, platformInfo, nextStepsService, homeIntegrationsService) {
|
||||
var root = {};
|
||||
var credentials = {};
|
||||
|
||||
// Not used yet
|
||||
var availableCountries = [{
|
||||
'country': 'Brazil',
|
||||
'currency': 'BRL',
|
||||
'name': 'Mercado Livre',
|
||||
'url': 'https://www.mercadolivre.com.br'
|
||||
}];
|
||||
|
||||
/*
|
||||
* Development: 'testnet'
|
||||
* Production: 'livenet'
|
||||
*/
|
||||
//credentials.NETWORK = 'livenet';
|
||||
credentials.NETWORK = 'testnet';
|
||||
|
||||
if (credentials.NETWORK == 'testnet') {
|
||||
credentials.BITPAY_API_URL = "https://test.bitpay.com";
|
||||
} else {
|
||||
credentials.BITPAY_API_URL = "https://bitpay.com";
|
||||
};
|
||||
|
||||
var homeItem = {
|
||||
name: 'mercadoLibre',
|
||||
title: 'Mercado Libre',
|
||||
icon: 'icon-ml',
|
||||
sref: 'tabs.giftcards.mercadoLibre',
|
||||
};
|
||||
|
||||
var nextStepItem = {
|
||||
name: 'mercadoLibre',
|
||||
title: 'Buy Mercado Libre Gift Cards',
|
||||
icon: 'icon-ml',
|
||||
sref: 'tabs.giftcards.mercadoLibre',
|
||||
};
|
||||
|
||||
var _getBitPay = function(endpoint) {
|
||||
return {
|
||||
method: 'GET',
|
||||
url: credentials.BITPAY_API_URL + endpoint,
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
var _postBitPay = function(endpoint, data) {
|
||||
return {
|
||||
method: 'POST',
|
||||
url: credentials.BITPAY_API_URL + endpoint,
|
||||
headers: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
data: data
|
||||
};
|
||||
};
|
||||
|
||||
root.getNetwork = function() {
|
||||
return credentials.NETWORK;
|
||||
};
|
||||
|
||||
root.savePendingGiftCard = function(gc, opts, cb) {
|
||||
var network = root.getNetwork();
|
||||
storageService.getMercadoLibreGiftCards(network, function(err, oldGiftCards) {
|
||||
if (lodash.isString(oldGiftCards)) {
|
||||
oldGiftCards = JSON.parse(oldGiftCards);
|
||||
}
|
||||
if (lodash.isString(gc)) {
|
||||
gc = JSON.parse(gc);
|
||||
}
|
||||
var inv = oldGiftCards || {};
|
||||
inv[gc.invoiceId] = gc;
|
||||
if (opts && (opts.error || opts.status)) {
|
||||
inv[gc.invoiceId] = lodash.assign(inv[gc.invoiceId], opts);
|
||||
}
|
||||
if (opts && opts.remove) {
|
||||
delete(inv[gc.invoiceId]);
|
||||
}
|
||||
|
||||
inv = JSON.stringify(inv);
|
||||
|
||||
|
||||
storageService.setMercadoLibreGiftCards(network, inv, function(err) {
|
||||
|
||||
homeIntegrationsService.register(homeItem);
|
||||
nextStepsService.unregister(nextStepItem.name);
|
||||
return cb(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
root.getPendingGiftCards = function(cb) {
|
||||
var network = root.getNetwork();
|
||||
storageService.getMercadoLibreGiftCards(network, function(err, giftCards) {
|
||||
var _gcds = giftCards ? JSON.parse(giftCards) : null;
|
||||
return cb(err, _gcds);
|
||||
});
|
||||
};
|
||||
|
||||
root.createBitPayInvoice = function(data, cb) {
|
||||
|
||||
// TODO
|
||||
var dataSrc = {
|
||||
currency: 'USD' || data.currency,
|
||||
amount: data.amount,
|
||||
clientId: data.uuid
|
||||
};
|
||||
console.log('[mercadoLibreService.js:106]',dataSrc); //TODO
|
||||
|
||||
$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);
|
||||
});
|
||||
};
|
||||
|
||||
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('BitPay Get Invoice: ERROR ' + data.data.error);
|
||||
return cb(data.data.error);
|
||||
});
|
||||
};
|
||||
|
||||
root.createGiftCard = function(data, cb) {
|
||||
console.log('[mercadoLibreService.js:132]',data); //TODO
|
||||
|
||||
return cb(null, {
|
||||
"id": "f2bd6204fc49661a56057d33e37e32f2518ae92eea2f5457c379434712e35537",
|
||||
"currency_id": data.currency,
|
||||
"external_reference": "external_id_123456",
|
||||
"initial_amount": data.amount,
|
||||
"balance": 59.99,
|
||||
"status": "active",
|
||||
"date_creation": "20170314T10:39:14.82603:00",
|
||||
"date_activation": "20170314T10:39:15.31603:00",
|
||||
"date_expiration": "20180914T10:39:15.31603:00",
|
||||
"date_last_updated": "20170314T10:39:15.32103:00",
|
||||
"pin": "UYNHSIONUY"
|
||||
});
|
||||
|
||||
var dataSrc = {
|
||||
"clientId": data.uuid,
|
||||
"invoiceId": data.invoiceId,
|
||||
"accessKey": data.accessKey
|
||||
};
|
||||
|
||||
$http(_postBitPay('/mercado-libre-gift/redeem', dataSrc)).then(function(data) {
|
||||
var status = data.data.status == 'new' ? 'PENDING' : (data.data.status == 'paid') ? 'PENDING' : data.data.status;
|
||||
data.data.status = status;
|
||||
$log.info('Mercado Libre Gift Card Create/Update: ' + status);
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('Mercado Libre Gift Card Create/Update: ' + data.data.message);
|
||||
return cb(data.data);
|
||||
});
|
||||
};
|
||||
|
||||
root.cancelGiftCard = function(data, cb) {
|
||||
|
||||
var dataSrc = {
|
||||
"clientId": data.uuid,
|
||||
"invoiceId": data.invoiceId,
|
||||
"accessKey": data.accessKey
|
||||
};
|
||||
|
||||
$http(_postBitPay('/mercado-libre-gift/cancel', dataSrc)).then(function(data) {
|
||||
$log.info('Mercado Libre Gift Card Cancel: SUCCESS');
|
||||
return cb(null, data.data);
|
||||
}, function(data) {
|
||||
$log.error('Mercado Libre Gift Card Cancel: ' + data.data.message);
|
||||
return cb(data.data);
|
||||
});
|
||||
};
|
||||
|
||||
var register = function() {
|
||||
storageService.getMercadoLibreGiftCards(root.getNetwork(), function(err, giftCards) {
|
||||
if (giftCards) {
|
||||
homeIntegrationsService.register(homeItem);
|
||||
} else {
|
||||
nextStepsService.register(nextStepItem);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
register();
|
||||
return root;
|
||||
});
|
||||
|
|
@ -610,5 +610,17 @@ angular.module('copayApp.services')
|
|||
storage.remove('txConfirmNotif-' + txid, cb);
|
||||
};
|
||||
|
||||
root.setMercadoLibreGiftCards = function(network, gcs, cb) {
|
||||
storage.set('mercadoLibreGiftCards-' + network, gcs, cb);
|
||||
};
|
||||
|
||||
root.getMercadoLibreGiftCards = function(network, cb) {
|
||||
storage.get('mercadoLibreGiftCards-' + network, cb);
|
||||
};
|
||||
|
||||
root.removeMercadoLibreGiftCards = function(network, cb) {
|
||||
storage.remove('MercadoLibreGiftCards-' + network, cb);
|
||||
};
|
||||
|
||||
return root;
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue