defra_ruby_mocks 2.3.2 → 2.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +32 -75
  3. data/app/controllers/defra_ruby_mocks/govpay_controller.rb +28 -3
  4. data/app/jobs/defra_ruby_mocks/application_job.rb +6 -0
  5. data/app/jobs/defra_ruby_mocks/govpay_payment_callback_job.rb +17 -0
  6. data/app/services/defra_ruby_mocks/govpay_create_payment_service.rb +41 -13
  7. data/app/services/defra_ruby_mocks/govpay_get_payment_service.rb +74 -16
  8. data/app/services/defra_ruby_mocks/govpay_refund_details_service.rb +43 -0
  9. data/app/services/defra_ruby_mocks/govpay_request_refund_service.rb +27 -0
  10. data/config/routes.rb +10 -10
  11. data/lib/defra_ruby_mocks/configuration.rb +1 -1
  12. data/lib/defra_ruby_mocks/engine.rb +0 -1
  13. data/lib/defra_ruby_mocks/version.rb +1 -1
  14. data/spec/dummy/log/test.log +1279 -972
  15. data/spec/examples.txt +68 -121
  16. data/spec/fixtures/files/govpay/create_payment_created_response.json +24 -0
  17. data/spec/fixtures/files/govpay/create_payment_error_response.json +5 -0
  18. data/spec/fixtures/files/govpay/get_payment_response_cancelled.json +61 -0
  19. data/spec/fixtures/files/govpay/get_payment_response_created.json +73 -0
  20. data/spec/fixtures/files/govpay/get_payment_response_error.json +5 -0
  21. data/spec/fixtures/files/govpay/get_payment_response_failure.json +9 -0
  22. data/spec/fixtures/files/govpay/get_payment_response_not_found.json +4 -0
  23. data/spec/fixtures/files/govpay/get_payment_response_submitted.json +75 -0
  24. data/spec/fixtures/files/govpay/get_payment_response_success.json +62 -0
  25. data/spec/requests/govpay_spec.rb +64 -1
  26. data/spec/services/govpay_create_payment_service_spec.rb +44 -0
  27. data/spec/services/govpay_get_payment_service_spec.rb +30 -0
  28. data/spec/services/govpay_refund_details_service_spec.rb +58 -0
  29. data/spec/services/govpay_request_refund_service_spec.rb +31 -0
  30. metadata +32 -48
  31. data/app/controllers/defra_ruby_mocks/worldpay_controller.rb +0 -57
  32. data/app/services/defra_ruby_mocks/worldpay_payment_service.rb +0 -47
  33. data/app/services/defra_ruby_mocks/worldpay_refund_service.rb +0 -37
  34. data/app/services/defra_ruby_mocks/worldpay_request_handler_service.rb +0 -40
  35. data/app/services/defra_ruby_mocks/worldpay_resource_service.rb +0 -55
  36. data/app/services/defra_ruby_mocks/worldpay_response_service.rb +0 -119
  37. data/app/views/defra_ruby_mocks/worldpay/payment_request.xml.erb +0 -4
  38. data/app/views/defra_ruby_mocks/worldpay/refund_request.xml.erb +0 -4
  39. data/app/views/defra_ruby_mocks/worldpay/stuck.html.erb +0 -37
  40. data/lib/defra_ruby_mocks/unrecognised_worldpay_request_error.rb +0 -5
  41. data/spec/fixtures/payment_request_invalid.xml +0 -6
  42. data/spec/fixtures/payment_request_valid.xml +0 -30
  43. data/spec/fixtures/refund_request_invalid.xml +0 -6
  44. data/spec/fixtures/refund_request_valid.xml +0 -11
  45. data/spec/fixtures/unrecognised_request.xml +0 -6
  46. data/spec/requests/worldpay_spec.rb +0 -163
  47. data/spec/services/worldpay_payment_service_spec.rb +0 -95
  48. data/spec/services/worldpay_refund_service_spec.rb +0 -68
  49. data/spec/services/worldpay_request_handler_service_spec.rb +0 -79
  50. data/spec/services/worldpay_resource_service_spec.rb +0 -120
  51. data/spec/services/worldpay_response_service_spec.rb +0 -280
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+
7
+ RSpec.describe GovpayCreatePaymentService do
8
+
9
+ before(:each) do
10
+ Helpers::Configuration.prep_for_tests
11
+ DefraRubyMocks.configure do |config|
12
+ config.govpay_domain = "http://localhost:3000/defra_ruby_mocks"
13
+ end
14
+ end
15
+
16
+ let(:amount) { Faker::Number.number(digits: 4) }
17
+ let(:description) { Faker::Lorem.sentence }
18
+ let(:return_url) { Faker::Internet.url }
19
+
20
+ describe ".run" do
21
+
22
+ subject { described_class.run(amount: amount, description: description, return_url: return_url).deep_symbolize_keys }
23
+
24
+ context "for a valid payment request" do
25
+
26
+ it "returns a payment response with the order amount" do
27
+ expect(subject[:amount]).to eq(amount)
28
+ end
29
+
30
+ it "returns a payment response with the description" do
31
+ expect(subject[:description]).to eq(description)
32
+ end
33
+
34
+ it "returns a payment response with the return_url" do
35
+ expect(subject[:_links][:next_url][:href]).to eq(return_url)
36
+ end
37
+
38
+ it "returns the expected status" do
39
+ expect(subject[:state][:status]).to eq("created")
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+
7
+ RSpec.describe GovpayGetPaymentService do
8
+ before { Helpers::Configuration.prep_for_tests }
9
+
10
+ let(:order_value) { Faker::Number.number(digits: 4) }
11
+ let(:payment_id) { SecureRandom.hex(26) }
12
+
13
+ # Note that the service currently supports only success responses.
14
+ describe ".run" do
15
+
16
+ subject { described_class.run(payment_id: payment_id, amount: order_value).deep_symbolize_keys }
17
+
18
+ context "when the payment is successful" do
19
+
20
+ it "returns a payment with the order amount" do
21
+ expect(subject[:amount]).to eq(order_value)
22
+ end
23
+
24
+ it "returns the expected status" do
25
+ expect(subject[:state][:status]).to eq("success")
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+
7
+ RSpec.describe GovpayRefundDetailsService do
8
+ let(:payment_id) { SecureRandom.hex(26) }
9
+ let(:refund_id) { SecureRandom.hex(22) }
10
+
11
+ before { Helpers::Configuration.prep_for_tests }
12
+
13
+ # Note that the service currently supports only success responses.
14
+ describe ".run" do
15
+ let(:create_request_time) { Time.zone.now }
16
+ let(:submitted_success_lag) { "10" }
17
+
18
+ subject { described_class.run(payment_id: payment_id, refund_id: refund_id).deep_symbolize_keys }
19
+
20
+ # the service shoud return "submitted" for up to GOVPAY_REFUND_SUBMITTED_SUCCESS_LAG seconds, "success" thereafter
21
+ before do
22
+ Timecop.freeze(create_request_time)
23
+ allow(ENV).to receive(:fetch).with("GOVPAY_REFUND_SUBMITTED_SUCCESS_LAG", any_args).and_return(submitted_success_lag)
24
+ FileUtils.rm_rf("#{Dir.tmpdir}/govpay_request_refund_service_last_run_time")
25
+ end
26
+
27
+ context "when no refund timestamp file exists" do
28
+ it { expect(subject[:status]).to eq "success" }
29
+ end
30
+
31
+ context "when a refund has been requested" do
32
+ before do
33
+ GovpayRequestRefundService.run(payment_id: payment_id, amount: 100, refund_amount_available: 100).deep_symbolize_keys
34
+ end
35
+
36
+ context "when less than 10 seconds has elapsed since the last create request" do
37
+ before { Timecop.freeze(create_request_time + (submitted_success_lag.to_i - 8).seconds) }
38
+
39
+ it { expect(subject[:status]).to eq "submitted" }
40
+ end
41
+
42
+ context "when 10 seconds has elapsed since the last create request" do
43
+ before { Timecop.freeze(create_request_time + (submitted_success_lag.to_i + 8).seconds) }
44
+
45
+ it { expect(subject[:status]).to eq "success" }
46
+ end
47
+
48
+ context "when GOVPAY_REFUND_SUBMITTED_SUCCESS_LAG is not set" do
49
+ let(:submitted_success_lag) { nil }
50
+
51
+ before { Timecop.freeze(create_request_time + 1.hour) }
52
+
53
+ it { expect(subject[:status]).to eq "success" }
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails_helper"
4
+
5
+ module DefraRubyMocks
6
+
7
+ RSpec.describe GovpayRequestRefundService do
8
+ let(:payment_id) { SecureRandom.hex(26) }
9
+ let(:amount) { 2000 }
10
+ let(:refund_amount_available) { amount }
11
+
12
+ before { Helpers::Configuration.prep_for_tests }
13
+
14
+ # Note that the service currently supports only "submitted" responses.
15
+ describe ".run" do
16
+
17
+ subject { described_class.run(payment_id: payment_id, amount: amount, refund_amount_available: refund_amount_available).deep_symbolize_keys }
18
+
19
+ context "when the refund has been successfully submitted" do
20
+
21
+ it "returns a response with the expected status" do
22
+ expect(subject[:status]).to eq("submitted")
23
+ end
24
+
25
+ it "returns a response with the expected amount" do
26
+ expect(subject[:amount]).to eq(amount)
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: defra_ruby_mocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.3.2
4
+ version: 2.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-06-23 00:00:00.000000000 Z
11
+ date: 2023-05-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -38,20 +38,6 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.7.2
41
- - !ruby/object:Gem::Dependency
42
- name: nokogiri
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
41
  - !ruby/object:Gem::Dependency
