Class: Kount::RIS::Inquiry

Inherits:
Base
  • Object
show all
Defined in:
app/models/kount/ris/inquiry.rb

Constant Summary collapse

PAYMENT_TYPES =
{
  'apay' => 'Apple Pay',
  'card' => 'Credit Card',
  'pypl' => 'PayPal',
  'chek' => 'Check',
  'none' => 'None',
  'gdmp' => 'Green Dot Money Pack',
  'goog' => 'Google Checkout',
  'blml' => 'Bill Me Later',
  'gift' => 'Gift Card',
  'bpay' => 'BPAY',
  'netellar' => 'Netellar',
  'giropay' => 'GiroPay',
  'elv' => 'ELV',
  'mercade_pago' => 'Mercade Pago',
  'sepa' => 'Single Euro Payments Area',
  'interac' => 'Interac',
  'poli' => 'POLi',
  'skrill' => 'Skrill/Moneybookers',
  'sofort' => 'Sofort',
  'token' => 'Token',
}.freeze
SHIPPING_METHODS =
{
  'sd' => 'Same Day',
  'nd' => 'Next Day',
  '2d' => 'Second Day',
  'st' => 'Standard',
}.freeze
MODES =
%w[q u p x e j w].freeze
RESPONSE_FORMATS =
%w[sdk xml json yaml].freeze
GENDERS =
%w[male female].freeze
CARD_TYPES =
%w[visa mastercard amex discover]

Constants inherited from Base

Base::COUNTRY_CODES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Inquiry

Returns a new instance of Inquiry.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'app/models/kount/ris/inquiry.rb', line 208

def initialize(attrs = {})
  attrs.symbolize_keys!
  attrs.reverse_merge!(Kount.configuration.except(:company_server_url, :salt))
  attrs.reverse_merge!({
    authorized: true,
    acknowledged: true,
    email: '[email protected]',
    ip_address: '10.0.0.1',
    mode: :q,
  })
  if attrs[:mode] == 'p'
    attrs.reverse_merge!({ id: '0123456789' })
    attrs.merge!({ ip_address: '10.0.0.1' })
  end

  super(attrs.except(:products_attributes, :shipping_address_attributes, :billing_address_attributes))

  self.products = Array(attrs[:products_attributes]).collect { |product_attributes| Product.new(product_attributes) }
  self.shipping_address = Address.new(attrs[:shipping_address_attributes]) if attrs.include?(:shipping_address_attributes)
  self.billing_address = Address.new(attrs[:billing_address_attributes]) if attrs.include?(:billing_address_attributes)

  self
end

Instance Attribute Details

#account_created_atObject

Returns the value of attribute account_created_at.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def 
  @account_created_at
end

#acknowledgedObject Also known as: acknowledged?

Returns the value of attribute acknowledged.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def acknowledged
  @acknowledged
end

#amountObject

Returns the value of attribute amount.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def amount
  @amount
end

#api_hostObject

Returns the value of attribute api_host.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def api_host
  @api_host
end

#api_keyObject

Returns the value of attribute api_key.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def api_key
  @api_key
end

#api_versionObject

Returns the value of attribute api_version.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def api_version
  @api_version
end

#authorizedObject Also known as: authorized?

Returns the value of attribute authorized.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def authorized
  @authorized
end

#avs_codeObject

Returns the value of attribute avs_code.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def avs_code
  @avs_code
end

#billing_addressObject

Returns the value of attribute billing_address.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def billing_address
  @billing_address
end

#birthdateObject

Returns the value of attribute birthdate.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def birthdate
  @birthdate
end

#card_numberObject

Returns the value of attribute card_number.



11
12
13
# File 'app/models/kount/ris/inquiry.rb', line 11

def card_number
  @card_number
end

#card_typeObject

Returns the value of attribute card_type.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def card_type
  @card_type
end

#customer_idObject

Returns the value of attribute customer_id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def customer_id
  @customer_id
end

#cvv_codeObject

Returns the value of attribute cvv_code.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def cvv_code
  @cvv_code
end

#emailObject

Returns the value of attribute email.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def email
  @email
end

#gateway_idObject

Returns the value of attribute gateway_id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def gateway_id
  @gateway_id
end

#genderObject

Returns the value of attribute gender.



11
12
13
# File 'app/models/kount/ris/inquiry.rb', line 11

def gender
  @gender
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def id
  @id
end

#ip_addressObject

Returns the value of attribute ip_address.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def ip_address
  @ip_address
end

#kount_idObject

Returns the value of attribute kount_id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def kount_id
  @kount_id
end

#last_fourObject

Returns the value of attribute last_four.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def last_four
  @last_four
end

#merchant_idObject

Returns the value of attribute merchant_id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def merchant_id
  @merchant_id
end

#modeObject

Returns the value of attribute mode.



11
12
13
# File 'app/models/kount/ris/inquiry.rb', line 11

def mode
  @mode
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def name
  @name
end

#order_numberObject

Returns the value of attribute order_number.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def order_number
  @order_number
end

#productsObject

Returns the value of attribute products.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def products
  @products
end

#recipient_emailObject

