1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-30 19:52:20 +01:00

fix bug: user cant debit his wallet when admin pay a plan for this user with invoice disabled

This commit is contained in:
Peng DU 2016-11-29 11:38:33 +01:00
parent 5a4eaefc02
commit f487133825
2 changed files with 13 additions and 6 deletions

View File

@ -15,7 +15,7 @@ class API::SubscriptionsController < API::ApiController
if current_user.is_admin? if current_user.is_admin?
@subscription = Subscription.find_or_initialize_by(user_id: subscription_params[:user_id]) @subscription = Subscription.find_or_initialize_by(user_id: subscription_params[:user_id])
@subscription.attributes = subscription_params @subscription.attributes = subscription_params
is_subscribe = @subscription.save_with_local_payment(!User.find(subscription_params[:user_id]).invoicing_disabled?, coupon_params[:coupon_code]) is_subscribe = @subscription.save_with_local_payment(true, coupon_params[:coupon_code])
else else
@subscription = Subscription.find_or_initialize_by(user_id: current_user.id) @subscription = Subscription.find_or_initialize_by(user_id: current_user.id)
@subscription.attributes = subscription_params @subscription.attributes = subscription_params

View File

@ -18,6 +18,8 @@ class Subscription < ActiveRecord::Base
after_save :notify_partner_subscribed_plan, if: :of_partner_plan? after_save :notify_partner_subscribed_plan, if: :of_partner_plan?
# Stripe subscription payment # Stripe subscription payment
# @params [invoice] if true then subscription pay itself, dont pay with reservation
# if false then subscription pay with reservation
def save_with_payment(invoice = true, coupon_code = nil) def save_with_payment(invoice = true, coupon_code = nil)
if valid? if valid?
begin begin
@ -131,6 +133,8 @@ class Subscription < ActiveRecord::Base
end end
end end
# @params [invoice] if true then subscription pay itself, dont pay with reservation
# if false then subscription pay with reservation
def save_with_local_payment(invoice = true, coupon_code = nil) def save_with_local_payment(invoice = true, coupon_code = nil)
if valid? if valid?
# very important to set expired_at to nil that can allow method is_new? to return true # very important to set expired_at to nil that can allow method is_new? to return true
@ -143,17 +147,20 @@ class Subscription < ActiveRecord::Base
if save if save
UsersCredits::Manager.new(user: self.user).reset_credits if expired_date_changed UsersCredits::Manager.new(user: self.user).reset_credits if expired_date_changed
if invoice if invoice
invoc = generate_invoice(nil, coupon_code)
@wallet_amount_debit = get_wallet_amount_debit @wallet_amount_debit = get_wallet_amount_debit
# debit wallet # debit wallet
wallet_transaction = debit_user_wallet wallet_transaction = debit_user_wallet
if !self.user.invoicing_disabled?
invoc = generate_invoice(nil, coupon_code)
if wallet_transaction if wallet_transaction
invoc.wallet_amount = @wallet_amount_debit invoc.wallet_amount = @wallet_amount_debit
invoc.wallet_transaction_id = wallet_transaction.id invoc.wallet_transaction_id = wallet_transaction.id
end end
invoc.save invoc.save
end end
end
return true return true
else else
return false return false