defra_ruby_mocks 5.1.3 → 5.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: aecd68d9ac0f7dd9256d9d8f39eb645d7fc32c45cf9bc8a6e76315029156d81e
4
- data.tar.gz: 470e1e68a160dcc92895eccfc862ca7b302fb8a185505afe554d56798a7b7f7a
3
+ metadata.gz: e5df956ffaf0644fb9dae96ae157ec7f45b8121d6bc42e5cd96164de8470342a
4
+ data.tar.gz: 6da12c96f050164e579932bf9ad188866198db203893ab436fc84c0a1e4ceeb8
5
5
  SHA512:
6
- metadata.gz: f5fe8f9332df764a30bc808156ec2c61c31ab582bd0d8784bcef5da2a7cdbb7b16d14d9459bcd97b9918f9775803f60ecbbd487d46fd7bfd8c11b7a711f91e54
7
- data.tar.gz: bce6423f17b916a7d7447faaee544f9786934e1425e16735b2de95742676cdccfad6746f75caae1c8241d9a1898a0c38e33fd1dd90daf68c7485b6ac3321aceb
6
+ metadata.gz: df8d21f730573cdf3d97f10a6e41a4c2d7464e62ac3c5d032190fb6b5b9a21c9bc7a76cce56fadcfb757affd1cb21b1c2b5a6bc8a24c33c1a8db882543ffab14
7
+ data.tar.gz: bed07b31c14573ba390b13294a69f3209538616bd360379d0efb7b6dcc7b57c42406b6b4cc6e1cdc307933851e07c4537f876c0f9f8e80afdc953bdeec10a47d
data/README.md CHANGED
@@ -171,19 +171,25 @@ This Govpay mock replicates those 2 interactions with the following url
171
171
 
172
172
  #### Configuration
173
173
 
174
- In order to use the govpay mock you'll need to provide additional configuration details:
174
+ In order to use the govpay mock you'll need to provide additional configuration details.
175
+ - The root Govpay mock URLs for both front- and back-office. Both are required because the mocks for front-office point at back-office and vice-versa, and the mock gem needs to know both values for the two hosting applications.
176
+ - The external URL for both applications. These are required because the mock gem needs to map from internal-EC2 only URLs to externally accessible URLs.
177
+
178
+ For example, for a front-office application, where the front-office and back-office mocks are mounted on `/fo/mocks` and `/bo/mocks` respectively:
175
179
 
176
180
  ```ruby
177
181
  config/initializers/defra_ruby_mocks.rb
178
182
  require "defra_ruby_mocks"
179
183
 
180
184
  DefraRubyMocks.configure do |config|
181
- config.govpay_domain = File.join(ENV["WCRS_GOVPAY_MOCK_DOMAIN"] || "http://localhost:3002", "/fo/mocks/govpay/v1")
185
+ configuration.govpay_mocks_external_root_url = ENV.fetch("MOCK_FO_GOVPAY_URL", "https://back-office.domain.cloud/bo/mocks/govpay/v1")
186
+ configuration.govpay_mocks_external_root_url_other = ENV.fetch("MOCK_BO_GOVPAY_URL", "https://front-office.domain.cloud/fo/mocks/govpay/v1")
187
+
188
+ configuration.govpay_mocks_internal_root_url = ENV.fetch("MOCK_FO_GOVPAY_URL_INTERNAL", "https://back-office-internal.domain.cloud:8001/bo/mocks/govpay/v1")
189
+ configuration.govpay_mocks_internal_root_url_other = ENV.fetch("MOCK_BO_GOVPAY_URL_INTERNAL", "https://front-office-internal.domain.cloud:8002/fo/mocks/govpay/v1")
182
190
  end
183
191
  ```
184
192
 
185
- The domain is used when generating the URL we tell the app to redirect users to. As this is just an engine and not a standalone service, we need to tell it what domain it is running from.
186
-
187
193
  You'll also need to provide AWS configuration details for the mocks, for example:
188
194
 
