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 15
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 27
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 41
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 20
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 56
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 84
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 71
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 80
def supports_scrubbing?
  true
end
verify(credit_card, options={}) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 64
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 49
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 127
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 168
def add_hmac(post)
  data = post.sort.collect { |key, value| "#{key}=#{value.to_s}" }.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 100
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 109
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[:startMonth] = payment_method.start_month if payment_method.start_month
  post[:startYear] = payment_method.start_year if payment_method.start_year
  post[:issueNumber] = payment_method.issue_number if payment_method.issue_number
  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 123
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 105
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 195
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 163
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 140
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 157
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 187
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 179
def parse(body)
  JSON.parse(body)
end
success_from(raw_response) click to toggle source
# File lib/active_merchant/billing/gateways/dibs.rb, line 183
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 199
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 175
def url(action)
  live_url + ACTIONS[action]
end