Returns the value of attribute recipient_email.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def recipient_email
  @recipient_email
end

#recipient_nameObject

Returns the value of attribute recipient_name.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def recipient_name
  @recipient_name
end

#responseObject (readonly)

Returns the value of attribute response.



11
12
13
# File 'app/models/kount/ris/inquiry.rb', line 11

def response
  @response
end

#response_formatObject

Returns the value of attribute response_format.



11
12
13
# File 'app/models/kount/ris/inquiry.rb', line 11

def response_format
  @response_format
end

#session_idObject

Returns the value of attribute session_id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def session_id
  @session_id
end

#shipping_addressObject

Returns the value of attribute shipping_address.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def shipping_address
  @shipping_address
end

#shipping_methodObject

Returns the value of attribute shipping_method.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def shipping_method
  @shipping_method
end

#site_idObject

Returns the value of attribute site_id.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def site_id
  @site_id
end

#totalObject

Returns the value of attribute total.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def total
  @total
end

#user_agentObject

Returns the value of attribute user_agent.



7
8
9
# File 'app/models/kount/ris/inquiry.rb', line 7

def user_agent
  @user_agent
end

Class Method Details

.card_typesObject



74
75
76
# File 'app/models/kount/ris/inquiry.rb', line 74

def card_types
  CARD_TYPES
end

.currency_codesObject



50
51
52
# File 'app/models/kount/ris/inquiry.rb', line 50

def currency_codes
  Kount::RIS::Base::COUNTRY_CODES
end

.gendersObject



70
71
72
# File 'app/models/kount/ris/inquiry.rb', line 70

def genders
  GENDERS
end

.modesObject



62
63
64
# File 'app/models/kount/ris/inquiry.rb', line 62

def modes
  MODES
end

.payment_typesObject



54
55
56
# File 'app/models/kount/ris/inquiry.rb', line 54

def payment_types
  PAYMENT_TYPES
end

.response_formatsObject



66
67
68
# File 'app/models/kount/ris/inquiry.rb', line 66

def response_formats
  RESPONSE_FORMATS
end

.shipping_methodsObject



58
59
60
# File 'app/models/kount/ris/inquiry.rb', line 58

def shipping_methods
  SHIPPING_METHODS
end

Instance Method Details

#avs_match_streetObject Also known as: avs_match_street?



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/kount/ris/inquiry.rb', line 236

def avs_match_street
  {
    a: { visa: true, mastercard: true, discover: true, amex: true },
    b: { visa: true },
    d: { visa: true },
    f: { visa: true, amex: true },
    m: { visa: true, amex: true },
    n: { visa: false, mastercard: false, discover: false, amex: false },
    o: { amex: true },
    t: { discover: false },
    w: { visa: false, mastercard: false, discover: false, amex: false },
    x: { visa: true, mastercard: true, discover: true },
    y: { visa: true, mastercard: true, discover: true, amex: true },
    z: { visa: false, mastercard: false, discover: false, amex: false },
  }.dig(avs_code.to_s.downcase.to_sym, card_type.to_sym) if avs_code.present? && card_type.present?
end

#avs_match_zipObject Also known as: avs_match_zip?



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'app/models/kount/ris/inquiry.rb', line 254

def avs_match_zip
  {
    a: { visa: false, mastercard: true, discover: false, amex: true },
    d: { visa: true },
    f: { visa: true },
    l: { amex: true },
    m: { visa: true, amex: true },
    n: { visa: false, mastercard: false, discover: false, amex: false },
    p: { visa: true },
    t: { discover: true },
    w: { visa: true, mastercard: true, discover: true, amex: false },
    x: { visa: true, mastercard: true, discover: true },
    y: { visa: true, mastercard: true, discover: true, amex: true },
    z: { visa: true, mastercard: true, discover: true, amex: true },
  }.dig(avs_code.to_s.downcase.to_sym, card_type.to_sym) if avs_code.present? && card_type.present?
end

#card_typesObject



103
104
105
# File 'app/models/kount/ris/inquiry.rb', line 103

def card_types
  self.class.card_types
end

#currency_codeObject



145
146
147
# File 'app/models/kount/ris/inquiry.rb', line 145

def currency_code
  @currency_code.upcase if @currency_code
end

#currency_code=(value) ⇒ Object



141
142
143
# File 'app/models/kount/ris/inquiry.rb', line 141

def currency_code=(value)
  @currency_code = value.to_s.downcase
end

#currency_codesObject



79
80
81
# File 'app/models/kount/ris/inquiry.rb', line 79

def currency_codes
  self.class.currency_codes
end

#cvv_matchObject Also known as: cvv_match?



272
273
274
275
276
277
# File 'app/models/kount/ris/inquiry.rb', line 272

def cvv_match
  case cvv_code.to_s.to_sym
  when :m, :y, :'0' then true
  when :n, :'1' then false
  end if cvv_code.present?
end

#gendersObject



99
100
101
# File 'app/models/kount/ris/inquiry.rb', line 99

def genders
  self.class.genders
end

#mode?(*modes) ⇒ Boolean