56
42
  name: github_changelog_generator
57
43
  requirement: !ruby/object:Gem::Requirement
@@ -150,29 +136,23 @@ files:
150
136
  - app/controllers/defra_ruby_mocks/application_controller.rb
151
137
  - app/controllers/defra_ruby_mocks/company_controller.rb
152
138
  - app/controllers/defra_ruby_mocks/govpay_controller.rb
153
- - app/controllers/defra_ruby_mocks/worldpay_controller.rb
139
+ - app/jobs/defra_ruby_mocks/application_job.rb
140
+ - app/jobs/defra_ruby_mocks/govpay_payment_callback_job.rb
154
141
  - app/services/defra_ruby_mocks/base_service.rb
155
142
  - app/services/defra_ruby_mocks/companies_house_service.rb
156
143
  - app/services/defra_ruby_mocks/govpay_create_payment_service.rb
157
144
  - app/services/defra_ruby_mocks/govpay_get_payment_service.rb
158
- - app/services/defra_ruby_mocks/worldpay_payment_service.rb
159
- - app/services/defra_ruby_mocks/worldpay_refund_service.rb
160
- - app/services/defra_ruby_mocks/worldpay_request_handler_service.rb
161
- - app/services/defra_ruby_mocks/worldpay_resource_service.rb
162
- - app/services/defra_ruby_mocks/worldpay_response_service.rb
145
+ - app/services/defra_ruby_mocks/govpay_refund_details_service.rb
146
+ - app/services/defra_ruby_mocks/govpay_request_refund_service.rb
163
147
  - app/views/defra_ruby_mocks/company/not_found.json.erb
