Wallet/src/js/services/payproService.js

69 lines
2 KiB
JavaScript
Raw Normal View History

'use strict';
angular.module('copayApp.services').factory('payproService',
2018-03-12 17:14:56 +09:00
function(profileService, platformInfo, gettextCatalog, ongoingProcess, $log, $http) {
2017-04-05 14:38:06 -03:00
var ret = {};
ret.getPayProDetails = function(uri, coin, cb, disableLoader) {
2017-04-05 14:38:06 -03:00
if (!cb) cb = function() {};
2017-04-05 14:38:06 -03:00
var wallet = profileService.getWallets({
onlyComplete: true,
coin: coin
2017-04-05 14:38:06 -03:00
})[0];
2017-04-05 14:38:06 -03:00
if (!wallet) return cb();
2017-04-05 14:38:06 -03:00
if (platformInfo.isChromeApp) {
return cb(gettextCatalog.getString('Payment Protocol not supported on Chrome App'));
}
2017-04-05 14:38:06 -03:00
$log.debug('Fetch PayPro Request...', uri);
if (!disableLoader) ongoingProcess.set('fetchingPayPro', true);
wallet.fetchPayPro({
payProUrl: uri,
}, function(err, paypro) {
if (!disableLoader) ongoingProcess.set('fetchingPayPro', false);
if (err) return cb(gettextCatalog.getString('Could Not Fetch Payment: Check if it is still valid'));
2017-04-05 14:38:06 -03:00
else if (!paypro.verified) {
$log.warn('Failed to verify payment protocol signatures');
return cb(gettextCatalog.getString('Payment Protocol Invalid'));
}
return cb(null, paypro);
});
};
2018-03-12 17:14:56 +09:00
ret.getPayProDetailsViaHttp = function(uri, cb) {
var config = {
headers: {'Accept': 'application/payment-request'}
};
$http.get(uri, config).then(function(response) {
return cb(null, response.data);
}, function(error) {
return cb(error, null);
});
}
ret.broadcastBchTx = function(signedTxp, cb) {
var config = {
headers: {'Content-Type': 'application/payment'}
};
var data = {
currency: 'BCH',
transactions: [signedTxp.raw]
};
$http.post(signedTxp.payProUrl, data, config).then(function(response) {
signedTxp.response = response.data;
return cb(null, signedTxp);
}, function(error) {
return cb(error.data, null);
});
}
2017-04-05 14:38:06 -03:00
return ret;
});