klarna-checkout-v3 0.1.1

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.
Files changed (36) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.gitlab-ci.yml +31 -0
  4. data/.rspec +3 -0
  5. data/.rubocop.yml +15 -0
  6. data/Gemfile +18 -0
  7. data/Gemfile.lock +83 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +40 -0
  10. data/Rakefile +8 -0
  11. data/bin/console +15 -0
  12. data/bin/setup +8 -0
  13. data/klarna-checkout.gemspec +36 -0
  14. data/lib/klarna/checkout.rb +60 -0
  15. data/lib/klarna/checkout/api_utilities/connection_utilities.rb +22 -0
  16. data/lib/klarna/checkout/api_utilities/parse_response.rb +16 -0
  17. data/lib/klarna/checkout/configuration.rb +116 -0
  18. data/lib/klarna/checkout/errors/configuration_error.rb +14 -0
  19. data/lib/klarna/checkout/errors/order_cancel_error.rb +14 -0
  20. data/lib/klarna/checkout/errors/order_capture_error.rb +14 -0
  21. data/lib/klarna/checkout/errors/order_not_found_error.rb +14 -0
  22. data/lib/klarna/checkout/errors/order_refund_error.rb +14 -0
  23. data/lib/klarna/checkout/errors/order_validation_error.rb +14 -0
  24. data/lib/klarna/checkout/operations/acknowledge.rb +15 -0
  25. data/lib/klarna/checkout/operations/cancel.rb +26 -0
  26. data/lib/klarna/checkout/operations/capture.rb +37 -0
  27. data/lib/klarna/checkout/operations/create.rb +40 -0
  28. data/lib/klarna/checkout/operations/create_recurring.rb +37 -0
  29. data/lib/klarna/checkout/operations/fetch.rb +68 -0
  30. data/lib/klarna/checkout/operations/refund.rb +36 -0
  31. data/lib/klarna/checkout/order.rb +117 -0
  32. data/lib/klarna/checkout/resources/authentication.rb +22 -0
  33. data/lib/klarna/checkout/resources/merchant_urls.rb +18 -0
  34. data/lib/klarna/checkout/validations/order_validations.rb +71 -0
  35. data/lib/klarna/checkout/version.rb +7 -0
  36. metadata +126 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 728f409f890b15aabe717fca71ea5f0e41d0a4213a17b5403ca1d9b2f1c66463