164
148
  - app/views/defra_ruby_mocks/company/officers.json.erb
165
149
  - app/views/defra_ruby_mocks/company/show.json.erb
166
- - app/views/defra_ruby_mocks/worldpay/payment_request.xml.erb
167
- - app/views/defra_ruby_mocks/worldpay/refund_request.xml.erb
168
- - app/views/defra_ruby_mocks/worldpay/stuck.html.erb
169
150
  - config/routes.rb
170
151
  - lib/defra_ruby_mocks.rb
171
152
  - lib/defra_ruby_mocks/configuration.rb
172
153
  - lib/defra_ruby_mocks/engine.rb
173
154
  - lib/defra_ruby_mocks/invalid_config_error.rb
174
155
  - lib/defra_ruby_mocks/missing_resource_error.rb
175
- - lib/defra_ruby_mocks/unrecognised_worldpay_request_error.rb
176
156
  - lib/defra_ruby_mocks/version.rb
177
157
  - lib/tasks/changelog.rake
178
158
  - lib/tasks/defra_ruby_mocks_tasks.rake
@@ -213,23 +193,25 @@ files:
213
193
  - spec/dummy/public/500.html
214
194
  - spec/dummy/public/favicon.ico
215
195
  - spec/examples.txt
216
- - spec/fixtures/payment_request_invalid.xml
217
- - spec/fixtures/payment_request_valid.xml
218
- - spec/fixtures/refund_request_invalid.xml
219
- - spec/fixtures/refund_request_valid.xml
220
- - spec/fixtures/unrecognised_request.xml
196
+ - spec/fixtures/files/govpay/create_payment_created_response.json
197
+ - spec/fixtures/files/govpay/create_payment_error_response.json
198
+ - spec/fixtures/files/govpay/get_payment_response_cancelled.json
199
+ - spec/fixtures/files/govpay/get_payment_response_created.json
200
+ - spec/fixtures/files/govpay/get_payment_response_error.json
201
+ - spec/fixtures/files/govpay/get_payment_response_failure.json
202
+ - spec/fixtures/files/govpay/get_payment_response_not_found.json
203
+ - spec/fixtures/files/govpay/get_payment_response_submitted.json
204
+ - spec/fixtures/files/govpay/get_payment_response_success.json
221
205
  - spec/lib/configuration_spec.rb
