Class: Klarna::Checkout::Order

Constant Summary

Constants included from Klarna::Checkout::Operations::Fetch

Klarna::Checkout::Operations::Fetch::PATH_CHECKOUT, Klarna::Checkout::Operations::Fetch::PATH_CONFIRMED

Constants included from ApiUtilities::ConnectionUtilities

ApiUtilities::ConnectionUtilities::KLARNA_PRODUCTION_URL, ApiUtilities::ConnectionUtilities::KLARNA_SANDBOX_URL

Constants included from Klarna::Checkout::Operations::Create

Klarna::Checkout::Operations::Create::CREATE_ORDER_PATH

Constants included from Validations::OrderValidations

Validations::OrderValidations::REQUIRED_ITEM_KEYS, Validations::OrderValidations::REQUIRED_ORDER_KEYS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ApiUtilities::ParseResponse

parse_response

Methods included from Klarna::Checkout::Operations::Acknowledge

acknowledge_order

Methods included from Klarna::Checkout::Operations::Fetch

fetch_checkout_order, fetch_confirmed_order

Methods included from ApiUtilities::ConnectionUtilities

#host, #https_connection

Methods included from Klarna::Checkout::Operations::CreateRecurring

create_recurring_order

Methods included from Resources::Authentication

authorization

Methods included from Resources::MerchantUrls

#merchant_urls

Methods included from Klarna::Checkout::Operations::Refund

#refund_order

Methods included from Klarna::Checkout::Operations::Cancel

#cancel_order

Methods included from Klarna::Checkout::Operations::Capture

#capture_order

Methods included from Klarna::Checkout::Operations::Create

#create

Methods included from Validations::OrderValidations

#amount_validation, #header_keys_existance, #item_keys_existance, #tax_amount_validation, #total_amount_validation, #total_tax_amount_validation

Constructor Details

#initialize(header:, items:, recurring: false, **args) ⇒ Order

Returns a new instance of Order.



47
48
49
50
51
52
53
54
55
# 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

Instance Attribute Details

#api_orderObject

Returns the value of attribute api_order.



22
23
24
# File 'lib/klarna/checkout/order.rb', line 22

def api_order
  @api_order
end

#klarna_responseObject

Returns the value of attribute klarna_response.



22
23
24
# File 'lib/klarna/checkout/order.rb', line 22

def klarna_response
  @klarna_response
end

#payment_formObject

Returns the value of attribute payment_form.



22
23
24
# File 'lib/klarna/checkout/order.rb', line 22

def payment_form
  @payment_form
end

#recurringObject

Returns the value of attribute recurring.



22
23
24
# File 'lib/klarna/checkout/order.rb', line 22

def recurring
  @recurring
end

#referenceObject

Returns the value of attribute reference.



22
23
24
# File 'lib/klarna/checkout/order.rb', line 22

def reference
  @reference
end

#statusObject

Returns the value of attribute status.



22
23
24
# File 'lib/klarna/checkout/order.rb', line 22

def status
  @status
end

Class Method Details

.acknowledge(ref) ⇒ Object



24
25
26
# File 'lib/klarna/checkout/order.rb', line 24

def self.acknowledge(ref)
  acknowledge_order(ref)
end

.create_recurring(**args) ⇒ Object

For creating an order, using a recurring_token



29
30
31
32
33
34
35
# 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) ⇒ Object

Returns an instance of the order



38
39
40
# File 'lib/klarna/checkout/order.rb', line 38

def self.find(ref)
  fetch_confirmed_order(ref)
end

.find_checkout(ref) ⇒ Object

Same as find but to be used during checkout stage



43
44
45
# File 'lib/klarna/checkout/order.rb', line 43

def self.find_checkout(ref)
  fetch_checkout_order(ref)
end

Instance Method Details

#cancelObject

Cancels the order through Klarna API



80
81
82
83
84
85
86
# 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

#captureObject

Captures the order through Klarna API



71
72
73
74
75
76
77
# 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

#executeObject

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



59
60
61
62
63
# File 'lib/klarna/checkout/order.rb', line 59

def execute
  add_defaults
  validate
  create(@header, @items)
end

#refundObject

Refunds the order through Klarna API



89
90
91
92
93
# File 'lib/klarna/checkout/order.rb', line 89

def refund
  raise Klarna::Checkout::Errors::OrderRefundError.new(@status, 'refund_not_allowed') unless @status == 'CAPTURED'

  refund_order
end