govuk-pay-ruby-client 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +21 -0
- data/CHANGELOG.md +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +107 -0
- data/Rakefile +10 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/govuk-pay-ruby-client.gemspec +36 -0
- data/lib/payments_api.rb +18 -0
- data/lib/payments_api/configuration.rb +54 -0
- data/lib/payments_api/errors.rb +34 -0
- data/lib/payments_api/http_client.rb +77 -0
- data/lib/payments_api/requests/create_card_payment.rb +69 -0
- data/lib/payments_api/requests/get_card_payment.rb +45 -0
- data/lib/payments_api/responses/payment_result.rb +63 -0
- data/lib/payments_api/traits/api_request.rb +23 -0
- data/lib/payments_api/version.rb +5 -0
- data/lib/tasks/rubocop.rake +8 -0
- metadata +176 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 94cd1833f6c5547b4868946ae94741dff326298c55a917d5ce9b0f5a0aef4546
|
4
|
+
data.tar.gz: 3dde6ea74ef42e77334c51ceb7ee428b4edaed47e2fefcf287c02ef6c5007c8c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 5a627bf115945ad3c8ab8e40e9a255ed0173c8fb6436a50f8476fdd2801bda1c6483f59c31128d81295535e7a396572d899bd218244b94b1f1d55e2072770541
|
7
|
+
data.tar.gz: 214e0f7a98fb956beb0588149c6c3528ed505e5cfd3def2b2a6318b8d20bf595215753daa49af1c782d70cc4b56aa2505456ce38a0cf13431b40d94d667ca0d3
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.5
|
3
|
+
NewCops: enable
|
4
|
+
Exclude:
|
5
|
+
- 'spec/**/*'
|
6
|
+
|
7
|
+
####################################
|
8
|
+
## Customization for this project ##
|
9
|
+
####################################
|
10
|
+
|
11
|
+
Style/Documentation:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Naming/MemoizedInstanceVariableName:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Metrics/AbcSize:
|
18
|
+
Max: 23
|
19
|
+
|
20
|
+
Metrics/MethodLength:
|
21
|
+
Max: 12
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Ministry of Justice
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
# GOV.UK Pay ruby client (basic functionality)
|
2
|
+
|
3
|
+
This is a basic little gem to serve as an API client for [GOV.UK Pay](https://www.payments.service.gov.uk).
|
4
|
+
|
5
|
+
Currently it supports:
|
6
|
+
|
7
|
+
* Create a credit card payment
|
8
|
+
* Get payment details by ID
|
9
|
+
|
10
|
+
It might support more operations in the future.
|
11
|
+
|
12
|
+
It is not intended to be a full featured API client and in fact is made simple on purpose. There are other clients out there with more functionality.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Prior to usage a GOV.UK Pay account must be created. This will provide the API credentials needed in you application.
|
17
|
+
|
18
|
+
You can then install the gem or require it in your application.
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
gem 'govuk-pay-ruby-client'
|
22
|
+
```
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### Configuration
|
27
|
+
|
28
|
+
You need to configure the client before you can use it. You can do this, for example with an initializer:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
require 'payments_api'
|
32
|
+
|
33
|
+
PaymentsApi.configure do |config|
|
34
|
+
config.api_key = ENV['GOVUK_PAY_API_KEY']
|
35
|
+
end
|
36
|
+
````
|
37
|
+
|
38
|
+
There are several options you can configure, like open and read timeouts, logging, and more. Please refer to the [Configuration class](lib/payments_api/configuration.rb) for more details.
|
39
|
+
|
40
|
+
### Creating a payment
|
41
|
+
|
42
|
+
Refer to the [API documentation](https://govukpay-api-browser.cloudapps.digital/#create-a-payment) to know the properties you can post.
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
response = PaymentsApi::Requests::CreateCardPayment.new(
|
46
|
+
amount: 215_00,
|
47
|
+
description: 'Payment description',
|
48
|
+
reference: 'Payment reference',
|
49
|
+
return_url: 'https://myservice.justice.uk',
|
50
|
+
|
51
|
+
# Any other optional properties, for example:
|
52
|
+
email: 'user@test.com',
|
53
|
+
metadata: {
|
54
|
+
foo: 'bar',
|
55
|
+
}
|
56
|
+
).call
|
57
|
+
```
|
58
|
+
|
59
|
+
This will return a `Responses::PaymentResult` class with the API response mapped to instance attributes, and some helper methods. For example:
|
60
|
+
|
61
|
+
```ruby
|
62
|
+
response.payment_id # "1234567890"
|
63
|
+
response.state # {"status"=>"created", "finished"=>false}
|
64
|
+
response.status # "created"
|
65
|
+
response.finished? # false
|
66
|
+
response.payment_url # URL where to redirect your user to complete the payment
|
67
|
+
```
|
68
|
+
|
69
|
+
### Retrieving a payment
|
70
|
+
|
71
|
+
Refer to the [API documentation](https://govukpay-api-browser.cloudapps.digital/#get-a-payment) for more details.
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
response = PaymentsApi::Requests::GetCardPayment.new(
|
75
|
+
payment_id: '1234567890'
|
76
|
+
).call
|
77
|
+
```
|
78
|
+
|
79
|
+
This, as with the create action, will return a `Responses::PaymentResult` class with the API response mapped to instance attributes, and some helper methods. For example:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
response.payment_id # "1234567890"
|
83
|
+
response.state # {"status"=>"success", "finished"=>true}
|
84
|
+
response.status # "success"
|
85
|
+
response.finished? # true
|
86
|
+
```
|
87
|
+
|
88
|
+
## Development
|
89
|
+
|
90
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
91
|
+
|
92
|
+
This gem uses rubocop and simplecov (at 100% coverage).
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
Bug reports and pull requests are welcome.
|
97
|
+
|
98
|
+
1. Fork the project
|
99
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
100
|
+
3. Commit until you are happy with your contribution (`git commit -am 'Add some feature'`)
|
101
|
+
4. Push the branch (`git push origin my-new-feature`)
|
102
|
+
5. Make sure your changes are covered by tests, so that we don't break it unintentionally in the future.
|
103
|
+
6. Create a new pull request.
|
104
|
+
|
105
|
+
## License
|
106
|
+
|
107
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require_relative '../lib/payments_api'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'payments_api/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'govuk-pay-ruby-client'
|
9
|
+
spec.version = PaymentsApi::VERSION
|
10
|
+
|
11
|
+
spec.authors = ['Jesus Laiz']
|
12
|
+
spec.email = ['zheileman@users.noreply.github.com']
|
13
|
+
|
14
|
+
spec.summary = 'Ruby client for GOV.UK Payments API basic functionality.'
|
15
|
+
spec.homepage = 'https://github.com/ministryofjustice/govuk-pay-ruby-client'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
|
26
|
+
|
27
|
+
spec.add_runtime_dependency 'faraday', '~> 1.0'
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
30
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.4'
|
31
|
+
spec.add_development_dependency 'rake', '~> 13.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.7'
|
33
|
+
spec.add_development_dependency 'rubocop', '~> 0.80'
|
34
|
+
spec.add_development_dependency 'simplecov', '~> 0.18'
|
35
|
+
spec.add_development_dependency 'webmock', '~> 3.4'
|
36
|
+
end
|
data/lib/payments_api.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'logger'
|
5
|
+
|
6
|
+
require_relative 'payments_api/configuration'
|
7
|
+
require_relative 'payments_api/errors'
|
8
|
+
require_relative 'payments_api/http_client'
|
9
|
+
|
10
|
+
require_relative 'payments_api/traits/api_request'
|
11
|
+
|
12
|
+
require_relative 'payments_api/requests/create_card_payment'
|
13
|
+
require_relative 'payments_api/requests/get_card_payment'
|
14
|
+
|
15
|
+
require_relative 'payments_api/responses/payment_result'
|
16
|
+
|
17
|
+
module PaymentsApi
|
18
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaymentsApi
|
4
|
+
class Configuration
|
5
|
+
attr_accessor :logger,
|
6
|
+
:api_root,
|
7
|
+
:api_key,
|
8
|
+
:open_timeout,
|
9
|
+
:read_timeout,
|
10
|
+
:request_headers,
|
11
|
+
:http_client_class
|
12
|
+
|
13
|
+
def initialize
|
14
|
+
@api_root = 'https://publicapi.payments.service.gov.uk'
|
15
|
+
|
16
|
+
@open_timeout = 10 # connection timeout in seconds
|
17
|
+
@read_timeout = 20 # read timeout in seconds
|
18
|
+
|
19
|
+
@request_headers = {
|
20
|
+
'User-Agent' => "govuk-pay-ruby-client v#{VERSION}",
|
21
|
+
'Content-Type' => 'application/json',
|
22
|
+
'Accept' => 'application/json'
|
23
|
+
}.freeze
|
24
|
+
|
25
|
+
@http_client_class = PaymentsApi::HttpClient
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
# Get current configuration
|
30
|
+
#
|
31
|
+
# @return [PaymentsApi::Configuration] current configuration
|
32
|
+
#
|
33
|
+
def self.configuration
|
34
|
+
@configuration ||= Configuration.new
|
35
|
+
end
|
36
|
+
|
37
|
+
# Configure the client
|
38
|
+
#
|
39
|
+
# Any attributes listed in +attr_accessor+ can be configured
|
40
|
+
#
|
41
|
+
# +api_root+, +open_timeout+, +read_timeout+ and +http_client_class+
|
42
|
+
# are set to sensible defaults already, but still can be changed
|
43
|
+
#
|
44
|
+
# +request_headers+ can also be changed or configured, but it is not
|
45
|
+
# recommended unless you know what you are doing
|
46
|
+
#
|
47
|
+
# @example
|
48
|
+
# PaymentsApi.configure do |config|
|
49
|
+
# config.api_key = 'secret'
|
50
|
+
# end
|
51
|
+
def self.configure
|
52
|
+
yield configuration
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'errors'
|
4
|
+
|
5
|
+
module PaymentsApi
|
6
|
+
module Errors
|
7
|
+
def raise_error!(response_body, status_code)
|
8
|
+
message = response_body.merge('http_code' => status_code)
|
9
|
+
|
10
|
+
case status_code
|
11
|
+
when 400 then raise BadRequest, message
|
12
|
+
when 401 then raise Unauthorized, message
|
13
|
+
when 404 then raise NotFoundError, message
|
14
|
+
when 422 then raise InvalidRequest, message
|
15
|
+
when 429 then raise ThrottleError, message
|
16
|
+
when 500 then raise ServerError, message
|
17
|
+
else
|
18
|
+
raise ApiError, message
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class ApiError < StandardError; end
|
23
|
+
|
24
|
+
class ClientError < ApiError; end
|
25
|
+
class ServerError < ApiError; end
|
26
|
+
class ConnectionError < ApiError; end
|
27
|
+
|
28
|
+
class BadRequest < ClientError; end
|
29
|
+
class NotFoundError < ClientError; end
|
30
|
+
class InvalidRequest < ClientError; end
|
31
|
+
class ThrottleError < ClientError; end
|
32
|
+
class Unauthorized < ClientError; end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
|
5
|
+
module PaymentsApi
|
6
|
+
class HttpClient
|
7
|
+
include Errors
|
8
|
+
|
9
|
+
attr_reader :options
|
10
|
+
|
11
|
+
def initialize(options = {})
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
# Only GET and POST verbs are used with Payments API, but more
|
16
|
+
# verbs can be easily added as needed.
|
17
|
+
|
18
|
+
def get(href, query = {})
|
19
|
+
execute_request!(:get, href) do |req|
|
20
|
+
req.params.update(query)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def post(href, payload = {})
|
25
|
+
execute_request!(:post, href) do |req|
|
26
|
+
req.body = JSON.dump(payload)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def config
|
33
|
+
PaymentsApi.configuration
|
34
|
+
end
|
35
|
+
|
36
|
+
def execute_request!(verb, href)
|
37
|
+
response = connection.send(verb) do |req|
|
38
|
+
req.url(href)
|
39
|
+
req.headers.update(config.request_headers)
|
40
|
+
|
41
|
+
yield(req) if block_given?
|
42
|
+
end
|
43
|
+
|
44
|
+
handle_response(
|
45
|
+
response
|
46
|
+
)
|
47
|
+
rescue Faraday::Error => e
|
48
|
+
raise ConnectionError, e
|
49
|
+
end
|
50
|
+
|
51
|
+
def handle_response(response)
|
52
|
+
parsed_body = JSON.parse(response.body)
|
53
|
+
status_code = response.status
|
54
|
+
|
55
|
+
raise_error!(parsed_body, status_code) unless response.success?
|
56
|
+
|
57
|
+
parsed_body
|
58
|
+
end
|
59
|
+
|
60
|
+
def connection
|
61
|
+
Faraday.new(url: config.api_root) do |conn|
|
62
|
+
conn.authorization(:Bearer, config.api_key)
|
63
|
+
|
64
|
+
conn.response(:logger, options.fetch(:logger, config.logger), bodies: false) do |logger|
|
65
|
+
logger.filter(/(Authorization:) "(Bearer .*)"/, '\1[REDACTED]')
|
66
|
+
end
|
67
|
+
|
68
|
+
conn.options.open_timeout = options.fetch(
|
69
|
+
:open_timeout, config.open_timeout
|
70
|
+
)
|
71
|
+
conn.options.timeout = options.fetch(
|
72
|
+
:read_timeout, config.read_timeout
|
73
|
+
)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaymentsApi
|
4
|
+
module Requests
|
5
|
+
class CreateCardPayment
|
6
|
+
include Traits::ApiRequest
|
7
|
+
|
8
|
+
attr_reader :amount,
|
9
|
+
:reference,
|
10
|
+
:description,
|
11
|
+
:return_url,
|
12
|
+
:optional_details
|
13
|
+
|
14
|
+
# Instantiate a new payment
|
15
|
+
#
|
16
|
+
# @param amount [Integer] The amount in pence, for instance, 14500 for 145
|
17
|
+
# @param reference [String] The reference you wish to associate with this payment
|
18
|
+
# @param description [String] A human-readable description of the payment that
|
19
|
+
# the user will see on the payment pages and admin tool
|
20
|
+
# @param return_url [String] This is a URL that your service hosts for the user
|
21
|
+
# to return to, after their payment journey on GOV.UK Pay ends
|
22
|
+
# @option optional_details [Hash] Additional properties to be sent in the payload,
|
23
|
+
# for example +email+ or +metadata+
|
24
|
+
#
|
25
|
+
# @raise [ArgumentError] if any of the mandatory params above are missing
|
26
|
+
# @return [PaymentsApi::Requests::CreateCardPayment] instance
|
27
|
+
#
|
28
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#create-a-payment
|
29
|
+
#
|
30
|
+
def initialize(amount:, reference:, description:, return_url:, **optional_details)
|
31
|
+
@optional_details = optional_details
|
32
|
+
|
33
|
+
@amount = amount
|
34
|
+
@reference = reference
|
35
|
+
@description = description
|
36
|
+
@return_url = return_url
|
37
|
+
end
|
38
|
+
|
39
|
+
# Create a new payment
|
40
|
+
#
|
41
|
+
# @raise [PaymentsApi::Errors::ApiError] refer to lib/payments_api/errors.rb
|
42
|
+
# @return [Responses::PaymentResult] result response
|
43
|
+
#
|
44
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#tocscreatecardpaymentrequest
|
45
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#tocscreatepaymentresult
|
46
|
+
#
|
47
|
+
def call
|
48
|
+
Responses::PaymentResult.new(
|
49
|
+
http_client.post(endpoint, payload)
|
50
|
+
)
|
51
|
+
end
|
52
|
+
|
53
|
+
def endpoint
|
54
|
+
'/v1/payments'
|
55
|
+
end
|
56
|
+
|
57
|
+
def payload
|
58
|
+
{
|
59
|
+
amount: amount,
|
60
|
+
reference: reference,
|
61
|
+
description: description,
|
62
|
+
return_url: return_url
|
63
|
+
}.merge(
|
64
|
+
optional_details
|
65
|
+
)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaymentsApi
|
4
|
+
module Requests
|
5
|
+
class GetCardPayment
|
6
|
+
include Traits::ApiRequest
|
7
|
+
|
8
|
+
attr_reader :payment_id
|
9
|
+
|
10
|
+
# Instantiate a get payment details request
|
11
|
+
#
|
12
|
+
# @param payment_id [String] The payment ID to retrieve their information
|
13
|
+
#
|
14
|
+
# @raise [ArgumentError] if +payment_id+ is missing or +nil+
|
15
|
+
# @return [PaymentsApi::Requests::GetCardPayment] instance
|
16
|
+
#
|
17
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#get-a-payment
|
18
|
+
#
|
19
|
+
def initialize(payment_id:)
|
20
|
+
@payment_id = payment_id
|
21
|
+
|
22
|
+
raise ArgumentError, '`payment_id` cannot be nil' unless payment_id
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get existing payment details
|
26
|
+
#
|
27
|
+
# @raise [PaymentsApi::Errors::ApiError] refer to lib/payments_api/errors.rb
|
28
|
+
# @return [Responses::PaymentResult] result response
|
29
|
+
#
|
30
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#tocsgetpaymentresult
|
31
|
+
#
|
32
|
+
def call
|
33
|
+
Responses::PaymentResult.new(
|
34
|
+
http_client.get(endpoint)
|
35
|
+
)
|
36
|
+
end
|
37
|
+
|
38
|
+
def endpoint
|
39
|
+
format(
|
40
|
+
'/v1/payments/%<payment_id>s', payment_id: payment_id
|
41
|
+
)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaymentsApi
|
4
|
+
module Responses
|
5
|
+
class PaymentResult
|
6
|
+
SUCCESS_STATUS = 'success'
|
7
|
+
|
8
|
+
FIELDS = %w[
|
9
|
+
amount
|
10
|
+
state
|
11
|
+
description
|
12
|
+
reference
|
13
|
+
language
|
14
|
+
payment_id
|
15
|
+
return_url
|
16
|
+
created_date
|
17
|
+
metadata
|
18
|
+
email
|
19
|
+
refund_summary
|
20
|
+
settlement_summary
|
21
|
+
_links
|
22
|
+
].freeze
|
23
|
+
|
24
|
+
attr_reader(*FIELDS)
|
25
|
+
|
26
|
+
# Instantiate a payment result
|
27
|
+
#
|
28
|
+
# @note Not all properties returned by the API will be mapped to instance
|
29
|
+
# attributes in this class, but most common are included
|
30
|
+
#
|
31
|
+
# @param response [Hash] The API response for the operation, currently
|
32
|
+
# create a payment, or get details of an existing payment by ID
|
33
|
+
#
|
34
|
+
# @return [PaymentsApi::Responses::PaymentResult] instance
|
35
|
+
#
|
36
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#tocscreatepaymentresult
|
37
|
+
# @see https://govukpay-api-browser.cloudapps.digital/#tocsgetpaymentresult
|
38
|
+
#
|
39
|
+
def initialize(response)
|
40
|
+
FIELDS.each do |field|
|
41
|
+
instance_variable_set(:"@#{field}", response.fetch(field, nil))
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# URL where to redirect the user to capture their payment details
|
46
|
+
def payment_url
|
47
|
+
_links.dig('next_url', 'href')
|
48
|
+
end
|
49
|
+
|
50
|
+
def status
|
51
|
+
state.dig('status')
|
52
|
+
end
|
53
|
+
|
54
|
+
def finished?
|
55
|
+
!!state.dig('finished')
|
56
|
+
end
|
57
|
+
|
58
|
+
def success?
|
59
|
+
status.eql?(SUCCESS_STATUS)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module PaymentsApi
|
4
|
+
module Traits
|
5
|
+
module ApiRequest
|
6
|
+
#:nocov:
|
7
|
+
def call
|
8
|
+
raise 'implement in subclasses'
|
9
|
+
end
|
10
|
+
|
11
|
+
def endpoint
|
12
|
+
raise 'implement in subclasses'
|
13
|
+
end
|
14
|
+
#:nocov:
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def http_client
|
19
|
+
@_http_client ||= PaymentsApi.configuration.http_client_class.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: govuk-pay-ruby-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jesus Laiz
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faraday
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '13.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '13.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.7'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.7'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.80'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.80'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.18'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.18'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: webmock
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.4'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.4'
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- zheileman@users.noreply.github.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- CHANGELOG.md
|
136
|
+
- Gemfile
|
137
|
+
- LICENSE.txt
|
138
|
+
- README.md
|
139
|
+
- Rakefile
|
140
|
+
- bin/console
|
141
|
+
- bin/setup
|
142
|
+
- govuk-pay-ruby-client.gemspec
|
143
|
+
- lib/payments_api.rb
|
144
|
+
- lib/payments_api/configuration.rb
|
145
|
+
- lib/payments_api/errors.rb
|
146
|
+
- lib/payments_api/http_client.rb
|
147
|
+
- lib/payments_api/requests/create_card_payment.rb
|
148
|
+
- lib/payments_api/requests/get_card_payment.rb
|
149
|
+
- lib/payments_api/responses/payment_result.rb
|
150
|
+
- lib/payments_api/traits/api_request.rb
|
151
|
+
- lib/payments_api/version.rb
|
152
|
+
- lib/tasks/rubocop.rake
|
153
|
+
homepage: https://github.com/ministryofjustice/govuk-pay-ruby-client
|
154
|
+
licenses:
|
155
|
+
- MIT
|
156
|
+
metadata: {}
|
157
|
+
post_install_message:
|
158
|
+
rdoc_options: []
|
159
|
+
require_paths:
|
160
|
+
- lib
|
161
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: 2.5.0
|
166
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
167
|
+
requirements:
|
168
|
+
- - ">="
|
169
|
+
- !ruby/object:Gem::Version
|
170
|
+
version: '0'
|
171
|
+
requirements: []
|
172
|
+
rubygems_version: 3.0.3
|
173
|
+
signing_key:
|
174
|
+
specification_version: 4
|
175
|
+
summary: Ruby client for GOV.UK Payments API basic functionality.
|
176
|
+
test_files: []
|