222
206
  - spec/rails_helper.rb
223
207
  - spec/requests/company_spec.rb
224
208
  - spec/requests/govpay_spec.rb
225
209
  - spec/requests/officers_spec.rb
226
- - spec/requests/worldpay_spec.rb
227
210
  - spec/services/companies_house_service_spec.rb
228
- - spec/services/worldpay_payment_service_spec.rb
229
- - spec/services/worldpay_refund_service_spec.rb
230
- - spec/services/worldpay_request_handler_service_spec.rb
231
- - spec/services/worldpay_resource_service_spec.rb
232
- - spec/services/worldpay_response_service_spec.rb
211
+ - spec/services/govpay_create_payment_service_spec.rb
212
+ - spec/services/govpay_get_payment_service_spec.rb
213
+ - spec/services/govpay_refund_details_service_spec.rb
214
+ - spec/services/govpay_request_refund_service_spec.rb
233
215
  - spec/spec_helper.rb
234
216
  - spec/support/helpers/configuration.rb
235
217
  - spec/support/helpers/helpers.rb
@@ -301,7 +283,6 @@ test_files:
301
283
  - spec/defra_ruby_mocks_spec.rb
302
284
  - spec/requests/govpay_spec.rb
303
285
  - spec/requests/company_spec.rb
304
- - spec/requests/worldpay_spec.rb
305
286
  - spec/requests/officers_spec.rb
306
287
  - spec/support/simplecov.rb
307
288
  - spec/support/pry.rb
@@ -310,15 +291,18 @@ test_files:
310
291
  - spec/support/helpers/configuration.rb
311
292
  - spec/support/helpers/waste_carriers_engine.rb
312
293
  - spec/lib/configuration_spec.rb
313
- - spec/fixtures/unrecognised_request.xml
314
- - spec/fixtures/payment_request_invalid.xml
315
- - spec/fixtures/refund_request_valid.xml
316
- - spec/fixtures/payment_request_valid.xml
317
- - spec/fixtures/refund_request_invalid.xml
294
+ - spec/fixtures/files/govpay/get_payment_response_submitted.json
295
+ - spec/fixtures/files/govpay/get_payment_response_not_found.json
296
+ - spec/fixtures/files/govpay/create_payment_created_response.json
297
+ - spec/fixtures/files/govpay/get_payment_response_created.json
298
+ - spec/fixtures/files/govpay/create_payment_error_response.json
299
+ - spec/fixtures/files/govpay/get_payment_response_success.json
300
+ - spec/fixtures/files/govpay/get_payment_response_failure.json
301
+ - spec/fixtures/files/govpay/get_payment_response_cancelled.json
302
+ - spec/fixtures/files/govpay/get_payment_response_error.json
318
303
  - spec/rails_helper.rb
