Skip to content

Commit

Permalink
Adds solicitation of donations to the league registration process.
Browse files Browse the repository at this point in the history
  • Loading branch information
toomuchpete committed Mar 31, 2024
1 parent 19b185c commit 0b1ef42
Show file tree
Hide file tree
Showing 14 changed files with 193 additions and 63 deletions.
3 changes: 2 additions & 1 deletion app/controllers/leagues_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,8 @@ def league_params
:female_registration_open, :female_registration_close, :male_registration_open, :male_registration_close,
:description, {commissioner_ids: []}, :male_limit, :female_limit,
:max_grank_age, :allow_pairs, :covid_vax_required, :track_spirit_scores, :display_spirit_scores, :self_rank_type, :eos_tourney, :mst_tourney, :eos_champion_id, :mst_champion_id,
{core_options: [:type, :male_limit, :female_limit, :rank_limit, :male_rank_constant, :female_rank_constant]}
{core_options: [:type, :male_limit, :female_limit, :rank_limit, :male_rank_constant, :female_rank_constant]},
:solicit_donations, :donation_earmark
]

if permitted_to? :assign_comps, self
Expand Down
21 changes: 18 additions & 3 deletions app/controllers/payments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ def create
return
end

price = registration.price
price = registration.price
donation_amount = 0

donation = Donation.new(amount: params[:donation_amount], registration: registration, user: registration.user)

if registration.league.solicit_donations? && donation.valid?
donation_amount = donation.amount
end

