klarna-checkout 0.0.3 → 0.0.4
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 +4 -4
- data/README.md +2 -1
- data/lib/klarna/checkout/client.rb +25 -0
- data/lib/klarna/checkout/exceptions.rb +20 -0
- data/lib/klarna/checkout/version.rb +1 -1
- data/spec/lib/klarna/checkout/client_spec.rb +103 -3
- data/spec/lib/klarna/checkout/version_spec.rb +1 -1
- metadata +4 -37
- data/.gitignore +0 -17
- data/.rspec +0 -1
- data/Gemfile +0 -4
- data/Guardfile +0 -5
- data/Rakefile +0 -1
- data/klarna-checkout.gemspec +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 99dbc87a1758ccba7b8707c8afa2fbcb5a4d5271
|
4
|
+
data.tar.gz: b5f08fb9d850af9cb1dd82dc8c9d4d3084d236b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7adf77484ebe60253d5c2cad1b800edaea3ebc25d5af7aa23720367b28df80f7acc92f11fe71b3bdb2f2de72073ba919a971a31a40bf7e6e6eddccb1e7e413f9
|
7
|
+
data.tar.gz: 2c8122a05a62840e2f2ab2aa06923b0c9c8a37e3249f7192e7e07d9d4faa25d21de9294c3268803d55f2907282270df3de2813fb048bd16de94c1cb024962543
|
data/README.md
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
[](https://magnum.travis-ci.com/Skalar/klarna-checkout-ruby) [](https://codeclimate.com/github/Skalar/klarna-checkout-ruby)
|
2
|
+
|
1
3
|
# Klarna Checkout
|
2
4
|
|
3
5
|
Unofficial Ruby Wrapper for Klarnas Checkout Rest API.
|
@@ -59,7 +61,6 @@ order = client.read_order("1234ABCD")
|
|
59
61
|
|
60
62
|
## TODO
|
61
63
|
|
62
|
-
* Raise exceptions on errors from API
|
63
64
|
* Validation of objects according to documentation
|
64
65
|
* Respect readonly attributes
|
65
66
|
|
@@ -2,6 +2,8 @@ require 'digest/sha2'
|
|
2
2
|
require 'base64'
|
3
3
|
require 'faraday'
|
4
4
|
|
5
|
+
require 'klarna/checkout/exceptions'
|
6
|
+
|
5
7
|
module Klarna
|
6
8
|
module Checkout
|
7
9
|
class Client
|
@@ -48,6 +50,8 @@ module Klarna
|
|
48
50
|
|
49
51
|
req.body = request_body
|
50
52
|
end
|
53
|
+
handle_status_code(response.status)
|
54
|
+
|
51
55
|
order.id = response.headers['Location'].split('/').last
|
52
56
|
order
|
53
57
|
end
|
@@ -60,6 +64,8 @@ module Klarna
|
|
60
64
|
req.headers['Accept'] = 'application/vnd.klarna.checkout.aggregated-order-v2+json'
|
61
65
|
req.headers['Accept-Encoding'] = ''
|
62
66
|
end
|
67
|
+
handle_status_code(response.status)
|
68
|
+
|
63
69
|
Order.new(JSON.parse(response.body))
|
64
70
|
end
|
65
71
|
|
@@ -70,6 +76,25 @@ module Klarna
|
|
70
76
|
Digest::SHA256.base64digest(payload)
|
71
77
|
end
|
72
78
|
|
79
|
+
def handle_status_code(code, &blk)
|
80
|
+
case Integer(code)
|
81
|
+
when 200, 201
|
82
|
+
yield if block_given?
|
83
|
+
when 401
|
84
|
+
raise Klarna::Checkout::UnauthorizedException.new
|
85
|
+
when 403
|
86
|
+
raise Klarna::Checkout::ForbiddenException.new
|
87
|
+
when 404
|
88
|
+
raise Klarna::Checkout::NotFoundException.new
|
89
|
+
when 405
|
90
|
+
raise Klarna::Checkout::MethodNotAllowedException.new
|
91
|
+
when 406
|
92
|
+
raise Klarna::Checkout::NotAcceptableException.new
|
93
|
+
when 415
|
94
|
+
raise Klarna::Checkout::UnsupportedMediaTypeException.new
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
73
98
|
private
|
74
99
|
|
75
100
|
def https_connection
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Klarna::Checkout::Exception < Exception
|
2
|
+
end
|
3
|
+
|
4
|
+
class Klarna::Checkout::UnauthorizedException < Klarna::Checkout::Exception
|
5
|
+
end
|
6
|
+
|
7
|
+
class Klarna::Checkout::ForbiddenException < Klarna::Checkout::Exception
|
8
|
+
end
|
9
|
+
|
10
|
+
class Klarna::Checkout::NotFoundException < Klarna::Checkout::Exception
|
11
|
+
end
|
12
|
+
|
13
|
+
class Klarna::Checkout::MethodNotAllowedException < Klarna::Checkout::Exception
|
14
|
+
end
|
15
|
+
|
16
|
+
class Klarna::Checkout::NotAcceptableException < Klarna::Checkout::Exception
|
17
|
+
end
|
18
|
+
|
19
|
+
class Klarna::Checkout::UnsupportedMediaTypeException < Klarna::Checkout::Exception
|
20
|
+
end
|
@@ -51,10 +51,12 @@ describe Klarna::Checkout::Client do
|
|
51
51
|
|
52
52
|
let(:order) { double('Order', to_json: JSON.generate({ foo: "bar" }), :id= => true) }
|
53
53
|
|
54
|
-
|
54
|
+
before(:each) do
|
55
55
|
stub_request(:post, "https://checkout.testdrive.klarna.com/checkout/orders")
|
56
|
-
.to_return(headers: { 'Location' => 'https://checkout.testdrive.klarna.com/checkout/orders/143F7BC0A1090B11C39E7220000' })
|
56
|
+
.to_return(headers: { 'Location' => 'https://checkout.testdrive.klarna.com/checkout/orders/143F7BC0A1090B11C39E7220000' }, status: 201)
|
57
|
+
end
|
57
58
|
|
59
|
+
it "sends a json representation of the object to Klarna" do
|
58
60
|
subject.create_order(order)
|
59
61
|
|
60
62
|
assert_requested :post, "https://checkout.testdrive.klarna.com/checkout/orders",
|
@@ -67,15 +69,108 @@ describe Klarna::Checkout::Client do
|
|
67
69
|
:body => JSON.generate({ foo: "bar" }),
|
68
70
|
:times => 1
|
69
71
|
end
|
72
|
+
|
73
|
+
it "checks the response" do
|
74
|
+
subject.should receive(:handle_status_code).with(201)
|
75
|
+
subject.create_order(order)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe "#handle_status_code" do
|
80
|
+
context "with 200" do
|
81
|
+
it "just yields the block given" do
|
82
|
+
yielded = false
|
83
|
+
subject.handle_status_code 200 do
|
84
|
+
yielded = true
|
85
|
+
end
|
86
|
+
yielded.should be_true
|
87
|
+
end
|
88
|
+
|
89
|
+
context "without block" do
|
90
|
+
it "does nothing" do
|
91
|
+
expect {
|
92
|
+
subject.handle_status_code(200)
|
93
|
+
}.to_not raise_error
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "with 201" do
|
99
|
+
it "just yields the block given" do
|
100
|
+
yielded = false
|
101
|
+
subject.handle_status_code 201 do
|
102
|
+
yielded = true
|
103
|
+
end
|
104
|
+
yielded.should be_true
|
105
|
+
end
|
106
|
+
|
107
|
+
context "without block" do
|
108
|
+
it "does nothing" do
|
109
|
+
expect {
|
110
|
+
subject.handle_status_code(201)
|
111
|
+
}.to_not raise_error
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
context "with 401" do
|
117
|
+
it "raises a Klarna::Checkout::UnauthorizedException" do
|
118
|
+
expect {
|
119
|
+
subject.handle_status_code(401)
|
120
|
+
}.to raise_error(Klarna::Checkout::UnauthorizedException)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
context "with 403" do
|
125
|
+
it "raises a Klarna::Checkout::ForbiddenException" do
|
126
|
+
expect {
|
127
|
+
subject.handle_status_code(403)
|
128
|
+
}.to raise_error(Klarna::Checkout::ForbiddenException)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
context "with 404" do
|
133
|
+
it "raises a Klarna::Checkout::NotFoundException" do
|
134
|
+
expect {
|
135
|
+
subject.handle_status_code(404)
|
136
|
+
}.to raise_error(Klarna::Checkout::NotFoundException)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
context "with 405" do
|
141
|
+
it "raises a Klarna::Checkout::MethodNotAllowedException" do
|
142
|
+
expect {
|
143
|
+
subject.handle_status_code(405)
|
144
|
+
}.to raise_error(Klarna::Checkout::MethodNotAllowedException)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
context "with 406" do
|
149
|
+
it "raises a Klarna::Checkout::NotAcceptableException" do
|
150
|
+
expect {
|
151
|
+
subject.handle_status_code(406)
|
152
|
+
}.to raise_error(Klarna::Checkout::NotAcceptableException)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
context "with 415" do
|
157
|
+
it "raises a Klarna::Checkout::UnsupportedMediaTypeException" do
|
158
|
+
expect {
|
159
|
+
subject.handle_status_code(415)
|
160
|
+
}.to raise_error(Klarna::Checkout::UnsupportedMediaTypeException)
|
161
|
+
end
|
162
|
+
end
|
70
163
|
end
|
71
164
|
|
72
165
|
describe "#read_order" do
|
73
166
|
subject { described_class.new({shared_secret: 'foobar'}) }
|
74
167
|
|
75
|
-
|
168
|
+
before(:each) do
|
76
169
|
stub_request(:get, "https://checkout.testdrive.klarna.com/checkout/orders/143F7BC0A1090B11C39E7220000")
|
77
170
|
.to_return(body: JSON.generate({ id: "143F7BC0A1090B11C39E7220000" }))
|
171
|
+
end
|
78
172
|
|
173
|
+
it "uses the correct endpoint at klarna" do
|
79
174
|
subject.read_order('143F7BC0A1090B11C39E7220000')
|
80
175
|
|
81
176
|
assert_requested :get, "https://checkout.testdrive.klarna.com/checkout/orders/143F7BC0A1090B11C39E7220000",
|
@@ -86,6 +181,11 @@ describe Klarna::Checkout::Client do
|
|
86
181
|
},
|
87
182
|
:times => 1
|
88
183
|
end
|
184
|
+
|
185
|
+
it "checks the response" do
|
186
|
+
subject.should receive(:handle_status_code).with(200)
|
187
|
+
subject.read_order('143F7BC0A1090B11C39E7220000')
|
188
|
+
end
|
89
189
|
end
|
90
190
|
|
91
191
|
describe "#sign_payload" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: klarna-checkout
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Theodor Tonum
|
@@ -24,20 +24,6 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: activemodel
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - '>='
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0'
|
34
|
-
type: :runtime
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - '>='
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0'
|
41
27
|
- !ruby/object:Gem::Dependency
|
42
28
|
name: bundler
|
43
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,20 +122,6 @@ dependencies:
|
|
136
122
|
- - '>='
|
137
123
|
- !ruby/object:Gem::Version
|
138
124
|
version: '0'
|
139
|
-
- !ruby/object:Gem::Dependency
|
140
|
-
name: shoulda-matchers
|
141
|
-
requirement: !ruby/object:Gem::Requirement
|
142
|
-
requirements:
|
143
|
-
- - '>='
|
144
|
-
- !ruby/object:Gem::Version
|
145
|
-
version: '0'
|
146
|
-
type: :development
|
147
|
-
prerelease: false
|
148
|
-
version_requirements: !ruby/object:Gem::Requirement
|
149
|
-
requirements:
|
150
|
-
- - '>='
|
151
|
-
- !ruby/object:Gem::Version
|
152
|
-
version: '0'
|
153
125
|
description: Ruby Wrapper for Klarna Checkout Rest API
|
154
126
|
email:
|
155
127
|
- theodor@tonum.no
|
@@ -157,15 +129,8 @@ executables: []
|
|
157
129
|
extensions: []
|
158
130
|
extra_rdoc_files: []
|
159
131
|
files:
|
160
|
-
- .gitignore
|
161
|
-
- .rspec
|
162
|
-
- Gemfile
|
163
|
-
- Guardfile
|
164
132
|
- LICENSE.txt
|
165
133
|
- README.md
|
166
|
-
- Rakefile
|
167
|
-
- klarna-checkout.gemspec
|
168
|
-
- lib/klarna/checkout.rb
|
169
134
|
- lib/klarna/checkout/address.rb
|
170
135
|
- lib/klarna/checkout/cart.rb
|
171
136
|
- lib/klarna/checkout/cart_item.rb
|
@@ -173,11 +138,13 @@ files:
|
|
173
138
|
- lib/klarna/checkout/concerns/has_many.rb
|
174
139
|
- lib/klarna/checkout/concerns/has_one.rb
|
175
140
|
- lib/klarna/checkout/customer.rb
|
141
|
+
- lib/klarna/checkout/exceptions.rb
|
176
142
|
- lib/klarna/checkout/gui.rb
|
177
143
|
- lib/klarna/checkout/merchant.rb
|
178
144
|
- lib/klarna/checkout/order.rb
|
179
145
|
- lib/klarna/checkout/resource.rb
|
180
146
|
- lib/klarna/checkout/version.rb
|
147
|
+
- lib/klarna/checkout.rb
|
181
148
|
- spec/acceptance/create_order_spec.rb
|
182
149
|
- spec/acceptance/read_order_spec.rb
|
183
150
|
- spec/fixtures/vcr_cassettes/create_order_spec.yml
|
@@ -190,7 +157,7 @@ files:
|
|
190
157
|
- spec/support/matchers/have_attribute.rb
|
191
158
|
- spec/support/matchers/have_many.rb
|
192
159
|
- spec/support/matchers/have_one.rb
|
193
|
-
homepage:
|
160
|
+
homepage: https://github.com/Skalar/klarna-checkout-ruby
|
194
161
|
licenses:
|
195
162
|
- MIT
|
196
163
|
metadata: {}
|
data/.gitignore
DELETED
data/.rspec
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
--color
|
data/Gemfile
DELETED
data/Guardfile
DELETED
data/Rakefile
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
require "bundler/gem_tasks"
|
data/klarna-checkout.gemspec
DELETED
@@ -1,32 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'klarna/checkout/version'
|
5
|
-
|
6
|
-
Gem::Specification.new do |spec|
|
7
|
-
spec.name = "klarna-checkout"
|
8
|
-
spec.version = Klarna::Checkout::VERSION
|
9
|
-
spec.authors = ["Theodor Tonum"]
|
10
|
-
spec.email = ["theodor@tonum.no"]
|
11
|
-
spec.description = %q{Ruby Wrapper for Klarna Checkout Rest API}
|
12
|
-
spec.summary = %q{...}
|
13
|
-
spec.homepage = ""
|
14
|
-
spec.license = "MIT"
|
15
|
-
|
16
|
-
spec.files = `git ls-files`.split($/)
|
17
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
-
spec.require_paths = ["lib"]
|
20
|
-
|
21
|
-
spec.add_dependency "faraday"
|
22
|
-
spec.add_dependency "activemodel"
|
23
|
-
|
24
|
-
spec.add_development_dependency "bundler", "~> 1.3"
|
25
|
-
spec.add_development_dependency "rake"
|
26
|
-
spec.add_development_dependency "rspec"
|
27
|
-
spec.add_development_dependency "guard"
|
28
|
-
spec.add_development_dependency "guard-rspec"
|
29
|
-
spec.add_development_dependency "webmock"
|
30
|
-
spec.add_development_dependency "vcr"
|
31
|
-
spec.add_development_dependency "shoulda-matchers"
|
32
|
-
end
|