class ActiveMerchant::Billing::DibsGateway

Constants

ACTIONS
CURRENCY_CODES

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/dibs.rb, line 14
def initialize(options = {})
  requires!(options, :merchant_id, :secret_key)
  super
end

Public Instance Methods

authorize(amount, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 26
def authorize(amount, payment_method, options = {})
  post = {}
  add_amount(post, amount)
  add_invoice(post, amount, options)
  if payment_method.respond_to?(:number)
    add_payment_method(post, payment_method, options)
    commit(:authorize, post)
  else
    add_ticket_id(post, payment_method)
    commit(:authorize_ticket, post)
  end
end
capture(amount, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 39
def capture(amount, authorization, options = {})
  post = {}
  add_amount(post, amount)
  add_reference(post, authorization)

  commit(:capture, post)
end
purchase(amount, payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 19
def purchase(amount, payment_method, options = {})
  MultiResponse.run(false) do |r|
    r.process { authorize(amount, payment_method, options) }
    r.process { capture(amount, r.authorization, options) }
  end
end
refund(amount, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 54
def refund(amount, authorization, options = {})
  post = {}
  add_amount(post, amount)
  add_reference(post, authorization)

  commit(:refund, post)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 82
def scrub(transcript)
  transcript.
    gsub(%r(("cardNumber\\?":\\?")[^"]*)i, '\1[FILTERED]').
    gsub(%r(("cvc\\?":\\?")[^"]*)i, '\1[FILTERED]')
end
store(payment_method, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 69
def store(payment_method, options = {})
  post = {}

  add_invoice(post, 0, options)
  add_payment_method(post, payment_method, options)

  commit(:store, post)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 78
def supports_scrubbing?
  true
end
verify(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 62
def verify(credit_card, options = {})
  MultiResponse.run(:use_first_response) do |r|
    r.process { authorize(100, credit_card, options) }
    r.process(:ignore_result) { void(r.authorization, options) }
  end
end
void(authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 47
def void(authorization, options = {})
  post = {}
  add_reference(post, authorization)

  commit(:void, post)
end

Private Instance Methods

add_amount(post, amount) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 120
def add_amount(post, amount)
  post[:amount] = amount
end
add_hmac(post) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 161
def add_hmac(post)
  data = post.sort.collect { |key, value| "#{key}=#{value}" }.join('&')
  digest = OpenSSL::Digest.new('sha256')
  key = [@options[:secret_key]].pack('H*')
  post[:MAC] = OpenSSL::HMAC.hexdigest(digest, key, data)
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 98
def add_invoice(post, money, options)
  post[:orderId] = options[:order_id] || generate_unique_id
  post[:currency] = CURRENCY_CODES[options[:currency] || currency(money)]
end
add_payment_method(post, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 107
def add_payment_method(post, payment_method, options)
  post[:cardNumber] = payment_method.number
  post[:cvc] = payment_method.verification_value if payment_method.verification_value
  post[:expYear] = format(payment_method.year, :two_digits)
  post[:expMonth] = payment_method.month
  post[:clientIp] = options[:ip] || '127.0.0.1'
  post[:test] = true if test?
end
add_reference(post, authorization) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 116
def add_reference(post, authorization)
  post[:transactionId] = authorization
end
add_ticket_id(post, payment_method) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 103
def add_ticket_id(post, payment_method)
  post[:ticketId] = payment_method
end
authorization_from(request, response) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 188
def authorization_from(request, response)
  response['transactionId'] || response['ticketId'] || request[:transactionId]
end
build_request(post) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 156
def build_request(post)
  add_hmac(post)
  post.to_json
end
commit(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 133
def commit(action, post)
  post[:merchantId] = @options[:merchant_id]

  data = build_request(post)
  raw = parse(ssl_post(url(action), "request=#{data}", headers))
  succeeded = success_from(raw)
  Response.new(
    succeeded,
    message_from(succeeded, raw),
    raw,
    authorization: authorization_from(post, raw),
    test: test?
  )
rescue JSON::ParserError
  unparsable_response(raw)
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 150
def headers
  {
    'Content-Type' => 'application/x-www-form-urlencoded'
  }
end
message_from(succeeded, response) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 180
def message_from(succeeded, response)
  if succeeded
    'Succeeded'
  else
    response['status'] + ': ' + response['declineReason'] || 'Unable to read error message'
  end
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 172
def parse(body)
  JSON.parse(body)
end
success_from(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 176
def success_from(raw_response)
  raw_response['status'] == 'ACCEPT'
end
unparsable_response(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 192
def unparsable_response(raw_response)
  message = 'Invalid JSON response received from Dibs. Please contact Dibs if you continue to receive this message.'
  message += " (The raw response returned by the API was #{raw_response.inspect})"
  return Response.new(false, message)
end
url(action) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 168
def url(action)
  live_url + ACTIONS[action]
end