refactor billing

This commit is contained in:
jysperm
2014-09-24 08:13:30 +08:00
parent 490a239876
commit fcb3c112e0
5 changed files with 18 additions and 30 deletions

View File

@@ -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

0
core/cache.coffee Normal file
View File

View File

@@ -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 {}

View File

@@ -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()

View File

@@ -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