Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide payment confirmation in back-office #895

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions app/forms/waste_carriers_engine/payment_summary_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class PaymentSummaryForm < ::WasteCarriersEngine::BaseForm
:card_confirmation_email

validates :temp_payment_method, inclusion: { in: %w[card bank_transfer] }
validates :card_confirmation_email, "defra_ruby/validators/email": true, if: :paying_by_card?
validates :card_confirmation_email, "defra_ruby/validators/email": true, unless: :ignore_card_confirmation_email?

def self.can_navigate_flexibly?
false
Expand All @@ -23,7 +23,8 @@ def initialize(transient_registration)
self.registration_cards = transient_registration.temp_cards || 0
self.registration_card_charge = transient_registration.total_registration_card_charge
self.total_charge = transient_registration.total_to_pay
self.card_confirmation_email = transient_registration.email_to_send_receipt

pre_populate_card_confirmation_email
end

def submit(params)
Expand All @@ -37,20 +38,29 @@ def submit(params)
}

# We only want to save the email address if the user is paying by card
attributes[:receipt_email] = card_confirmation_email if paying_by_card?
# and we are in the front-office
attributes[:receipt_email] = card_confirmation_email unless ignore_card_confirmation_email?

super(attributes)
end

private

def pre_populate_card_confirmation_email
return if WasteCarriersEngine.configuration.host_is_back_office?

self.card_confirmation_email = transient_registration.email_to_send_receipt
end

def assign_params(params)
self.temp_payment_method = params[:temp_payment_method]
self.card_confirmation_email = params[:card_confirmation_email]
end

def paying_by_card?
temp_payment_method == "card"
def ignore_card_confirmation_email?
return true if WasteCarriersEngine.configuration.host_is_back_office?

temp_payment_method != "card"
end
end
end
36 changes: 20 additions & 16 deletions app/views/waste_carriers_engine/payment_summary_forms/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<% content_for :page_scripts do %>
<%= javascript_include_tag "receipt_email" %>
<% unless WasteCarriersEngine.configuration.host_is_back_office? %>
<% content_for :page_scripts do %>
<%= javascript_include_tag "receipt_email" %>
<% end %>
<% end %>

<%= render("waste_carriers_engine/shared/back", back_path: back_payment_summary_forms_path(@payment_summary_form.token)) %>
Expand Down Expand Up @@ -68,21 +70,23 @@
<% end %>
</div>

<div class="panel panel-border-narrow" id="receipt_email_div">
<h2 class="heading-medium"><%= t(".payment_confirmation.subheading") %></h2>
<p>
<%= t(".payment_confirmation.paragraph_1") %>
</p>
<div class="form-group <%= "form-group-error" if @payment_summary_form.errors[:card_confirmation_email].any? %>">
<fieldset id="card_confirmation_email">
<% if @payment_summary_form.errors[:card_confirmation_email].any? %>
<span class="error-message"><%= @payment_summary_form.errors[:card_confirmation_email].join(", ") %></span>
<% end %>
<%= f.label :card_confirmation_email, t(".payment_confirmation.label"), class: "form-label" %>
<%= f.email_field :card_confirmation_email, value: @payment_summary_form.card_confirmation_email, class: "form-control" %>
</fieldset>
<% unless WasteCarriersEngine.configuration.host_is_back_office? %>
<div class="panel panel-border-narrow" id="receipt_email_div">
<h2 class="heading-medium"><%= t(".payment_confirmation.subheading") %></h2>
<p>
<%= t(".payment_confirmation.paragraph_1") %>
</p>
<div class="form-group <%= "form-group-error" if @payment_summary_form.errors[:card_confirmation_email].any? %>">
<fieldset id="card_confirmation_email">
<% if @payment_summary_form.errors[:card_confirmation_email].any? %>
<span class="error-message"><%= @payment_summary_form.errors[:card_confirmation_email].join(", ") %></span>
<% end %>
<%= f.label :card_confirmation_email, t(".payment_confirmation.label"), class: "form-label" %>
<%= f.email_field :card_confirmation_email, value: @payment_summary_form.card_confirmation_email, class: "form-control" %>
</fieldset>
</div>
</div>
</div>
<% end %>

