Merge pull request #824 from rasika/monetize-cloud

Fixing https://github.com/wso2/product-iots/issues/1119: monetization
revert-70aa11f8
Rasika Perera 7 years ago committed by GitHub
commit 5e87ce2d66

@ -25,8 +25,33 @@
}
}
},
"Account": {
"url": "#",
"icon": "fw fw-resource",
"isAdminOnly": false,
"billingEnabled": true,
"billingApi": {
"username": "admin",
"password": "admin"
},
"cloudMgtIndexPage": "https://cloudmgt.cloud.wso2.com/cloudmgt/site/pages/index.jag",
"dropDown": {
"Upgrade Now": {
"url": "https://cloudmgt.cloud.wso2.com/cloudmgt/site/pages/payment-plans.jag?cloud-type=device_cloud",
"icon": "fw fw-export",
"dropDown": "true",
"target": "_self"
},
"Request Extension": {
"url": "https://cloudmgt.cloud.wso2.com/cloudmgt/site/pages/contact-us.jag?cloud-type=device_cloud&request-extension=true",
"icon": "fa fa-mail",
"dropDown": "true",
"target": "_self"
}
}
},
"Support": {
"url": "https://cloudmgt.cloud.wso2.com/cloudmgt/site/pages/contact-us.jag",
"url": "https://cloudmgt.cloud.wso2.com/cloudmgt/site/pages/contact-us.jag?cloud-type=device_cloud",
"icon": "fw fw-mail",
"isAdminOnly": false,
"target": "_self",
@ -63,7 +88,7 @@
"target": "_self"
},
"Logout": {
"url": "https://api.cloud.wso2.com/publisher/site/pages/logout.jag",
"url": "https://device.cloud.wso2.com/devicemgt/logout",
"icon": "fw fw-sign-out",
"dropDown": "true",
"target": "_self"

@ -0,0 +1,24 @@
/*
* Copyright (c) 2017, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* WSO2 Inc. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
$(document).ready(function(){
var expireModal = $('#expire-modal-content');
modalDialog.content(expireModal.find('.modal-body'));
modalDialog.header(expireModal.find('.modal-header .modal-title'));
modalDialog.footer(expireModal.find('.modal-footer'));
modalDialog.show();
});

@ -15,6 +15,7 @@
specific language governing permissions and limitations
under the License.
}}
{{unit "cdmf.unit.ui.modal"}}
<ul class="nav navbar-right float-remove-xs text-center-xs">
{{#each Main}}
<li class="visible-inline-block">
@ -25,25 +26,37 @@
title="
{{#if this.isDomain }}
{{@user.domain}}
{{else}}
{{#if this.label}}
{{this.label}}
{{else}}
{{@key}}
{{/if}}
{{/if}}
">
<span class="icon fw-stack fw-lg" {{#if this.isSupport}} style="color: #ff8c27;" {{/if}}>
<span class="icon fw-stack fw-lg" {{#if this.isSupport}} style="color: #ff8c27;" {{/if}} {{#if this.color}} style="color: {{this.color}};" {{/if}}>
<i class="{{this.icon}} fw-stack-1x" title="
{{#if this.isDomain }}
{{@user.domain}}
{{else}}
{{#if this.label}}
{{this.label}}
{{else}}
{{@key}}
{{/if}}
{{/if}}
"></i>
</span>
<span class="hidden-xs {{#if this.isDomain }} username {{/if}}">
<span class="hidden-xs {{#if this.isDomain }} username {{/if}}" {{#if this.color}} style="color: {{this.color}};" {{/if}}>
{{#if this.isDomain }}
{{@user.domain}}
{{else}}
{{#if this.label}}
{{this.label}}
{{else}}
{{@key}}
{{/if}}
{{/if}}
</span>
{{#if this.dropDownVisible}}
<span class="caret"></span>
@ -136,6 +149,42 @@
</div>
</div>
{{#if Main.Account.isExpired}}
<div id="expire-modal-content" class="hide">
<div class="modal-header">
<h3 class="pull-left modal-title">
<span>
<span class="fw-stack">
<i class="fw fw-circle-outline fw-stack-2x"></i>
<i class="fw fw-alert fw-stack-1x"></i>
</span> Trial Expired
</span>
</h3>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><i
class="fw fw-cancel"></i></button>
</div>
<div class="modal-body add-margin-top-2x add-margin-bottom-2x">
<div id="notification-error-msg" class="alert alert-danger hidden" role="alert">
<i class="icon fw fw-error"></i><span></span>
</div>
<div id="user-groups">Was it a successful trial? Your trial unfortunately expired, however it is really easy to upgrade to a paid plan and keep using WSO2 Device Cloud</div>
</div>
<div class="modal-footer">
<div class="buttons">
<a href="{{Main.Account.dropDown.[Upgrade Now].url}}" id="expire-upgrade-now-link" class="btn-operations">
Upgrade Now
</a>
<a href="{{Main.Account.dropDown.[Request Extension].url}}" id="expire-req-ext-link" class="btn-operations">
Request Exception
</a>
</div>
</div>
</div>
{{/if}}
{{#zone "bottomJs"}}
{{#if Main.Account.isExpired}}
{{js "/js/monetize.js"}}
{{/if}}
{{js "/js/user-menu.js"}}
{{/zone}}

@ -20,6 +20,10 @@ function onRequest(context) {
var constants = require("/app/modules/constants.js");
var mdmProps = require("/app/modules/conf-reader/main.js")["conf"];
var cloudProps = require("/app/modules/conf-reader/cloud.js")["conf"];
var log = new Log("user-menu.js");
var sqlDateFormatter = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
var user = context.user;
var isSuperTenant = false;
if (user.tenantId == -1234) {
@ -32,6 +36,79 @@ function onRequest(context) {
viewModal.Main.Domain.isDomain = true;
viewModal.Main.Support.isSupport = true;
//get billing info
var type = {PAID: "PAID", TRIAL: "TRIAL", FREE: "FREE"};
var status = {
ACTIVE: "ACTIVE",
INACTIVE: "INACTIVE",
EXTENDED: "EXTENDED",
PENDING_DISABLED: "PENDING_DISABLED",
DISABLED: "DISABLED"
};
var BILLING_INFO_KEY = 'BILLING_INFO_' + context.user.domain;
var BILLING_INFO_RETRY_COUNT_KEY = 'BILLING_INFO_RETRY_COUNT_' + context.user.domain;
if (viewModal.Main.Account.billingEnabled) {
if (!session.get(BILLING_INFO_KEY) || daysAfterLastCheck(session.get(BILLING_INFO_KEY).lastChecked) > 1) {
session.put(BILLING_INFO_RETRY_COUNT_KEY, 0);
getBillingData(getLoginCookie());
}
var billingInfo = session.get(BILLING_INFO_KEY);
var isExpired = false;
var isTrial = false;
var trialPeriod = 14;
var cloudMgtIndexPage = viewModal.Main.Account.cloudMgtIndexPage;
if (!billingInfo) {
log.info("Access denied for tenant: " + context.user.domain
+ " with a NULL subscription. Redirected to CloudMgt");
response.sendRedirect(cloudMgtIndexPage);
exit(0);
} else if (billingInfo.isPaidAccount && (billingInfo.billingPlanStatus === status.ACTIVE
|| billingInfo.billingPlanStatus === status.PENDING_DISABLED)) {
isExpired = false;
//change menu item name
delete viewModal.Main.Account.color;
delete viewModal.Main.Account["Request Extension"];
} else if (!billingInfo.isPaidAccount) {
var accountContent = "Account";
if (billingInfo.billingPlanStatus === status.ACTIVE || billingInfo.billingPlanStatus === status.EXTENDED) {
var currDate = new java.util.Date();
var endDate = sqlDateFormatter.parse(billingInfo.endDate);
var diff = endDate.getTime() - currDate.getTime();
isTrial = true;
if (diff > 0) {
noOfDays = java.util.concurrent.TimeUnit.MILLISECONDS.toDays(diff);
accountContent = "Trial - " + (noOfDays + 1) + " days to upgrade";
} else {
isExpired = true;
accountContent = "Trial Expired";
}
} else if (billingInfo.billingPlanStatus === status.INACTIVE) {
isExpired = false;
isTrial = true;
accountContent = "Trial " + (trialPeriod) + " days tade";
}
//change menu item name
viewModal.Main.Account.label = accountContent;
viewModal.Main.Account.isDomain = false;
viewModal.Main.Account.color = "red";
viewModal.Main.Account.isExpired = isExpired;
} else if (billingInfo.billingPlanStatus === status.DISABLED) {
log.info(
"Access denied for tenant: " + context.user.domain
+ " with a DISABLED subscription. Redirected to CloudMgt");
response.sendRedirect(cloudMgtIndexPage);
exit(0);
}
} else {
// delete viewModal.Main.Account;
viewModal.Main.Account = {"billingEnabled": false}
}
for (var key in viewModal.Main) {
var tempDropDownCheck = false;
@ -80,4 +157,63 @@ function onRequest(context) {
viewModal.USER_SESSION_KEY = session.get(constants["USER_SESSION_KEY"]);
viewModal.isCloud = mdmProps.isCloud;
return viewModal;
function getBillingData(cookie, attempt) {
serviceUrl = "https://cloudmgt.cloudstaging.wso2.com/cloudmgt/site/blocks/admin/admin.jag";
result = post(serviceUrl,
'action=getBillingStatusOfTenant&tenantDomain=' + context.user.domain + '&cloudType=device_cloud',
{"Cookie": cookie});
if (result.data) {
var billing = JSON.parse(result.data).data;
if (!billing.isPaidAccount && billing.billingPlanStatus === status.INACTIVE) {
var rv = post(serviceUrl,
'action=informFirstLogin&tenantDomain=' + context.user.domain + '&cloudType=device_cloud',
{"Cookie": cookie});
if (!attempt) attempt = 1;
var failStr = "First login capturing failed";
var successStr = "First login captured successfully";
if (rv.data.substring(0, failStr.length) === failStr && attempt < 3) {
getBillingData(cookie, ++attempt); //retry
} else if(rv.data.substring(0, successStr.length) === successStr) {
getBillingData(cookie); //get expiry details
}
} else {
session.put(BILLING_INFO_KEY, JSON.parse(result.data).data);
session.put(BILLING_INFO_RETRY_COUNT_KEY, 0);
}
}
}
function getLoginCookie(){
var retryCount = session.get(BILLING_INFO_RETRY_COUNT_KEY) || 0;
if (retryCount <= 3) {
retryCount++;
session.put(BILLING_INFO_RETRY_COUNT_KEY, retryCount);
var username = viewModal.Main.Account.billingApi.username;
var password = viewModal.Main.Account.billingApi.password;
var serviceUrl = "https://cloudmgt.cloudstaging.wso2.com/cloudmgt/site/blocks/user/authenticate/ajax/login.jag";
var result = post(serviceUrl, 'action=login&userName=' + username + '&password=' + password,
{"Content-Type": "application/x-www-form-urlencoded"});
if (result.data && result.data.trim() === "true") {
var cookieHeader = result.xhr.getResponseHeader("Set-Cookie");
if (cookieHeader) {
var cookie = cookieHeader.split(";")[0];
return cookie;
}
}
} else {
log.error("Billing info api failed after " + session.get(BILLING_INFO_RETRY_COUNT_KEY) + " attempts!");
}
}
function daysAfterLastCheck(checkedDate){
if(!checkedDate) return 0;
var currDate = new java.util.Date();
var endDate = sqlDateFormatter.parse(checkedDate);
var diff = endDate.getTime() - currDate.getTime();
if (diff > 0) {
return java.util.concurrent.TimeUnit.MILLISECONDS.toDays(diff);
}
return 0;
}
}
Loading…
Cancel
Save