diff --git a/app/controllers/api/subscriptions_controller.rb b/app/controllers/api/subscriptions_controller.rb index cd16e617c..cd6db0454 100644 --- a/app/controllers/api/subscriptions_controller.rb +++ b/app/controllers/api/subscriptions_controller.rb @@ -15,7 +15,7 @@ class API::SubscriptionsController < API::ApiController if current_user.is_admin? @subscription = Subscription.find_or_initialize_by(user_id: subscription_params[:user_id]) @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 @subscription = Subscription.find_or_initialize_by(user_id: current_user.id) @subscription.attributes = subscription_params diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 5882036ad..b6224a6e0 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -18,6 +18,8 @@ class Subscription < ActiveRecord::Base after_save :notify_partner_subscribed_plan, if: :of_partner_plan? # 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) if valid? begin @@ -131,6 +133,8 @@ class Subscription < ActiveRecord::Base 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) if valid? # very important to set expired_at to nil that can allow method is_new? to return true @@ -143,16 +147,19 @@ class Subscription < ActiveRecord::Base if save UsersCredits::Manager.new(user: self.user).reset_credits if expired_date_changed if invoice - invoc = generate_invoice(nil, coupon_code) @wallet_amount_debit = get_wallet_amount_debit # debit wallet wallet_transaction = debit_user_wallet - if wallet_transaction - invoc.wallet_amount = @wallet_amount_debit - invoc.wallet_transaction_id = wallet_transaction.id + + if !self.user.invoicing_disabled? + invoc = generate_invoice(nil, coupon_code) + if wallet_transaction + invoc.wallet_amount = @wallet_amount_debit + invoc.wallet_transaction_id = wallet_transaction.id + end + invoc.save end - invoc.save end return true else