<div class="multiple-choice">
<%= f.radio_button :temp_payment_method, "bank_transfer" %>
Expand Down
123 changes: 78 additions & 45 deletions spec/forms/waste_carriers_engine/payment_summary_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,73 @@
module WasteCarriersEngine
RSpec.describe PaymentSummaryForm, type: :model do
describe "#submit" do
context "when the form is valid" do
let(:payment_summary_form) { build(:payment_summary_form, :has_required_data) }
let(:valid_params) do
let(:payment_summary_form) { build(:payment_summary_form, :has_required_data) }

context "when hosted in the front-office" do
let(:params) do
{
token: payment_summary_form.token,
temp_payment_method: payment_summary_form.temp_payment_method,
card_confirmation_email: "foo@example.com"
temp_payment_method: payment_method,
card_confirmation_email: card_confirmation_email
}
end

it "should submit" do
expect(payment_summary_form.submit(valid_params)).to eq(true)
context "and the form is valid" do
let(:payment_method) { "card" }
let(:card_confirmation_email) { "foo@example.com" }

it "should submit" do
expect(payment_summary_form.submit(params)).to eq(true)
end
end

context "and the form is not valid" do
let(:payment_method) { "foo" }
let(:card_confirmation_email) { "foo@com" }

it "should not submit" do
expect(payment_summary_form.submit(params)).to eq(false)
end
end
end

context "when the form is not valid" do
let(:payment_summary_form) { build(:payment_summary_form, :has_required_data) }
let(:invalid_params) do
context "when hosted in the back-office" do
before(:each) do
allow(WasteCarriersEngine.configuration).to receive(:host_is_back_office?).and_return(true)
end

let(:params) do
{
token: payment_summary_form.token,
temp_payment_method: "foo",
card_confirmation_email: "foo@com"
temp_payment_method: payment_method
}
end

it "should not submit" do
expect(payment_summary_form.submit(invalid_params)).to eq(false)
context "and the form is valid" do
let(:payment_method) { "card" }

it "should submit" do
expect(payment_summary_form.submit(params)).to eq(true)
end
end

context "and the form is not valid" do
let(:payment_method) { "foo" }

it "should not submit" do
expect(payment_summary_form.submit(params)).to eq(false)
end
end
end

end

describe "#valid?" do
context "when a valid transient registration exists" do
before(:each) do
payment_summary_form.transient_registration.temp_payment_method = temp_payment_method
end

context "when hosted in the front-office" do
let(:payment_summary_form) do
build(
:payment_summary_form,
Expand All @@ -47,67 +81,66 @@ module WasteCarriersEngine
)
end

before do
payment_summary_form.transient_registration.temp_payment_method = temp_payment_method
end

context "when a temp_payment_method is bank_transfer" do
let(:temp_payment_method) { "bank_transfer" }
context "and the temp_payment_method is card" do
let(:temp_payment_method) { "card" }
let(:card_confirmation_email) { "hello@example.com" }

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

context "but the card_confirmation_email has been set" do
context "and the receipt email has been set" do
context "to something invalid" do
let(:card_confirmation_email) { "foo@bar" }

it "is still valid" do
expect(payment_summary_form).to be_valid
it "is not valid" do
expect(payment_summary_form).not_to be_valid
end
end

context "to nothing" do
let(:card_confirmation_email) { "" }

it "is still valid" do
expect(payment_summary_form).to be_valid
it "is not valid" do
expect(payment_summary_form).not_to be_valid
end
end
end
end

context "when a temp_payment_method is card" do
let(:temp_payment_method) { "card" }
context "and the temp_payment_method is anything else" do
let(:temp_payment_method) { "I am a payment method, don't you know?" }
let(:card_confirmation_email) { "hello@example.com" }

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

context "and the receipt email has been set" do
context "to something invalid" do
let(:card_confirmation_email) { "foo@bar" }
context "when hosted in the back-office" do
before(:each) do
allow(WasteCarriersEngine.configuration).to receive(:host_is_back_office?).and_return(true)
end

it "is not valid" do
expect(payment_summary_form).not_to be_valid
end
end
let(:payment_summary_form) do
build(
:payment_summary_form,
:has_required_data,
temp_payment_method: temp_payment_method
)
end

context "to nothing" do
let(:card_confirmation_email) { "" }
context "and the temp_payment_method is card" do
let(:temp_payment_method) { "card" }

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

context "when a temp_payment_method is anything else" do
context "and the temp_payment_method is anything else" do
let(:temp_payment_method) { "I am a payment method, don't you know?" }
let(:card_confirmation_email) { "hello@example.com" }

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