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:, options: {}, recurring: false, customer: {}, checkout_url: nil, terms_url: nil) ⇒ Order

Returns a new instance of Order.



54
55
56
57
58
59
60
61
62
# File 'lib/klarna/checkout/order.rb', line 54

def initialize(header:, items:, options: {}, recurring: false, customer: {}, checkout_url: nil, terms_url: nil)
  @header       = header
  @items        = items
  @recurring    = recurring
  @customer     = customer
  @options      = options
  @checkout_url = checkout_url || Klarna::Checkout.configuration.checkout_uri
  @terms_url    = terms_url || Klarna::Checkout.configuration.terms_uri
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(locale:, order_lines:, order_amount:, order_tax_amount:, purchase_currency:, recurring_token:) ⇒ Object

For creating an order, using a recurring_token



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/klarna/checkout/order.rb', line 29

def self.create_recurring(locale:, order_lines:, order_amount:, order_tax_amount:, purchase_currency:, recurring_token:)
  if recurring_token.nil?
    raise Klarna::Checkout::Errors::OrderValidationError.new('Argument missing', 'missing_recurring_token')
  end

  create_recurring_order(
    locale: locale,
    order_lines: order_lines,
    order_amount: order_amount,
    order_tax_amount: order_tax_amount,
    purchase_currency: purchase_currency,
    recurring_token: recurring_token
  )
end

.find(ref) ⇒ Object

Returns an instance of the order



45
46
47
# File 'lib/klarna/checkout/order.rb', line 45

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

.find_checkout(ref) ⇒ Object

Same as find but to be used during checkout stage



50
51
52
# File 'lib/klarna/checkout/order.rb', line 50

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

Instance Method Details

#cancelObject

Cancels the order through Klarna API



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

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



78
79
80
81
82
83
84
# File 'lib/klarna/checkout/order.rb', line 78

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



66
67
68
69
70
# File 'lib/klarna/checkout/order.rb', line 66

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

#refund(amount: nil, description: nil) ⇒ Object

Refunds the order through Klarna API



96
97
98
99
100
# File 'lib/klarna/checkout/order.rb', line 96

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