class ActiveMerchant::Billing::OppGateway

Public Class Methods

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

Public Instance Methods

authorize(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 131
def authorize(money, payment, options = {})
  # preauthorization PA
  execute_dbpa('PA', money, payment, options)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 136
def capture(money, authorization, options = {})
  # capture CP
  execute_referencing('CP', money, authorization, options)
end
purchase(money, payment, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 125
def purchase(money, payment, options = {})
  # debit
  options[:registrationId] = payment if payment.is_a?(String)
  execute_dbpa(options[:risk_workflow] ? 'PA.CP' : 'DB', money, payment, options)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 141
def refund(money, authorization, options = {})
  # refund RF
  execute_referencing('RF', money, authorization, options)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 166
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Bearer )\w+)i, '\1[FILTERED]').
    gsub(%r((card\.number=)\d+), '\1[FILTERED]').
    gsub(%r((card\.cvv=)\d+), '\1[FILTERED]')
end
store(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 158
def store(credit_card, options = {})
  execute_store(credit_card, options.merge(store: true))
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 162
def supports_scrubbing?
  true
end
verify(credit_card, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 151
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/opp.rb, line 146
def void(authorization, options = {})
  # reversal RV
  execute_referencing('RV', nil, authorization, options)
end

Private Instance Methods

add_3d_secure(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 276
def add_3d_secure(post, options)
  return unless options[:eci] && options[:cavv] && options[:xid]

  post[:threeDSecure] = {
    eci: options[:eci],
    verificationId: options[:cavv],
    xid: options[:xid]
  }
end
add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 225
def add_address(post, options)
  if billing_address = options[:billing_address] || options[:address]
    address(post, billing_address, 'billing')
  end
  if shipping_address = options[:shipping_address]
    address(post, shipping_address, 'shipping')
    if shipping_address[:name]
      firstname, lastname = shipping_address[:name].split(' ')
      post[:shipping] = { givenName: firstname, surname: lastname }
    end
  end
end
add_authentication(post) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 203
def add_authentication(post)
  post[:authentication] = { entityId: @options[:entity_id] }
end
add_customer_data(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 207
def add_customer_data(post, payment, options)
  if options[:customer]
    post[:customer] = {
      merchantCustomerId:  options[:customer][:merchant_customer_id],
      givenName:  options[:customer][:givenname] || payment.first_name,
      surname:  options[:customer][:surname] || payment.last_name,
      birthDate:  options[:customer][:birth_date],
      phone:  options[:customer][:phone],
      mobile:  options[:customer][:mobile],
      email:  options[:customer][:email] || options[:email],
      companyName:  options[:customer][:company_name],
      identificationDocType:  options[:customer][:identification_doctype],
      identificationDocId:  options[:customer][:identification_docid],
      ip:  options[:customer][:ip] || options[:ip]
    }
  end
end
add_invoice(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 249
def add_invoice(post, money, options)
  post[:amount] = amount(money)
  post[:currency] = options[:currency] || currency(money) unless post[:paymentType] == 'RV'
  post[:descriptor] = options[:description] || options[:descriptor]
  post[:merchantInvoiceId] = options[:merchantInvoiceId] || options[:order_id]
  post[:merchantTransactionId] = options[:merchant_transaction_id] || generate_unique_id
end
add_options(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 286
def add_options(post, options)
  post[:createRegistration] = options[:create_registration] if options[:create_registration] && !options[:registrationId]
  post[:testMode] = options[:test_mode] if test? && options[:test_mode]
  options.each { |key, value| post[key] = value if key.to_s =~ /'customParameters\[[a-zA-Z0-9\._]{3,64}\]'/ }
  post['customParameters[SHOPPER_pluginId]'] = 'activemerchant'
  post['customParameters[custom_disable3DSecure]'] = options[:disable_3d_secure] if options[:disable_3d_secure]
end
add_payment_method(post, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 257
def add_payment_method(post, payment, options)
  return if payment.is_a?(String)

  if options[:registrationId]
    post[:card] = {
      cvv: payment.verification_value
    }
  else
    post[:paymentBrand] = payment.brand.upcase
    post[:card] = {
      holder: payment.name,
      number: payment.number,
      expiryMonth: '%02d' % payment.month,
      expiryYear: payment.year,
      cvv: payment.verification_value
    }
  end
end
address(post, address, prefix) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 238
def address(post, address, prefix)
  post[prefix] = {
    street1: address[:address1],
    street2: address[:address2],
    city: address[:city],
    state: address[:state],
    postcode: address[:zip],
    country: address[:country]
  }
end
authorization_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 372
def authorization_from(response)
  response['id']
end
build_url(url, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 294
def build_url(url, authorization, options)
  if options[:store]
    url.gsub(/payments/, 'registrations')
  elsif options[:registrationId]
    "#{url.gsub(/payments/, 'registrations')}/#{options[:registrationId]}/payments"
  elsif authorization
    "#{url}/#{authorization}"
  else
    url
  end
end
commit(post, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 306
def commit(post, authorization, options)
  url = build_url(test? ? test_url : live_url, authorization, options)
  add_authentication(post)
  post = flatten_hash(post)

  response =
    begin
      parse(
        ssl_post(
          url,
          post.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&'),
          headers
        )
      )
    rescue ResponseError => e
      parse(e.response.body)
    end

  success = success_from(response)

  Response.new(
    success,
    message_from(response),
    response,
    authorization: authorization_from(response),
    test: test?,
    error_code: success ? nil : error_code_from(response)
  )
end
error_code_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 376
def error_code_from(response)
  response['result']['code']
end
execute_dbpa(txtype, money, payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 184
def execute_dbpa(txtype, money, payment, options)
  post = {}
  post[:paymentType] = txtype
  add_invoice(post, money, options)
  add_payment_method(post, payment, options)
  add_address(post, options)
  add_customer_data(post, payment, options)
  add_options(post, options)
  add_3d_secure(post, options)
  commit(post, nil, options)
end
execute_referencing(txtype, money, authorization, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 196
def execute_referencing(txtype, money, authorization, options)
  post = {}
  post[:paymentType] = txtype
  add_invoice(post, money, options)
  commit(post, authorization, options)
end
execute_store(payment, options) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 175
def execute_store(payment, options)
  post = {}
  add_payment_method(post, payment, options)
  add_address(post, options)
  add_options(post, options)
  add_3d_secure(post, options)
  commit(post, nil, options)
end
flatten_hash(hash) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 380
def flatten_hash(hash)
  hash.each_with_object({}) do |(k, v), h|
    if v.is_a? Hash
      flatten_hash(v).map do |h_k, h_v|
        h["#{k}.#{h_k}".to_sym] = h_v
      end
    else
      h[k] = v
    end
  end
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 336
def headers
  {
    'Content-Type' => 'application/x-www-form-urlencoded;charset=UTF-8',
    'Authorization' => "Bearer #{@options[:access_token]}"
  }
end
json_error(body) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 349
def json_error(body)
  message = "Invalid response received #{body.inspect}"
  { 'result' => { 'description' => message, 'code' => 'unknown' } }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 366
def message_from(response)
  return 'Failed' unless response['result']

  response['result']['description']
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 343
def parse(body)
  JSON.parse(body)
rescue JSON::ParserError
  json_error(body)
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/opp.rb, line 354
def success_from(response)
  return false unless response['result']

  success_regex = /^(000\.000\.|000\.100\.1|000\.[36])/

  if success_regex.match?(response['result']['code'])
    true
  else
    false
  end
end