Class: Klarna::Checkout::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/klarna/checkout/client.rb

Constant Summary collapse

VALID_ENVS =
[:test, :production]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Client

Returns a new instance of Client.



12
13
14
15
16
17
# File 'lib/klarna/checkout/client.rb', line 12

def initialize(args = {})
  args.each do |(k,v)|
    self.public_send("#{k}=", v)
  end
  self.shared_secret ||= Klarna::Checkout.shared_secret
end

Instance Attribute Details

#environmentObject

Returns the value of attribute environment.



10
11
12
# File 'lib/klarna/checkout/client.rb', line 10

def environment
  @environment
end

#shared_secretObject

Returns the value of attribute shared_secret.



10
11
12
# File 'lib/klarna/checkout/client.rb', line 10

def shared_secret
  @shared_secret
end

Instance Method Details

#create_order(order) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/klarna/checkout/client.rb', line 42

def create_order(order)
  return false unless order.valid?

  response = write_order(order)
  order.id = response.headers['Location'].split('/').last
  order
end

#handle_status_code(code, msg = nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/klarna/checkout/client.rb', line 77

def handle_status_code(code, msg = nil)
  case Integer(code)
    when 200, 201
      yield if block_given?
    when 400
      raise Klarna::Checkout::BadRequest.new(msg)
    when 401
      raise Klarna::Checkout::UnauthorizedException.new(msg)
    when 403
      raise Klarna::Checkout::ForbiddenException.new(msg)
    when 404
      raise Klarna::Checkout::NotFoundException.new(msg)
    when 405
      raise Klarna::Checkout::MethodNotAllowedException.new(msg)
    when 406
      raise Klarna::Checkout::NotAcceptableException.new(msg)
    when 415
      raise Klarna::Checkout::UnsupportedMediaTypeException.new(msg)
    when 500
      raise Klarna::Checkout::InternalServerErrorException.new(msg)
  end
end

#hostObject



34
35
36
37
38
39
40
# File 'lib/klarna/checkout/client.rb', line 34

def host
  if environment == :production
    'https://checkout.klarna.com'
  else
    'https://checkout.testdrive.klarna.com'
  end
end

#read_order(id) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/klarna/checkout/client.rb', line 50

def read_order(id)
  response = https_connection.get do |req|
    req.url "/checkout/orders/#{id}"

    req.headers['Authorization']   = "Klarna #{sign_payload}"
    req.headers['Accept']          = 'application/vnd.klarna.checkout.aggregated-order-v2+json'
    req.headers['Accept-Encoding'] = ''
  end
  handle_status_code(response.status, response.body)

  Order.new(JSON.parse(response.body))
end

#sign_payload(request_body = '') ⇒ Object



72
73
74
75
# File 'lib/klarna/checkout/client.rb', line 72

def sign_payload(request_body = '')
  payload = "#{request_body}#{shared_secret}"
  Digest::SHA256.base64digest(payload)
end

#update_order(order) ⇒ Object



63
64
65
66
67
68
# File 'lib/klarna/checkout/client.rb', line 63

def update_order(order)
  return false unless order.valid?

  response = write_order(order)
  Order.new(JSON.parse(response.body))
end