# COPIED TO league.rb
result = Braintree::Transaction.sale(
amount: price,
amount: price + donation_amount,
payment_method_nonce: params['payment_method_nonce'],
channel: 'leagues.afdc.com',
options: {
submit_for_settlement: true
},
custom_fields: {
donation_amount: donation_amount,
registration_id: registration._id.to_s,
league_id: registration.league._id.to_s,
user_id: registration.user._id.to_s
Expand All @@ -30,7 +38,7 @@ def create
)

if result.success?
PaymentTransaction.create({
pt = PaymentTransaction.create({
transaction_id: result.transaction.id,
payment_method: result.transaction.payment_instrument_type,
amount: result.transaction.amount,
Expand All @@ -39,6 +47,13 @@ def create
user: registration.user,
league: registration.league
})

if donation_amount > 0
donation.payment_transaction = pt
donation.save
log_audit('Donate', league: registration.league, registration: registration)
end

registration.paid = true
registration.activate!

Expand Down
7 changes: 6 additions & 1 deletion app/controllers/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class RegistrationsController < ApplicationController
before_filter :load_registration_from_params, only: [:cancel, :edit, :update, :checkout, :approved, :canceled, :show, :pay, :waitlist_authorize]
before_filter :load_registration_from_params, only: [:cancel, :edit, :update, :checkout, :approved, :canceled, :show, :pay, :donate, :waitlist_authorize]
filter_access_to [:edit, :update, :show, :checkout, :cancel, :pay], attribute_check: true

def create
Expand Down Expand Up @@ -112,6 +112,11 @@ def update
return
end

if @registration.league.comped?(@registration.user) == false && @registration.league.solicit_donations?
redirect_to donate_registration_path(@registration)
return
end

# If we get here, the user has successfully registered and now just needs to pay
redirect_to pay_registration_path(@registration)
end
Expand Down
14 changes: 14 additions & 0 deletions app/models/donation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Donation
include Mongoid::Document
include Mongoid::Timestamps

belongs_to :user
belongs_to :registration

belongs_to :payment_transaction

field :amount, type: Float
field :earmark, type: String

validates :amount, :numericality => { integer_only: false, greater_than: 0 }
end
4 changes: 4 additions & 0 deletions app/models/league.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class League
field :invited_player_ids, type: Array, default: []
field :covid_vax_required, type: Boolean, default: false
embeds_one :core_options, class_name: 'LeagueCoreOptions'
field :solicit_donations, type: Boolean, default: false

field :donation_earmark, type: String, default: nil
field :donation_pitch, type: String, default: nil

after_initialize :build_options_if_nil
after_find :migrate_self_rank_opts
Expand Down
1 change: 1 addition & 0 deletions app/models/payment_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ class PaymentTransaction
belongs_to :user
belongs_to :league
belongs_to :registration
has_one :donation
end
1 change: 1 addition & 0 deletions app/models/registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Registration
belongs_to :g_rank_result
belongs_to :pair, class_name: "User"
has_many :payment_transactions
has_one :donation

before_save :ensure_price
after_save :bust_league_cache
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class User

has_many :g_rank_results, order: :_id.desc
has_many :registrations
has_many :donations
has_many :payment_transactions
has_many :notification_methods
has_and_belongs_to_many :teams, foreign_key: :teams
Expand Down
31 changes: 23 additions & 8 deletions app/views/leagues/_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,7 @@
= f.check_box :allow_pairs
Allow Pairs

.control-group
%label.control-label Vaccination
.controls
%label.checkbox
= f.check_box :covid_vax_required
COVID Vaccine Required

.control-group
.control-group.hide
%label.control-label Spirit
.controls
%label.checkbox
Expand All @@ -153,6 +146,28 @@
= f.check_box :display_spirit_scores
Display Spirit Scores

%h4 Donations at Registration
%p.muted
This determines whether or not donations are permitted with league registration.
In order to advertise a specific use for donations from this leauge (e.g. YCC fundraising),
place a descriptive identifier in the Earmark field (this is for internal/accounting use).
Then, describe what the donations will be used for in the large text area. This text will
be visible to users who click to see more information about where their donation goes. If
the pitch field is left blank, a default message will be added about the donation going to
the AFDC's general budget.

.control-group
.controls
%label.checkbox
= f.check_box :solicit_donations
Enable Donations
%label.control-label Earmark
.controls
=f.text_field :donation_earmark
%label.control-label Pitch
.controls
=f.text_area :donation_pitch, class: "span4", rows: 6

%h4 Gender-based Registration
%p.muted
These dates will be used <em>in addition</em> to the general registrationdates
Expand Down
42 changes: 42 additions & 0 deletions app/views/registrations/donate.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<div class="hero-unit">
<h1>Boost Your Impact</h1>
<h2>Add a donation with your registration and become a game-changer!</h2>
<p>
By stepping onto the field, you're already supporting Ultimate in Atlanta. But here's a
chance to have an even greater impact. When you add a donation with your registration,
you're not just a player - you're a pillar of our community. Your <b>tax-deductible donation</b>
expands the reach of our programs, empowering youth development, fostering diversity and inclusivity,
and amplifying our mission to teach and celebrate the games of Ultimate and Goaltimate.
It's like adding a strategic play to your game; your contribution helps us build a stronger,
more vibrant community for everyone who steps onto the field.
</p>
<p>Ready to be more than a player? Be a game-changer with us!</p>
<p style="margin-top: 64px" class="text-center">
<%=link_to "$5", pay_registration_path(@registration, donation_amount: 5), class: "btn btn-primary btn-large"%>
<%=link_to "$10", pay_registration_path(@registration, donation_amount: 10), class: "btn btn-primary btn-large"%>
<%=link_to "$25", pay_registration_path(@registration, donation_amount: 25), class: "btn btn-primary btn-large"%>
<%=link_to "$50", pay_registration_path(@registration, donation_amount: 50), class: "btn btn-primary btn-large"%>
</p>
<div>
<%= form_tag(pay_registration_path(@registration), method: "GET", class: "form-inline") do %>
<div style=" max-width: fit-content; margin-left: auto; margin-right: auto;">
<div class="input-prepend">
<span class="add-on">$</span>
<%=text_field_tag :donation_amount, "", class: "input-medium", placeholder: "amount" %>
</div>
<%=submit_tag "Pay", class: "btn btn-primary"%>
</div>
<% end %>
</div>
<p class="text-center">
<%=link_to "No thanks, Maybe Next Time", pay_registration_path(@registration)%>
</p>
</div>

<% if @registration.league.donation_pitch.present? %>
<div class="well">
<h2>Heads Up! <small>Donations made via this league may be reserved for a specific purpose</small></h2>
<h5>A message from your commissioner:</h5>
<%= @registration.league.donation_pitch.html_safe %>
</div>
<% end %>
79 changes: 79 additions & 0 deletions app/views/registrations/pay.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<% content_for :title, @registration.league.name %>
<%= render :partial => '/pageheader', :locals => {subtitle: 'Join the League', breadcrumbs: {'Leagues' => leagues_path, @registration.league.name => league_path(@registration.league), "Registration for #{@registration.user.name}" => nil}}%>
<%
donation = Donation.new(amount: params[:donation_amount], user: @registration.user, registration: @registration)
if @registration.league.solicit_donations? == false || donation.valid? == false
donation = nil
end
%>

<div class="row">
<div class="span6 offset3">
<div class="alert alert-success">
<strong style="color: #222">
<i class="icon-lock"></i>
Relax! Your connection to our payment processor is secure.
</strong>
<p style="font-size: 80%">Your payment details will be submitted directly from your browser to our payment processor over a secure connection; the AFDC will never see your credit card information.</p>
</div>
<%= form_tag payments_path, id: "payment_form" do %>
<div class="row" style="padding: 15px 0">
<div class="span4">
<b>League Registration</b>
<small><%= @registration.league.name %></small>
</div>
<div class="span2 text-right"><%= number_to_currency @registration.price %>
</div>
</div>
<% if donation %>
<div class="row" style="margin-bottom: 20px;">
<div class="span4"><b>Tax-deductible Donation</b></div>
<div class="span2 text-right"><%= number_to_currency donation.amount %></div>
</div>
<%= hidden_field_tag 'donation_amount', donation.amount %>
<% end %>
<div class="row"><div class="span6">
<hr />
<h1 class="text-right"><small>Total:</small> <%= number_to_currency(@registration.price + donation&.amount.to_f) %></h1>
</div></div>
<div id="braintree_dropin"></div>
<%= hidden_field_tag 'payment_method_nonce', '' %>
<%= hidden_field_tag 'registration_id', @registration._id %>
<%= button_tag "Pay now to join the league!", type: :submit, class: "btn btn-block btn-success btn-large", id: "submit_button" do %>
<i class="icon-lock"></i>
Pay now to join the league!
<% end %>
<% end %>
</div>
</div>

<% content_for :page_scripts do %>
<script src="https://js.braintreegateway.com/web/dropin/1.40.2/js/dropin.min.js"></script>
<script>
$(function(){
form = $("#payment_form");
braintree.dropin.create({
container: document.getElementById('braintree_dropin'),
authorization: "<%=current_user.braintree_token%>",
}, (error, dropinInstance) => {
if (error) {
console.error(error);
return;
}

form.on('submit', function (e) {
e.preventDefault();
dropinInstance.requestPaymentMethod(function (requestPaymentMethodErr, payload) {
if (requestPaymentMethodErr) {
console.error(requestPaymentMethodErr);
return;
}

$("#payment_method_nonce").val(payload["nonce"]);
form.off().submit();
});
});
});
});
</script>
<% end %>
49 changes: 0 additions & 49 deletions app/views/registrations/pay.html.haml

This file was deleted.

2 changes: 1 addition & 1 deletion config/authorization_rules.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
if_attribute sender_id: is { user._id }
end

has_permission_on :registrations, to: [:checkout, :show, :pay, :waitlist_authorize, :edit, :update, :cancel] do
has_permission_on :registrations, to: [:checkout, :show, :pay, :donate, :waitlist_authorize, :edit, :update, :cancel] do
if_attribute user_id: is { user._id }
end

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
resources :registrations do
member do
put 'cancel'
get 'donate'
get 'pay'
get 'waitlist_authorize'
end
Expand Down

0 comments on commit 0b1ef42

Please sign in to comment.