SupremeA
SupremeA

Reputation: 1621

call object in another controller before save rails 4

I have an object called reportapproval. I start the object in a form and before it is saved I want it to go thru a charge controller. If the charge does not go thru then it wont save the object but if it does then it will save.

I am trying to pass the unsaved object to the charge controller. How can I do this. Here are the 2 actions in their respective controller.

reportapproval_controller.rb

def create
        @reportapproval = current_manager.reportapprovals.new(authorization_params)
          if @reportapproval.valid?
            if @reportapproval.manager_paying_report == true
              flash[:notice] = "Please make payment before proceeding"
              redirect_to new_managers_charge_path(id: @reportapproval_id)
              charge_error = nil
            end
          end
end

charge_controller.rb

def create
    # Amount in cents
    @reportapproval = Reportapproval.new(session[:reportapproval_id])
    @manager = current_manager
    @amount = @reportapproval.manager_request_report_type

    customer = Stripe::Customer.create(
      :email => @manager.email,
      :card  => params[:stripeToken]
     )

    charge = Stripe::Charge.create(
       :customer    => customer.id,
    ...
    )
     @reportapproval.report_paid = true
     @reportapproval.save!
     redirect_to managers_dashboard_path, :notice => "You have successfully ..."

    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to managers_charges_path
    end 
end

How can I pass this object to the charge and then back to the reportapproval before saving to the DB. I am using mongoid btw. Thanks in advance.

Upvotes: 1

Views: 160

Answers (1)

Thomas R. Koll
Thomas R. Koll

Reputation: 3139

You can but you shouldn't do that. Save the Reportapproval with a flag to indicate that it hasn't been paid for yet. After the Stripe procedure went through remove the flag to allow the user full access.

Btw, this will also help you find out wether and how many user do stop during the Stripe procedure and not come back again.

Upvotes: 1

Related Questions