Skip to content

Commit

Permalink
Merge pull request #320 from debtcollective/od/billing-address
Browse files Browse the repository at this point in the history
Add billing address to donation form
  • Loading branch information
orlando authored Sep 17, 2020
2 parents de59b7c + 8025b52 commit 4d126c2
Show file tree
Hide file tree
Showing 17 changed files with 446 additions and 311 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"editor.formatOnSave": true,
"[ruby]": {
"editor.formatOnSave": true
},
"ruby.lint": {
"standard": true
},
"ruby.format": "standard",
"ruby.useLanguageServer": true
}
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ gem 'jbuilder', '~> 2.7'
gem 'redis', '~> 4.0'
gem 'redis-namespace', '1.8'
gem 'ffi', '~> 1.9', '>= 1.9.25'
gem 'valid_email2', '3.4.0'

# Emails
gem 'inky-rb', '1.3.8.0', require: 'inky'
gem 'premailer-rails', '1.11.1'
gem 'valid_email2', '3.4.0'

# front-end libraries
gem 'react_on_rails', '~> 11.3'
gem 'mini_racer', platforms: :ruby
gem 'country_select', '~> 4.0'

# Payments
gem 'stripe', '5.25.0'
Expand Down
13 changes: 13 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ GEM
coderay (1.1.3)
concurrent-ruby (1.1.7)
connection_pool (2.2.3)
countries (3.0.1)
i18n_data (~> 0.10.0)
sixarm_ruby_unaccent (~> 1.1)
unicode_utils (~> 1.4)
country_select (4.0.0)
countries (~> 3.0)
sort_alphabetical (~> 1.0)
crass (1.0.6)
css_parser (1.7.1)
addressable
Expand Down Expand Up @@ -149,6 +156,7 @@ GEM
htmlentities (4.3.4)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
i18n_data (0.10.0)
inky-rb (1.3.8.0)
foundation_emails (~> 2)
nokogiri
Expand Down Expand Up @@ -325,6 +333,7 @@ GEM
docile (~> 1.1)
simplecov-html (~> 0.11)
simplecov-html (0.12.2)
sixarm_ruby_unaccent (1.2.0)
skylight (4.3.1)
skylight-core (= 4.3.1)
skylight-core (4.3.1)
Expand All @@ -343,6 +352,8 @@ GEM
thor (~> 1.0)
tilt (~> 2.0)
yard (~> 0.9, >= 0.9.24)
sort_alphabetical (1.1.0)
unicode_utils (>= 1.2.2)
spring (2.1.0)
spring-watcher-listen (2.0.1)
listen (>= 2.7, < 4.0)
Expand Down Expand Up @@ -374,6 +385,7 @@ GEM
tzinfo (1.2.7)
thread_safe (~> 0.1)
unicode-display_width (1.7.0)
unicode_utils (1.4.0)
valid_email2 (3.4.0)
activemodel (>= 3.2)
mail (~> 2.5)
Expand Down Expand Up @@ -410,6 +422,7 @@ DEPENDENCIES
capybara-screenshot (= 1.0.24)
climate_control (~> 0.2.0)
codecov (= 0.2.11)
country_select (~> 4.0)
database_cleaner-active_record (= 1.8.0)
dotenv-rails (= 2.7.6)
e2mmap
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ $ bundle exec rspec

### User sessions

We use cookie based authentication across subdomains instead of creating sessions between apps. This provides a better experience and fixes out of sync sessions between Discourse and other apps. [Read how to setup a user session](https://github.com/debtcollective/discourse-debtcollective-sso/blob/od/v2/README.md).
We use cookie based authentication across subdomains instead of creating sessions between apps. This provides a better experience and fixes out of sync sessions between Discourse and other apps. [Read how to setup a user session](https://github.com/debtcollective/discourse-debtcollective-sso/blob/master/README.md).

## Developer notes

Expand Down
99 changes: 53 additions & 46 deletions app/controllers/charges_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,72 @@
require "recaptcha"

class ChargesController < ApplicationController
before_action :set_funds, only: [:new, :create]
before_action :set_funds, only: %i[new create]
before_action :set_fund_by_slug, only: :new
before_action :set_fund_by_id, only: :create

def new
end

def create
unless verify_recaptcha
return render "new"
return render "new" unless verify_recaptcha

donation_params =
charge_params.to_h.merge(
{
customer_ip: request.remote_ip,
fund_id: @fund.id
}
)

donation, errors = DonationService.new(donation_params, current_user).execute

respond_to do |format|
if donation.persisted?
# send thank you email
DonationMailer.thank_you_email(donation: donation).deliver_later

message = I18n.t(
"charge.alerts.success",
amount: ActionController::Base.helpers.number_to_currency(donation.amount)
)

format.html do
flash[:success] = message
redirect_to thank_you_path
end
format.json { render json: {status: "succeeded", message: message}, status: :ok }
else
format.html do
error = "Oops! Something went wrong. Please try again"

if errors["base"].any?
error = errors["base"].first
end

flash[:error] = error
render :new
end
format.json { render json: {status: "failed", errors: errors.messages}, status: :unprocessable_entity }
end
end

amount = charge_params[:amount].to_i

if amount.nil? || amount.zero? || amount.negative?
flash[:error] = "You must set a valid amount"
return render :new
end

amount_cents = amount * 100
if amount_cents < 500
flash[:error] = I18n.t("charge.errors.min_amount")

return render :new
end

donation_params = charge_params.to_h.merge({
amount: amount_cents,
customer_ip: request.remote_ip,
fund_id: @fund.id
})

donation, error = if current_user
DonationService.save_donation_with_user(current_user, donation_params)
else
DonationService.save_donation_without_user(donation_params)
end

if error
flash[:error] = error
return render :new
end

if donation.instance_of?(String)
flash[:error] = donation
return render :new
end

# send thank you email
DonationMailer.thank_you_email(donation: donation).deliver_later

flash[:success] = I18n.t("charge.alerts.success", amount: DonationService.displayable_amount(amount_cents))
redirect_to thank_you_path
end

private

def charge_params
params.require(:charge).permit(:name, :email, :phone_number, :amount, :stripe_token, :fund_id)
params.require(:charge).permit(
:name,
:email,
:phone_number,
:amount,
:stripe_token,
:fund_id,
:address_line1,
:address_city,
:address_country_code,
:address_zip
)
end

def set_funds
Expand Down
Loading

0 comments on commit 4d126c2

Please sign in to comment.