@@ -6,6 +6,9 @@ class PardotFormHandlerSubmitter
66
77 REQUEST_TIMEOUT_SECONDS = 10
88 OPEN_TIMEOUT_SECONDS = 5
9+ SUCCESS_STATUS_CODES = [ 200 , 302 ] . freeze
10+ SUCCESS_LOCATION_PATTERNS = [ '/success' ] . freeze
11+ ERROR_LOCATION_PATTERNS = [ '/error' ] . freeze
912
1013 def initialize ( endpoint_url :)
1114 @endpoint_url = endpoint_url
@@ -15,14 +18,12 @@ def call(form_payload:)
1518 return missing_configuration_result if endpoint_url . blank?
1619
1720 response = faraday . post ( endpoint_url , form_payload )
18- return Result . new ( success? : true ) if response . success?
19-
20- Result . new (
21- success? : false ,
22- status : :bad_gateway ,
23- error_code : 'subscription_provider_rejected' ,
24- message : 'Subscription provider rejected the request.'
21+ Rails . logger . info (
22+ "[subscriptions#provider] status=#{ response . status } " \
23+ "location=#{ redirect_location ( response ) } " \
24+ "classification=#{ classification_for ( response ) } "
2525 )
26+ classify_response ( response )
2627 rescue Faraday ::Error
2728 # Sentry.capture_exception(e)
2829 Result . new (
@@ -53,5 +54,55 @@ def missing_configuration_result
5354 message : 'Subscription provider endpoint is not configured.'
5455 )
5556 end
57+
58+ def classify_response ( response )
59+ return reject_result unless SUCCESS_STATUS_CODES . include? ( response . status )
60+ return Result . new ( success? : true ) if response . status == 200
61+ return reject_result if redirect_to_error_location? ( response )
62+ return Result . new ( success? : true ) if redirect_to_success_location? ( response )
63+
64+ ambiguous_result
65+ end
66+
67+ def reject_result
68+ Result . new (
69+ success? : false ,
70+ status : :bad_gateway ,
71+ error_code : 'subscription_provider_rejected' ,
72+ message : 'Subscription provider rejected the request.'
73+ )
74+ end
75+
76+ def ambiguous_result
77+ Result . new (
78+ success? : false ,
79+ status : :bad_gateway ,
80+ error_code : 'subscription_provider_ambiguous' ,
81+ message : 'Subscription provider response was ambiguous.'
82+ )
83+ end
84+
85+ def redirect_to_success_location? ( response )
86+ location = redirect_location ( response )
87+ SUCCESS_LOCATION_PATTERNS . any? { |pattern | location . include? ( pattern ) }
88+ end
89+
90+ def redirect_to_error_location? ( response )
91+ location = redirect_location ( response )
92+ ERROR_LOCATION_PATTERNS . any? { |pattern | location . include? ( pattern ) }
93+ end
94+
95+ def redirect_location ( response )
96+ response . headers . fetch ( 'location' , '' ) . to_s . downcase
97+ end
98+
99+ def classification_for ( response )
100+ return 'rejected_status' unless SUCCESS_STATUS_CODES . include? ( response . status )
101+ return 'accepted_200' if response . status == 200
102+ return 'rejected_error_redirect' if redirect_to_error_location? ( response )
103+ return 'accepted_success_redirect' if redirect_to_success_location? ( response )
104+
105+ 'ambiguous_redirect'
106+ end
56107 end
57108end
0 commit comments