Skip to content

Commit

Permalink
Merge 6538560 into c6a71af
Browse files Browse the repository at this point in the history
  • Loading branch information
viroulep committed Oct 5, 2019
2 parents c6a71af + 6538560 commit 67bc89e
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 5 deletions.
2 changes: 2 additions & 0 deletions WcaOnRails/app/controllers/registrations_controller.rb
Expand Up @@ -481,6 +481,7 @@ def process_payment_intent
charge.amount,
charge.currency,
charge.id,
current_user.id,
)
stripe_charge.update!(
status: "success",
Expand Down Expand Up @@ -530,6 +531,7 @@ def refund_payment
refund.currency,
refund.id,
payment.id,
current_user.id,
)

flash[:success] = 'Payment was refunded'
Expand Down
8 changes: 8 additions & 0 deletions WcaOnRails/app/helpers/registrations_helper.rb
Expand Up @@ -41,4 +41,12 @@ def registration_date_and_tooltip(competition, registration)
[registration.created_at.to_date, registration.created_at]
end
end

def name_for_payment(registration_payment)
if registration_payment.user
link_to(registration_payment.user.name, edit_user_path(registration_payment.user))
else
"<unknown user>"
end
end
end
7 changes: 5 additions & 2 deletions WcaOnRails/app/models/registration.rb
Expand Up @@ -127,25 +127,28 @@ def show_details?
competition.registration_opened? || !(new_or_deleted?)
end

def record_payment(amount, currency_code, stripe_charge_id)
def record_payment(amount, currency_code, stripe_charge_id, user_id)
registration_payments.create!(
amount_lowest_denomination: amount,
currency_code: currency_code,
stripe_charge_id: stripe_charge_id,
user_id: user_id,
)
end

def record_refund(
amount,
currency_code,
stripe_refund_id,
refunded_registration_payment_id
refunded_registration_payment_id,
user_id
)
registration_payments.create!(
amount_lowest_denomination: amount * -1,
currency_code: currency_code,
stripe_charge_id: stripe_refund_id,
refunded_registration_payment_id: refunded_registration_payment_id,
user_id: user_id,
)
end

Expand Down
1 change: 1 addition & 0 deletions WcaOnRails/app/models/registration_payment.rb
Expand Up @@ -2,6 +2,7 @@

class RegistrationPayment < ApplicationRecord
belongs_to :registration
belongs_to :user

monetize :amount_lowest_denomination,
as: "amount",
Expand Down
7 changes: 5 additions & 2 deletions WcaOnRails/app/views/registrations/edit.html.erb
Expand Up @@ -38,8 +38,11 @@
<h4>Payment History</h4>
<% @registration.registration_payments.each do |payment| %>
<p>
<%= wca_local_time(payment.created_at) %>
<%= format_money payment.amount %>
<b><%= wca_local_time(payment.created_at) %></b>
<br />
Amount: <%= format_money payment.amount %>
<br />
Made by: <%= name_for_payment(payment) %>
<% if payment.amount_available_for_refund > 0 %>
<%= horizontal_simple_form_for :payment, url: registration_payment_refund_path(@registration, payment), html: { id: :form_refund } do |f| %>
<%= f.input :refund_amount, as: :money_amount, currency: payment.amount.currency.iso_code, value: payment.amount_available_for_refund, label: t('registrations.refund_form.labels.refund_amount'), hint: t('registrations.refund_form.hints.refund_amount') %>
Expand Down
@@ -0,0 +1,9 @@
# frozen_string_literal: true

class AddUserToRegistrationPayments < ActiveRecord::Migration[5.2]
def change
# Default for index is 'true', but we don't intend to search/access this table
# by user. Default type is :bigint, whereas our users' id column is :integer.
add_reference :registration_payments, :user, index: false, type: :integer
end
end
4 changes: 3 additions & 1 deletion WcaOnRails/db/structure.sql
Expand Up @@ -1100,6 +1100,7 @@ CREATE TABLE `registration_payments` (
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
`refunded_registration_payment_id` int(11) DEFAULT NULL,
`user_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `index_registration_payments_on_stripe_charge_id` (`stripe_charge_id`),
KEY `idx_reg_payments_on_refunded_registration_payment_id` (`refunded_registration_payment_id`)
Expand Down Expand Up @@ -1603,4 +1604,5 @@ INSERT INTO `schema_migrations` (version) VALUES
('20190818102517'),
('20190825095512'),
('20190826005902'),
('20190916133253');
('20190916133253'),
('20191005203556');
2 changes: 2 additions & 0 deletions WcaOnRails/spec/controllers/registrations_controller_spec.rb
Expand Up @@ -610,6 +610,8 @@
expect(refund.amount).to eq competition.base_entry_fee.cents
expect(flash[:success]).to eq "Payment was refunded"
expect(@payment.reload.amount_available_for_refund).to eq 0
# Check that the website actually records who made the refund
expect(registration.registration_payments.last.user).to eq organizer
end

it 'issues a 50% refund' do
Expand Down
2 changes: 2 additions & 0 deletions WcaOnRails/spec/requests/registrations_spec.rb
Expand Up @@ -497,6 +497,8 @@
expect(charge.metadata.wca_id).to eq user.wca_id
expect(charge.metadata.email).to eq user.email
expect(charge.metadata.competition).to eq competition.name
# Check that the website actually records who made the charge
expect(registration.registration_payments.first.user).to eq user
end

it "processes payment with donation and valid credit card without SCA" do
Expand Down

0 comments on commit 67bc89e

Please sign in to comment.