Stackedit/src/services/sponsorSvc.js

55 lines
1.6 KiB
JavaScript
Raw Normal View History

import store from '../store';
import networkSvc from './networkSvc';
import utils from './utils';
const checkPaymentEvery = 15 * 60 * 1000; // 15 min
let lastCheck = 0;
const appId = 'ESTHdCYOi18iLhhO';
let monetize;
const getMonetize = () => Promise.resolve()
.then(() => networkSvc.loadScript('https://cdn.monetizejs.com/api/js/latest/monetize.min.js'))
.then(() => {
monetize = monetize || new window.MonetizeJS({
applicationID: appId,
});
});
const isGoogleSponsor = () => {
2017-12-17 15:08:52 +00:00
const sponsorToken = store.getters['workspace/sponsorToken'];
return sponsorToken && sponsorToken.isSponsor;
};
const checkPayment = () => {
const currentDate = Date.now();
2017-12-11 00:53:46 +00:00
if (!isGoogleSponsor() && networkSvc.isUserActive() && !store.state.offline &&
lastCheck + checkPaymentEvery < currentDate
) {
lastCheck = currentDate;
getMonetize()
.then(() => monetize.getPaymentsImmediate((err, payments) => {
const isSponsor = payments && payments.app === appId && (
(payments.chargeOption && payments.chargeOption.alias === 'once') ||
(payments.subscriptionOption && payments.subscriptionOption.alias === 'yearly'));
if (isSponsor !== store.state.monetizeSponsor) {
store.commit('setMonetizeSponsor', isSponsor);
}
}));
}
};
export default {
2017-12-11 00:53:46 +00:00
init: () => {
utils.setInterval(checkPayment, 2000);
},
getToken() {
if (isGoogleSponsor() || store.state.offline) {
return Promise.resolve();
}
return getMonetize()
.then(() => new Promise(resolve =>
monetize.getTokenImmediate((err, result) => resolve(result))));
},
};