class Klarna::Checkout::Order

Attributes

api_order[RW]
klarna_response[RW]
payment_form[RW]
recurring[RW]
reference[RW]
status[RW]

Public Class Methods

acknowledge(ref) click to toggle source
# File lib/klarna/checkout/order.rb, line 24
def self.acknowledge(ref)
  acknowledge_order(ref)
end
create_recurring(**args) click to toggle source

For creating an order, using a recurring_token

# File lib/klarna/checkout/order.rb, line 29
def self.create_recurring(**args)
  if args[:recurring_token].nil?
    raise Klarna::Checkout::Errors::OrderValidationError.new('Argument missing', 'missing_recurring_token')
  end

  create_recurring_order(args)
end
find(ref) click to toggle source

Returns an instance of the order

# File lib/klarna/checkout/order.rb, line 38
def self.find(ref)
  fetch_confirmed_order(ref)
end
find_checkout(ref) click to toggle source

Same as find but to be used during checkout stage

# File lib/klarna/checkout/order.rb, line 43
def self.find_checkout(ref)
  fetch_checkout_order(ref)
end
new(header:, items:, recurring: false, **args) click to toggle source
# File lib/klarna/checkout/order.rb, line 47
def initialize(header:, items:, recurring: false, **args)
  @header    = header
  @items     = items
  @recurring = recurring
  @customer  = args[:customer].nil? ? {} : args[:customer]
  @options   = args[:options]
  @checkout_url = args[:checkout_url].nil? ? Klarna::Checkout.configuration.checkout_uri : args[:checkout_url]
  @terms_url    = args[:terms_url].nil? ? Klarna::Checkout.configuration.terms_uri : args[:terms_url]
end

Public Instance Methods

cancel() click to toggle source

Cancels the order through Klarna API

# File lib/klarna/checkout/order.rb, line 80
def cancel
  unless @status == 'AUTHORIZED'
    raise Klarna::Checkout::Errors::OrderCancelError.new(@status, 'cancel_not_allowed')
  end

  cancel_order
end
capture() click to toggle source

Captures the order through Klarna API

# File lib/klarna/checkout/order.rb, line 71
def capture
  unless @status == 'AUTHORIZED'
    raise Klarna::Checkout::Errors::OrderCaptureError.new(@status, 'capture_not_allowed')
  end

  capture_order
end
execute() click to toggle source

Creates an order Returns prepolulated order object based on Klarna API response

# File lib/klarna/checkout/order.rb, line 59
def execute
  add_defaults
  validate
  create(@header, @items)
end
refund(amount: nil, description: nil) click to toggle source

Refunds the order through Klarna API

# File lib/klarna/checkout/order.rb, line 89
def refund(amount: nil, description: nil)
  raise Klarna::Checkout::Errors::OrderRefundError.new(@status, 'refund_not_allowed') unless @status == 'CAPTURED'

  refund_order(amount: amount, description: description)
end

Private Instance Methods

add_defaults() click to toggle source
# File lib/klarna/checkout/order.rb, line 97
def add_defaults
  @header.merge!(
    {
      purchase_country: Klarna::Checkout.configuration.purchase_country,
      purchase_currency: Klarna::Checkout.configuration.purchase_currency,
      locale: Klarna::Checkout.configuration.locale
    }
  )
end
validate() click to toggle source
# File lib/klarna/checkout/order.rb, line 107
def validate
  header_keys_existance
  item_keys_existance
  amount_validation
  tax_amount_validation
  total_amount_validation
  total_tax_amount_validation
end