defra_ruby_mocks 2.3.0 → 2.3.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/defra_ruby_mocks/govpay_controller.rb +40 -0
- data/app/jobs/defra_ruby_mocks/application_job.rb +6 -0
- data/app/jobs/defra_ruby_mocks/govpay_payment_callback_job.rb +17 -0
- data/app/services/defra_ruby_mocks/govpay_create_payment_service.rb +62 -0
- data/app/services/defra_ruby_mocks/govpay_get_payment_service.rb +90 -0
- data/app/services/defra_ruby_mocks/worldpay_resource_service.rb +2 -2
- data/app/services/defra_ruby_mocks/worldpay_response_service.rb +2 -0
- data/config/routes.rb +9 -0
- data/lib/defra_ruby_mocks/configuration.rb +1 -1
- data/lib/defra_ruby_mocks/version.rb +1 -1
- data/spec/dummy/log/test.log +2377 -2251
- data/spec/examples.txt +127 -111
- data/spec/fixtures/files/govpay/create_payment_created_response.json +24 -0
- data/spec/fixtures/files/govpay/create_payment_error_response.json +5 -0
- data/spec/fixtures/files/govpay/get_payment_response_cancelled.json +61 -0
- data/spec/fixtures/files/govpay/get_payment_response_created.json +73 -0
- data/spec/fixtures/files/govpay/get_payment_response_error.json +5 -0
- data/spec/fixtures/files/govpay/get_payment_response_failure.json +9 -0
- data/spec/fixtures/files/govpay/get_payment_response_not_found.json +4 -0
- data/spec/fixtures/files/govpay/get_payment_response_submitted.json +75 -0
- data/spec/fixtures/files/govpay/get_payment_response_success.json +62 -0
- data/spec/fixtures/{payment_request_invalid.xml → files/worldpay/payment_request_invalid.xml} +0 -0
- data/spec/fixtures/{payment_request_valid.xml → files/worldpay/payment_request_valid.xml} +0 -0
- data/spec/fixtures/{refund_request_invalid.xml → files/worldpay/refund_request_invalid.xml} +0 -0
- data/spec/fixtures/{refund_request_valid.xml → files/worldpay/refund_request_valid.xml} +0 -0
- data/spec/fixtures/{unrecognised_request.xml → files/worldpay/unrecognised_request.xml} +0 -0
- data/spec/requests/govpay_spec.rb +127 -0
- data/spec/requests/worldpay_spec.rb +3 -3
- data/spec/services/govpay_create_payment_service_spec.rb +44 -0
- data/spec/services/govpay_get_payment_service_spec.rb +30 -0
- data/spec/services/worldpay_payment_service_spec.rb +2 -2
- data/spec/services/worldpay_refund_service_spec.rb +2 -2
- data/spec/services/worldpay_request_handler_service_spec.rb +3 -3
- data/spec/services/worldpay_resource_service_spec.rb +8 -0
- data/spec/services/worldpay_response_service_spec.rb +9 -1
- metadata +44 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9215253284f8928c0b731ac83de618da7e328ee04a75453290956132bb72d65d
|
4
|
+
data.tar.gz: 56087ad7a0263a7d6cb70d2a6d02df385c33262735a544921fbd0f25a7a607f8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c2fa8dee0d93abf131a79cad79b6a94c58fa6e9541051c9d39fbba645b294b35ce7246a4ebc69a4854765ac002b6e77acb98fa6cad785b0a82bb4ad4b0c4f7f4
|
7
|
+
data.tar.gz: c01f83850554a60d11341677892d92e9c13613481aa48d5967ed3fdbdd6de5f45620ceb7623294d58656e23b3a4e4decfc9ffa1431ae656757db5bc559c9197e
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRubyMocks
|
4
|
+
class GovpayController < ::DefraRubyMocks::ApplicationController
|
5
|
+
|
6
|
+
skip_before_action :verify_authenticity_token
|
7
|
+
|
8
|
+
def create_payment
|
9
|
+
valid_create_params
|
10
|
+
|
11
|
+
# Enqueue the payment callback to run after the controller responds
|
12
|
+
DefraRubyMocks::GovpayPaymentCallbackJob.perform_later(params[:return_url])
|
13
|
+
|
14
|
+
render json: GovpayCreatePaymentService.new.run(
|
15
|
+
amount: params[:amount], description: params[:description], return_url: params[:return_url]
|
16
|
+
)
|
17
|
+
rescue StandardError => e
|
18
|
+
Rails.logger.error("MOCKS: Govpay payment creation error: #{e.message}")
|
19
|
+
head 500
|
20
|
+
end
|
21
|
+
|
22
|
+
def payment_details
|
23
|
+
valid_payment_id
|
24
|
+
render json: GovpayGetPaymentService.new.run(payment_id: params[:payment_id])
|
25
|
+
rescue StandardError => e
|
26
|
+
Rails.logger.error("MOCKS: Govpay payment details error: #{e.message}")
|
27
|
+
head 422
|
28
|
+
end
|
29
|
+
|
30
|
+
def valid_create_params
|
31
|
+
params.require(%i[amount description return_url])
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_payment_id
|
35
|
+
return true if params[:payment_id].length > 20 && params[:payment_id].match(/\A[a-zA-Z0-9]*\z/)
|
36
|
+
|
37
|
+
raise ArgumentError, "Invalid Govpay payment ID #{params[:payment_id]}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module DefraRubyMocks
|
4
|
+
class GovpayPaymentCallbackJob < DefraRubyMocks::ApplicationJob
|
5
|
+
queue_as :default
|
6
|
+
|
7
|
+
def perform(*response_url)
|
8
|
+
Rails.logger.debug "GovpayPaymentCallbackJob calling response URL #{response_url}"
|
9
|
+
RestClient::Request.execute(method: :GET, url: response_url)
|
10
|
+
rescue RestClient::ExceptionWithResponse => e
|
11
|
+
Rails.logger.debug "GovpayPaymentCallbackJob: RestClient received response: #{e}"
|
12
|
+
rescue StandardError => e
|
13
|
+
Rails.logger.error("GovpayPaymentCallbackJob: Error sending request to govpay: #{e}")
|
14
|
+
Airbrake.notify(e, message: "Error on govpay request")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
|
5
|
+
module DefraRubyMocks
|
6
|
+
class GovpayCreatePaymentService < BaseService
|
7
|
+
|
8
|
+
def run(amount:, description:, return_url:)
|
9
|
+
success_response.merge(
|
10
|
+
{
|
11
|
+
_links: {
|
12
|
+
self: { href: "#{base_url}/#{payment_id}", method: "GET" },
|
13
|
+
next_url: { href: return_url, method: "GET" }
|
14
|
+
},
|
15
|
+
amount: amount.to_i,
|
16
|
+
description: description,
|
17
|
+
payment_id: payment_id
|
18
|
+
}
|
19
|
+
)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def base_url
|
25
|
+
File.join(DefraRubyMocks.configuration.govpay_domain, "/payments")
|
26
|
+
end
|
27
|
+
|
28
|
+
def payment_id
|
29
|
+
@payment_id ||= SecureRandom.alphanumeric(26)
|
30
|
+
end
|
31
|
+
|
32
|
+
# rubocop:disable Metrics/MethodLength
|
33
|
+
def success_response
|
34
|
+
{
|
35
|
+
"created_date": "2020-03-03T16:17:19.554Z",
|
36
|
+
"state": {
|
37
|
+
"status": "created",
|
38
|
+
"finished": false
|
39
|
+
},
|
40
|
+
"_links": {
|
41
|
+
"self": {
|
42
|
+
"href": "https://publicapi.payments.service.gov.uk/v1/payments/hu20sqlact5260q2nanm0q8u93",
|
43
|
+
"method": "GET"
|
44
|
+
},
|
45
|
+
"next_url": {
|
46
|
+
"href": "https://www.payments.service.gov.uk/secure/bb0a272c-8eaf-468d-b3xf-ae5e000d2231",
|
47
|
+
"method": "GET"
|
48
|
+
}
|
49
|
+
},
|
50
|
+
"amount": 14_500,
|
51
|
+
"reference": "12345",
|
52
|
+
"description": "Pay your council tax",
|
53
|
+
"return_url": "https://your.service.gov.uk/completed",
|
54
|
+
"payment_id": "hu20sqlact5260q2nanm0q8u93",
|
55
|
+
"payment_provider": "worldpay",
|
56
|
+
"provider_id": "10987654321"
|
57
|
+
}
|
58
|
+
end
|
59
|
+
# rubocop:enable Metrics/MethodLength
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "securerandom"
|
4
|
+
|
5
|
+
module DefraRubyMocks
|
6
|
+
class GovpayGetPaymentService < BaseService
|
7
|
+
|
8
|
+
def run(payment_id:, amount: Random.rand(100..1_000), created_at: Time.current)
|
9
|
+
# This currently supports only success results:
|
10
|
+
response_success.merge(
|
11
|
+
{
|
12
|
+
created_date: created_at,
|
13
|
+
amount: amount,
|
14
|
+
payment_id: payment_id,
|
15
|
+
total_amount: amount
|
16
|
+
}
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
# rubocop:disable Metrics/MethodLength
|
23
|
+
def response_success
|
24
|
+
{
|
25
|
+
"amount": 10_501,
|
26
|
+
"description": "Waste carrier registration upper tier",
|
27
|
+
"reference": "12345",
|
28
|
+
"language": "en",
|
29
|
+
"metadata": {
|
30
|
+
"ledger_code": "AB100",
|
31
|
+
"an_internal_reference_number": 200
|
32
|
+
},
|
33
|
+
"email": "sherlock.holmes@example.com",
|
34
|
+
"state": {
|
35
|
+
"status": "success",
|
36
|
+
"finished": true
|
37
|
+
},
|
38
|
+
"payment_id": "cnnffa1e6s3u9a6n24u2cp527d",
|
39
|
+
"payment_provider": "sandbox",
|
40
|
+
"created_date": "2022-05-18T11:52:13.669Z",
|
41
|
+
"refund_summary": {
|
42
|
+
"status": "available",
|
43
|
+
"amount_available": 10_501,
|
44
|
+
"amount_submitted": 0
|
45
|
+
},
|
46
|
+
"settlement_summary": {
|
47
|
+
"capture_submit_time": "2022-05-18T11:52:39.172Z",
|
48
|
+
"captured_date": "2022-05-18"
|
49
|
+
},
|
50
|
+
"card_details": {
|
51
|
+
"last_digits_card_number": "5100",
|
52
|
+
"first_digits_card_number": "510510",
|
53
|
+
"cardholder_name": "Sherlock Holmes",
|
54
|
+
"expiry_date": "01/24",
|
55
|
+
"billing_address": {
|
56
|
+
"line1": "221 Baker Street",
|
57
|
+
"line2": "Flat b",
|
58
|
+
"postcode": "NW1 6XE",
|
59
|
+
"city": "London",
|
60
|
+
"country": "GB"
|
61
|
+
},
|
62
|
+
"card_brand": "Mastercard",
|
63
|
+
"card_type": "debit"
|
64
|
+
},
|
65
|
+
"delayed_capture": false,
|
66
|
+
"moto": false,
|
67
|
+
"provider_id": "9bb0c2c1-d0c5-4a63-8945-f4240e06f8ae",
|
68
|
+
"return_url": "https://some-wcr-env.defra.gov.uk/completed",
|
69
|
+
"authorisation_mode": "web",
|
70
|
+
"card_brand": "Mastercard",
|
71
|
+
"_links": {
|
72
|
+
"self": {
|
73
|
+
"href": "https://publicapi.payments.service.gov.uk/v1/payments/cnnffa1e6s3u9a6n24u2cp527d",
|
74
|
+
"method": "GET"
|
75
|
+
},
|
76
|
+
"events": {
|
77
|
+
"href": "https://publicapi.payments.service.gov.uk/v1/payments/cnnffa1e6s3u9a6n24u2cp527d/events",
|
78
|
+
"method": "GET"
|
79
|
+
},
|
80
|
+
"refunds": {
|
81
|
+
"href": "https://publicapi.payments.service.gov.uk/v1/payments/cnnffa1e6s3u9a6n24u2cp527d/refunds",
|
82
|
+
"method": "GET"
|
83
|
+
}
|
84
|
+
}
|
85
|
+
}
|
86
|
+
end
|
87
|
+
# rubocop:enable Metrics/MethodLength
|
88
|
+
|
89
|
+
end
|
90
|
+
end
|
@@ -46,9 +46,9 @@ module DefraRubyMocks
|
|
46
46
|
|
47
47
|
def company_name
|
48
48
|
if resource.class.to_s == "WasteCarriersEngine::OrderCopyCardsRegistration"
|
49
|
-
locate_original_registration(resource.reg_identifier).company_name
|
49
|
+
locate_original_registration(resource.reg_identifier).company_name&.downcase
|
50
50
|
else
|
51
|
-
resource.company_name
|
51
|
+
resource.company_name&.downcase
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
@@ -65,6 +65,8 @@ module DefraRubyMocks
|
|
65
65
|
end
|
66
66
|
|
67
67
|
def payment_status
|
68
|
+
return :AUTHORISED unless @resource.company_name
|
69
|
+
|
68
70
|
return :REFUSED if @resource.company_name.include?("reject")
|
69
71
|
return :STUCK if @resource.company_name.include?("stuck")
|
70
72
|
return :SENT_FOR_AUTHORISATION if @resource.company_name.include?("pending")
|
data/config/routes.rb
CHANGED
@@ -21,4 +21,13 @@ DefraRubyMocks::Engine.routes.draw do
|
|
21
21
|
as: "worldpay_dispatcher",
|
22
22
|
constraints: ->(_request) { DefraRubyMocks.configuration.enabled? }
|
23
23
|
|
24
|
+
post "/govpay/v1/payments",
|
25
|
+
to: "govpay#create_payment",
|
26
|
+
as: "govpay_create_payment",
|
27
|
+
constraints: ->(_request) { DefraRubyMocks.configuration.enabled? }
|
28
|
+
|
29
|
+
get "/govpay/v1/payments/:payment_id",
|
30
|
+
to: "govpay#payment_details",
|
31
|
+
as: "govpay_payment_details",
|
32
|
+
constraints: ->(_request) { DefraRubyMocks.configuration.enabled? }
|
24
33
|
end
|
@@ -5,7 +5,7 @@ module DefraRubyMocks
|
|
5
5
|
|
6
6
|
DEFAULT_DELAY = 1000
|
7
7
|
|
8
|
-
attr_accessor :worldpay_admin_code, :worldpay_mac_secret, :worldpay_merchant_code, :worldpay_domain
|
8
|
+
attr_accessor :worldpay_admin_code, :worldpay_mac_secret, :worldpay_merchant_code, :worldpay_domain, :govpay_domain
|
9
9
|
attr_reader :delay
|
10
10
|
|
11
11
|
def initialize
|