module Klarna::Checkout::Operations::Fetch

Constants

PATH_CHECKOUT
PATH_CONFIRMED

Public Instance Methods

fetch_checkout_order(ref) click to toggle source

To fetch order during checkout stage

# File lib/klarna/checkout/operations/fetch.rb, line 12
def fetch_checkout_order(ref)
  fetch_order(PATH_CHECKOUT, ref)
end
fetch_confirmed_order(ref) click to toggle source

To fetch order after checkout stage

# File lib/klarna/checkout/operations/fetch.rb, line 17
def fetch_confirmed_order(ref)
  fetch_order(PATH_CONFIRMED, ref)
end

Private Instance Methods

build_order(header, body) click to toggle source
# File lib/klarna/checkout/operations/fetch.rb, line 52
def build_order(header, body)
  order = Klarna::Checkout::Order.new(
    header: header,
    items: body['order_lines']
  )

  order.status = body['status']
  order.reference = body['order_id']
  order.klarna_response = body
  order.recurring = (body['recurring'] == true)

  order
end
execute_fetch_request(path, ref) click to toggle source
# File lib/klarna/checkout/operations/fetch.rb, line 42
def execute_fetch_request(path, ref)
  https_connection.get do |req|
    req.url "#{path}#{ref}"
    req.options.timeout = 10

    req.headers['Authorization'] = authorization
    req.headers['Content-Type']  = 'application/json'
  end
end
fetch_order(path, ref) click to toggle source
# File lib/klarna/checkout/operations/fetch.rb, line 23
def fetch_order(path, ref)
  response = execute_fetch_request(path, ref)
  # Raise error if order is not found
  unless response.status == 200
    raise Klarna::Checkout::Errors::OrderNotFoundError.new("Unable to fetch order: #{ref}", 'order_not_found')
  end

  response_body = JSON.parse(response.body)
  order_header  = {
    purchase_country: response_body['purchase_country'],
    purchase_currency: response_body['purchase_currency'],
    locale: response_body['locale'],
    order_amount: response_body['order_amount'],
    order_tax_amount: response_body['order_tax_amount']
  }

  build_order(order_header, response_body)
end