Skip to content

Commit

Permalink
Revert "Revert "[RUBY-3387] Remove govpay_status field from Order
Browse files Browse the repository at this point in the history
… model and refactor related methods (#1566)""

This reverts commit 49eb018.
  • Loading branch information
jjromeo committed Sep 30, 2024
1 parent ed36a08 commit 16499c2
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 30 deletions.
13 changes: 8 additions & 5 deletions app/models/waste_carriers_engine/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class Order
field :dateCreated, as: :date_created, type: DateTime
field :worldPayStatus, as: :world_pay_status, type: String
field :govpayId, as: :govpay_id, type: String
field :govpayStatus, as: :govpay_status, type: String
field :dateLastUpdated, as: :date_last_updated, type: DateTime
field :updatedByUser, as: :updated_by_user, type: String
field :description, type: String
Expand All @@ -45,13 +44,19 @@ def self.new_order_for(user_email)
order
end

def govpay_status
return nil unless govpay_id

payment = finance_details.payments.find_by(govpay_id: govpay_id)
payment&.govpay_payment_status
end

def add_bank_transfer_attributes
self.payment_method = "OFFLINE"
end

def add_govpay_attributes
self.payment_method = "ONLINE"
self.govpay_status = "IN_PROGRESS"
end

def generate_id
Expand All @@ -62,9 +67,7 @@ def set_description
self.description = generate_description
end

def update_after_online_payment(status, govpay_id = nil)
self.govpay_status = status
self.govpay_id = govpay_id if govpay_id
def update_after_online_payment
self.date_last_updated = Time.current
save!
end
Expand Down
4 changes: 2 additions & 2 deletions app/services/waste_carriers_engine/govpay_callback_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ def order_by_payment_uuid
def valid_unsuccessful_payment?(validation_method)
return false unless govpay_response_validator(@payment_status).public_send(validation_method)

@order.update_after_online_payment(@payment_status)
@order.update_after_online_payment
true
end

def update_payment_data
@order.update_after_online_payment(Payment::STATUS_SUCCESS)
@order.update_after_online_payment
payment = Payment.new_from_online_payment(@order, user_email)
payment.update_after_online_payment(
govpay_status: Payment::STATUS_SUCCESS,
Expand Down
12 changes: 11 additions & 1 deletion spec/factories/finance_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@
trait :has_pending_govpay_order do
has_required_data

orders { [build(:order, :has_pending_govpay_status)] }
transient do
govpay_id { SecureRandom.hex(22) }
end

orders do
[build(:order, :has_required_data, govpay_id: govpay_id)]
end

payments do
[build(:payment, :govpay, govpay_id: govpay_id, govpay_payment_status: WasteCarriersEngine::Payment::STATUS_CREATED)]
end
end

trait :has_order do
Expand Down
6 changes: 0 additions & 6 deletions spec/factories/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
total_amount { order_items.sum { |item| item[:amount] } }
end

trait :has_pending_govpay_status do
has_required_data

govpay_status { WasteCarriersEngine::Payment::STATUS_CREATED }
end

trait :has_copy_cards_item do
date_created { Time.now }

Expand Down
7 changes: 7 additions & 0 deletions spec/factories/payment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

trait :govpay do
payment_type { "GOVPAY" }
govpay_id { SecureRandom.hex(22) }
end

trait :govpay_pending do
payment_type { "GOVPAY" }
govpay_id { SecureRandom.hex(22) }
govpay_payment_status { WasteCarriersEngine::Payment::STATUS_SUBMITTED }
end

trait :bank_transfer do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ module WasteCarriersEngine
end

context "when there is a pending govpay payment" do
subject { build(:new_registration, :has_pending_govpay_status, workflow_state: "govpay_form") }
let(:finance_details) { build(:finance_details, :has_pending_govpay_order) }

subject { create(:new_registration, :has_pending_govpay_status, finance_details: finance_details, workflow_state: "govpay_form") }

include_examples "has next transition", next_state: "registration_received_pending_govpay_payment_form"
end
Expand Down
44 changes: 38 additions & 6 deletions spec/models/waste_carriers_engine/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,12 @@ module WasteCarriersEngine
let(:finance_details) { transient_registration.prepare_for_payment(:govpay, current_user) }
let(:order) { finance_details.orders.first }

it "copies the govpay status to the order" do
order.update_after_online_payment(Payment::STATUS_CREATED)
expect(order.govpay_status).to eq(Payment::STATUS_CREATED)
end

it "updates the date_last_updated" do
Timecop.freeze(Time.new(2004, 8, 15, 16, 23, 42)) do
# Wipe the date first so we know the value has been added
order.update_attributes(date_last_updated: nil)

order.update_after_online_payment(Payment::STATUS_CREATED)
order.update_after_online_payment
expect(order.date_last_updated).to eq(Time.new(2004, 8, 15, 16, 23, 42))
end
end
Expand All @@ -51,6 +46,43 @@ module WasteCarriersEngine
expect(order.payment_uuid).to eq uuid
end
end

describe "#govpay_status" do
let(:order) { build(:order) }

context "when govpay_id is nil" do
before { order.govpay_id = nil }

it "returns nil" do
expect(order.govpay_status).to be_nil
end
end

context "when govpay_id is present" do
let(:transient_registration) { create(:new_registration, :has_required_data, finance_details: build(:finance_details, :has_pending_govpay_order)) }
let(:govpay_id) { transient_registration.finance_details.payments.first.govpay_id }
let(:order) { transient_registration.finance_details.orders.first }

context "when associated payment exists" do
let(:payment) { transient_registration.finance_details.payments.first }

it "returns the govpay_payment_status of the associated payment" do
expect(order.govpay_status).to eq(payment.govpay_payment_status)

end
end

context "when associated payment does not exist" do
before do
transient_registration.finance_details.orders = [build(:order, :has_required_data, govpay_id: "1234")]
end

it "returns nil" do
expect(order.govpay_status).to be_nil
end
end
end
end
end
end
end
4 changes: 0 additions & 4 deletions spec/models/waste_carriers_engine/payment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,10 +214,6 @@ module WasteCarriersEngine
end
end

it "updates the payment status" do
expect(payment.govpay_payment_status).to eq(Payment::STATUS_CREATED)
end

it "updates the payment date_received" do
expect(payment.date_received).to eq(Date.new(2018, 3, 4))
end
Expand Down
6 changes: 5 additions & 1 deletion spec/requests/waste_carriers_engine/govpay_forms_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,11 @@ module WasteCarriersEngine

context "when the payment uuid is valid" do
before do
order.update!(govpay_status: Payment::STATUS_CREATED)
govpay_id = SecureRandom.hex(22)
order.update!(govpay_id: govpay_id)
payment = build(:payment, amount: order.total_amount, govpay_payment_status: Payment::STATUS_CREATED, govpay_id: govpay_id)
transient_registration.finance_details.payments = [payment]
transient_registration.finance_details.save
end

it "redirects to renewal_received_pending_govpay_payment_form" do
Expand Down
4 changes: 0 additions & 4 deletions spec/support/shared_examples/build_finance_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ module WasteCarriersEngine
it "has the correct payment_method" do
expect(order.payment_method).to eq("ONLINE")
end

it "has the correct govpay_status" do
expect(order.govpay_status).to eq("IN_PROGRESS")
end
end

context "when it is a bank transfer order" do
Expand Down

0 comments on commit 16499c2

Please sign in to comment.