Returns:

  • (Boolean)


280
281
282
# File 'app/models/kount/ris/inquiry.rb', line 280

def mode?(*modes)
  Array(modes).collect(&:to_s).include? mode
end

#modesObject



91
92
93
# File 'app/models/kount/ris/inquiry.rb', line 91

def modes
  self.class.modes
end

#payment_tokenObject



232
233
234
# File 'app/models/kount/ris/inquiry.rb', line 232

def payment_token
  card_number =~ /x/i ? card_number : KHASH.new(card_number).to_s
end

#payment_typeObject



157
158
159
# File 'app/models/kount/ris/inquiry.rb', line 157

def payment_type
  payment_types[@payment_type]
end

#payment_type=(value) ⇒ Object

Raises:

  • (ArgumentError)


149
150
151
152
153
154
155
# File 'app/models/kount/ris/inquiry.rb', line 149

def payment_type=(value)
  return @payment_type = nil if value.nil?

  payment_type_value = payment_types.detect { |k,v| k if v.downcase == value.to_s.downcase }&.first || value.to_s.downcase
  raise ArgumentError, "'#{value}' is not a valid payment type" unless payment_types.include?(payment_type_value)
  @payment_type = payment_type_value
end

#payment_typesObject



83
84
85
# File 'app/models/kount/ris/inquiry.rb', line 83

def payment_types
  self.class.payment_types
end

#response_formatsObject



95
96
97
# File 'app/models/kount/ris/inquiry.rb', line 95

def response_formats
  self.class.response_formats
end

#shipping_methodsObject



87
88
89
# File 'app/models/kount/ris/inquiry.rb', line 87

def shipping_methods
  self.class.shipping_methods
end

#submit!Object



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'app/models/kount/ris/inquiry.rb', line 358

def submit!
  uri = URI.parse("https://#{api_host}")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true

  request = Net::HTTP::Post.new(uri.request_uri)
  request["X-Kount-Api-Key"] = api_key
  request.set_form_data(to_query_params)

  response = http.request(request)

  response_params = response.body.split("\n").collect { |params| Rack::Utils.parse_nested_query(params) }.reduce({}, :merge)

  @response = Response.new(response_params)
end

#to_queryObject



354
355
356
# File 'app/models/kount/ris/inquiry.rb', line 354

def to_query
  to_query_params.to_query
end

#to_query_paramsObject



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'app/models/kount/ris/inquiry.rb', line 284

def to_query_params
  query_params = {
    ANID: id,
    AUTH: ((authorized? ? 'A' : 'D') unless authorized.nil?),
    CURR: currency_code,
    EMAL: email,
    IPAD: ip_address,
    MACK: ('Y' if acknowledged?),
    MERC: merchant_id,
    MODE: (mode.to_s.upcase unless mode.nil?),
    PROD_DESC: products.collect(&:description),
    PROD_ITEM: products.collect(&:id),
    PROD_PRICE: products.collect(&:price),
    PROD_QUANT: products.collect(&:quantity),
    PROD_TYPE: products.collect(&:category),
    PTOK: payment_token,
    PTYP: @payment_type,
    SESS: session_id,
    SITE: site_id,
    TOTL: (total || products.sum { |product| product.price * product.quantity }),
    TRAN: kount_id,
    VERS: api_version,
    CUSTOMER_ID: gateway_id,
    AVST: (avs_match_street.nil? ? 'X' : (avs_match_street? ? 'M' : 'N')),
    AVSZ: (avs_match_zip.nil? ? 'X' : (avs_match_zip? ? 'M' : 'N')),
    CASH: amount,
    CVVR: (cvv_match.nil? ? 'X' : (cvv_match? ? 'M' : 'N')),
    S2EM: recipient_email,
    S2NM: recipient_name,
    SHTP: @shipping_method,
    UNIQ: customer_id,
    UAGT: user_agent,
    DOB: (birthdate.strftime('%Y-%m-%d') unless birthdate.nil?),
    EPOC: (.to_i unless .nil?),
    FRMT: (response_format.to_s.upcase unless response_format.nil?),
    GENDER: gender,
    LAST4: last_four,
    NAME: name,
    ORDR: order_number,
  }

  query_params.merge!({
    B2A1: shipping_address.address_1,
    B2A2: shipping_address.address_2,
    B2CC: shipping_address.country_code,
    B2CI: shipping_address.city,
    B2PC: shipping_address.postal_code,
    B2PN: shipping_address.phone,
    B2ST: shipping_address.region,
    BPREMISE: shipping_address.premise,
    BSTREET: shipping_address.thoroughfare,
  }) if shipping_address.present?

  query_params.merge!({
    S2A1: billing_address.address_1,
    S2A2: billing_address.address_2,
    S2CC: billing_address.country_code,
    S2CI: billing_address.city,
    S2PC: billing_address.postal_code,
    S2PN: billing_address.phone,
    S2ST: billing_address.region,
    SPREMISE: billing_address.premise,
    SSTREET: billing_address.thoroughfare,
  }) if billing_address.present?

  # validate total length of URL including domain - 4k limit

  query_params.compact!
end