189
195
  ```ruby
@@ -25,16 +25,16 @@ module DefraRubyMocks
25
25
 
26
26
  # This schedules a job to send a mock payment webhook.
27
27
  def send_payment_webhook
28
- Rails.logger.warn "[DefraRubyMocks] [send_payment_webhook] " \
29
- "params: #{params[:govpay_id]}, status #{params[:payment_status]}"
28
+ Rails.logger.warn "[DefraRubyMocks] [send_payment_webhook] #{params.slice(:govpay_payment_id,
29
+ :govpay_payment_payment_status)}"
30
30
 
31
- %w[govpay_id payment_status callback_url signing_secret].each do |p|
31
+ %w[govpay_payment_id govpay_payment_status callback_url signing_secret].each do |p|
32
32
  raise StandardError, "Missing parameter: '#{p}'" unless params[p].present?
33
33
  end
34
34
 
35
35
  SendPaymentWebhookJob.perform_later(
36
- govpay_id: params[:govpay_id],
37
- status: params[:payment_status],
36
+ govpay_payment_id: params[:govpay_payment_id],
37
+ govpay_payment_status: params[:govpay_payment_status],
38
38
  callback_url: params[:callback_url],
39
39
  signing_secret: params[:signing_secret]
40
40
  )
@@ -44,16 +44,17 @@ module DefraRubyMocks
44
44
 
45
45
  # This schedules a job to send a mock refund webhook.
46
46
  def send_refund_webhook
47
- Rails.logger.warn "[DefraRubyMocks] [send_refund webhook] " \
48
- "params: #{params[:govpay_id]}, status #{params[:refund_status]}"
47
+ Rails.logger.warn "[DefraRubyMocks] [send_refund webhook] #{params.slice(:govpay_refund_id, :govpay_payment_id,
48
+ :govpay_refund_status)}"
49
49
 
50
- %w[govpay_id refund_status callback_url signing_secret].each do |p|
50
+ %w[govpay_payment_id govpay_refund_id govpay_refund_status callback_url signing_secret].each do |p|
51
51
  raise StandardError, "Missing parameter: '#{p}'" unless params[p].present?
52
52
  end
53
53
 
54
54
  SendRefundWebhookJob.perform_later(
55
- govpay_id: params[:govpay_id],
56
- status: params[:refund_status],
55
+ govpay_payment_id: params[:govpay_payment_id],
56
+ govpay_refund_id: params[:govpay_refund_id],
57
+ govpay_refund_status: params[:govpay_refund_status],
57
58
  callback_url: params[:callback_url],
58
59
  signing_secret: params[:signing_secret]
59
60
  )
@@ -1,15 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class BaseSendWebhookJob < ApplicationJob
4
- def perform(govpay_id:, status:, callback_url:, signing_secret:)
5
- body = webhook_body(govpay_id:, status:)
6
- Rails.logger.warn "[DefraRubyMocks] [BaseSendWebhookJob] sending #{webhook_type} webhook " \
7
- "for #{govpay_id}, status \"#{status}\" to #{callback_url}"
4
+ attr_accessor :govpay_payment_id, :callback_url, :signing_secret
5
+
6
+ def post_callback
7
+ body = webhook_body
8
+
8
9
  RestClient::Request.execute(
9
10
  method: :post,
10
11
  url: callback_url,
11
- payload: body.to_json,
12
- headers: { "Pay-Signature": webhook_signature(body, signing_secret) }
12
+ headers: { "Pay-Signature": webhook_signature(body, signing_secret) },
13
+ payload: body.to_json
13
14
  )
14
15
  rescue StandardError => e
15
16
  Rails.logger.error "[DefraRubyMocks] [BaseSendWebhookJob] error sending " \
@@ -22,7 +23,7 @@ class BaseSendWebhookJob < ApplicationJob
22
23
  raise NotImplementedError
23
24
  end
24
25
 
25
- def webhook_body(govpay_id:, status:)
26
+ def webhook_body
26
27
  raise NotImplementedError
27
28
  end
28
29
 
@@ -1,6 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class SendPaymentWebhookJob < BaseSendWebhookJob
4
+ attr_accessor :govpay_payment_status
5
+
6
+ def perform(govpay_payment_id:, govpay_payment_status:, callback_url:, signing_secret:)
7
+ @govpay_payment_id = govpay_payment_id
8
+ @govpay_payment_status = govpay_payment_status
9
+ @callback_url = callback_url
10
+ @signing_secret = signing_secret
11
+
12
+ Rails.logger.warn "[DefraRubyMocks] [SendPaymentWebhookJob] sending #{webhook_type} webhook " \
13
+ "for #{govpay_payment_id}, status \"#{govpay_payment_status}\" to #{callback_url}"
14
+
15
+ post_callback
16
+ end
4
17
 
5
18
  private
6
19
 
@@ -8,11 +21,11 @@ class SendPaymentWebhookJob < BaseSendWebhookJob
8
21
  "payment"
9
22
  end
10
23
 
11
- def webhook_body(govpay_id:, status:)
24
+ def webhook_body
12
25
  webhook_body ||= JSON.parse(File.read("lib/fixtures/files/govpay/webhook_payment_update_body.json"))
13
26
 
14
- webhook_body["resource"]["payment_id"] = govpay_id
15
- webhook_body["resource"]["state"]["status"] = status
27
+ webhook_body["resource"]["payment_id"] = govpay_payment_id
28
+ webhook_body["resource"]["state"]["status"] = govpay_payment_status
16
29
 
17
30
  webhook_body
18
31
  end
@@ -1,6 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class SendRefundWebhookJob < BaseSendWebhookJob
4
+ attr_accessor :govpay_refund_id, :govpay_refund_status
5
+
6
+ def perform(govpay_payment_id:, govpay_refund_id:, govpay_refund_status:, callback_url:, signing_secret:)
7
+ @govpay_payment_id = govpay_payment_id
8
+ @govpay_refund_status = govpay_refund_status
9
+ @govpay_refund_id = govpay_refund_id
10
+ @callback_url = callback_url
11
+ @signing_secret = signing_secret
12
+
13
+ Rails.logger.warn "[DefraRubyMocks] [SendRefundWebhookJob] sending #{webhook_type} webhook " \
14
+ "for payment #{govpay_payment_id}, refund #{govpay_refund_id}, status " \
15
+ "\"#{govpay_refund_status}\" to #{callback_url}"
16
+
17
+ post_callback
18
+ end
4
19
 
5
20
  private
6
21
 
@@ -8,11 +23,12 @@ class SendRefundWebhookJob < BaseSendWebhookJob
8
23
  "refund"
9
24
  end
10
25
 
11
- def webhook_body(govpay_id:, status:)
26
+ def webhook_body
12
27
  webhook_body ||= JSON.parse(File.read("lib/fixtures/files/govpay/webhook_refund_update_body.json"))
13
28
 
14
- webhook_body["refund_id"] = govpay_id
15
- webhook_body["status"] = status
29
+ webhook_body["refund_id"] = govpay_refund_id
30
+ webhook_body["payment_id"] = govpay_payment_id
31
+ webhook_body["status"] = govpay_refund_status
16
32
 
17
33
  webhook_body
18
34
  end
@@ -8,17 +8,7 @@ module DefraRubyMocks
8
8
  include CanUseAwsS3
9
9
 
10
10
  def run(amount:, description:)
11
- success_response.merge(
12
- {
13
- _links: {
14
- self: { href: "#{base_url}/#{payment_id}", method: "GET" },
15
- next_url: { href: "#{base_url}/secure/next-url-uuid-abc123", method: "GET" }
16
- },
17
- amount: amount.to_i,
18
- description: description,
19
- payment_id: payment_id
20
- }
21
- )
11
+ success_response(amount, description)
22
12
  end
23
13
 
24
14
  private
@@ -28,15 +18,31 @@ module DefraRubyMocks
28
18
  end
29
19
 
30
20
  def base_url
31
- File.join(DefraRubyMocks.configuration.govpay_domain, "/payments")
21
+ File.join(DefraRubyMocks.configuration.govpay_mocks_internal_root_url, "/payments")
32
22
  end
33
23
 
34
24
  def payment_id
35
25
  @payment_id ||= SecureRandom.alphanumeric(26)
36
26
  end
37
27
 
28
+ def return_url
29
+ "#{DefraRubyMocks.configuration.govpay_mocks_internal_root_url}/payments/secure/next-url-uuid-abc123"
30
+ end
31
+
32
+ def next_url
33
+ "#{DefraRubyMocks.configuration.govpay_mocks_external_root_url_other}/payments/secure/next-url-uuid-abc123"
34
+ end
35
+
36
+ def url_root(url)
37
+ uri = URI.parse(url)
38
+ url_root = "#{uri.scheme}://#{uri.host}"
39
+ url_root += ":#{uri.port}" if uri.port.present? && uri.port != 80
40
+
41
+ url_root
42
+ end
43
+
38
44
  # rubocop:disable Metrics/MethodLength
39
- def success_response
45
+ def success_response(amount, description)
40
46
  {
41
47
  created_date: "2020-03-03T16:17:19.554Z",
42
48
  state: {
@@ -45,19 +51,19 @@ module DefraRubyMocks
45
51
  },
46
52
  _links: {
47
53
  self: {
48
- href: "https://publicapi.payments.service.gov.uk/v1/payments/hu20sqlact5260q2nanm0q8u93",
54
+ href: "#{base_url}/#{payment_id}",
49
55
  method: "GET"
50
56
  },
51
57
  next_url: {
52
- href: "https://www.payments.service.gov.uk/secure/bb0a272c-8eaf-468d-b3xf-ae5e000d2231",
58
+ href: next_url,
53
59
  method: "GET"
54
60
  }
55
61
  },
56
- amount: 14_500,
62
+ amount: amount.to_i,
57
63
  reference: "12345",
58
- description: "Pay your council tax",
59
- return_url: "https://your.service.gov.uk/completed",
60
- payment_id: "hu20sqlact5260q2nanm0q8u93",
64
+ description:,
65
+ return_url:,
66
+ payment_id:,
61
67
  payment_provider: "worldpay",
62
68
  provider_id: "10987654321"
63
69
  }
data/config/routes.rb CHANGED
@@ -47,12 +47,12 @@ DefraRubyMocks::Engine.routes.draw do # rubocop:disable Metrics/BlockLength
47
47
  as: "govpay_set_test_refund_response_status",
48
48
  constraints: ->(_request) { DefraRubyMocks.configuration.enabled? }
49
49
 
50
- post "/govpay/v1/payments/:govpay_id/send_payment_webhook",
50
+ post "/govpay/v1/payments/send_payment_webhook",
51
51
  to: "govpay_test_helpers#send_payment_webhook",
52
52
  as: "send_payment_webhook",
53
53
  constraints: ->(_request) { DefraRubyMocks.configuration.enabled? }
54
54
 
55
- post "/govpay/v1/payments/:govpay_id/send_refund_webhook",
55
+ post "/govpay/v1/payments/send_refund_webhook",
56
56
  to: "govpay_test_helpers#send_refund_webhook",
57
57
  as: "send_refund_webhook",
58
58
  constraints: ->(_request) { DefraRubyMocks.configuration.enabled? }
@@ -5,7 +5,10 @@ module DefraRubyMocks
5
5
 
6
6
  DEFAULT_DELAY = 1000
7
7
 
8
- attr_accessor :govpay_domain
8
+ attr_accessor :govpay_mocks_external_root_url,
9
+ :govpay_mocks_external_root_url_other,
10
+ :govpay_mocks_internal_root_url,
11
+ :govpay_mocks_internal_root_url_other
9
12
  attr_reader :delay
10
13
 
11
14
  def initialize
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DefraRubyMocks
4
- VERSION = "5.1.3"
4
+ VERSION = "5.3.0"
5
5
  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: 5.1.3
4
+ version: 5.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Defra
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-05-19 00:00:00.000000000 Z
11
+ date: 2025-08-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails