Class: Caboose::MyAccountInvoicesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/my_account_invoices_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_action, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#authnet_relayObject


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 193

def authnet_relay
  Caboose.log("Authorize.net relay for my account, invoice #{params[:x_invoice_id]}")
  
  invoice = Caboose::Invoice.find(params[:x_invoice_num])
  ot = Caboose::InvoiceTransaction.new(
    :invoice_id => invoice.id,
    :date_processed => DateTime.now.utc,
    :transaction_type => Caboose::InvoiceTransaction::TYPE_AUTHORIZE
  )
  ot.success        = params[:x_response_code] && params[:x_response_code] == '1'
  ot.transaction_id = params[:x_trans_id] if params[:x_trans_id]              
  ot.auth_code      = params[:x_auth_code] if params[:x_auth_code]
  ot.response_code  = params[:x_response_code] if params[:x_response_code]
  ot.amount         = invoice.total
  ot.save
  
  error = nil
  if ot.success
    invoice.financial_status = Invoice::FINANCIAL_STATUS_AUTHORIZED
    invoice.status = Invoice::STATUS_PENDING if invoice.status == Invoice::STATUS_CART
    invoice.invoice_number = @site.store_config.next_invoice_number if invoice.invoice_number.nil?
    
    # Send out emails        
    InvoicesMailer.configure_for_site(@site.id).customer_new_invoice(invoice).deliver                
    
    # Emit invoice event
    Caboose.plugin_hook('invoice_authorized', invoice)        
  else
    invoice.financial_status = Invoice::FINANCIAL_STATUS_PENDING        
    error = "There was a problem processing your payment."
  end
        
  invoice.save
  
  @url = params[:x_after_relay]
  @url << (ot.success ? "?success=1" : "?error=#{error}")             
              
  render :layout => false
end

#authnet_responseObject


235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 235

def authnet_response
  Caboose.log("Authorize.net response for my account, invoice #{params[:id]}")
  
  @resp = Caboose::StdClass.new
  @resp.success = true if params[:success]
  @resp.error = params[:error] if params[:error]
  
  # Go ahead and capture funds if the invoice only contained downloadable items
  @invoice = Invoice.find(params[:id])
  if !@invoice.has_shippable_items?
    capture_resp = @invoice.capture_funds
    if capture_resp.error
      @resp.success = false
      @resp.error = capture_resp.error
    end        
  end      
  render :layout => false
end

#confirmObject


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 69

def confirm      
  sc = @site.store_config
  @invoice = Invoice.find(params[:id])

  # Make sure all the variants still exist      
  @invoice.line_items.each do |li|
    v = Variant.where(:id => li.variant_id).first
    if v.nil? || v.status == 'Deleted'
      render :json => { :error => 'One or more of the products you are purchasing are no longer available.' }
      return
    end
  end

  error = false      
  requires_payment = @invoice.line_items.count > 0 && @invoice.total > 0
  if requires_payment
    ot = nil

    Stripe.api_key = sc.stripe_secret_key.strip
    begin
      c = Stripe::Charge.create(
        :amount => (@invoice.total * 100).to_i,
        :currency => 'usd',
        :customer => logged_in_user.stripe_customer_id,
        :capture => false,
        :metadata => { :invoice_id => @invoice.id },
        :statement_descriptor => "#{@site.description.truncate(22)}"
      )
    rescue Exception => ex
      render :json => { :error => ex.message }
      return
    end
    ot = Caboose::InvoiceTransaction.create(
      :invoice_id        => @invoice.id,
      :transaction_id    => c.id,
      :transaction_type  => c.captured ? Caboose::InvoiceTransaction::TYPE_AUTHCAP : Caboose::InvoiceTransaction::TYPE_AUTHORIZE,
      :payment_processor => sc.pp_name,
      :amount            => c.amount/100.0,
      :date_processed    => DateTime.now.utc,
      :success           => c.status == 'succeeded'
    )

    if !ot.success
      render :json => { :error => error }
      return
    else
      capture_resp = @invoice.capture_funds
      if capture_resp.success == true
        @invoice.take_gift_card_funds
        @invoice.status = Caboose::Invoice::STATUS_PROCESSED
      end
    end
  end
  
  @invoice.invoice_number = @site.store_config.next_invoice_number
  
  # Send out emails
  begin
    InvoicesMailer.configure_for_site(@site.id).customer_new_invoice(@invoice).deliver
    InvoicesMailer.configure_for_site(@site.id).fulfillment_new_invoice(@invoice).deliver
  rescue
    puts "=================================================================="
    puts "Error sending out invoice confirmation emails for invoice ID #{@invoice.id}"
    puts "=================================================================="
  end

  # Save the invoice
  @invoice.save

  # Decrement quantities of variants
  @invoice.decrement_quantities

  render :json => {
    :success => true,
    :message => "Thank you for your payment!"
  }
  return
