class ActiveMerchant::Billing::FirstGivingGateway

Public Class Methods

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

Public Instance Methods

purchase(money, creditcard, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 20
def purchase(money, creditcard, options = {})
  post = {}
  add_invoice(post, options)
  add_creditcard(post, creditcard)
  add_address(post, options)
  add_customer_data(post, options)
  add_donation_data(post, money, options)
  commit('/donation/creditcard', post)
end
refund(money, identifier, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 30
def refund(money, identifier, options = {})
  get = {}
  get[:transactionId] = identifier
  get[:tranType] = 'REFUNDREQUEST'
  commit('/transaction/refundrequest?' + encode(get))
end

Private Instance Methods

add_address(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 51
def add_address(post, options)
  if (billing_address = (options[:billing_address] || options[:address]))
    post[:billToAddressLine1]  = billing_address[:address1]
    post[:billToCity]          = billing_address[:city]
    post[:billToState]         = billing_address[:state]
    post[:billToZip]           = billing_address[:zip]
    post[:billToCountry]       = billing_address[:country]
  end
end
add_creditcard(post, creditcard) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 65
def add_creditcard(post, creditcard)
  post[:billToFirstName]     = creditcard.first_name
  post[:billToLastName]      = creditcard.last_name
  post[:ccNumber]            = creditcard.number
  post[:ccType]              = creditcard_brand(creditcard.brand)
  post[:ccExpDateMonth]      = creditcard.month
  post[:ccExpDateYear]       = creditcard.year
  post[:ccCardValidationNum] = creditcard.verification_value
end
add_customer_data(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 46
def add_customer_data(post, options)
  post[:billToEmail] = (options[:email] || 'activemerchant@example.com')
  post[:remoteAddr]  = (options[:ip] || '127.0.0.1')
end
add_donation_data(post, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 39
def add_donation_data(post, money, options)
  post[:amount] = amount(money)
  post[:charityId] = @options[:charity_id]
  post[:description] = (options[:description] || 'Purchase')
  post[:currencyCode] = (options[:currency] || currency(money))
end
add_invoice(post, options) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 61
def add_invoice(post, options)
  post[:orderId] = options[:order_id]
end
commit(action, post = nil) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 93
def commit(action, post = nil)
  url = (test? ? self.test_url : self.live_url) + action

  begin
    if post
      response = parse(ssl_post(url, post_data(post), headers))
    else
      response = parse(ssl_get(url, headers))
    end
  rescue ResponseError => e
    response = parse(e.response.body)
  end

  Response.new(
    (response['acknowledgement'] == 'Success'),
    (response['friendlyErrorMessage'] || response['verboseErrorMessage'] || response['acknowledgement']),
    response,
    authorization: response['transactionId'],
    test: test?
  )
end
creditcard_brand(brand) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 123
def creditcard_brand(brand)
  case brand
  when 'visa' then 'VI'
  when 'master' then 'MC'
  when 'discover' then 'DI'
  when 'american_express' then 'AX'
  else
    raise "Unhandled credit card brand #{brand}"
  end
end
encode(hash) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 119
def encode(hash)
  hash.collect { |(k, v)| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 134
def headers
  {
    'User-Agent'        => "ActiveMerchantBindings/#{ActiveMerchant::VERSION}",
    'JG_APPLICATIONKEY' => @options[:application_key].to_s,
    'JG_SECURITYTOKEN'  => @options[:security_token].to_s
  }
end
parse(body) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 75
def parse(body)
  response = {}

  xml = Nokogiri::XML(body)
  element = xml.xpath('//firstGivingDonationApi/firstGivingResponse').first

  element.attributes.each do |name, attribute|
    response[name] = attribute.content
  end
  element.children.each do |child|
    next if child.text?

    response[child.name] = child.text
  end

  response
end
post_data(post) click to toggle source
# File lib/active_merchant/billing/gateways/first_giving.rb, line 115
def post_data(post)
  post.collect { |key, value| "#{key}=#{CGI.escape(value.to_s)}" }.join('&')
end