diff --git a/core/billing.coffee b/core/billing.coffee index c7001fa..8c45790 100644 --- a/core/billing.coffee +++ b/core/billing.coffee @@ -43,7 +43,7 @@ exports.triggerBilling = (account, callback) -> return false async.each account.billing.plans, (plan_name, callback) -> - exports.generateBilling account, plan_name, is_force, (result) -> + exports.generateBilling account, plan_name, {is_force: is_force}, (result) -> callback null, result , (err, result) -> @@ -77,7 +77,8 @@ exports.triggerBilling = (account, callback) -> else callback account -exports.generateBilling = (account, plan_name, is_force, callback) -> +exports.generateBilling = (account, plan_name, options, callback) -> + {is_force} = options plan_info = config.plans[plan_name] unless plan_info.billing_by_time diff --git a/core/cache.coffee b/core/cache.coffee new file mode 100644 index 0000000..e69de29 diff --git a/core/router/billing.coffee b/core/router/billing.coffee index 365e682..6e971ce 100644 --- a/core/router/billing.coffee +++ b/core/router/billing.coffee @@ -12,25 +12,22 @@ exports.post '/join_plan', requireAuthenticate, (req, res) -> unless req.body.plan in _.keys(config.plans) return res.error 'invaild_plan' - if req.body.plan in req.account.attribute.plans + if req.body.plan in req.account.billing.plans return res.error 'already_in_plan' plan_info = config.plans[req.body.plan] - billing.calcBilling req.account, true, (account) -> - if plan_info.price and account.attribute.balance <= 0 + billing.triggerBilling req.account, (account) -> + if account.billing.balance < config.billing.force_freeze.when_balance_below return res.error 'insufficient_balance' - if account.attribute.balance < 0 - return res.error 'insufficient_balance' - - plan.joinPlan account, req.body.plan, -> + billing.joinPlan account, req.body.plan, -> res.json {} exports.post '/leave_plan', requireAuthenticate, (req, res) -> - unless req.body.plan in req.account.attribute.plans + unless req.body.plan in req.account.billing.plans return res.error 'not_in_plan' - billing.calcBilling req.account, true, (account) -> - plan.leavePlan account, req.body.plan, -> + billing.generateBilling req.account, req.body.plan, {is_force: true}, (account) -> + billing.leavePlan account, req.body.plan, -> res.json {} diff --git a/core/static/script/panel.coffee b/core/static/script/panel.coffee index 9a6346d..f38ef81 100644 --- a/core/static/script/panel.coffee +++ b/core/static/script/panel.coffee @@ -11,25 +11,15 @@ $ -> .success -> location.reload() - $('.plan-list .btn-danger').click -> + $('.action-leave-plan').click -> if window.confirm 'Are you sure?' - $.post "/plan/unsubscribe/", JSON.stringify + request '/billing/leave_plan/', plan: $(@).parents('tr').data 'name' - .fail (jqXHR) -> - if jqXHR.responseJSON?.error - alert jqXHR.responseJSON.error - else - alert jqXHR.statusText - .success -> + , (result) -> location.reload() - $('.plan-list .btn-success').click -> - $.post "/plan/subscribe/", JSON.stringify + $('.action-join-plan').click -> + request '/billing/join_plan/', plan: $(@).parents('tr').data 'name' - .fail (jqXHR) -> - if jqXHR.responseJSON?.error - alert jqXHR.responseJSON.error - else - alert jqXHR.statusText - .success -> + , (result) -> location.reload() diff --git a/core/view/panel.jade b/core/view/panel.jade index ab5d3e0..e4553ec 100644 --- a/core/view/panel.jade +++ b/core/view/panel.jade @@ -26,9 +26,9 @@ block main td= t(plan.t_description) td if plan.is_enable - button.btn.btn-danger.btn-sm 退订方案 + button.action-join-plan.btn.btn-danger.btn-sm 退订方案 else - button.btn.btn-success.btn-sm 订购方案 + button.action-leave-plan.btn.btn-success.btn-sm 订购方案 if selectHook('view.panel.switch_buttons').length #service-switch.row