end

#editObject


255
256
257
258
259
260
261
262
263
264
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 255

def edit
  return if !verify_logged_in
  
  @invoice = Invoice.find(params[:id])
  if @invoice.customer_id != logged_in_user.id
    @error = "The given invoice does not belong to you."
    render :file => 'caboose/extras/error'
    return
  end
end

#indexObject


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 8

def index
  return if !verify_logged_in
        
  @pager = Caboose::PageBarGenerator.new(params, {
    'customer_id'          => logged_in_user.id,         
    'status'               => [          
      Invoice::STATUS_PENDING       ,    
      Invoice::STATUS_CANCELED      ,
      Invoice::STATUS_READY_TO_SHIP ,
      Invoice::STATUS_PROCESSED          
    ]
  }, {
    'model'          => 'Caboose::Invoice',
    'sort'           => 'id',
    'desc'           => 1,
    'base_url'       => '/my-account/invoices',
    'use_url_params' => false
  })      
  @invoices = @pager.all_items
end

#invoice_jsonObject


149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 149

def invoice_json
  return if !logged_in?
  
  invoice = Invoice.find(params[:id])
  if invoice.customer_id != logged_in_user.id        
    render :json => { :error => "The given invoice does not belong to you." } 
    return
  end
  
  if invoice.shipping_address_id.nil?
    sa = Address.create
    invoice.shipping_address_id = sa.id
    invoice.save
  end
  render :json => invoice.as_json(:include => [        
    { :line_items => { :include => { :variant => { :include => :product }}}},
    { :invoice_packages => { :include => [:shipping_package, :shipping_method] }},
    { :discounts => { :include => :gift_card }},
    :customer,
    :shipping_address,
    :billing_address,
    :invoice_transactions
  ])
end

#invoice_pdfObject


175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 175

def invoice_pdf
  invoice = Invoice.find(params[:id])

  if invoice.customer_id != logged_in_user.id
    @error = "The given invoice does not belong to you."
    render :file => 'caboose/extras/error'
    return
  end

  pdf = @site.store_config.custom_invoice_pdf
  pdf = "InvoicePdf" if pdf.nil? || pdf.strip.length == 0                              
  eval("pdf = #{pdf}.new")
  pdf.invoice = Invoice.find(params[:id])             
  send_data pdf.to_pdf, :filename => "invoice_#{pdf.invoice.id}.pdf", :type => "application/pdf", :disposition => "inline"   
end

#payment_formObject


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/caboose/my_account_invoices_controller.rb', line 30

def payment_form
  return if !logged_in?
  
  @invoice = Invoice.find(params[:id])
  if @invoice.customer_id != logged_in_user.id
    @error = "The given invoice does not belong to you."
    render :file => 'caboose/extras/error'
    return
  end
  
  if @invoice.financial_status != Invoice::FINANCIAL_STATUS_PENDING        
    @error = "This invoice does not require payment at this time."
    render :file => 'caboose/extras/error'
    return
  end
    
  sc = @site.store_config
  case sc.pp_name
    when 'authorize.net'
                  
      @sim_transaction = AuthorizeNet::SIM::Transaction.new(
        sc., 
        sc.authnet_api_transaction_key, 
        @invoice.total,                          
        :relay_response => 'TRUE',              
        :relay_url => "#{sc.authnet_relay_domain}/my-account/invoices/authnet-relay",
        :transaction_type => 'AUTH_ONLY',                        
        :test => sc.pp_testing
      )
      @request = request
      @show_relay = params[:show_relay] && params[:show_relay].to_i == 1
      
    when 'stripe'
      # TODO: Implement manual invoice payment for stripe
  end      
  render :layout => false      
end