defra_ruby_mocks 2.3.1 → 2.3.2

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: 234acb79fe01017da60fcef23605cd3ff4506fefb54765f29cbe707533016ba2
4
- data.tar.gz: 4a52336dbd39d3c03ffab8b96f7d1c4b90ca83af1dca3ffb72973635f85d40e1
3
+ metadata.gz: 72e4545b335d8982579d4e72ec7319ad6d2a18dff08f931576dbace757736e30
4
+ data.tar.gz: 4b8a88a381bd24fd027214c2b86016d4b532d74dce07284050e2a48f3dfe726d
5
5
  SHA512:
6
- metadata.gz: 6f5256bb6536f34b6d7aa373bd02f6ba9e7c200cbfcf7c344d5f0ab127d960ba3768c73b3c150a6b4c3d46e2c405d76dd4f5ea8ab784ad9f22a852db8a330414
7
- data.tar.gz: da3cfafbe7aa6939ca4cec698f00b982b004da1cf39bfcb97e26d72a4b219e9e6df88cea29effadfa3ccfbbb9bbbf6bb058671cd34db71b80fe9ab607e98f5ee
6
+ metadata.gz: 5043858781580f1d769aecbe6c0e185311421811c4dad12154e50b83e58f69efde797a854d190384219b8a2d8fb2b73da66ddabecc2064a52c51140022e01b85
7
+ data.tar.gz: 45699483045aba318c7421ff48dbea130aaba08f3b92204fc9b2c16e1a63e89838c1429aaae4f72c32e8a32a7bf4757463b721469c4125d7e50e6413cf56f023
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DefraRubyMocks
4
+ class GovpayController < ::DefraRubyMocks::ApplicationController
5
+
6
+ protect_from_forgery with: :exception, except: [:create_payemnt]
7
+
8
+ def create_payment
9
+ valid_create_params
10
+ render json: GovpayCreatePaymentService.new.run(
11
+ amount: params[:amount], description: params[:description], return_url: params[:return_url]
12
+ )
13
+ rescue StandardError => e
14
+ Rails.logger.error("MOCKS: Govpay payment creation error: #{e.message}")
15
+ Rails.logger.error e.backtrace
16
+ head 500
17
+ end
18
+
19
+ def payment_details
20
+ valid_payment_id
21
+ render json: GovpayGetPaymentService.new.run(payment_id: params[:payment_id])
22
+ rescue StandardError => e
23
+ Rails.logger.error("MOCKS: Govpay payment details error: #{e.message}")
24
+ Rails.logger.error e.backtrace
25
+ head 422
26
+ end
27
+
28
+ def valid_create_params
29
+ params.require(%i[amount description return_url])
30
+ end
31
+
32
+ def valid_payment_id
33
+ return true if params[:payment_id].length > 20 && params[:payment_id].match(/\A[a-zA-Z0-9]*\z/)
34
+
35
+ raise ArgumentError, "Invalid Govpay payment ID #{params[:payment_id]}"
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,34 @@
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
+ {
10
+ created_date: Time.current,
11
+ state: { status: "created", finished: false },
12
+ _links: {
13
+ self: { href: "#{base_url}/#{payment_id}", method: "GET" },
14
+ next_url: { href: return_url, method: "GET" }
15
+ },
16
+ amount: amount.to_i,
17
+ reference: "12345",
18
+ description: description,
19
+ payment_id: payment_id,
20
+ payment_provider: "sandbox"
21
+ }
22
+ end
23
+
24
+ private
25
+
26
+ def base_url
27
+ File.join(DefraRubyMocks.configuration.govpay_domain, "/payments")
28
+ end
29
+
30
+ def payment_id
31
+ @payment_id ||= SecureRandom.alphanumeric(26)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,32 @@
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
+ {
10
+ created_date: created_at,
11
+ amount: amount,
12
+ state: {
13
+ status: "success",
14
+ finished: true
15
+ },
16
+ description: "Your waste carriers registration fee",
17
+ reference: "12345",
18
+ language: "en",
19
+ payment_id: payment_id,
20
+ refund_summary: {
21
+ status: "available",
22
+ amount_available: amount - 100,
23
+ amount_submitted: 0
24
+ },
25
+ total_amount: amount,
26
+ payment_provider: "worldpay",
27
+ provider_id: "10987654321"
28
+ }
29
+ end
30
+
31
+ end
32
+ end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DefraRubyMocks
4
- VERSION = "2.3.1"
4
+ VERSION = "2.3.2"
5
5
  end