From 6538560c645db7a5c1982f833ea2a336da606d64 Mon Sep 17 00:00:00 2001 From: Philippe Virouleau Date: Sat, 5 Oct 2019 22:41:39 +0200 Subject: [PATCH] Record user for registration_payments --- WcaOnRails/app/controllers/registrations_controller.rb | 2 ++ WcaOnRails/app/helpers/registrations_helper.rb | 8 ++++++++ WcaOnRails/app/models/registration.rb | 7 +++++-- WcaOnRails/app/models/registration_payment.rb | 1 + WcaOnRails/app/views/registrations/edit.html.erb | 7 +++++-- .../20191005203556_add_user_to_registration_payments.rb | 9 +++++++++ WcaOnRails/db/structure.sql | 4 +++- .../spec/controllers/registrations_controller_spec.rb | 2 ++ WcaOnRails/spec/requests/registrations_spec.rb | 2 ++ 9 files changed, 37 insertions(+), 5 deletions(-) create mode 100644 WcaOnRails/db/migrate/20191005203556_add_user_to_registration_payments.rb diff --git a/WcaOnRails/app/controllers/registrations_controller.rb b/WcaOnRails/app/controllers/registrations_controller.rb index bf8c5f0394..118a791ca7 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 15e130f97c..2f1403c55c 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 a4ddefbd85..f67c426f22 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 7c567e147d..d4813e6492 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 fce85e1fac..ebfbb6ea6b 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 0000000000..1f4ddfb4fb --- /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 a392cf8e0a..a9e8647929 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 eaf6b29c06..bc0dc01f82 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 9be50a84af..e8b9865b3e 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