4
+ data.tar.gz: 07dbdbfbe243316383e61588a7028fbcdb3706349746268b9e662a156e24c6ac
5
+ SHA512:
6
+ metadata.gz: adf673682ed31c4c4cb74b5b92d992ad91d103fcb7cf7b19a464cf71930fb8b433bf36723d4835da63e2f747ef743c49b7544a20088a1ca78b56895b8ab2b84a
7
+ data.tar.gz: 6bd5d8d3aa38f059ab92566008e434a4d204f910f5bf9452b072f42e976b98c832af2b458284fd82194c8f6473d767effe586df3bd2ad6eace1a4e1428bcb406
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ *.DS_Store
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,31 @@
1
+ image: "ruby:latest"
2
+
3
+ cache:
4
+ paths:
5
+ - vendor/
6
+
7
+ variables:
8
+ BUNDLE_PATH: vendor
9
+ RUBYOPT: "-W:no-deprecated -W:no-experimental"
10
+
11
+ before_script:
12
+ - bundle check || bundle install --jobs=2
13
+
14
+ stages:
15
+ - lint
16
+ - test
17
+
18
+ Linter:
19
+ stage: lint
20
+ script:
21
+ - bundle exec rubocop --parallel
22
+ only:
23
+ - merge_requests
24
+
25
+ Rspec:
26
+ stage: test
27
+ script:
28
+ - RAILS_ENV=test bundle exec rspec
29
+
30
+ only:
31
+ - merge_requests
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ NewCops: enable
4
+
5
+ Style/Documentation:
6
+ Enabled: false
7
+
8
+ Metrics/BlockLength:
9
+ Max: 60
10
+
11
+ Metrics/MethodLength:
12
+ Max: 15
13
+
14
+ Metrics/BlockLength:
15
+ ExcludedMethods: ['describe', 'context']
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in klarna.gemspec
6
+ gemspec
7
+
8
+ gem 'faraday', '< 2.0'
9
+ gem 'rake', '< 14.0'
10
+ gem 'rspec', '< 4.0'
11
+ gem 'vcr', '< 7.0'
12
+ gem 'webmock', '< 4.0'
13
+
14
+ group :development, :test do
15
+ gem 'pry', '< 1.0'
16
+ gem 'pry-byebug'
17
+ gem 'rubocop', require: false
18
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ klarna-checkout (0.1.1)
5
+ faraday
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ ast (2.4.1)
13
+ byebug (11.1.3)
14
+ coderay (1.1.3)
15
+ crack (0.4.4)
16
+ diff-lcs (1.4.4)
17
+ faraday (1.0.1)
18
+ multipart-post (>= 1.2, < 3)
19
+ hashdiff (1.0.1)
20
+ method_source (1.0.0)
21
+ multipart-post (2.1.1)
22
+ parallel (1.19.2)
23
+ parser (2.7.1.5)
24
+ ast (~> 2.4.1)
25
+ pry (0.13.1)
26
+ coderay (~> 1.1)
27
+ method_source (~> 1.0)
28
+ pry-byebug (3.9.0)
29
+ byebug (~> 11.0)
30
+ pry (~> 0.13.0)
31
+ public_suffix (4.0.6)
32
+ rainbow (3.0.0)
33
+ rake (13.0.1)
34
+ regexp_parser (1.8.1)
35
+ rexml (3.2.4)
36
+ rspec (3.9.0)
37
+ rspec-core (~> 3.9.0)
38
+ rspec-expectations (~> 3.9.0)
39
+ rspec-mocks (~> 3.9.0)
40
+ rspec-core (3.9.3)
41
+ rspec-support (~> 3.9.3)
42
+ rspec-expectations (3.9.2)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.9.0)
45
+ rspec-mocks (3.9.1)
46
+ diff-lcs (>= 1.2.0, < 2.0)
47
+ rspec-support (~> 3.9.0)
48
+ rspec-support (3.9.3)
49
+ rubocop (0.92.0)
50
+ parallel (~> 1.10)
51
+ parser (>= 2.7.1.5)
52
+ rainbow (>= 2.2.2, < 4.0)
53
+ regexp_parser (>= 1.7)
54
+ rexml
55
+ rubocop-ast (>= 0.5.0)
56
+ ruby-progressbar (~> 1.7)
57
+ unicode-display_width (>= 1.4.0, < 2.0)
58
+ rubocop-ast (0.7.1)
59
+ parser (>= 2.7.1.5)
60
+ ruby-progressbar (1.10.1)
61
+ unicode-display_width (1.7.0)
62
+ vcr (6.0.0)
63
+ webmock (3.9.1)
64
+ addressable (>= 2.3.6)
65
+ crack (>= 0.3.2)
66
+ hashdiff (>= 0.4.0, < 2.0.0)
67
+
68
+ PLATFORMS
69
+ ruby
70
+
71
+ DEPENDENCIES
72
+ faraday (< 2.0)
73
+ klarna-checkout!
74
+ pry (< 1.0)
75
+ pry-byebug
76
+ rake (< 14.0)
77
+ rspec (< 4.0)
78
+ rubocop
79
+ vcr (< 7.0)
80
+ webmock (< 4.0)
81
+
82
+ BUNDLED WITH
83
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sverrir Steindorsson
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,40 @@
1
+ # Klarna::Checkout
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/klarna/checkout`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'klarna-checkout'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install klarna-checkout
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/klarna-checkout.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
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 'klarna/checkout'
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,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/klarna/checkout/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'klarna-checkout-v3'
7
+ spec.version = Klarna::Checkout::VERSION
8
+ spec.authors = ['Sverrir Steindorsson', 'Mike Eyrikh']
9
+ spec.email = ['sverrir.steindorsson@allabrf.se']
10
+
11
+ spec.summary = 'A Ruby wrapper for the Klarna Checkout API.'
12
+ spec.description = 'The gem provides methods to fetch, create, capture, cancel and refund Klarna orders.'
13
+ spec.homepage = 'https://www.rubygems.org/gems/klarna-checkout-v3'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
16
+
17
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://gitlab.com/qyre-ab/klarna-checkout-v3'
21
+ spec.metadata['changelog_uri'] = 'https://gitlab.com/qyre-ab/klarna-checkout-v3/CHANGELOG.md'
22
+
23
+ # Specify which files should be added to the gem when it is released.
24
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
26
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ spec.add_dependency 'faraday'
33
+
34
+ spec.add_development_dependency 'pry', '~> 0.13.1'
35
+ spec.add_development_dependency 'rspec', '~> 3.2'
36
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Gem dependencies
4
+ require 'faraday'
5
+ require 'json'
6
+ require 'set'
7
+
8
+ # Gem version
9
+ require 'klarna/checkout/version'
10
+
11
+ # Configuration
12
+ require 'klarna/checkout/configuration'
13
+
14
+ # Utilities
15
+ require 'klarna/checkout/api_utilities/connection_utilities'
16
+ require 'klarna/checkout/api_utilities/parse_response'
17
+
18
+ # Errors
19
+ require 'klarna/checkout/errors/order_validation_error'
20
+ require 'klarna/checkout/errors/order_capture_error'
21
+ require 'klarna/checkout/errors/order_cancel_error'
22
+ require 'klarna/checkout/errors/order_refund_error'
23
+ require 'klarna/checkout/errors/configuration_error'
24
+ require 'klarna/checkout/errors/order_not_found_error'
25
+
26
+ # Operations
27
+ require 'klarna/checkout/operations/acknowledge'
28
+ require 'klarna/checkout/operations/capture'
29
+ require 'klarna/checkout/operations/cancel'
30
+ require 'klarna/checkout/operations/create'
31
+ require 'klarna/checkout/operations/fetch'
32
+ require 'klarna/checkout/operations/refund'
33
+ require 'klarna/checkout/operations/create_recurring'
34
+
35
+ # Resources
36
+ require 'klarna/checkout/resources/authentication'
37
+ require 'klarna/checkout/resources/merchant_urls'
38
+
39
+ # Order validations
40
+ require 'klarna/checkout/validations/order_validations'
41
+
42
+ # The order class
43
+ require 'klarna/checkout/order'
44
+
45
+ # For configuration
46
+ module Klarna
47
+ module Checkout
48
+ class << self
49
+ attr_writer :configuration
50
+
51
+ def configuration
52
+ @configuration ||= Klarna::Checkout::Configuration.new
53
+ end
54
+
55
+ def configure
56
+ yield(configuration)
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module ApiUtilities
6
+ module ConnectionUtilities
7
+ KLARNA_SANDBOX_URL = 'https://api.playground.klarna.com'
8
+ KLARNA_PRODUCTION_URL = 'https://api.klarna.com'
9
+
10
+ def host
11
+ return KLARNA_PRODUCTION_URL if Klarna::Checkout.configuration.environment == 'production'
12
+
13
+ KLARNA_SANDBOX_URL
14
+ end
15
+
16
+ def https_connection
17
+ @https_connection ||= Faraday.new(url: host)
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module ApiUtilities
6
+ module ParseResponse
7
+ def parse_response(data)
8
+ @klarna_response = OpenStruct.new(JSON.parse(data))
9
+ @payment_form = @klarna_response.html_snippet
10
+ @reference = @klarna_response.order_id
11
+ @status = @klarna_response.status
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,116 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ class Configuration
6
+ attr_writer :purchase_country,
7
+ :purchase_currency,
8
+ :locale,
9
+ :terms_uri,
10
+ :checkout_uri,
11
+ :confirmation_uri,
12
+ :push_uri,
13
+ :environment,
14
+ :user_id,
15
+ :passcode
16
+
17
+ def initialize
18
+ @purchase_country = nil
19
+ @purchase_currency = nil
20
+ @locale = nil
21
+ @terms_uri = nil
22
+ @checkout_uri = nil
23
+ @confirmation_uri = nil
24
+ @push_uri = nil
25
+ @environment = nil
26
+ @user_id = nil
27
+ @passcode = nil
28
+ @checkout_url = nil
29
+ end
30
+
31
+ def purchase_country
32
+ unless @purchase_country
33
+ raise Klarna::Checkout::Errors::ConfigurationError.new('purchase_country', 'missing_configuration_item')
34
+ end
35
+
36
+ @purchase_country
37
+ end
38
+
39
+ def purchase_currency
40
+ unless @purchase_currency
41
+ raise Klarna::Checkout::Errors::ConfigurationError.new('purchase_currency', 'missing_configuration_item')
42
+ end
43
+
44
+ @purchase_currency
45
+ end
46
+
47
+ def locale
48
+ raise Klarna::Checkout::Errors::ConfigurationError.new('locale', 'missing_configuration_item') unless @locale
49
+
50
+ @locale
51
+ end
52
+
53
+ def terms_uri
54
+ unless @terms_uri
55
+ raise Klarna::Checkout::Errors::ConfigurationError.new('terms_uri', 'missing_configuration_item')
56
+ end
57
+
58
+ @terms_uri
59
+ end
60
+
61
+ def checkout_uri
62
+ unless @checkout_uri
63
+ raise Klarna::Checkout::Errors::ConfigurationError.new('checkout_uri', 'missing_configuration_item')
64
+ end
65
+
66
+ @checkout_uri
67
+ end
68
+
69
+ def confirmation_uri
70
+ unless @confirmation_uri
71
+ raise Klarna::Checkout::Errors::ConfigurationError.new('confirmation_uri', 'missing_configuration_item')
72
+ end
73
+
74
+ @confirmation_uri
75
+ end
76
+
77
+ def push_uri
78
+ unless @push_uri
79
+ raise Klarna::Checkout::Errors::ConfigurationError.new('push_uri', 'missing_configuration_item')
80
+ end
81
+
82
+ @push_uri
83
+ end
84
+
85
+ def environment
86
+ unless @environment
87
+ raise Klarna::Checkout::Errors::ConfigurationError.new('environment', 'missing_configuration_item')
88
+ end
89
+
90
+ @environment
91
+ end
92
+
93
+ def user_id
94
+ raise Klarna::Checkout::Errors::ConfigurationError.new('user_id', 'missing_configuration_item') unless @user_id
95
+
96
+ @user_id
97
+ end
98
+
99
+ def passcode
100
+ unless @passcode
101
+ raise Klarna::Checkout::Errors::ConfigurationError.new('passcode', 'missing_configuration_item')
102
+ end
103
+
104
+ @passcode
105
+ end
106
+
107
+ def checkout_url
108
+ unless @checkout_url
109
+ raise Klarna::Checkout::Errors::ConfigurationError.new('checkout_url', 'missing_configuration_item')
110
+ end
111
+
112
+ @checkout_url
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Errors
6
+ class ConfigurationError < StandardError
7
+ def initialize(message, exception_type)
8
+ @exception_type = exception_type
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Errors
6
+ class OrderCancelError < StandardError
7
+ def initialize(message, exception_type)
8
+ @exception_type = exception_type
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Errors
6
+ class OrderCaptureError < StandardError
7
+ def initialize(message, exception_type)
8
+ @exception_type = exception_type
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Errors
6
+ class OrderNotFoundError < StandardError
7
+ def initialize(message, exception_type)
8
+ @exception_type = exception_type
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Errors
6
+ class OrderRefundError < StandardError
7
+ def initialize(message, exception_type)
8
+ @exception_type = exception_type
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Errors
6
+ class OrderValidationError < StandardError
7
+ def initialize(message, exception_type)
8
+ @exception_type = exception_type
9
+ super(message)
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module Acknowledge
7
+ def acknowledge_order(reference)
8
+ request = Faraday.new(host)
9
+ request.headers['Authorization'] = authorization
10
+ request.post("/ordermanagement/v1/orders/#{reference}/acknowledge")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module Cancel
7
+ def cancel_order
8
+ response = https_connection.post do |req|
9
+ req.url "/ordermanagement/v1/orders/#{@reference}/cancel"
10
+ req.options.timeout = 10
11
+
12
+ req.headers['Authorization'] = authorization
13
+ req.headers['Content-Type'] = 'application/json'
14
+ end
15
+
16
+ unless response.status == 204
17
+ raise Klarna::Checkout::Errors::OrderCancelError.new(@status, 'cancel_not_allowed')
18
+ end
19
+
20
+ @status = 'CANCELLED'
21
+ response
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module Capture
7
+ def capture_order(amount = nil)
8
+ response = execute_capture_request(amount)
9
+
10
+ unless response.status == 201
11
+ raise Klarna::Checkout::Errors::OrderCaptureError.new(@status, 'capture_not_allowed')
12
+ end
13
+
14
+ @status = 'CAPTURED'
15
+ response
16
+ end
17
+
18
+ private
19
+
20
+ def execute_capture_request(amount)
21
+ https_connection.post do |req|
22
+ req.url "/ordermanagement/v1/orders/#{@reference}/captures"
23
+ req.options.timeout = 10
24
+
25
+ req.headers['Authorization'] = authorization
26
+ req.headers['Content-Type'] = 'application/json'
27
+
28
+ req.body = {
29
+ 'captured_amount': amount.nil? ? @klarna_response['order_amount'] : amount,
30
+ 'order_lines': @items
31
+ }.to_json
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module Create
7
+ CREATE_ORDER_PATH = '/checkout/v3/orders/'
8
+
9
+ def create(header, items)
10
+ payload = header.merge({
11
+ order_lines: items,
12
+ merchant_urls: merchant_urls
13
+ })
14
+
15
+ payload.merge!({ recurring: true }) if @recurring
16
+ payload.merge!({ customer: @customer }) if @customer
17
+ payload.merge!({ options: @options }) if @options
18
+ @api_order = payload
19
+
20
+ parse_response(
21
+ request(payload.to_json)
22
+ )
23
+ end
24
+
25
+ private
26
+
27
+ def request(payload)
28
+ https_connection.post do |req|
29
+ req.url CREATE_ORDER_PATH
30
+ req.options.timeout = 10
31
+
32
+ req.headers['Authorization'] = authorization
33
+ req.headers['Content-Type'] = 'application/json'
34
+ req.body = payload
35
+ end.body
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module CreateRecurring
7
+ # args contain the following:
8
+ # [{order_lines}], order_amount, order_tax_amount, purchase_currency, locale, recurring_token
9
+ def create_recurring_order(**args)
10
+ payload = {
11
+ 'locale': args[:locale],
12
+ 'order_lines': args[:order_lines],
13
+ 'order_amount': args[:order_amount],
14
+ 'order_tax_amount': args[:order_tax_amount],
15
+ 'purchase_currency': args[:purchase_currency],
16
+ 'auto_capture': true
17
+ }
18
+
19
+ JSON.parse(request(payload.to_json, args[:recurring_token]))
20
+ end
21
+
22
+ private
23
+
24
+ def request(payload, recurring_token)
25
+ https_connection.post do |req|
26
+ req.url "/customer-token/v1/tokens/#{recurring_token}/order"
27
+ req.options.timeout = 10
28
+
29
+ req.headers['Authorization'] = authorization
30
+ req.headers['Content-Type'] = 'application/json'
31
+ req.body = payload
32
+ end.body
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module Fetch
7
+ include Klarna::Checkout::ApiUtilities::ConnectionUtilities
8
+ PATH_CHECKOUT = '/checkout/v3/orders/'
9
+ PATH_CONFIRMED = '/ordermanagement/v1/orders/'
10
+
11
+ # To fetch order during checkout stage
12
+ def fetch_checkout_order(ref)
13
+ fetch_order(PATH_CHECKOUT, ref)
14
+ end
15
+
16
+ # To fetch order after checkout stage
17
+ def fetch_confirmed_order(ref)
18
+ fetch_order(PATH_CONFIRMED, ref)
19
+ end
20
+
21
+ private
22
+
23
+ def fetch_order(path, ref)
24
+ response = execute_fetch_request(path, ref)
25
+ # Raise error if order is not found
26
+ unless response.status == 200
27
+ raise Klarna::Checkout::Errors::OrderNotFoundError.new("Unable to fetch order: #{ref}", 'order_not_found')
28
+ end
29
+
30
+ response_body = JSON.parse(response.body)
31
+ order_header = {
32
+ purchase_country: response_body['purchase_country'],
33
+ purchase_currency: response_body['purchase_currency'],
34
+ locale: response_body['locale'],
35
+ order_amount: response_body['order_amount'],
36
+ order_tax_amount: response_body['order_tax_amount']
37
+ }
38
+
39
+ build_order(order_header, response_body)
40
+ end
41
+
42
+ def execute_fetch_request(path, ref)
43
+ https_connection.get do |req|
44
+ req.url "#{path}#{ref}"
45
+ req.options.timeout = 10
46
+
47
+ req.headers['Authorization'] = authorization
48
+ req.headers['Content-Type'] = 'application/json'
49
+ end
50
+ end
51
+
52
+ def build_order(header, body)
53
+ order = Klarna::Checkout::Order.new(
54
+ header: header,
55
+ items: body['order_lines']
56
+ )
57
+
58
+ order.status = body['status']
59
+ order.reference = body['order_id']
60
+ order.klarna_response = body
61
+ order.recurring = (body['recurring'] == true)
62
+
63
+ order
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Operations
6
+ module Refund
7
+ def refund_order(amount = nil)
8
+ response = execute_refund_request(amount)
9
+
10
+ unless response.status == 201
11
+ raise Klarna::Checkout::Errors::OrderRefundError.new(@status, 'refund_not_allowed')
12
+ end
13
+
14
+ @status = 'REFUNDED'
15
+ response
16
+ end
17
+
18
+ private
19
+
20
+ def execute_refund_request(amount)
21
+ https_connection.post do |req|
22
+ req.url "/ordermanagement/v1/orders/#{@reference}/refunds"
23
+ req.options.timeout = 10
24
+
25
+ req.headers['Authorization'] = authorization
26
+ req.headers['Content-Type'] = 'application/json'
27
+ req.body = {
28
+ 'refunded_amount': amount.nil? ? @klarna_response['order_amount'] : amount,
29
+ 'order_lines': @items
30
+ }.to_json
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,117 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ class Order
6
+ extend Klarna::Checkout::ApiUtilities::ParseResponse
7
+ extend Klarna::Checkout::Operations::Acknowledge
8
+ extend Klarna::Checkout::Operations::Fetch
9
+ extend Klarna::Checkout::Operations::CreateRecurring
10
+ extend Klarna::Checkout::Resources::Authentication
11
+
12
+ include Klarna::Checkout::ApiUtilities::ConnectionUtilities
13
+ include Klarna::Checkout::ApiUtilities::ParseResponse
14
+ include Klarna::Checkout::Validations::OrderValidations
15
+ include Klarna::Checkout::Operations::Create
16
+ include Klarna::Checkout::Operations::Capture
17
+ include Klarna::Checkout::Operations::Cancel
18
+ include Klarna::Checkout::Operations::Refund
19
+ include Klarna::Checkout::Resources::Authentication
20
+ include Klarna::Checkout::Resources::MerchantUrls
21
+
22
+ attr_accessor :status, :payment_form, :reference, :api_order, :klarna_response, :recurring
23
+
24
+ def self.acknowledge(ref)
25
+ acknowledge_order(ref)
26
+ end
27
+
28
+ # For creating an order, using a recurring_token
29
+ def self.create_recurring(**args)
30
+ if args[:recurring_token].nil?
31
+ raise Klarna::Checkout::Errors::OrderValidationError.new('Argument missing', 'missing_recurring_token')
32
+ end
33
+
34
+ create_recurring_order(args)
35
+ end
36
+
37
+ # Returns an instance of the order
38
+ def self.find(ref)
39
+ fetch_confirmed_order(ref)
40
+ end
41
+
42
+ # Same as find but to be used during checkout stage
43
+ def self.find_checkout(ref)
44
+ fetch_checkout_order(ref)
45
+ end
46
+
47
+ def initialize(header:, items:, recurring: false, **args)
48
+ @header = header
49
+ @items = items
50
+ @recurring = recurring
51
+ @customer = args[:customer].nil? ? {} : args[:customer]
52
+ @options = args[:options]
53
+ @checkout_url = args[:checkout_url].nil? ? Klarna::Checkout.configuration.checkout_uri : args[:checkout_url]
54
+ @terms_url = args[:terms_url].nil? ? Klarna::Checkout.configuration.terms_uri : args[:terms_url]
55
+ end
56
+
57
+ # Creates an order
58
+ # Returns prepolulated order object based on Klarna API response
59
+ def execute
60
+ add_defaults
61
+ validate
62
+ create(@header, @items)
63
+ end
64
+
65
+ #
66
+ # Existing order methods
67
+ #
68
+
69
+ #
70
+ # Captures the order through Klarna API
71
+ def capture
72
+ unless @status == 'AUTHORIZED'
73
+ raise Klarna::Checkout::Errors::OrderCaptureError.new(@status, 'capture_not_allowed')
74
+ end
75
+
76
+ capture_order
77
+ end
78
+
79
+ # Cancels the order through Klarna API
80
+ def cancel
81
+ unless @status == 'AUTHORIZED'
82
+ raise Klarna::Checkout::Errors::OrderCancelError.new(@status, 'cancel_not_allowed')
83
+ end
84
+
85
+ cancel_order
86
+ end
87
+
88
+ # Refunds the order through Klarna API
89
+ def refund
90
+ raise Klarna::Checkout::Errors::OrderRefundError.new(@status, 'refund_not_allowed') unless @status == 'CAPTURED'
91
+
92
+ refund_order
93
+ end
94
+
95
+ private
96
+
97
+ def add_defaults
98
+ @header.merge!(
99
+ {
100
+ purchase_country: Klarna::Checkout.configuration.purchase_country,
101
+ purchase_currency: Klarna::Checkout.configuration.purchase_currency,
102
+ locale: Klarna::Checkout.configuration.locale
103
+ }
104
+ )
105
+ end
106
+
107
+ def validate
108
+ header_keys_existance
109
+ item_keys_existance
110
+ amount_validation
111
+ tax_amount_validation
112
+ total_amount_validation
113
+ total_tax_amount_validation
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Resources
6
+ module Authentication
7
+ def authorization
8
+ uid = Klarna::Checkout.configuration.user_id
9
+ pass = Klarna::Checkout.configuration.passcode
10
+
11
+ encode_base64(uid, pass)
12
+ end
13
+
14
+ private
15
+
16
+ def encode_base64(username, password)
17
+ ['Basic', Base64.encode64("#{username}:#{password}").chomp].join(' ')
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Resources
6
+ module MerchantUrls
7
+ def merchant_urls
8
+ {
9
+ push: Klarna::Checkout.configuration.push_uri,
10
+ terms: @terms_url,
11
+ confirmation: Klarna::Checkout.configuration.confirmation_uri,
12
+ checkout: @checkout_url
13
+ }
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,71 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ module Validations
6
+ module OrderValidations
7
+ REQUIRED_ORDER_KEYS = %i[purchase_country purchase_currency locale order_amount order_tax_amount].freeze
8
+ REQUIRED_ITEM_KEYS = %i[type name quantity unit_price tax_rate total_amount total_tax_amount].freeze
9
+
10
+ def header_keys_existance
11
+ missing_keys = REQUIRED_ORDER_KEYS - @header.keys
12
+ return true if missing_keys.empty?
13
+
14
+ raise Klarna::Checkout::Errors::OrderValidationError.new(missing_keys, 'missing_order_keys')
15
+ end
16
+
17
+ def item_keys_existance
18
+ @items.map(&:keys).each do |item_keys|
19
+ missing_keys = REQUIRED_ITEM_KEYS - item_keys
20
+
21
+ unless missing_keys.empty?
22
+ raise Klarna::Checkout::Errors::OrderValidationError.new(missing_keys, 'missing_item_keys')
23
+ end
24
+ end
25
+
26
+ true
27
+ end
28
+
29
+ # 1. order_amount == sum of all items total_amounts
30
+ def amount_validation
31
+ return if @items.map { |i| i[:total_amount] }.sum == @header[:order_amount]
32
+
33
+ raise Klarna::Checkout::Errors::OrderValidationError.new(
34
+ 'inconsistent_values', 'order_amount_not_matching_total_amounts'
35
+ )
36
+ end
37
+
38
+ # 2. order_tax_amount == sum of all items total_tax_amounts
39
+ def tax_amount_validation
40
+ return if @items.map { |i| i[:total_tax_amount] }.sum == @header[:order_tax_amount]
41
+
42
+ raise Klarna::Checkout::Errors::OrderValidationError.new(
43
+ 'inconsistent_values', 'total_tax_amount_not_matching_total_tax_amounts'
44
+ )
45
+ end
46
+
47
+ # 3. [loop through all items] -> unit_price * quantity == total_amount
48
+ def total_amount_validation
49
+ @items.each do |i|
50
+ next if i[:quantity] * (i[:unit_price] - i[:total_discount_amount].to_i) == i[:total_amount]
51
+
52
+ raise Klarna::Checkout::Errors::OrderValidationError.new(
53
+ 'inconsistent_order_line_totals', 'total_line_amount_not_matching_qty_times_unit_price'
54
+ )
55
+ end
56
+ end
57
+
58
+ # 4. [loop through all items] -> total_tax_amount / (total_amount - total_tax_amount) == tax_rate * 10_000
59
+ def total_tax_amount_validation
60
+ @items.each do |i|
61
+ next if i[:total_tax_amount] * 100 / (i[:total_amount] - i[:total_tax_amount]) == i[:tax_rate] / 100
62
+
63
+ raise Klarna::Checkout::Errors::OrderValidationError.new(
64
+ 'inconsistent_order_line_total_tax', 'line_total_tax_amount_not_matching_tax_rate'
65
+ )
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Klarna
4
+ module Checkout
5
+ VERSION = '0.1.1'
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: klarna-checkout-v3
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Sverrir Steindorsson
8
+ - Mike Eyrikh
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2021-05-03 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: pry
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: 0.13.1
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: 0.13.1
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '3.2'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '3.2'
56
+ description: The gem provides methods to fetch, create, capture, cancel and refund
57
+ Klarna orders.
58
+ email:
59
+ - sverrir.steindorsson@allabrf.se
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".gitlab-ci.yml"
66
+ - ".rspec"
67
+ - ".rubocop.yml"
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE.txt
71
+ - README.md
72
+ - Rakefile
73
+ - bin/console
74
+ - bin/setup
75
+ - klarna-checkout.gemspec
76
+ - lib/.DS_Store
77
+ - lib/klarna/.DS_Store
78
+ - lib/klarna/checkout.rb
79
+ - lib/klarna/checkout/api_utilities/connection_utilities.rb
80
+ - lib/klarna/checkout/api_utilities/parse_response.rb
81
+ - lib/klarna/checkout/configuration.rb
82
+ - lib/klarna/checkout/errors/configuration_error.rb
83
+ - lib/klarna/checkout/errors/order_cancel_error.rb
84
+ - lib/klarna/checkout/errors/order_capture_error.rb
85
+ - lib/klarna/checkout/errors/order_not_found_error.rb
86
+ - lib/klarna/checkout/errors/order_refund_error.rb
87
+ - lib/klarna/checkout/errors/order_validation_error.rb
88
+ - lib/klarna/checkout/operations/acknowledge.rb
89
+ - lib/klarna/checkout/operations/cancel.rb
90
+ - lib/klarna/checkout/operations/capture.rb
91
+ - lib/klarna/checkout/operations/create.rb
92
+ - lib/klarna/checkout/operations/create_recurring.rb
93
+ - lib/klarna/checkout/operations/fetch.rb
94
+ - lib/klarna/checkout/operations/refund.rb
95
+ - lib/klarna/checkout/order.rb
96
+ - lib/klarna/checkout/resources/authentication.rb
97
+ - lib/klarna/checkout/resources/merchant_urls.rb
98
+ - lib/klarna/checkout/validations/order_validations.rb
99
+ - lib/klarna/checkout/version.rb
100
+ homepage: https://www.rubygems.org/gems/klarna-checkout-v3
101
+ licenses:
102
+ - MIT
103
+ metadata:
104
+ homepage_uri: https://www.rubygems.org/gems/klarna-checkout-v3
105
+ source_code_uri: https://gitlab.com/qyre-ab/klarna-checkout-v3
106
+ changelog_uri: https://gitlab.com/qyre-ab/klarna-checkout-v3/CHANGELOG.md
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 2.5.0
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.0.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: A Ruby wrapper for the Klarna Checkout API.
126
+ test_files: []