Skip to content

Commit

Permalink
Set up form values and persist them
Browse files Browse the repository at this point in the history
A first pass at setting up the content for the page and saving the user's answer about how they'd like to pay.

The copy cards fee depends on a different PR being merged in (#139). For now we've got a temporary holding value.
  • Loading branch information
irisfaraway committed Apr 26, 2018
1 parent 9d35fd0 commit 6b690d2
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 24 deletions.
31 changes: 26 additions & 5 deletions app/forms/payment_summary_form.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,38 @@
class PaymentSummaryForm < BaseForm
# TODO: Define accessible attributes, eg attr_accessor :field
attr_accessor :temp_payment_method, :type_change, :registration_cards, :registration_card_charge, :total_charge

def initialize(transient_registration)
super
# TODO: Define params to get from transient_registration, eg self.field = @transient_registration.field
self.temp_payment_method = @transient_registration.temp_payment_method

self.type_change = @transient_registration.registration_type_changed?
# TODO: Uncomment this once https://github.com/DEFRA/waste-carriers-renewals/pull/139/ is merged in
# self.registration_cards = @transient_registration.temp_cards || 0
self.registration_cards = 2 # Temp value for testing
self.registration_card_charge = determine_registration_card_charge
self.total_charge = determine_total_charge
end

def submit(params)
# Assign the params for validation and pass them to the BaseForm method for updating
# TODO: Define allowed params, eg self.field = params[:field]
# TODO: Include attributes to update in the attributes hash, eg { field: field }
attributes = {}
self.temp_payment_method = params[:temp_payment_method]
attributes = { temp_payment_method: temp_payment_method }

super(attributes, params[:reg_identifier])
end

validates :temp_payment_method, inclusion: { in: %w[card bank_transfer] }

private

def determine_total_charge
charges = [Rails.configuration.renewal_charge]
charges << Rails.configuration.type_change_charge if type_change
charges << registration_card_charge
charges.sum
end

def determine_registration_card_charge
registration_cards * 5
end
end
3 changes: 3 additions & 0 deletions app/models/transient_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ class TransientRegistration
field :temp_company_postcode, type: String
field :temp_contact_postcode, type: String
field :temp_os_places_error, type: Boolean
field :temp_payment_method, type: String
field :temp_tier_check, type: Boolean

# Check if the user has changed the registration type, as this incurs an additional 40GBP charge
def registration_type_changed?
# Don't compare registration types if the new one hasn't been set
return false unless registration_type
original_registration_type = Registration.where(reg_identifier: reg_identifier).first.registration_type
original_registration_type != registration_type
end
Expand Down
62 changes: 62 additions & 0 deletions app/views/payment_summary_forms/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<%= render("shared/back", back_path: back_payment_summary_forms_path(@payment_summary_form.reg_identifier)) %>

<div class="text">
<% if @payment_summary_form.errors.any? %>

<h1 class="heading-large"><%= t(".error_heading") %></h1>
Expand All @@ -15,10 +16,71 @@

<h1 class="heading-large"><%= t(".heading") %></h1>

<div class="form-group">
<table>
<tbody>
<tr>
<td><%= t(".renewal_fee") %></td>
<td>£<%= Rails.configuration.renewal_charge %></td>
</tr>
<% if @payment_summary_form.type_change %>
<tr>
<td><%= t(".cbd_fee") %></td>
<td>£<%= Rails.configuration.type_change_charge %></td>
</tr>
<% end %>
<% if @payment_summary_form.registration_cards > 0 %>
<tr>
<td><%= t(".cards_fee") %></td>
<td>£<%= @payment_summary_form.registration_card_charge %></td>
</tr>
<% end %>
<tr>
<td><%= t(".total_fee") %></td>
<td>£<%= @payment_summary_form.total_charge %></td>
</tr>
</tbody>
</table>
</div>

<p class="font-xsmall"><%= t(".vat_statement") %></p>

<h2 class="heading-medium"><%= t(".make_payment_subheading") %></h2>

<% if @payment_summary_form.errors[:temp_payment_method].any? %>
<div class="form-group form-group-error">
<% else %>
<div class="form-group">
<% end %>
<fieldset id="temp_payment_method">

<% if @payment_summary_form.errors[:temp_payment_method].any? %>
<span class="error-message"><%= @payment_summary_form.errors[:temp_payment_method].join(", ") %></span>
<% end %>

<div class="multiple-choice">
<%= f.radio_button :temp_payment_method, "card" %>
<%= f.label :temp_payment_method, value: "card", class: "form-label" do %>
<%= t(".options.card") %>
<span class='form-hint'><%= t(".hint_pay_by_card") %></span>
<% end %>
</div>

<div class="multiple-choice">
<%= f.radio_button :temp_payment_method, "bank_transfer" %>
<%= f.label :temp_payment_method, value: "bank_transfer", class: "form-label" do %>
<%= t(".options.bank_transfer") %>
<span class='form-hint'><%= t(".hint_pay_by_bank_transfer") %></span>
<% end %>
</div>
</fieldset>
</div>

<%= f.hidden_field :reg_identifier, value: @payment_summary_form.reg_identifier %>
<div class="form-group">
<%= f.submit t(".next_button"), class: "button" %>
</div>
<% end %>
<% end %>
</div>
15 changes: 14 additions & 1 deletion config/locales/forms/payment_summary_forms/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ en:
new:
title: Payment summary
heading: Payment summary
renewal_fee: Renewal of registration
cbd_fee: Additional charge for changing registration type
cards_fee: Registration cards
total_fee: Total charge
vat_statement: All charges are outside the scope of VAT.
make_payment_subheading: Make a payment
options:
card: Pay by credit card or debit card
bank_transfer: Pay by bank transfer
hint_pay_by_card: You will be transferred to the secure Worldpay site for you to enter your credit or debit card details. We accept Mastercard, Maestro and Visa.
hint_pay_by_bank_transfer: Paying by bank transfer requires 2 steps. Both must be done to complete your registration.
error_heading: Something is wrong
next_button: Continue
next_button: Proceed to payment
activemodel:
errors:
models:
payment_summary_form:
attributes:
temp_payment_method:
inclusion: "You must select a payment method"
reg_identifier:
invalid_format: "The registration ID is not in a valid format"
no_registration: "There is no registration matching this ID"
Expand Down
2 changes: 2 additions & 0 deletions spec/factories/forms/payment_summary_form.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
FactoryBot.define do
factory :payment_summary_form do
trait :has_required_data do
temp_payment_method "card"

initialize_with { new(create(:transient_registration, :has_required_data, workflow_state: "payment_summary_form")) }
end
end
Expand Down
46 changes: 34 additions & 12 deletions spec/forms/payment_summary_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@
describe "#submit" do
context "when the form is valid" do
let(:payment_summary_form) { build(:payment_summary_form, :has_required_data) }
let(:valid_params) { { reg_identifier: payment_summary_form.reg_identifier } }
let(:valid_params) do
{
reg_identifier: payment_summary_form.reg_identifier,
temp_payment_method: payment_summary_form.temp_payment_method
}
end

it "should submit" do
expect(payment_summary_form.submit(valid_params)).to eq(true)
Expand All @@ -21,27 +26,34 @@
end
end

describe "#reg_identifier" do
context "when a valid transient registration exists" do
let(:transient_registration) do
create(:transient_registration,
:has_required_data,
workflow_state: "payment_summary_form")
context "when a valid transient registration exists" do
let(:payment_summary_form) { build(:payment_summary_form, :has_required_data) }
describe "#reg_identifier" do
context "when a reg_identifier meets the requirements" do
it "is valid" do
expect(payment_summary_form).to be_valid
end
end
# Don't use FactoryBot for this as we need to make sure it initializes with a specific object
let(:payment_summary_form) { PaymentSummaryForm.new(transient_registration) }

context "when a reg_identifier meets the requirements" do
context "when a reg_identifier is blank" do
before(:each) do
payment_summary_form.reg_identifier = transient_registration.reg_identifier
payment_summary_form.reg_identifier = ""
end

it "is not valid" do
expect(payment_summary_form).to_not be_valid
end
end
end

describe "#temp_payment_method" do
context "when a temp_payment_method meets the requirements" do
it "is valid" do
expect(payment_summary_form).to be_valid
end
end

context "when a reg_identifier is blank" do
context "when a temp_payment_method is blank" do
before(:each) do
payment_summary_form.reg_identifier = ""
end
Expand All @@ -50,6 +62,16 @@
expect(payment_summary_form).to_not be_valid
end
end

context "when a temp_payment_method not an allowed string" do
before(:each) do
payment_summary_form.reg_identifier = "foo"
end

it "is not valid" do
expect(payment_summary_form).to_not be_valid
end
end
end
end

Expand Down
17 changes: 11 additions & 6 deletions spec/requests/payment_summary_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,14 @@
context "when valid params are submitted" do
let(:valid_params) {
{
reg_identifier: transient_registration[:reg_identifier]
reg_identifier: transient_registration[:reg_identifier],
temp_payment_method: "card"
}
}

it "updates the transient registration" do
# TODO: Add test once data is submitted through the form
post payment_summary_forms_path, payment_summary_form: valid_params
expect(transient_registration.reload[:temp_payment_method]).to eq(valid_params[:temp_payment_method])
end

it "returns a 302 response" do
Expand All @@ -78,7 +80,8 @@
context "when invalid params are submitted" do
let(:invalid_params) {
{
reg_identifier: "foo"
reg_identifier: "foo",
temp_payment_method: "foo"
}
}

Expand All @@ -89,7 +92,7 @@

it "does not update the transient registration" do
post payment_summary_forms_path, payment_summary_form: invalid_params
expect(transient_registration.reload[:reg_identifier]).to_not eq(invalid_params[:reg_identifier])
expect(transient_registration.reload[:temp_payment_method]).to_not eq(invalid_params[:temp_payment_method])
end
end
end
Expand All @@ -104,12 +107,14 @@

let(:valid_params) {
{
reg_identifier: transient_registration[:reg_identifier]
reg_identifier: transient_registration[:reg_identifier],
temp_payment_method: "card"
}
}

it "does not update the transient registration" do
# TODO: Add test once data is submitted through the form
post payment_summary_forms_path, payment_summary_form: valid_params
expect(transient_registration.reload[:temp_payment_method]).to_not eq(valid_params[:temp_payment_method])
end

it "returns a 302 response" do
Expand Down

0 comments on commit 6b690d2

Please sign in to comment.