class ActiveMerchant::Billing::AdyenGateway

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/adyen.rb, line 15
def initialize(options = {})
  requires!(options, :company, :merchant, :password)
  super
end

Public Instance Methods

authorize(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 29
def authorize(money, creditcard, options = {})
  requires!(options, :order_id)

  post = {}
  post[:paymentRequest] = payment_request(money, options)
  post[:paymentRequest][:amount] = amount_hash(money, options[:currency])
  post[:paymentRequest][:card] = credit_card_hash(creditcard)

  if address = (options[:billing_address] || options[:address])
    post[:paymentRequest][:billingAddress] = address_hash(address)
  end

  if options[:shipping_address]
    post[:paymentRequest][:deliveryAddress] = address_hash(options[:shipping_address])
  end

  commit('Payment.authorise', post)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 48
def capture(money, authorization, options = {})
  requires!(options, :order_id)

  post = {}
  post[:modificationRequest] = modification_request(authorization, options)
  post[:modificationRequest][:modificationAmount] = amount_hash(money, options[:currency])

  commit('Payment.capture', post)
end
purchase(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 20
def purchase(money, creditcard, options = {})
  requires!(options, :order_id)

  MultiResponse.run do |r|
    r.process { authorize(money, creditcard, options) }
    r.process { capture(money, r.authorization, options) }
  end
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 58
def refund(money, authorization, options = {})
  requires!(options, :order_id)

  post = {}
  post[:modificationRequest] = modification_request(authorization, options)
  post[:modificationRequest][:modificationAmount] = amount_hash(money, options[:currency])

  commit('Payment.refund', post)
end
verify(creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 77
def verify(creditcard, options = {})
  authorize(0, creditcard, options)
end
void(identification, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 68
def void(identification, options = {})
  requires!(options, :order_id)

  post = {}
  post[:modificationRequest] = modification_request(identification, options)

  commit('Payment.cancel', post)
end

Private Instance Methods

address_hash(address) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 161
def address_hash(address)
  full_address = "#{address[:address1]} #{address[:address2]}"

  {
    :city              => address[:city],
    :street            => full_address.split(/\s+/).keep_if { |x| x !~ /\d/ }.join(' '),
    :houseNumberOrName => full_address.split(/\s+/).keep_if { |x| x =~ /\d/ }.join(' '),
    :postalCode        => address[:zip],
    :stateOrProvince   => address[:state],
    :country           => address[:country]
  }
end
amount_hash(money, currency) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 174
def amount_hash(money, currency)
  {
    :currency => (currency || currency(money)),
    :value    => amount(money)
  }
end
commit(action, post) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 83
def commit(action, post)
  request = post_data(flatten_hash(post.merge(:action => action)))
  raw_response = ssl_post(url, request, headers)
  response = parse(raw_response)

  Response.new(
    success_from(response),
    message_from(response),
    response,
    test: test?,
    authorization: response['pspReference']
  )

rescue ResponseError => e
  case e.response.code
  when '401'
    return Response.new(false, 'Invalid credentials', {}, :test => test?)
  when '500'
    if e.response.body.split(' ')[0] == 'validation'
      return Response.new(false, e.response.body.split(' ', 3)[2], {}, :test => test?)
    end
  end
  raise
end
credit_card_hash(creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 181
def credit_card_hash(creditcard)
  {
    :cvc         => creditcard.verification_value,
    :expiryMonth => format(creditcard.month, :two_digits),
    :expiryYear  => format(creditcard.year, :four_digits),
    :holderName  => creditcard.name,
    :number      => creditcard.number
  }
end
flatten_hash(hash, prefix = nil) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 108
def flatten_hash(hash, prefix = nil)
  flat_hash = {}
  hash.each_pair do |key, val|
    conc_key = prefix.nil? ? key : "#{prefix}.#{key}"
    if val.is_a?(Hash)
      flat_hash.merge!(flatten_hash(val, conc_key))
    else
      flat_hash[conc_key] = val
    end
  end
  flat_hash
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 121
def headers
  {
    'Authorization' => 'Basic ' + Base64.encode64("ws@Company.#{@options[:company]}:#{@options[:password]}").strip
  }
end
message_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 144
def message_from(response)
  return response['resultCode'] if response.has_key?('resultCode') # Payment request
  return response['response'] if response['response'] # Modification request
  "Failure" # Negative fallback in case of error
end
modification_request(reference, options) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 191
def modification_request(reference, options)
  {
    :merchantAccount    => @options[:merchant],
    :originalReference  => reference
  }.keep_if { |_, v| v }
end
parse(response) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 127
def parse(response)
  Hash[
    response.split('&').map do |x|
      key, val = x.split('=', 2)
      [key.split('.').last, CGI.unescape(val)]
    end
  ]
end
payment_request(money, options) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 198
def payment_request(money, options)
  {
    :merchantAccount  => @options[:merchant],
    :reference        => options[:order_id],
    :shopperEmail     => options[:email],
    :shopperIP        => options[:ip],
    :shopperReference => options[:customer]
  }.keep_if { |_, v| v }
end
post_data(data) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 136
def post_data(data)
  data.map do |key, val|
    "#{key}=#{CGI.escape(val.to_s)}"
  end.reduce do |x, y|
    "#{x}&#{y}"
  end
end
success_from(response) click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 150
def success_from(response)
  return true if response.has_key?('authCode')

  successful_responses = %w([capture-received] [cancel-received] [refund-received])
  successful_responses.include?(response['response'])
end
url() click to toggle source
# File lib/active_merchant/billing/gateways/adyen.rb, line 157
def url
  test? ? self.test_url : self.live_url
end