Class: ActiveMerchant::Billing::KlarnaGateway

Inherits:
Gateway
  • Object
show all
Defined in:
lib/active_merchant/billing/gateways/klarna.rb

Constant Summary

Constants inherited from Gateway

Gateway::CREDIT_DEPRECATION_MESSAGE, Gateway::RECURRING_DEPRECATION_MESSAGE, Gateway::STANDARD_ERROR_CODE

Instance Attribute Summary

Attributes inherited from Gateway

#options

Instance Method Summary collapse

Methods inherited from Gateway

#add_field_to_post_if_present, #add_fields_to_post_if_present, #card_brand, card_brand, #generate_unique_id, inherited, #scrub, #supported_countries, supported_countries, supported_countries=, supports?, #supports_network_tokenization?, #supports_scrubbing?, #test?

Methods included from CreditCardFormatting

#expdate, #format

Methods included from PostsData

included, #raw_ssl_request, #ssl_get, #ssl_post, #ssl_request

Constructor Details

#initialize(options = {}) ⇒ KlarnaGateway



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 12

def initialize(options = {})
  requires!(options, :login, :password, :zone)
  @options = options

  super
  Klarna.configure do |config|
    config.environment = @options[:test] ? 'test' : 'production'
    config.api_key =  @options[:login]
    config.api_secret = @options[:password]
    config.user_agent = "Klarna Gateway/Rails/#{::Rails.version}"
    config.zone = options[:zone].downcase.to_sym
  end
end

Instance Method Details

#acknowledge(order_id) ⇒ Object



148
149
150
151
152
153
154
155
156
157
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 148

def acknowledge(order_id)
  response = Klarna.client.acknowledge(order_id)

  if response.success?
    message = "Extended Period for order with Klarna id: #{order_id}"
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#authorize(amount, authorize_token, options = {}) ⇒ Object



68
69
70
71
72
73
74
75
76
77
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 68

def authorize(amount, authorize_token, options = {})
  response = Klarna.client(:payment).place_order(authorize_token, options)

  if response.success?
    message = "Placed order #{response.order_id}"
    generate_success_response(response, message, response.order_id, response.fraud_status)
  else
    generate_failure_response(response)
  end
end

#cancel(order_id) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 181

def cancel(order_id)
  response = Klarna.client.cancel(order_id)

  if response.success?
    message = "Cancelled order with Klarna id: #{order_id}"
    generate_success_response(response, message, order_id)
  else
    generate_failure_response(response)
  end
end

#capture(amount, order_id, options = {}) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 90

def capture(amount, order_id, options = {})
  response = Klarna.client.capture(order_id, { captured_amount: amount, shipping_info: options[:shipping_info] })

  if response.success?
    message = "Captured order with Klarna id: '#{order_id}' Capture id: '#{response['Capture-ID']}'"
    generate_success_response(response, message, order_id)
  else
    generate_failure_response(response)
  end
end

#create_session(options) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 26

def create_session(options)
  post = {}
  prepare_billing_address(post, options)
  prepare_line_items(post, options)
  prepare_order_data(post, options)

  response = Klarna.client(:payment).create_session(post)

  if response.success?
    message = "Session Created #{response.session_id}"
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#customer_details(order_id, data) ⇒ Object



206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 206

def customer_details(order_id, data)
  response = Klarna.client.customer_details(
    order_id,
    data
  )
  if response.success?
    message = "Updated customer details for order: #{order_id}"
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#customer_order(amount, customer_token, options = {}) ⇒ Object



79
80
81
82
83
84
85
86
87
88
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 79

def customer_order(amount, customer_token, options = {})
  response = Klarna.client(:customer_token).place_order(customer_token, options)

  if response.success?
    message = "Placed order #{response.order_id}"
    generate_success_response(response, message, response.order_id, response.fraud_status)
  else
    generate_failure_response(response)
  end
end

#extend_period(order_id) ⇒ Object



159
160
161
162
163
164
165
166
167
168
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 159

def extend_period(order_id)
  response = Klarna.client.extend(order_id)

  if response.success?
    message = "Extended Period for order with Klarna id: #{order_id}"
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#get(order_id) ⇒ Object



144
145
146
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 144

def get(order_id)
  Klarna.client.get(order_id)
end

#get_customer_token(authorize_token) ⇒ Object



131
132
133
134
135
136
137
138
139
140
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 131

def get_customer_token(authorize_token)
  response = Klarna.client(:customer_token).get(authorize_token)

  if response.success?
    message = 'Client token details'
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#purchase(amount, authorize_token, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 58

def purchase(amount, authorize_token, options = {})
  post = {}
  prepare_billing_address(post, options)
  prepare_line_items(post, options)
  prepare_order_data(post, options)
  post['order_amount'] = amount.to_f

  customer_order(amount, authorize_token, post)
end

#refund(amount, order_id, options = {}) ⇒ Object Also known as: credit



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 101

def refund(amount, order_id, options = {})
  # Get the refunded line items for better customer communications
  post = {}
  prepare_line_items(post, options)

  response = Klarna.client(:refund).create(order_id, { refunded_amount: amount })

  if response.success?
    message = "Refunded order with Klarna id: #{order_id}"
    generate_success_response(response, message, response['Refund-ID'])
  else
    generate_failure_response(response)
  end
end

#release(order_id) ⇒ Object



170
171
172
173
174
175
176
177
178
179
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 170

def release(order_id)
  response = Klarna.client.release(order_id)

  if response.success?
    message = "Released reamining amount for order with Klarna id: #{order_id}"
    generate_success_response(response, message, order_id)
  else
    generate_failure_response(response)
  end
end

#shipping_info(order_id, capture_id, shipping_info) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 192

def shipping_info(order_id, capture_id, shipping_info)
  response = Klarna.client(:capture).shipping_info(
    order_id,
    capture_id,
    shipping_info
  )
  if response.success?
    message = "Updated shipment info for order: #{order_id}, capture: #{capture_id}"
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#store(authorize_token, options = {}) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 116

def store(authorize_token, options = {})
  post = {}
  prepare_billing_address(post, options)
  prepare_customer_data(post, options)

  response = Klarna.client(:payment).customer_token(authorize_token, post)

  if response.success?
    message = 'Client token Created'
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end

#update_session(session_id, options) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/active_merchant/billing/gateways/klarna.rb', line 42

def update_session(session_id, options)
  post = {}
  prepare_billing_address(post, options)
  prepare_line_items(post, options)
  prepare_order_data(post, options)

  response = Klarna.client(:payment).update_session(session_id, post)

  if response.success?
    message = "Session Updated #{session_id}"
    generate_success_response(response, message)
  else
    generate_failure_response(response)
  end
end