319
- - spec/services/worldpay_response_service_spec.rb
320
- - spec/services/worldpay_resource_service_spec.rb
304
+ - spec/services/govpay_get_payment_service_spec.rb
305
+ - spec/services/govpay_request_refund_service_spec.rb
321
306
  - spec/services/companies_house_service_spec.rb
322
- - spec/services/worldpay_payment_service_spec.rb
323
- - spec/services/worldpay_refund_service_spec.rb
324
- - spec/services/worldpay_request_handler_service_spec.rb
307
+ - spec/services/govpay_refund_details_service_spec.rb
308
+ - spec/services/govpay_create_payment_service_spec.rb
@@ -1,57 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyMocks
4
- class WorldpayController < ::DefraRubyMocks::ApplicationController
5
- protect_from_forgery with: :exception, except: [:payments_service]
6
-
7
- before_action :set_default_response_format
8
-
9
- def payments_service
10
- @values = WorldpayRequestHandlerService.run(convert_request_body_to_xml)
11
-
12
- render_payment_response if @values[:request_type] == :payment
13
- render_refund_response if @values[:request_type] == :refund
14
- rescue StandardError => e
15
- Rails.logger.error("MOCKS: Worldpay payments service error: #{e.message}")
16
- head 500
17
- end
18
-
19
- def dispatcher
20
- @response = WorldpayResponseService.run(
21
- success_url: params[:successURL],
22
- failure_url: params[:failureURL],
23
- pending_url: params[:pendingURL],
24
- cancel_url: params[:cancelURL],
25
- error_url: params[:errorURL]
26
- )
27
-
28
- if @response.status == :STUCK
29
- render formats: :html, action: "stuck", layout: false
30
- else
31
- redirect_to @response.url
32
- end
33
- rescue StandardError => e
34
- Rails.logger.error("MOCKS: Worldpay dispatcher error: #{e.message}")
35
- head 500
36
- end
37
-
38
- private
39
-
40
- def set_default_response_format
41
- request.format = :xml
42
- end
43
-
44
- def convert_request_body_to_xml
45
- Nokogiri::XML(request.body.read)
46
- end
47
-
48
- def render_payment_response
49
- render "defra_ruby_mocks/worldpay/payment_request"
50
- end
51
-
52
- def render_refund_response
53
- render "defra_ruby_mocks/worldpay/refund_request"
54
- end
55
-
56
- end
57
- end
@@ -1,47 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyMocks
4
- class WorldpayPaymentService < BaseService
5
- def run(merchant_code:, xml:)
6
- check_config
7
-
8
- @merchant_code = merchant_code
9
- @order_code = extract_order_code(xml)
10
-
11
- {
12
- merchant_code: @merchant_code,
13
- order_code: @order_code,
14
- id: generate_id,
15
- url: generate_url
16
- }
17
- end
18
-
19
- private
20
-
21
- def check_config
22
- domain = DefraRubyMocks.configuration.worldpay_domain
23
-
24
- raise InvalidConfigError, :worldpay_domain if domain.blank?
25
- end
26
-
27
- def extract_order_code(xml)
28
- order = xml.at_xpath("//order")
29
- order.attribute("orderCode").text
30
- end
31
-
32
- def generate_id
33
- # Worldpay seems to generate 10 digit numbers for all its ID's. So we
34
- # replicate that here with this.
35
- # https://stackoverflow.com/a/31043825
36
- rand(1e9...1e10).to_i.to_s
37
- end
38
-
39
- def generate_url
40
- "#{base_url}?OrderKey=#{@merchant_code}%5E#{@order_code}"
41
- end
42
-
43
- def base_url
44
- File.join(DefraRubyMocks.configuration.worldpay_domain, "/worldpay/dispatcher")
45
- end
46
- end
47
- end
@@ -1,37 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyMocks
4
- class WorldpayRefundService < BaseService
5
- def run(merchant_code:, xml:)
6
- {
7
- merchant_code: merchant_code,
8
- order_code: extract_order_code(xml),
9
- refund_value: extract_refund_value(xml),
10
- currency_code: extract_currency_code(xml),
11
- exponent: extract_exponent(xml)
12
- }
13
- end
14
-
15
- private
16
-
17
- def extract_order_code(xml)
18
- order_modification = xml.at_xpath("//orderModification")
19
- order_modification.attribute("orderCode").text
20
- end
21
-
22
- def extract_refund_value(xml)
23
- amount = xml.at_xpath("//amount")
24
- amount.attribute("value").text
25
- end
26
-
27
- def extract_currency_code(xml)
28
- amount = xml.at_xpath("//amount")
29
- amount.attribute("currencyCode").text
30
- end
31
-
32
- def extract_exponent(xml)
33
- amount = xml.at_xpath("//amount")
34
- amount.attribute("exponent").text
35
- end
36
- end
37
- end
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyMocks
4
- class WorldpayRequestHandlerService < BaseService
5
- def run(xml)
6
- arguments = {
7
- merchant_code: extract_merchant_code(xml),
8
- xml: xml
9
- }
10
-
11
- generate_response(arguments)
12
- end
13
-
14
- private
15
-
16
- def extract_merchant_code(xml)
17
- payment_service = xml.at_xpath("//paymentService")
18
- payment_service.attribute("merchantCode").text
19
- end
20
-
21
- def generate_response(arguments)
22
- return WorldpayPaymentService.run(arguments).merge(request_type: :payment) if payment_request?(arguments[:xml])
23
- return WorldpayRefundService.run(arguments).merge(request_type: :refund) if refund_request?(arguments[:xml])
24
-
25
- raise UnrecognisedWorldpayRequestError
26
- end
27
-
28
- def payment_request?(xml)
29
- submit = xml.at_xpath("//submit")
30
-
31
- !submit.nil?
32
- end
33
-
34
- def refund_request?(xml)
35
- modify = xml.at_xpath("//modify")
36
-
37
- !modify.nil?
38
- end
39
- end
40
- end
@@ -1,55 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyMocks
4
- class WorldpayResourceService < BaseService
5
-
6
- def run(reference:)
7
- @reference = reference
8
-
9
- raise(MissingResourceError, @reference) if resource.nil?
10
-
11
- WorldpayResource.new(resource, order, company_name)
12
- end
13
-
14
- private
15
-
16
- WorldpayResource = Struct.new(:resource, :order, :company_name)
17
-
18
- def resource
19
- @_resource ||= locate_transient_registration || locate_completed_registration
20
- end
21
-
22
- def locate_transient_registration
23
- "WasteCarriersEngine::TransientRegistration"
24
- .constantize
25
- .where(token: @reference)
26
- .first
27
- end
28
-
29
- def locate_completed_registration
30
- "WasteCarriersEngine::Registration"
31
- .constantize
32
- .where(reg_uuid: @reference)
33
- .first
34
- end
35
-
36
- def locate_original_registration(reg_identifier)
37
- "WasteCarriersEngine::Registration"
38
- .constantize
39
- .where(reg_identifier: reg_identifier)
40
- .first
41
- end
42
-
43
- def order
44
- @_order ||= resource.finance_details&.orders&.order_by(dateCreated: :desc)&.first
45
- end
46
-
47
- def company_name
48
- if resource.class.to_s == "WasteCarriersEngine::OrderCopyCardsRegistration"
49
- locate_original_registration(resource.reg_identifier).company_name&.downcase
50
- else
51
- resource.company_name&.downcase
52
- end
53
- end
54
- end
55
- end
@@ -1,119 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module DefraRubyMocks
4
- class WorldpayResponseService < BaseService
5
-
6
- def run(success_url:, failure_url:, pending_url:, cancel_url:, error_url:)
7
- urls = {
8
- success: success_url,
9
- failure: failure_url,
10
- pending: pending_url,
11
- cancel: cancel_url,
12
- error: error_url
13
- }
14
-
15
- parse_reference(urls[:success])
16
- @resource = WorldpayResourceService.run(reference: @reference)
17
-
18
- generate_response(urls)
19
- end
20
-
21
- private
22
-
23
- WorldpayResponse = Struct.new(:supplied_url, :separator, :order_key, :mac, :value, :status, :reference) do
24
- def url
25
- [supplied_url, separator, params].join
26
- end
27
-
28
- def params
29
- [
30
- "orderKey=#{order_key}",
31
- "paymentStatus=#{status}",
32
- "paymentAmount=#{value}",
33
- "paymentCurrency=GBP",
34
- "mac=#{mac}",
35
- "source=WP"
36
- ].join("&")
37
- end
38
- end
39
-
40
- def parse_reference(url)
41
- path = URI.parse(url).path
42
- parts = path.split("/")
43
-
44
- if parts[1].downcase == "your-registration"
45
- # ["", "your-registration", "xP2zj9nqWYI0SAsMtGZn6w", "worldpay", "success", "1577812071", "NEWREG"]
46
- @reference = parts[-5]
47
- @url_format = :old
48
- else
49
- # ["", "fo", "jq6sTt2RQguAu4ZyKFfRg9zm", "worldpay", "success"]
50
- @reference = parts[-3]
51
- @url_format = :new
52
- end
53
- end
54
-
55
- def order_key
56
- [
57
- DefraRubyMocks.configuration.worldpay_admin_code,
58
- DefraRubyMocks.configuration.worldpay_merchant_code,
59
- @resource.order.order_code
60
- ].join("^")
61
- end
62
-
63
- def order_value
64
- @resource.order.total_amount.to_s
65
- end
66
-
67
- def payment_status
68
- return :AUTHORISED unless @resource.company_name
69
-
70
- return :REFUSED if @resource.company_name.include?("reject")
71
- return :STUCK if @resource.company_name.include?("stuck")
72
- return :SENT_FOR_AUTHORISATION if @resource.company_name.include?("pending")
73
- return :CANCELLED if @resource.company_name.include?("cancel")
74
- return :ERROR if @resource.company_name.include?("error")
75
-
76
- :AUTHORISED
77
- end
78
-
79
- def url(payment_status, urls)
80
- return urls[:failure] if %i[REFUSED STUCK].include?(payment_status)
81
- return urls[:pending] if payment_status == :SENT_FOR_AUTHORISATION
82
- return urls[:cancel] if payment_status == :CANCELLED
83
- return urls[:error] if payment_status == :ERROR
84
-
85
- urls[:success]
86
- end
87
-
88
- # Generate a mac that matches what Worldpay would generate
89
- #
90
- # For whatever reason, if the payment is cancelled by the user Worldpay does
91
- # not include the payment status in the mac it generates. Plus the order of
92
- # things in the array is important.
93
- def generate_mac(status)
94
- data = [
95
- order_key,
96
- order_value,
97
- "GBP"
98
- ]
99
- data << status unless status == :CANCELLED
100
- data << DefraRubyMocks.configuration.worldpay_mac_secret
101
-
102
- Digest::MD5.hexdigest(data.join).to_s
103
- end
104
-
105
- def generate_response(urls)
106
- status = payment_status
107
-
108
- WorldpayResponse.new(
109
- url(status, urls),
110
- @url_format == :new ? "?" : "&",
111
- order_key,
112
- generate_mac(status),
113
- order_value,
114
- status,
115
- @reference
116
- )
117
- end
118
- end
119
- end
@@ -1,4 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE paymentService PUBLIC "-//WorldPay//DTD WorldPay PaymentService v1//EN"
3
- "http://dtd.worldpay.com/paymentService_v1.dtd">
4
- <paymentService version="1.4" merchantCode="<%= @values[:merchant_code] %>"><reply><orderStatus orderCode="<%= @values[:order_code] %>"><reference id="<%= @values[:id] %>"><%= @values[:url] %></reference></orderStatus></reply></paymentService>
@@ -1,4 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!DOCTYPE paymentService PUBLIC "-//WorldPay//DTD WorldPay PaymentService v1//EN"
3
- "http://dtd.worldpay.com/paymentService_v1.dtd">
4
- <paymentService version="1.4" merchantCode="<%= @values[:merchant_code] %>"><reply><ok><refundReceived orderCode="<%= @values[:order_code] %>"><amount value="<%= @values[:refund_value] %>" currencyCode="<%= @values[:currency_code] %>" exponent="<%= @values[:exponent] %>" debitCreditIndicator="credit"/></refundReceived></ok></reply></paymentService>