0

I would like to handle Stripe errors with my cash_in_order model.

> CashInOrder.new
=> #<CashInOrder:0x000055e51bee03d0
 id: nil,
 deposit_id: nil,
 order_sum: nil,
 stripe_charge_id: nil,
 created_at: nil,
 updated_at: nil>

I attached to cash_in_order form select_tag to choose a payment card

<%= form_with model: @cash_in_order, class: 'form', id: 'cash_in_order'  do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= select_tag 'cash_in_order[order_source]', options_for_select(cards.collect{ |c| ["**** **** **** #{c.last4}", c.id] }, cards.first.id), class: "form-control" %>
    <br>
    <%= f.text_field :order_sum, class: "form-control", placeholder: "Amount" %>
    <br>
    <%= f.submit "add founds", class: "btn btn-default" %>
<% end %>

And add to cash_in_order_params order_source attribute

class CashInOrdersController < ApplicationController
    
    create
        @user = current_user
        @deposit = @user.deposit
        @cash_in_order = @deposit.cash_in_orders.build
        if current_user and current_user.deposit.id == @deposit.id
          @cash_in_order.save
        end
        respond_to do |format|
          if @cash_in_order.save
        format.html { redirect_to @deposit, notice: 'Founds were added to deposit!!' }
        format.json {head :no_content}
        format.js { flash.now[:notice] = "Founds were added to deposit!" }
          else
        format.html { redirect_to @deposit, notice: 'Founds were not added to deposit! Something was going wrong' }
        format.json { render json: @cash_in_order.errors.full_messages, status: :unprocessable_entity }
          end
        end
    end
    
    private
        def cash_in_order_params
          params.require(:cash_in_order).permit(:order_source, :order_sum)
        end
end

After form submit I see in console

Started POST "/cash_in_orders" for ::1 at 2020-09-29 07:46:11 -0400
Processing by CashInOrdersController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Qgznx99ZUDwBG4R7moEE1gIP9o7FCpzbQh+FO8ccosrfMy5V7O5ydg/b6QvT7jZWUmQKtz+JDRO5rM4msDhzrw==", "cash_in_order"=>{"order_source"=>"card_1HSnL8KcFr6ZSgIRyUZUQrtE", "order_sum"=>""}, "commit"=>"add founds"}

order_sum params I leaved empty to get error from Stripe (greater than zero).

To handle Stripe errors I created validator in cash_in_order model

class CashInOrder < ApplicationRecord
validate :is_cashed_in

    private

    def is_cashed_in
            card = self.deposit.user.retrieve_card(self.order_source) #Here is my error!!!
            Stripe::Charge.create(:amount => (amount.to_f*100).to_i, :currency => 'usd', :source => card.id, :customer => self.deposit.user.stripe_customer_id)
    
            rescue Stripe::InvalidRequestError => e
                logger.error "Stripe error: #{e.message}"
                errors.add :base, "There was a problem with your credit card."
                false
    end

end

I getting error

NoMethodError in CashInOrdersController#create

undefined method `order_source' for #CashInOrder:0x00007f42f8f129e8

Sure, my model didn't have an order_source atrribute, but I added it to cash_in_order_params and I can see it in request to my server

So my question is How can I send to model validator this order_source params correctly?

Thanks for any advance!

anndrew78
  • 196
  • 1
  • 20
  • I don't know the answer to your question. But, putting a Stripe transaction inside an ActiveRecord instance seems like, IMO, code smell. – jvillian Sep 29 '20 at 12:56
  • It is unfinished code, I can't step forward until fix this error. Yes, at this moment code looks abnormal) – anndrew78 Sep 29 '20 at 13:02

1 Answers1

1

Try using attr_accessor

class CashInOrder < ApplicationRecord
  attr_accessor :order_source
end
Salil
  • 46,566
  • 21
  • 122
  • 156