Skip to content

Commit

Permalink
[(finishes) #187093505 #187093494] Added Winson's version and modifie…
Browse files Browse the repository at this point in the history
…d store_controller_spec to check for the fund_code param
  • Loading branch information
MayZamudio committed Mar 11, 2024
1 parent 6ecac30 commit 9815583
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
17 changes: 9 additions & 8 deletions app/controllers/store_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def donate
reset_shopping # even if order in progress, going to donation page cancels it
@account_code = AccountCode.find_by_code(params[:fund_code]) ||
AccountCode.find_by_id(params[:fund_code]) || AccountCode.default_account_code #using the Code
# @account_code = AccountCode.find_by_id(params[:fund_code]) || AccountCode.default_account_code # Using id

@account_code_name = @account_code.name
@account_code_description = @account_code.description
if @customer == Customer.anonymous_customer
Expand All @@ -127,24 +127,25 @@ def donate

def process_donation
@amount = to_numeric(params[:donation])
# @account_code = AccountCode.find_by(code: params[:fund_code])
# puts "Account Code Id: #{@account_code.id}"
# @account_code_id = AccountCode.find(params[:fund_code])

@account_code = AccountCode.find_by_code(params[:fund_code]) ||
AccountCode.find_by_id(params[:fund_code]) || AccountCode.default_account_code

if params[:customer_id].blank?
customer_params = params.require(:customer).permit(Customer.user_modifiable_attributes)
@customer = Customer.for_donation(customer_params)
@customer.errors.empty? or return redirect_to(quick_donate_path(:customer => params[:customer],
:fund_code => @account_code, :donation => @amount),
:alert => "Incomplete or invalid donor information: #{@customer.errors.as_html}")
@customer.errors.empty? or return redirect_to(
quick_donate_path(
:customer => params[:customer],
:fund_code => @account_code,
:donation => @amount),
:alert => "Incomplete or invalid donor information: #{@customer.errors.as_html}")
else # we got here via a logged-in customer
@customer = Customer.find params[:customer_id]
end

# At this point, the customer has been persisted, so future redirects just use the customer id.
redirect_route = quick_donate_path(:customer_id => @customer.id, :donation => @amount)
redirect_route = quick_donate_path(:customer_id => @customer.id, :fund_code => @account_code.code, :donation => @amount)
@amount > 0 or return redirect_to(redirect_route, :alert => 'Donation amount must be provided')
# Given valid donation, customer, and charge token, create & place credit card order.

Expand Down
2 changes: 1 addition & 1 deletion app/views/store/donate.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
= form_tag(process_donation_path, {:id => '_stripe_payment_form', :onsubmit => 'return confirmAndSubmit()' }) do
= hidden_field_tag 'referer', 'donate'
= hidden_field_tag 'customer_id', @customer.id
= hidden_field_tag 'fund_code', @account_code.id
= hidden_field_tag 'fund_code', @account_code.code
#billing= render :partial => 'customers/billing_address', :locals => {:customer => @customer}

%fieldset#donation_info
Expand Down
12 changes: 6 additions & 6 deletions spec/controllers/store_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,20 @@
allow(Stripe::Charge).to receive(:create).and_raise(Stripe::StripeError)
end
it 'redirects having created the customer' do
post :process_donation, {:customer => @new_valid_customer, :fund_code => @new_account_code.id, :donation => 5, :credit_card_token => 'dummy'}
post :process_donation, {:customer => @new_valid_customer, :fund_code => @new_account_code, :donation => 5, :credit_card_token => 'dummy'}
created_customer = Customer.find_by!(:email => @new_valid_customer[:email])
expect(response).to redirect_to(quick_donate_path(:donation => 5, :customer_id => created_customer.id))
expect(response).to redirect_to(quick_donate_path(:donation => 5, :customer_id => created_customer, :fund_code => @new_account_code.code))
end
it 'shows error message' do
post :process_donation, {:customer => @new_valid_customer, :donation => 5, :fund_code => @new_account_code.id, :credit_card_token => 'dummy'}
post :process_donation, {:customer => @new_valid_customer, :donation => 5, :fund_code => @new_account_code, :credit_card_token => 'dummy'}
expect(flash[:alert]).to match(/credit card payment error/i)
end
end
context 'with invalid donation amount' do
it 'redirects having created the customer' do
post :process_donation, {:customer => @new_valid_customer, :fund_code => @new_account_code.id, :credit_card_token => 'dummy'}
post :process_donation, {:customer => @new_valid_customer, :fund_code => @new_account_code, :credit_card_token => 'dummy'}
created_customer = Customer.find_by!(:email => @new_valid_customer[:email])
expect(response).to redirect_to(quick_donate_path(:donation => 0, :customer_id => created_customer.id))
expect(response).to redirect_to(quick_donate_path(:donation => 0, :customer_id => created_customer, :fund_code => @new_account_code.code))
end
it 'shows error message' do
post :process_donation, :customer => @new_valid_customer, :fund_code => @new_account_code, :credit_card_token => 'dummy'
Expand All @@ -134,7 +134,7 @@
context 'when new customer not valid as purchaser' do
before(:each) do
@invalid_customer = attributes_for(:customer).except(:city,:state)
@params = {:customer => @invalid_customer, :fund_code => @new_account_code.id, :donation => 5, :credit_card_token => 'dummy'}
@params = {:customer => @invalid_customer, :fund_code => @new_account_code, :donation => 5, :credit_card_token => 'dummy'}
end
it 'does not create new customer' do
expect { post :process_donation, @params }.not_to change { Customer.all.size }
Expand Down

0 comments on commit 9815583

Please sign in to comment.