diff --git a/WcaOnRails/app/controllers/registrations_controller.rb b/WcaOnRails/app/controllers/registrations_controller.rb index bf8c5f0394c..118a791ca7f 100644 --- a/WcaOnRails/app/controllers/registrations_controller.rb +++ b/WcaOnRails/app/controllers/registrations_controller.rb @@ -481,6 +481,7 @@ def process_payment_intent charge.amount, charge.currency, charge.id, + current_user.id, ) stripe_charge.update!( status: "success", @@ -530,6 +531,7 @@ def refund_payment refund.currency, refund.id, payment.id, + current_user.id, ) flash[:success] = 'Payment was refunded' diff --git a/WcaOnRails/app/helpers/registrations_helper.rb b/WcaOnRails/app/helpers/registrations_helper.rb index 15e130f97cb..2f1403c55cb 100644 --- a/WcaOnRails/app/helpers/registrations_helper.rb +++ b/WcaOnRails/app/helpers/registrations_helper.rb @@ -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 + "" + end + end end diff --git a/WcaOnRails/app/models/registration.rb b/WcaOnRails/app/models/registration.rb index a4ddefbd853..f67c426f22e 100644 --- a/WcaOnRails/app/models/registration.rb +++ b/WcaOnRails/app/models/registration.rb @@ -127,11 +127,12 @@ 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 @@ -139,13 +140,15 @@ 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 diff --git a/WcaOnRails/app/models/registration_payment.rb b/WcaOnRails/app/models/registration_payment.rb index 7c567e147d5..d4813e64923 100644 --- a/WcaOnRails/app/models/registration_payment.rb +++ b/WcaOnRails/app/models/registration_payment.rb @@ -2,6 +2,7 @@ class RegistrationPayment < ApplicationRecord belongs_to :registration + belongs_to :user monetize :amount_lowest_denomination, as: "amount", diff --git a/WcaOnRails/app/views/registrations/edit.html.erb b/WcaOnRails/app/views/registrations/edit.html.erb index fce85e1fac9..ebfbb6ea6bb 100644 --- a/WcaOnRails/app/views/registrations/edit.html.erb +++ b/WcaOnRails/app/views/registrations/edit.html.erb @@ -38,8 +38,11 @@

Payment History

<% @registration.registration_payments.each do |payment| %>

- <%= wca_local_time(payment.created_at) %> - <%= format_money payment.amount %> + <%= wca_local_time(payment.created_at) %> +
+ Amount: <%= format_money payment.amount %> +
+ 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') %> diff --git a/WcaOnRails/db/migrate/20191005203556_add_user_to_registration_payments.rb b/WcaOnRails/db/migrate/20191005203556_add_user_to_registration_payments.rb new file mode 100644 index 00000000000..1f4ddfb4fb3 --- /dev/null +++ b/WcaOnRails/db/migrate/20191005203556_add_user_to_registration_payments.rb @@ -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 diff --git a/WcaOnRails/db/structure.sql b/WcaOnRails/db/structure.sql index a392cf8e0ac..a9e8647929a 100644 --- a/WcaOnRails/db/structure.sql +++ b/WcaOnRails/db/structure.sql @@ -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`) @@ -1603,4 +1604,5 @@ INSERT INTO `schema_migrations` (version) VALUES ('20190818102517'), ('20190825095512'), ('20190826005902'), -('20190916133253'); +('20190916133253'), +('20191005203556'); diff --git a/WcaOnRails/spec/controllers/registrations_controller_spec.rb b/WcaOnRails/spec/controllers/registrations_controller_spec.rb index eaf6b29c06c..bc0dc01f82d 100644 --- a/WcaOnRails/spec/controllers/registrations_controller_spec.rb +++ b/WcaOnRails/spec/controllers/registrations_controller_spec.rb @@ -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 diff --git a/WcaOnRails/spec/requests/registrations_spec.rb b/WcaOnRails/spec/requests/registrations_spec.rb index 9be50a84af9..e8b9865b3e9 100644 --- a/WcaOnRails/spec/requests/registrations_spec.rb +++ b/WcaOnRails/spec/requests/registrations_spec.rb @@ -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