-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor callback service and controller
https://eaflood.atlassian.net/browse/RUBY-1891 https://eaflood.atlassian.net/browse/RUBY-1892 https://eaflood.atlassian.net/browse/RUBY-1893 https://eaflood.atlassian.net/browse/RUBY-1894
- Loading branch information
1 parent
02d0c3e
commit 3538517
Showing
69 changed files
with
2,180 additions
and
94 deletions.
There are no files selected for viewing
95 changes: 95 additions & 0 deletions
95
app/controllers/waste_carriers_engine/govpay_forms_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class GovpayFormsController < ::WasteCarriersEngine::FormsController | ||
include UnsubmittableForm | ||
|
||
def new | ||
super(GovpayForm, "govpay_form") | ||
|
||
payment_info = prepare_for_payment | ||
|
||
if payment_info == :error | ||
flash[:error] = I18n.t(".waste_carriers_engine.govpay_forms.new.setup_error") | ||
go_back | ||
else | ||
redirect_to payment_info[:url] | ||
end | ||
end | ||
|
||
def payment_callback | ||
find_or_initialize_transient_registration(params[:token]) | ||
|
||
if @transient_registration.finance_details.orders.first.govpay_status == "success" | ||
Rails.logger.warn "Attempt to pay for an order with govpay_status already set to success" | ||
respond_to_acceptable_payment(:success) | ||
|
||
else | ||
@transient_registration.with_lock do | ||
payment_status = GovpayCallbackService.new(params[:uuid]).run | ||
|
||
case payment_status | ||
when :success, :pending | ||
respond_to_acceptable_payment(payment_status) | ||
else | ||
respond_to_unsuccessful_payment(payment_status) | ||
end | ||
end | ||
end | ||
rescue ArgumentError | ||
Rails.logger.warn "Govpay payment callback error: invalid payment uuid \"#{params[:uuid]}\"" | ||
Airbrake.notify("Govpay callback error", "Invalid payment uuid \"#{params[:uuid]}\"") | ||
flash[:error] = I18n.t(".waste_carriers_engine.govpay_forms.new.internal_error") | ||
go_back | ||
end | ||
|
||
private | ||
|
||
def prepare_for_payment | ||
@transient_registration.prepare_for_payment(:govpay, current_user) | ||
order = @transient_registration.finance_details.orders.first | ||
govpay_service = GovpayPaymentService.new(@transient_registration, order, current_user) | ||
govpay_service.prepare_for_payment | ||
end | ||
|
||
def respond_to_acceptable_payment(action) | ||
return unless valid_transient_registration? | ||
|
||
if action != :error | ||
log_and_send_govpay_response(true, action) | ||
@transient_registration.next! | ||
redirect_to_correct_form | ||
else | ||
log_and_send_govpay_response(false, action) | ||
flash[:error] = I18n.t(".waste_carriers_engine.govpay_forms.#{action}.invalid_response") | ||
go_back | ||
end | ||
end | ||
|
||
def respond_to_unsuccessful_payment(action) | ||
return unless valid_transient_registration? | ||
|
||
if action != :error | ||
log_and_send_govpay_response(true, action) | ||
flash[:error] = I18n.t(".waste_carriers_engine.govpay_forms.#{action}.message") | ||
else | ||
log_and_send_govpay_response(false, action) | ||
flash[:error] = I18n.t(".waste_carriers_engine.govpay_forms.#{action}.invalid_response") | ||
end | ||
|
||
go_back | ||
end | ||
|
||
def valid_transient_registration? | ||
setup_checks_pass? | ||
end | ||
|
||
def log_and_send_govpay_response(is_valid, action) | ||
valid_text = is_valid ? "Valid" : "Invalid" | ||
title = "#{valid_text} Govpay response: #{action}" | ||
|
||
Rails.logger.debug [title, "Params:", params.to_json].join("\n") | ||
Airbrake.notify(title, error_message: params) unless is_valid && action == :success | ||
end | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
...rs/waste_carriers_engine/registration_received_pending_govpay_payment_forms_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class RegistrationReceivedPendingGovpayPaymentFormsController < ::WasteCarriersEngine::FormsController | ||
include UnsubmittableForm | ||
include CannotGoBackForm | ||
|
||
def new | ||
return unless super( | ||
RegistrationReceivedPendingGovpayPaymentForm, | ||
"registration_received_pending_govpay_payment_form" | ||
) | ||
|
||
begin | ||
@registration = RegistrationCompletionService.run(@transient_registration) | ||
rescue StandardError => e | ||
Airbrake.notify(e, reg_identifier: @transient_registration.reg_identifier) | ||
Rails.logger.error e | ||
end | ||
end | ||
end | ||
end |
12 changes: 12 additions & 0 deletions
12
...rollers/waste_carriers_engine/renewal_received_pending_govpay_payment_forms_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class RenewalReceivedPendingGovpayPaymentFormsController < ::WasteCarriersEngine::FormsController | ||
include CannotGoBackForm | ||
include UnsubmittableForm | ||
|
||
def new | ||
super(RenewalReceivedPendingGovpayPaymentForm, "renewal_received_pending_govpay_payment_form") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class GovpayForm < ::WasteCarriersEngine::BaseForm | ||
def self.can_navigate_flexibly? | ||
false | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
app/forms/waste_carriers_engine/registration_received_pending_govpay_payment_form.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class RegistrationReceivedPendingGovpayPaymentForm < ::WasteCarriersEngine::BaseForm | ||
include CannotSubmit | ||
|
||
def self.can_navigate_flexibly? | ||
false | ||
end | ||
end | ||
end |
11 changes: 11 additions & 0 deletions
11
app/forms/waste_carriers_engine/renewal_received_pending_govpay_payment_form.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module WasteCarriersEngine | ||
class RenewalReceivedPendingGovpayPaymentForm < ::WasteCarriersEngine::BaseForm | ||
include CannotSubmit | ||
|
||
def self.can_navigate_flexibly? | ||
false | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.