2017-11-04 16:59:48 +00:00
|
|
|
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;
|
2017-11-04 16:59:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const checkPayment = () => {
|
|
|
|
const currentDate = Date.now();
|
2017-12-11 00:53:46 +00:00
|
|
|
if (!isGoogleSponsor() && networkSvc.isUserActive() && !store.state.offline &&
|
2017-11-04 16:59:48 +00:00
|
|
|
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);
|
|
|
|
},
|
2017-11-04 16:59:48 +00:00
|
|
|
getToken() {
|
|
|
|
if (isGoogleSponsor() || store.state.offline) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return getMonetize()
|
|
|
|
.then(() => new Promise(resolve =>
|
|
|
|
monetize.getTokenImmediate((err, result) => resolve(result))));
|
|
|
|
},
|
|
|
|
};
|