klarna-checkout 0.0.5 → 0.0.6
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 +45 -1
- data/lib/klarna/checkout.rb +3 -1
- data/lib/klarna/checkout/cart.rb +12 -4
- data/lib/klarna/checkout/cart_item.rb +6 -3
- data/lib/klarna/checkout/client.rb +25 -1
- data/lib/klarna/checkout/configuration.rb +26 -0
- data/lib/klarna/checkout/exceptions.rb +3 -0
- data/lib/klarna/checkout/merchant.rb +7 -0
- data/lib/klarna/checkout/merchant_reference.rb +16 -0
- data/lib/klarna/checkout/order.rb +42 -8
- data/lib/klarna/checkout/resource.rb +27 -5
- data/lib/klarna/checkout/version.rb +1 -1
- data/lib/klarna/hash/deep_merge.rb +17 -0
- data/spec/acceptance/update_order_spec.rb +40 -0
- data/spec/fixtures/vcr_cassettes/update_order_spec-2.yml +47 -0
- data/spec/fixtures/vcr_cassettes/update_order_spec.yml +89 -0
- data/spec/lib/klarna/checkout/cart_spec.rb +0 -7
- data/spec/lib/klarna/checkout/client_spec.rb +85 -1
- data/spec/lib/klarna/checkout/merchant_reference_spec.rb +35 -0
- data/spec/lib/klarna/checkout/order_spec.rb +236 -9
- data/spec/lib/klarna/checkout/version_spec.rb +1 -1
- data/spec/lib/klarna/checkout_spec.rb +53 -0
- metadata +43 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e1b79981f5fb770e090d953d53a6dd120a08494e
|
4
|
+
data.tar.gz: 2085d55dc1ef5452eba54563ab92a8deac2af02c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 852dcf3da70a38b9f2d6c6f169c584ab65a4f5e1f66da2eda6dcbcff230df9c042c1a8495b49abefbff4e04b6ccf0609d5718a99e5298453e0ff38e6ade480c0
|
7
|
+
data.tar.gz: d1c22b98bf704686e5ced24abee60731a478ede2e7609a04bfeaea6a4389aec2f0a3a033861f0c29f185dabb4b0f07a12e7b57f18dbaf1c999d9764758de1ce9
|
data/README.md
CHANGED
@@ -23,11 +23,22 @@ Or install it yourself as:
|
|
23
23
|
```ruby
|
24
24
|
require 'klarna/checkout'
|
25
25
|
|
26
|
+
# a) Create a client with shared_secret directly
|
26
27
|
client = Klarna::Checkout::Client.new({
|
27
28
|
shared_secret: 'your-shared-secret',
|
28
29
|
environment: :test # or :production
|
29
30
|
})
|
30
31
|
|
32
|
+
# b) Create a client with shared_secret from module configuration
|
33
|
+
Klarna::Checkout.configure do |config|
|
34
|
+
config.shared_secret = 'your-shared-secret'
|
35
|
+
end
|
36
|
+
|
37
|
+
client = Klarna::Checkout::Client.new(environment: :test)
|
38
|
+
client.shared_secret
|
39
|
+
# => 'your-shared-secret'
|
40
|
+
|
41
|
+
|
31
42
|
# Initialize an order
|
32
43
|
order = Klarna::Checkout::Order.new({
|
33
44
|
purchase_country: 'NO',
|
@@ -51,12 +62,45 @@ order = Klarna::Checkout::Order.new({
|
|
51
62
|
}
|
52
63
|
})
|
53
64
|
|
65
|
+
|
54
66
|
# Create the order with Klarna
|
55
67
|
client.create_order(order)
|
56
|
-
order.id
|
68
|
+
order.id
|
69
|
+
# => ID of the order (no other attributes are updated)
|
70
|
+
|
57
71
|
|
58
72
|
# Read an order from Klarna
|
59
73
|
order = client.read_order("1234ABCD")
|
74
|
+
|
75
|
+
|
76
|
+
# Configuring some global variables
|
77
|
+
Klarna::Checkout.configure do |config|
|
78
|
+
config.shared_secret = 'your-shared-secret'
|
79
|
+
config.merchant_id = '12345'
|
80
|
+
config.default_country = 'NO'
|
81
|
+
config.default_currency = 'NOK'
|
82
|
+
end
|
83
|
+
|
84
|
+
|
85
|
+
# Instead of repeating yourself with supplying the same attributes for each
|
86
|
+
# order you can configure some default attributes
|
87
|
+
Klarna::Checkout::Order.defaults = {
|
88
|
+
purchase_country: 'NO',
|
89
|
+
purchase_currency: 'NOK',
|
90
|
+
locale: 'nb-no',
|
91
|
+
merchant: {
|
92
|
+
id: '1337',
|
93
|
+
terms_uri: 'http://www.example.com/terms',
|
94
|
+
checkout_uri: 'http://www.example.com/checkout',
|
95
|
+
confirmation_uri: 'http://www.example.com/confirmation_uri',
|
96
|
+
push_uri: 'http://www.example.com/push'
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
# All newly created orders will then have these attributes
|
101
|
+
order = Klarna::Checkout::Order.new
|
102
|
+
order.purchase_country
|
103
|
+
# => 'NO'
|
60
104
|
```
|
61
105
|
|
62
106
|
## TODO
|
data/lib/klarna/checkout.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require "klarna/checkout/version"
|
2
2
|
require "klarna/checkout/client"
|
3
|
+
require "klarna/checkout/configuration"
|
3
4
|
|
4
5
|
require "klarna/checkout/resource"
|
5
6
|
|
@@ -8,10 +9,11 @@ require "klarna/checkout/cart"
|
|
8
9
|
require "klarna/checkout/customer"
|
9
10
|
require "klarna/checkout/gui"
|
10
11
|
require "klarna/checkout/merchant"
|
12
|
+
require "klarna/checkout/merchant_reference"
|
11
13
|
require "klarna/checkout/order"
|
12
14
|
|
13
15
|
module Klarna
|
14
16
|
module Checkout
|
15
|
-
|
17
|
+
extend Klarna::Checkout::Configuration
|
16
18
|
end
|
17
19
|
end
|
data/lib/klarna/checkout/cart.rb
CHANGED
@@ -1,21 +1,29 @@
|
|
1
|
-
require
|
1
|
+
require 'active_model'
|
2
|
+
require 'klarna/checkout/cart_item'
|
2
3
|
|
3
4
|
module Klarna
|
4
5
|
module Checkout
|
5
6
|
class Cart < Resource
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
6
9
|
attr_accessor :total_price_including_tax, :total_price_excluding_tax,
|
7
10
|
:total_tax_amount
|
8
11
|
|
9
12
|
has_many :items, Klarna::Checkout::CartItem
|
10
13
|
|
14
|
+
validate :items_validation
|
15
|
+
|
11
16
|
def as_json
|
12
17
|
json_sanitize({
|
13
|
-
:total_price_including_tax => @total_price_including_tax,
|
14
|
-
:total_price_excluding_tax => @total_price_excluding_tax,
|
15
|
-
:total_tax_amount => @total_tax_amount,
|
16
18
|
:items => @items.map(&:as_json)
|
17
19
|
})
|
18
20
|
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def items_validation
|
25
|
+
errors.add(:items, :invalid) unless items.all?(&:valid?)
|
26
|
+
end
|
19
27
|
end
|
20
28
|
end
|
21
29
|
end
|
@@ -1,10 +1,16 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
1
3
|
module Klarna
|
2
4
|
module Checkout
|
3
5
|
class CartItem < Resource
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
4
8
|
attr_accessor :type, :ean, :reference, :name, :uri, :image_uri, :quantity,
|
5
9
|
:unit_price, :total_price_excluding_tax, :total_tax_amount,
|
6
10
|
:total_price_including_tax, :discount_rate, :tax_rate
|
7
11
|
|
12
|
+
validates_presence_of :reference, :name, :quantity, :unit_price, :tax_rate
|
13
|
+
|
8
14
|
def as_json
|
9
15
|
json_sanitize({
|
10
16
|
:type => @type,
|
@@ -15,9 +21,6 @@ module Klarna
|
|
15
21
|
:image_uri => @image_uri,
|
16
22
|
:quantity => @quantity,
|
17
23
|
:unit_price => @unit_price,
|
18
|
-
:total_price_excluding_tax => @total_price_excluding_tax,
|
19
|
-
:total_tax_amount => @total_tax_amount,
|
20
|
-
:total_price_including_tax => @total_price_including_tax,
|
21
24
|
:discount_rate => @discount_rate,
|
22
25
|
:tax_rate => @tax_rate
|
23
26
|
})
|
@@ -12,7 +12,8 @@ module Klarna
|
|
12
12
|
def initialize(args = {})
|
13
13
|
args.each do |(k,v)|
|
14
14
|
self.public_send("#{k}=", v)
|
15
|
-
end
|
15
|
+
end
|
16
|
+
self.shared_secret ||= Klarna::Checkout.shared_secret
|
16
17
|
end
|
17
18
|
|
18
19
|
VALID_ENVS = [:test, :production]
|
@@ -39,6 +40,8 @@ module Klarna
|
|
39
40
|
end
|
40
41
|
|
41
42
|
def create_order(order)
|
43
|
+
return false unless order.valid?
|
44
|
+
|
42
45
|
request_body = order.to_json
|
43
46
|
response = https_connection.post do |req|
|
44
47
|
req.url '/checkout/orders'
|
@@ -69,6 +72,25 @@ module Klarna
|
|
69
72
|
Order.new(JSON.parse(response.body))
|
70
73
|
end
|
71
74
|
|
75
|
+
def update_order(order)
|
76
|
+
return false unless order.valid?
|
77
|
+
|
78
|
+
request_body = order.to_json
|
79
|
+
response = https_connection.post do |req|
|
80
|
+
req.url "/checkout/orders/#{order.id}"
|
81
|
+
|
82
|
+
req.headers['Authorization'] = "Klarna #{sign_payload(request_body)}"
|
83
|
+
req.headers['Accept'] = 'application/vnd.klarna.checkout.aggregated-order-v2+json',
|
84
|
+
req.headers['Content-Type'] = 'application/vnd.klarna.checkout.aggregated-order-v2+json'
|
85
|
+
req.headers['Accept-Encoding'] = ''
|
86
|
+
|
87
|
+
req.body = request_body
|
88
|
+
end
|
89
|
+
handle_status_code(response.status)
|
90
|
+
|
91
|
+
Order.new(JSON.parse(response.body))
|
92
|
+
end
|
93
|
+
|
72
94
|
# Based on example from:
|
73
95
|
# http://developers.klarna.com/en/api-references-v1/klarna-checkout#authorization
|
74
96
|
def sign_payload(request_body = '')
|
@@ -92,6 +114,8 @@ module Klarna
|
|
92
114
|
raise Klarna::Checkout::NotAcceptableException.new
|
93
115
|
when 415
|
94
116
|
raise Klarna::Checkout::UnsupportedMediaTypeException.new
|
117
|
+
when 500
|
118
|
+
raise Klarna::Checkout::InternalServerErrorException.new
|
95
119
|
end
|
96
120
|
end
|
97
121
|
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Klarna
|
2
|
+
module Checkout
|
3
|
+
module Configuration
|
4
|
+
%w{shared_secret merchant_id default_country default_currency}.each do |var|
|
5
|
+
define_method("#{var}=") do |attr|
|
6
|
+
class_variable_set("@@#{var}".to_sym, attr)
|
7
|
+
end
|
8
|
+
|
9
|
+
define_method(var) do
|
10
|
+
class_variable_get("@@#{var}".to_sym) rescue nil
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def configure(&blk)
|
15
|
+
yield(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def reset_configuration!
|
19
|
+
self.shared_secret = nil
|
20
|
+
self.merchant_id = nil
|
21
|
+
self.default_country = nil
|
22
|
+
self.default_currency = nil
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -1,9 +1,16 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
1
3
|
module Klarna
|
2
4
|
module Checkout
|
3
5
|
class Merchant < Resource
|
6
|
+
include ActiveModel::Validations
|
7
|
+
|
4
8
|
attr_accessor :id, :terms_uri, :checkout_uri, :confirmation_uri,
|
5
9
|
:push_uri, :validation_uri
|
6
10
|
|
11
|
+
validates_presence_of :id, :terms_uri, :checkout_uri, :confirmation_uri,
|
12
|
+
:push_uri
|
13
|
+
|
7
14
|
def as_json
|
8
15
|
json_sanitize({
|
9
16
|
:id => @id,
|
@@ -1,24 +1,32 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'active_model'
|
2
3
|
|
3
4
|
module Klarna
|
4
5
|
module Checkout
|
5
6
|
class Order < Resource
|
7
|
+
include ActiveModel::Validations
|
8
|
+
|
6
9
|
attr_accessor :id, :status, :reference, :reservation, :started_at,
|
7
10
|
:completed_at, :created_at, :last_modified_at, :expires_at,
|
8
11
|
:locale
|
9
12
|
|
10
|
-
attr_accessor :
|
13
|
+
attr_accessor :purchase_country, :purchase_currency
|
14
|
+
|
15
|
+
has_one :merchant_reference, Klarna::Checkout::MerchantReference
|
16
|
+
has_one :billing_address, Klarna::Checkout::Address
|
17
|
+
has_one :shipping_address, Klarna::Checkout::Address
|
18
|
+
has_one :cart, Klarna::Checkout::Cart
|
19
|
+
has_one :customer, Klarna::Checkout::Customer
|
20
|
+
has_one :merchant, Klarna::Checkout::Merchant
|
21
|
+
has_one :gui, Klarna::Checkout::Gui
|
11
22
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
has_one :customer, Klarna::Checkout::Customer
|
16
|
-
has_one :merchant, Klarna::Checkout::Merchant
|
17
|
-
has_one :gui, Klarna::Checkout::Gui
|
23
|
+
validates_presence_of :purchase_country, :purchase_currency, :locale
|
24
|
+
validate :merchant_validation
|
25
|
+
validate :cart_validation
|
18
26
|
|
19
27
|
def as_json
|
20
28
|
json_sanitize({
|
21
|
-
:merchant_reference => @merchant_reference,
|
29
|
+
:merchant_reference => (@merchant_reference && @merchant_reference.as_json),
|
22
30
|
:purchase_country => @purchase_country,
|
23
31
|
:purchase_currency => @purchase_currency,
|
24
32
|
:locale => @locale,
|
@@ -26,6 +34,32 @@ module Klarna
|
|
26
34
|
:merchant => @merchant.as_json
|
27
35
|
})
|
28
36
|
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def merchant_validation
|
41
|
+
errors.add(:merchant, :invalid) unless merchant.valid?
|
42
|
+
end
|
43
|
+
|
44
|
+
def cart_validation
|
45
|
+
errors.add(:cart, :invalid) unless cart.valid?
|
46
|
+
end
|
47
|
+
|
48
|
+
class << self
|
49
|
+
def defaults
|
50
|
+
defaults = super
|
51
|
+
|
52
|
+
defaults = {
|
53
|
+
purchase_country: Klarna::Checkout.default_country,
|
54
|
+
purchase_currency: Klarna::Checkout.default_currency,
|
55
|
+
merchant: {
|
56
|
+
id: Klarna::Checkout.merchant_id
|
57
|
+
}
|
58
|
+
}.deep_merge(defaults)
|
59
|
+
|
60
|
+
defaults
|
61
|
+
end
|
62
|
+
end
|
29
63
|
end
|
30
64
|
end
|
31
65
|
end
|
@@ -1,4 +1,7 @@
|
|
1
1
|
require 'json'
|
2
|
+
require 'active_support/core_ext/hash/slice'
|
3
|
+
|
4
|
+
require 'klarna/hash/deep_merge'
|
2
5
|
require 'klarna/checkout/concerns/has_one'
|
3
6
|
require 'klarna/checkout/concerns/has_many'
|
4
7
|
|
@@ -9,19 +12,38 @@ module Klarna
|
|
9
12
|
extend HasMany
|
10
13
|
|
11
14
|
def initialize(args = {})
|
12
|
-
args.each_pair do |attr, value|
|
15
|
+
self.class.defaults.deep_merge(args).each_pair do |attr, value|
|
13
16
|
setter = "#{attr.to_s}="
|
14
17
|
self.send(setter, value) if respond_to?(setter)
|
15
18
|
end
|
16
19
|
end
|
17
20
|
|
18
|
-
def to_json
|
19
|
-
sanitized_json = json_sanitize(self.as_json)
|
21
|
+
def to_json(*keys)
|
22
|
+
sanitized_json = json_sanitize(self.as_json, keys)
|
20
23
|
JSON.generate(sanitized_json)
|
21
24
|
end
|
22
25
|
|
23
|
-
def json_sanitize(hash)
|
24
|
-
hash.reject { |k, v| v.nil? }
|
26
|
+
def json_sanitize(hash, keys = [])
|
27
|
+
hash.reject! { |k, v| v.nil? }
|
28
|
+
hash.slice!(*Array(keys)) if keys.any?
|
29
|
+
hash
|
30
|
+
end
|
31
|
+
|
32
|
+
class << self
|
33
|
+
def defaults=(hash)
|
34
|
+
if hash
|
35
|
+
raise ArgumentError.new unless hash.is_a? Hash
|
36
|
+
|
37
|
+
@defaults ||= {}
|
38
|
+
@defaults.deep_merge!(hash)
|
39
|
+
else
|
40
|
+
@defaults = {}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def defaults
|
45
|
+
@defaults || {}
|
46
|
+
end
|
25
47
|
end
|
26
48
|
end
|
27
49
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Borrowed from activesupport
|
2
|
+
|
3
|
+
unless Hash.respond_to?(:deep_merge)
|
4
|
+
class Hash
|
5
|
+
def deep_merge(other_hash)
|
6
|
+
dup.deep_merge!(other_hash)
|
7
|
+
end
|
8
|
+
|
9
|
+
def deep_merge!(other_hash)
|
10
|
+
other_hash.each_pair do |k,v|
|
11
|
+
tv = self[k]
|
12
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
13
|
+
end
|
14
|
+
self
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "updating existing order" do
|
4
|
+
around(:each) do |example|
|
5
|
+
VCR.use_cassette('update_order_spec') do
|
6
|
+
example.run
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
let(:client) do
|
11
|
+
Klarna::Checkout::Client.new \
|
12
|
+
shared_secret: 'dhd1CwWIVrZSp1O'
|
13
|
+
end
|
14
|
+
|
15
|
+
let(:order) { client.read_order('1445FB9ACFD90B11C39E7220000') }
|
16
|
+
|
17
|
+
describe "updating the order" do
|
18
|
+
context "after updating the merchant_reference and cart item quantity" do
|
19
|
+
before(:each) do
|
20
|
+
order.merchant_reference = {
|
21
|
+
orderid1: 'newreference'
|
22
|
+
}
|
23
|
+
order.cart.items[0].quantity = 50
|
24
|
+
|
25
|
+
client.update_order(order)
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "the order read from the api" do
|
29
|
+
subject do
|
30
|
+
VCR.use_cassette('update_order_spec-2') do
|
31
|
+
client.read_order('1445FB9ACFD90B11C39E7220000')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
specify { subject.merchant_reference.orderid1.should eq 'newreference' }
|
36
|
+
specify { subject.cart.items[0].quantity.should eq 50 }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://checkout.testdrive.klarna.com/checkout/orders/1445FB9ACFD90B11C39E7220000
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.9.0
|
12
|
+
Authorization:
|
13
|
+
- Klarna QewkVlXa+6xDGNlS6yCgnKFKAZcJ/HAL+ocIwktB2K0=
|
14
|
+
Accept:
|
15
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json
|
16
|
+
Accept-Encoding:
|
17
|
+
- ''
|
18
|
+
response:
|
19
|
+
status:
|
20
|
+
code: 200
|
21
|
+
message: OK
|
22
|
+
headers:
|
23
|
+
Server:
|
24
|
+
- MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
|
25
|
+
Pragma:
|
26
|
+
- no-cache
|
27
|
+
Date:
|
28
|
+
- Mon, 24 Feb 2014 10:57:12 GMT
|
29
|
+
Content-Type:
|
30
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json
|
31
|
+
Content-Length:
|
32
|
+
- '2120'
|
33
|
+
Cache-Control:
|
34
|
+
- max-age=0, no-cache, no-store
|
35
|
+
Connection:
|
36
|
+
- keep-alive
|
37
|
+
body:
|
38
|
+
encoding: UTF-8
|
39
|
+
string: '{"id":"1445FB9ACFD90B11C39E7220000","merchant_reference":{"orderid1":"newreference"},"purchase_country":"no","purchase_currency":"nok","locale":"nb-no","status":"checkout_incomplete","started_at":"2014-02-23T18:10:07+01:00","last_modified_at":"2014-02-23T18:10:07+01:00","cart":{"total_price_excluding_tax":26640,"total_tax_amount":6660,"total_price_including_tax":33300,"items":[{"reference":"1123581220325","name":"Widget","quantity":50,"unit_price":666,"tax_rate":2500,"discount_rate":0,"type":"physical","total_price_including_tax":33300,"total_price_excluding_tax":26640,"total_tax_amount":6660}]},"customer":{"type":"person"},"shipping_address":{"country":"no"},"billing_address":{"country":"no"},"gui":{"layout":"desktop","snippet":"<div
|
40
|
+
id=\"klarna-checkout-container\" style=\"overflow-x:\nhidden;\"><script type=\"text/javascript\">/*
|
41
|
+
<![CDATA[ */(function(w,k,i,d,u,n,c){w[k]=w[k]||function(){(w[k].q=w[k].q||[]).push(arguments)};w[k].config={container:w.document.getElementById(i),ORDER_URL:''https://checkout.testdrive.klarna.com/checkout/orders/1445FB9ACFD90B11C39E7220000'',TESTDRIVE:true,AUTH_HEADER:''KlarnaCheckout
|
42
|
+
aQdfgpMjeNNvH3JorWv3'',LAYOUT:''desktop'',LOCALE:''nb-no'',ORDER_STATUS:''checkout_incomplete'',MERCHANT_TAC_URI:''http://www.example.com/terms'',MERCHANT_TAC_TITLE:''R\u00f8rkj\u00f8p
|
43
|
+
KCO'',MERCHANT_NAME:''R\u00f8rkj\u00f8p KCO'',GUI_OPTIONS:[],ALLOW_SEPARATE_SHIPPING_ADDRESS:false,PURCHASE_COUNTRY:''nor'',PURCHASE_CURRENCY:''nok'',DISALLOW_SKIP_NIN:false,BOOTSTRAP_SRC:u};n=d.createElement(''script'');c=d.getElementById(i);n.async=!0;n.src=u;c.insertBefore(n,c.firstChild);})(this,''_klarnaCheckout'',''klarna-checkout-container'',document,''https://checkout.testdrive.klarna.com/140218-2318fcb/checkout.bootstrap.js'');/*
|
44
|
+
]]> */</script><noscript>Please <a href=\"http://enable-javascript.com\">enable\nJavaScript</a>.</noscript></div>"},"options":{"allow_separate_shipping_address":false},"merchant":{"id":"1667","terms_uri":"http://www.example.com/terms","checkout_uri":"http://www.example.com/checkout","confirmation_uri":"http://www.example.com/confirmation_uri","push_uri":"http://www.example.com/push"}}'
|
45
|
+
http_version:
|
46
|
+
recorded_at: Mon, 24 Feb 2014 10:57:04 GMT
|
47
|
+
recorded_with: VCR 2.8.0
|
@@ -0,0 +1,89 @@
|
|
1
|
+
---
|
2
|
+
http_interactions:
|
3
|
+
- request:
|
4
|
+
method: get
|
5
|
+
uri: https://checkout.testdrive.klarna.com/checkout/orders/1445FB9ACFD90B11C39E7220000
|
6
|
+
body:
|
7
|
+
encoding: US-ASCII
|
8
|
+
string: ''
|
9
|
+
headers:
|
10
|
+
User-Agent:
|
11
|
+
- Faraday v0.9.0
|
12
|
+
Authorization:
|
13
|
+
- Klarna QewkVlXa+6xDGNlS6yCgnKFKAZcJ/HAL+ocIwktB2K0=
|
14
|
+
Accept:
|
15
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json
|
16
|
+
Accept-Encoding:
|
17
|
+
- ''
|
18
|
+
response:
|
19
|
+
status:
|
20
|
+
code: 200
|
21
|
+
message: OK
|
22
|
+
headers:
|
23
|
+
Server:
|
24
|
+
- MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
|
25
|
+
Pragma:
|
26
|
+
- no-cache
|
27
|
+
Date:
|
28
|
+
- Mon, 24 Feb 2014 10:57:10 GMT
|
29
|
+
Content-Type:
|
30
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json
|
31
|
+
Content-Length:
|
32
|
+
- '2109'
|
33
|
+
Cache-Control:
|
34
|
+
- max-age=0, no-cache, no-store
|
35
|
+
Connection:
|
36
|
+
- keep-alive
|
37
|
+
body:
|
38
|
+
encoding: UTF-8
|
39
|
+
string: '{"id":"1445FB9ACFD90B11C39E7220000","merchant_reference":{"orderid1":"oldreference"},"purchase_country":"no","purchase_currency":"nok","locale":"nb-no","status":"checkout_incomplete","started_at":"2014-02-23T18:10:07+01:00","last_modified_at":"2014-02-23T18:10:07+01:00","cart":{"total_price_excluding_tax":532,"total_tax_amount":134,"total_price_including_tax":666,"items":[{"reference":"1123581220325","name":"Widget","quantity":1,"unit_price":666,"tax_rate":2500,"discount_rate":0,"type":"physical","total_price_including_tax":666,"total_price_excluding_tax":532,"total_tax_amount":134}]},"customer":{"type":"person"},"shipping_address":{"country":"no"},"billing_address":{"country":"no"},"gui":{"layout":"desktop","snippet":"<div
|
40
|
+
id=\"klarna-checkout-container\" style=\"overflow-x:\nhidden;\"><script type=\"text/javascript\">/*
|
41
|
+
<![CDATA[ */(function(w,k,i,d,u,n,c){w[k]=w[k]||function(){(w[k].q=w[k].q||[]).push(arguments)};w[k].config={container:w.document.getElementById(i),ORDER_URL:''https://checkout.testdrive.klarna.com/checkout/orders/1445FB9ACFD90B11C39E7220000'',TESTDRIVE:true,AUTH_HEADER:''KlarnaCheckout
|
42
|
+
aQdfgpMjeNNvH3JorWv3'',LAYOUT:''desktop'',LOCALE:''nb-no'',ORDER_STATUS:''checkout_incomplete'',MERCHANT_TAC_URI:''http://www.example.com/terms'',MERCHANT_TAC_TITLE:''R\u00f8rkj\u00f8p
|
43
|
+
KCO'',MERCHANT_NAME:''R\u00f8rkj\u00f8p KCO'',GUI_OPTIONS:[],ALLOW_SEPARATE_SHIPPING_ADDRESS:false,PURCHASE_COUNTRY:''nor'',PURCHASE_CURRENCY:''nok'',DISALLOW_SKIP_NIN:false,BOOTSTRAP_SRC:u};n=d.createElement(''script'');c=d.getElementById(i);n.async=!0;n.src=u;c.insertBefore(n,c.firstChild);})(this,''_klarnaCheckout'',''klarna-checkout-container'',document,''https://checkout.testdrive.klarna.com/140218-2318fcb/checkout.bootstrap.js'');/*
|
44
|
+
]]> */</script><noscript>Please <a href=\"http://enable-javascript.com\">enable\nJavaScript</a>.</noscript></div>"},"options":{"allow_separate_shipping_address":false},"merchant":{"id":"1667","terms_uri":"http://www.example.com/terms","checkout_uri":"http://www.example.com/checkout","confirmation_uri":"http://www.example.com/confirmation_uri","push_uri":"http://www.example.com/push"}}'
|
45
|
+
http_version:
|
46
|
+
recorded_at: Mon, 24 Feb 2014 10:57:02 GMT
|
47
|
+
- request:
|
48
|
+
method: post
|
49
|
+
uri: https://checkout.testdrive.klarna.com/checkout/orders/1445FB9ACFD90B11C39E7220000
|
50
|
+
body:
|
51
|
+
encoding: UTF-8
|
52
|
+
string: '{"merchant_reference":{"orderid1":"newreference"},"purchase_country":"no","purchase_currency":"nok","locale":"nb-no","cart":{"items":[{"type":"physical","reference":"1123581220325","name":"Widget","quantity":50,"unit_price":666,"discount_rate":0,"tax_rate":2500}]},"merchant":{"id":"1667","terms_uri":"http://www.example.com/terms","checkout_uri":"http://www.example.com/checkout","confirmation_uri":"http://www.example.com/confirmation_uri","push_uri":"http://www.example.com/push"}}'
|
53
|
+
headers:
|
54
|
+
User-Agent:
|
55
|
+
- Faraday v0.9.0
|
56
|
+
Authorization:
|
57
|
+
- Klarna 3ZkUvS8NRQauMItIEXfpkDH6f+EZsZanDyvjpq8vsqY=
|
58
|
+
Content-Type:
|
59
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json
|
60
|
+
Accept:
|
61
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json, application/vnd.klarna.checkout.aggregated-order-v2+json
|
62
|
+
Accept-Encoding:
|
63
|
+
- ''
|
64
|
+
response:
|
65
|
+
status:
|
66
|
+
code: 200
|
67
|
+
message: OK
|
68
|
+
headers:
|
69
|
+
Server:
|
70
|
+
- MochiWeb/1.1 WebMachine/1.10.0 (never breaks eye contact)
|
71
|
+
Date:
|
72
|
+
- Mon, 24 Feb 2014 10:57:11 GMT
|
73
|
+
Content-Type:
|
74
|
+
- application/vnd.klarna.checkout.aggregated-order-v2+json
|
75
|
+
Content-Length:
|
76
|
+
- '2120'
|
77
|
+
Connection:
|
78
|
+
- keep-alive
|
79
|
+
body:
|
80
|
+
encoding: UTF-8
|
81
|
+
string: '{"id":"1445FB9ACFD90B11C39E7220000","merchant_reference":{"orderid1":"newreference"},"purchase_country":"no","purchase_currency":"nok","locale":"nb-no","status":"checkout_incomplete","started_at":"2014-02-23T18:10:07+01:00","last_modified_at":"2014-02-23T18:10:07+01:00","cart":{"total_price_excluding_tax":26640,"total_tax_amount":6660,"total_price_including_tax":33300,"items":[{"reference":"1123581220325","name":"Widget","quantity":50,"unit_price":666,"tax_rate":2500,"discount_rate":0,"type":"physical","total_price_including_tax":33300,"total_price_excluding_tax":26640,"total_tax_amount":6660}]},"customer":{"type":"person"},"shipping_address":{"country":"no"},"billing_address":{"country":"no"},"gui":{"layout":"desktop","snippet":"<div
|
82
|
+
id=\"klarna-checkout-container\" style=\"overflow-x:\nhidden;\"><script type=\"text/javascript\">/*
|
83
|
+
<![CDATA[ */(function(w,k,i,d,u,n,c){w[k]=w[k]||function(){(w[k].q=w[k].q||[]).push(arguments)};w[k].config={container:w.document.getElementById(i),ORDER_URL:''https://checkout.testdrive.klarna.com/checkout/orders/1445FB9ACFD90B11C39E7220000'',TESTDRIVE:true,AUTH_HEADER:''KlarnaCheckout
|
84
|
+
aQdfgpMjeNNvH3JorWv3'',LAYOUT:''desktop'',LOCALE:''nb-no'',ORDER_STATUS:''checkout_incomplete'',MERCHANT_TAC_URI:''http://www.example.com/terms'',MERCHANT_TAC_TITLE:''R\u00f8rkj\u00f8p
|
85
|
+
KCO'',MERCHANT_NAME:''R\u00f8rkj\u00f8p KCO'',GUI_OPTIONS:[],ALLOW_SEPARATE_SHIPPING_ADDRESS:false,PURCHASE_COUNTRY:''nor'',PURCHASE_CURRENCY:''nok'',DISALLOW_SKIP_NIN:false,BOOTSTRAP_SRC:u};n=d.createElement(''script'');c=d.getElementById(i);n.async=!0;n.src=u;c.insertBefore(n,c.firstChild);})(this,''_klarnaCheckout'',''klarna-checkout-container'',document,''https://checkout.testdrive.klarna.com/140218-2318fcb/checkout.bootstrap.js'');/*
|
86
|
+
]]> */</script><noscript>Please <a href=\"http://enable-javascript.com\">enable\nJavaScript</a>.</noscript></div>"},"options":{"allow_separate_shipping_address":false},"merchant":{"id":"1667","terms_uri":"http://www.example.com/terms","checkout_uri":"http://www.example.com/checkout","confirmation_uri":"http://www.example.com/confirmation_uri","push_uri":"http://www.example.com/push"}}'
|
87
|
+
http_version:
|
88
|
+
recorded_at: Mon, 24 Feb 2014 10:57:03 GMT
|
89
|
+
recorded_with: VCR 2.8.0
|
@@ -39,10 +39,6 @@ describe Klarna::Checkout::Cart do
|
|
39
39
|
json_hash
|
40
40
|
end
|
41
41
|
|
42
|
-
its([:total_price_including_tax]) { should eq 250 }
|
43
|
-
its([:total_price_excluding_tax]) { should eq 200 }
|
44
|
-
its([:total_tax_amount]) { should eq 50 }
|
45
|
-
|
46
42
|
describe "items/0" do
|
47
43
|
subject { json_hash[:items][0] }
|
48
44
|
|
@@ -54,9 +50,6 @@ describe Klarna::Checkout::Cart do
|
|
54
50
|
its([:image_uri]) { should eq 'http://www.example.com/product-image-uri' }
|
55
51
|
its([:quantity]) { should eq 1 }
|
56
52
|
its([:unit_price]) { should eq 250 }
|
57
|
-
its([:total_price_excluding_tax]) { should eq 200 }
|
58
|
-
its([:total_tax_amount]) { should eq 50 }
|
59
|
-
its([:total_price_including_tax]) { should eq 250 }
|
60
53
|
its([:discount_rate]) { should eq 0 }
|
61
54
|
its([:tax_rate]) { should eq 2500 }
|
62
55
|
end
|
@@ -10,6 +10,22 @@ describe Klarna::Checkout::Client do
|
|
10
10
|
end
|
11
11
|
|
12
12
|
its(:shared_secret) { should eq 'foobar' }
|
13
|
+
|
14
|
+
context "when Klarna::Checkout is configured with a shared_secret" do
|
15
|
+
before(:each) do
|
16
|
+
Klarna::Checkout.configure do |config|
|
17
|
+
config.shared_secret = 'foobar'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
after(:each) do
|
22
|
+
Klarna::Checkout.reset_configuration!
|
23
|
+
end
|
24
|
+
|
25
|
+
it "shouldn't be necessary to provide the secret twice" do
|
26
|
+
described_class.new.shared_secret.should eq 'foobar'
|
27
|
+
end
|
28
|
+
end
|
13
29
|
end
|
14
30
|
|
15
31
|
describe "#environment" do
|
@@ -49,7 +65,7 @@ describe Klarna::Checkout::Client do
|
|
49
65
|
describe "#create_order" do
|
50
66
|
subject { described_class.new({shared_secret: 'foobar'}) }
|
51
67
|
|
52
|
-
let(:order) { double('Order', to_json: JSON.generate({ foo: "bar" }), :id= => true) }
|
68
|
+
let(:order) { double('Order', to_json: JSON.generate({ foo: "bar" }), :id= => true, valid?: true) }
|
53
69
|
|
54
70
|
before(:each) do
|
55
71
|
stub_request(:post, "https://checkout.testdrive.klarna.com/checkout/orders")
|
@@ -74,6 +90,22 @@ describe Klarna::Checkout::Client do
|
|
74
90
|
subject.should receive(:handle_status_code).with(201)
|
75
91
|
subject.create_order(order)
|
76
92
|
end
|
93
|
+
|
94
|
+
it "returns the order" do
|
95
|
+
return_value = subject.create_order(order)
|
96
|
+
return_value.should eq order
|
97
|
+
end
|
98
|
+
|
99
|
+
context "if the order is invalid" do
|
100
|
+
before(:each) do
|
101
|
+
order.stub(:valid?) { false }
|
102
|
+
end
|
103
|
+
|
104
|
+
it "returns a falsy value" do
|
105
|
+
return_value = subject.create_order(order)
|
106
|
+
return_value.should be_false
|
107
|
+
end
|
108
|
+
end
|
77
109
|
end
|
78
110
|
|
79
111
|
describe "#handle_status_code" do
|
@@ -160,6 +192,14 @@ describe Klarna::Checkout::Client do
|
|
160
192
|
}.to raise_error(Klarna::Checkout::UnsupportedMediaTypeException)
|
161
193
|
end
|
162
194
|
end
|
195
|
+
|
196
|
+
context "with 500" do
|
197
|
+
it "raises a Klarna::Checkout::InternalServerErrorException" do
|
198
|
+
expect {
|
199
|
+
subject.handle_status_code(500)
|
200
|
+
}.to raise_error(Klarna::Checkout::InternalServerErrorException)
|
201
|
+
end
|
202
|
+
end
|
163
203
|
end
|
164
204
|
|
165
205
|
describe "#read_order" do
|
@@ -188,6 +228,50 @@ describe Klarna::Checkout::Client do
|
|
188
228
|
end
|
189
229
|
end
|
190
230
|
|
231
|
+
describe "#update_order" do
|
232
|
+
subject { described_class.new({shared_secret: 'foobar'}) }
|
233
|
+
let(:order) { double('Order', to_json: JSON.generate({ foo: "bar" }), :id => "143F7BC0A1090B11C39E7220000", valid?: true) }
|
234
|
+
|
235
|
+
before(:each) do
|
236
|
+
stub_request(:post, "https://checkout.testdrive.klarna.com/checkout/orders/143F7BC0A1090B11C39E7220000")
|
237
|
+
.to_return(status: 200, body: JSON.generate({ id: "143F7BC0A1090B11C39E7220000" }))
|
238
|
+
end
|
239
|
+
|
240
|
+
it "uses the correct endpoint at klarna" do
|
241
|
+
subject.update_order(order)
|
242
|
+
|
243
|
+
assert_requested :post, "https://checkout.testdrive.klarna.com/checkout/orders/143F7BC0A1090B11C39E7220000",
|
244
|
+
:headers => {
|
245
|
+
# TODO: Investigate double definition in header
|
246
|
+
# 'Accept' => 'application/vnd.klarna.checkout.aggregated-order-v2+json',
|
247
|
+
'Authorization' => 'Klarna dM+worqeBUs4UrOB3Jr/jSZWI39vP4LNw7NfDjGtW2w=',
|
248
|
+
'Content-Type' => 'application/vnd.klarna.checkout.aggregated-order-v2+json',
|
249
|
+
},
|
250
|
+
:times => 1
|
251
|
+
end
|
252
|
+
|
253
|
+
it "checks the response" do
|
254
|
+
subject.should receive(:handle_status_code).with(200)
|
255
|
+
subject.update_order(order)
|
256
|
+
end
|
257
|
+
|
258
|
+
it "returns an order" do
|
259
|
+
return_value = subject.update_order(order)
|
260
|
+
return_value.should be_kind_of(Klarna::Checkout::Order)
|
261
|
+
end
|
262
|
+
|
263
|
+
context "if the order is invalid" do
|
264
|
+
before(:each) do
|
265
|
+
order.stub(:valid?) { false }
|
266
|
+
end
|
267
|
+
|
268
|
+
it "returns a falsy value" do
|
269
|
+
return_value = subject.update_order(order)
|
270
|
+
return_value.should be_false
|
271
|
+
end
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
191
275
|
describe "#sign_payload" do
|
192
276
|
let(:client) { described_class.new(shared_secret: 'foobar') }
|
193
277
|
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Klarna::Checkout::MerchantReference do
|
4
|
+
describe ".initialize" do
|
5
|
+
subject do
|
6
|
+
described_class.new \
|
7
|
+
orderid1: 'bar',
|
8
|
+
orderid2: 'foo'
|
9
|
+
end
|
10
|
+
|
11
|
+
its(:orderid1) { should eq 'bar' }
|
12
|
+
its(:orderid2) { should eq 'foo' }
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "attributes" do
|
16
|
+
it { should have_attribute(:orderid1) } #, :optional) }
|
17
|
+
it { should have_attribute(:orderid2) } #, :optional) }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#as_json" do
|
21
|
+
let(:merchant_reference) do
|
22
|
+
described_class.new \
|
23
|
+
orderid1: 'foo',
|
24
|
+
orderid2: 'bar'
|
25
|
+
end
|
26
|
+
|
27
|
+
let(:json_hash) { merchant_reference.as_json }
|
28
|
+
subject do
|
29
|
+
json_hash
|
30
|
+
end
|
31
|
+
|
32
|
+
its([:orderid1]) { should eq 'foo' }
|
33
|
+
its([:orderid2]) { should eq 'bar' }
|
34
|
+
end
|
35
|
+
end
|
@@ -14,7 +14,6 @@ describe Klarna::Checkout::Order do
|
|
14
14
|
|
15
15
|
describe "attributes" do
|
16
16
|
it { should have_attribute(:id) } #, :readonly) }
|
17
|
-
it { should have_attribute(:merchant_reference) } #, :optional) }
|
18
17
|
it { should have_attribute(:purchase_country) } #, :mandatory) }
|
19
18
|
it { should have_attribute(:purchase_currency) } #, :mandatory) }
|
20
19
|
it { should have_attribute(:locale) } #, :mandatory) }
|
@@ -29,18 +28,137 @@ describe Klarna::Checkout::Order do
|
|
29
28
|
end
|
30
29
|
|
31
30
|
describe "relations" do
|
32
|
-
it { should have_one(:
|
33
|
-
it { should have_one(:
|
34
|
-
it { should have_one(:
|
35
|
-
it { should have_one(:
|
36
|
-
it { should have_one(:
|
37
|
-
it { should have_one(:
|
31
|
+
it { should have_one(:merchant_reference, as: Klarna::Checkout::MerchantReference) }
|
32
|
+
it { should have_one(:billing_address, as: Klarna::Checkout::Address) }
|
33
|
+
it { should have_one(:shipping_address, as: Klarna::Checkout::Address) }
|
34
|
+
it { should have_one(:cart, as: Klarna::Checkout::Cart) }
|
35
|
+
it { should have_one(:customer, as: Klarna::Checkout::Customer) }
|
36
|
+
it { should have_one(:merchant, as: Klarna::Checkout::Merchant) }
|
37
|
+
it { should have_one(:gui, as: Klarna::Checkout::Gui) }
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "validations" do
|
41
|
+
subject(:valid_order) do
|
42
|
+
described_class.new \
|
43
|
+
merchant_reference: {
|
44
|
+
orderid1: 'foo',
|
45
|
+
orderid2: 'bar'
|
46
|
+
},
|
47
|
+
purchase_country: 'NO',
|
48
|
+
purchase_currency: 'NOK',
|
49
|
+
locale: 'nb-no',
|
50
|
+
cart: {
|
51
|
+
items: [{
|
52
|
+
reference: '1123581220325',
|
53
|
+
name: 'Widget',
|
54
|
+
quantity: 1,
|
55
|
+
unit_price: 666,
|
56
|
+
tax_rate: 2500
|
57
|
+
}]
|
58
|
+
},
|
59
|
+
merchant: {
|
60
|
+
id: '666666',
|
61
|
+
terms_uri: 'http://www.example.com/terms',
|
62
|
+
checkout_uri: 'http://www.example.com/checkout',
|
63
|
+
confirmation_uri: 'http://www.example.com/confirmation_uri',
|
64
|
+
push_uri: 'http://www.example.com/push'
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
it { should be_valid }
|
69
|
+
|
70
|
+
context "when status is 'checkout_incomplete'" do
|
71
|
+
before(:each) do
|
72
|
+
subject.status = 'checkout_incomplete'
|
73
|
+
end
|
74
|
+
|
75
|
+
it "is invalid without a purchase country" do
|
76
|
+
subject.purchase_country = nil
|
77
|
+
subject.should_not be_valid
|
78
|
+
end
|
79
|
+
|
80
|
+
it "is invalid without a purchase currency" do
|
81
|
+
subject.purchase_currency = nil
|
82
|
+
subject.should_not be_valid
|
83
|
+
end
|
84
|
+
|
85
|
+
it "is invalid without a locale" do
|
86
|
+
subject.locale = nil
|
87
|
+
subject.should_not be_valid
|
88
|
+
end
|
89
|
+
|
90
|
+
let(:cart_item) { subject.cart.items[0] }
|
91
|
+
|
92
|
+
it "is invalid without a cart item reference" do
|
93
|
+
cart_item.reference = nil
|
94
|
+
subject.should_not be_valid
|
95
|
+
end
|
96
|
+
|
97
|
+
it "is invalid without a cart item name" do
|
98
|
+
cart_item.name = nil
|
99
|
+
subject.should_not be_valid
|
100
|
+
end
|
101
|
+
|
102
|
+
it "is invalid without a cart item quantity" do
|
103
|
+
cart_item.quantity = nil
|
104
|
+
subject.should_not be_valid
|
105
|
+
end
|
106
|
+
|
107
|
+
it "is invalid without a cart item unit price" do
|
108
|
+
cart_item.unit_price = nil
|
109
|
+
subject.should_not be_valid
|
110
|
+
end
|
111
|
+
|
112
|
+
it "is invalid without a cart item tax rate" do
|
113
|
+
cart_item.tax_rate = nil
|
114
|
+
subject.should_not be_valid
|
115
|
+
end
|
116
|
+
|
117
|
+
let(:merchant) { subject.merchant }
|
118
|
+
|
119
|
+
it "is invalid without a merchant id" do
|
120
|
+
merchant.id = nil
|
121
|
+
subject.should_not be_valid
|
122
|
+
end
|
123
|
+
|
124
|
+
it "is invalid without a merchant terms_uri" do
|
125
|
+
merchant.terms_uri = nil
|
126
|
+
subject.should_not be_valid
|
127
|
+
end
|
128
|
+
|
129
|
+
it "is invalid without a merchant checkout_uri" do
|
130
|
+
merchant.checkout_uri = nil
|
131
|
+
subject.should_not be_valid
|
132
|
+
end
|
133
|
+
|
134
|
+
it "is invalid without a merchant confirmation_uri" do
|
135
|
+
merchant.confirmation_uri = nil
|
136
|
+
subject.should_not be_valid
|
137
|
+
end
|
138
|
+
|
139
|
+
it "is invalid without a merchant push_uri" do
|
140
|
+
merchant.push_uri = nil
|
141
|
+
subject.should_not be_valid
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
context "when status is 'checkout_complete' or 'created'" do
|
146
|
+
['checkout_complete', 'created'].each do |status|
|
147
|
+
before(:each) do
|
148
|
+
subject.status = 'checkout_complete'
|
149
|
+
end
|
150
|
+
|
151
|
+
end
|
152
|
+
end
|
38
153
|
end
|
39
154
|
|
40
155
|
describe "#as_json" do
|
41
156
|
let(:order) do
|
42
157
|
described_class.new \
|
43
|
-
merchant_reference:
|
158
|
+
merchant_reference: {
|
159
|
+
orderid1: 'foo',
|
160
|
+
orderid2: 'bar'
|
161
|
+
},
|
44
162
|
purchase_country: 'NO',
|
45
163
|
purchase_currency: 'NOK',
|
46
164
|
locale: 'nb-no',
|
@@ -67,7 +185,13 @@ describe Klarna::Checkout::Order do
|
|
67
185
|
json_hash
|
68
186
|
end
|
69
187
|
|
70
|
-
|
188
|
+
describe "merchant_reference" do
|
189
|
+
subject { json_hash[:merchant_reference] }
|
190
|
+
|
191
|
+
its([:orderid1]) { 'foo' }
|
192
|
+
its([:orderid2]) { 'bar' }
|
193
|
+
end
|
194
|
+
|
71
195
|
its([:purchase_country]) { should eq 'NO' }
|
72
196
|
its([:purchase_currency]) { should eq 'NOK' }
|
73
197
|
its([:locale]) { should eq 'nb-no' }
|
@@ -102,4 +226,107 @@ describe Klarna::Checkout::Order do
|
|
102
226
|
subject.to_json.should eq JSON.generate({ bar: "foo" })
|
103
227
|
end
|
104
228
|
end
|
229
|
+
|
230
|
+
describe ".defaults" do
|
231
|
+
context "with some defaults set" do
|
232
|
+
before(:each) do
|
233
|
+
described_class.defaults = {
|
234
|
+
purchase_country: 'NO',
|
235
|
+
purchase_currency: 'NOK',
|
236
|
+
merchant: {
|
237
|
+
id: '666666'
|
238
|
+
}
|
239
|
+
}
|
240
|
+
end
|
241
|
+
after(:each) do
|
242
|
+
described_class.defaults = nil
|
243
|
+
end
|
244
|
+
|
245
|
+
it "all new orders have the default values" do
|
246
|
+
order = described_class.new
|
247
|
+
order.purchase_country.should eq 'NO'
|
248
|
+
order.purchase_currency.should eq 'NOK'
|
249
|
+
end
|
250
|
+
|
251
|
+
it "should be possible to override the default values" do
|
252
|
+
order = described_class.new(purchase_currency: 'SEK')
|
253
|
+
order.purchase_currency.should eq 'SEK'
|
254
|
+
end
|
255
|
+
|
256
|
+
it "should be possible to provide any nested values without affecting the defaults" do
|
257
|
+
order = described_class.new({
|
258
|
+
merchant: {
|
259
|
+
terms_uri: 'http://www.example.com/terms'
|
260
|
+
}
|
261
|
+
})
|
262
|
+
order.merchant.id.should eq '666666'
|
263
|
+
order.merchant.terms_uri.should eq 'http://www.example.com/terms'
|
264
|
+
end
|
265
|
+
|
266
|
+
context "when I override an already specified default value" do
|
267
|
+
before(:each) do
|
268
|
+
described_class.defaults = {
|
269
|
+
purchase_currency: 'SEK'
|
270
|
+
}
|
271
|
+
end
|
272
|
+
|
273
|
+
it "newly created objects should use the new default value" do
|
274
|
+
order = described_class.new
|
275
|
+
order.purchase_currency.should eq 'SEK'
|
276
|
+
end
|
277
|
+
|
278
|
+
it "shouldn't remove the old default values" do
|
279
|
+
order = described_class.new
|
280
|
+
order.purchase_country.should eq 'NO'
|
281
|
+
order.merchant.id.should eq '666666'
|
282
|
+
end
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
it "doesn't allow setting something other than a hash" do
|
287
|
+
expect {
|
288
|
+
described_class.defaults = :foobar
|
289
|
+
}.to raise_error(ArgumentError)
|
290
|
+
end
|
291
|
+
end
|
292
|
+
|
293
|
+
context "when Klarna::Checkout has been configured with some default values" do
|
294
|
+
before(:each) do
|
295
|
+
Klarna::Checkout.configure do |config|
|
296
|
+
config.merchant_id = '424242'
|
297
|
+
config.default_country = 'NO'
|
298
|
+
config.default_currency = 'NOK'
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
after(:each) do
|
303
|
+
Klarna::Checkout.reset_configuration!
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should use the configuration's default values" do
|
307
|
+
order = described_class.new
|
308
|
+
order.purchase_country.should eq 'NO'
|
309
|
+
order.purchase_currency.should eq 'NOK'
|
310
|
+
order.merchant.id.should eq '424242'
|
311
|
+
end
|
312
|
+
|
313
|
+
context "if I specify another default merchant ID" do
|
314
|
+
before(:each) do
|
315
|
+
described_class.defaults = {
|
316
|
+
purchase_country: 'SE',
|
317
|
+
purchase_currency: 'SEK',
|
318
|
+
merchant: {
|
319
|
+
id: '666666'
|
320
|
+
}
|
321
|
+
}
|
322
|
+
end
|
323
|
+
|
324
|
+
it "should use my default values instead" do
|
325
|
+
order = described_class.new
|
326
|
+
order.purchase_country.should eq 'SE'
|
327
|
+
order.purchase_currency.should eq 'SEK'
|
328
|
+
order.merchant.id.should eq '666666'
|
329
|
+
end
|
330
|
+
end
|
331
|
+
end
|
105
332
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
describe Klarna::Checkout do
|
5
|
+
describe ".configure" do
|
6
|
+
context "without configuration" do
|
7
|
+
describe "accessing the variables shouldn't blow up" do
|
8
|
+
specify do
|
9
|
+
expect { Klarna::Checkout.shared_secret }.to_not raise_error
|
10
|
+
end
|
11
|
+
|
12
|
+
specify do
|
13
|
+
expect { Klarna::Checkout.merchant_id }.to_not raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
specify do
|
17
|
+
expect { Klarna::Checkout.default_country }.to_not raise_error
|
18
|
+
end
|
19
|
+
|
20
|
+
specify do
|
21
|
+
expect { Klarna::Checkout.default_currency }.to_not raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
context "after I have configured the module" do
|
27
|
+
before(:each) do
|
28
|
+
Klarna::Checkout.configure do |config|
|
29
|
+
config.shared_secret = 'mysecret'
|
30
|
+
config.merchant_id = '1337'
|
31
|
+
config.default_country = 'NO'
|
32
|
+
config.default_currency = 'NOK'
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
its(:shared_secret) { should eq 'mysecret' }
|
37
|
+
its(:merchant_id) { should eq '1337' }
|
38
|
+
its(:default_country) { should eq 'NO' }
|
39
|
+
its(:default_currency) { should eq 'NOK' }
|
40
|
+
|
41
|
+
context "after resetting the configuration" do
|
42
|
+
before(:each) do
|
43
|
+
Klarna::Checkout.reset_configuration!
|
44
|
+
end
|
45
|
+
|
46
|
+
its(:shared_secret) { should eq nil }
|
47
|
+
its(:merchant_id) { should eq nil }
|
48
|
+
its(:default_country) { should eq nil }
|
49
|
+
its(:default_currency) { should eq nil }
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
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.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Theodor Tonum
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-02-
|
11
|
+
date: 2014-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -24,6 +24,34 @@ dependencies:
|
|
24
24
|
- - '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
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
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activemodel
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
27
55
|
- !ruby/object:Gem::Dependency
|
28
56
|
name: bundler
|
29
57
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,22 +165,30 @@ files:
|
|
137
165
|
- lib/klarna/checkout/client.rb
|
138
166
|
- lib/klarna/checkout/concerns/has_many.rb
|
139
167
|
- lib/klarna/checkout/concerns/has_one.rb
|
168
|
+
- lib/klarna/checkout/configuration.rb
|
140
169
|
- lib/klarna/checkout/customer.rb
|
141
170
|
- lib/klarna/checkout/exceptions.rb
|
142
171
|
- lib/klarna/checkout/gui.rb
|
143
172
|
- lib/klarna/checkout/merchant.rb
|
173
|
+
- lib/klarna/checkout/merchant_reference.rb
|
144
174
|
- lib/klarna/checkout/order.rb
|
145
175
|
- lib/klarna/checkout/resource.rb
|
146
176
|
- lib/klarna/checkout/version.rb
|
147
177
|
- lib/klarna/checkout.rb
|
178
|
+
- lib/klarna/hash/deep_merge.rb
|
148
179
|
- spec/acceptance/create_order_spec.rb
|
149
180
|
- spec/acceptance/read_order_spec.rb
|
181
|
+
- spec/acceptance/update_order_spec.rb
|
150
182
|
- spec/fixtures/vcr_cassettes/create_order_spec.yml
|
151
183
|
- spec/fixtures/vcr_cassettes/read_order_spec.yml
|
184
|
+
- spec/fixtures/vcr_cassettes/update_order_spec-2.yml
|
185
|
+
- spec/fixtures/vcr_cassettes/update_order_spec.yml
|
152
186
|
- spec/lib/klarna/checkout/cart_spec.rb
|
153
187
|
- spec/lib/klarna/checkout/client_spec.rb
|
188
|
+
- spec/lib/klarna/checkout/merchant_reference_spec.rb
|
154
189
|
- spec/lib/klarna/checkout/order_spec.rb
|
155
190
|
- spec/lib/klarna/checkout/version_spec.rb
|
191
|
+
- spec/lib/klarna/checkout_spec.rb
|
156
192
|
- spec/spec_helper.rb
|
157
193
|
- spec/support/matchers/have_attribute.rb
|
158
194
|
- spec/support/matchers/have_many.rb
|
@@ -184,12 +220,17 @@ summary: '...'
|
|
184
220
|
test_files:
|
185
221
|
- spec/acceptance/create_order_spec.rb
|
186
222
|
- spec/acceptance/read_order_spec.rb
|
223
|
+
- spec/acceptance/update_order_spec.rb
|
187
224
|
- spec/fixtures/vcr_cassettes/create_order_spec.yml
|
188
225
|
- spec/fixtures/vcr_cassettes/read_order_spec.yml
|
226
|
+
- spec/fixtures/vcr_cassettes/update_order_spec-2.yml
|
227
|
+
- spec/fixtures/vcr_cassettes/update_order_spec.yml
|
189
228
|
- spec/lib/klarna/checkout/cart_spec.rb
|
190
229
|
- spec/lib/klarna/checkout/client_spec.rb
|
230
|
+
- spec/lib/klarna/checkout/merchant_reference_spec.rb
|
191
231
|
- spec/lib/klarna/checkout/order_spec.rb
|
192
232
|
- spec/lib/klarna/checkout/version_spec.rb
|
233
|
+
- spec/lib/klarna/checkout_spec.rb
|
193
234
|
- spec/spec_helper.rb
|
194
235
|
- spec/support/matchers/have_attribute.rb
|
195
236
|
- spec/support/matchers/have_many.rb
|