class ActiveMerchant::Billing::SpreedlyCoreGateway

Public: This gateway allows you to interact with any gateway you’ve created in Spreedly (spreedly.com). It’s an adapter which can be particularly useful if you already have code interacting with ActiveMerchant and want to easily take advantage of Spreedly’s vault.

Public Class Methods

new(options = {}) click to toggle source

Public: Create a new Spreedly gateway.

options - A hash of options:

:login         - The environment key.
:password      - The access secret.
:gateway_token - The token of the gateway you've created in
                 Spreedly.
Calls superclass method ActiveMerchant::Billing::Gateway::new
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 30
def initialize(options = {})
  requires!(options, :login, :password, :gateway_token)
  super
end

Public Instance Methods

authorize(money, payment_method, options = {}) click to toggle source

Public: Run an authorize transaction.

money - The monetary amount of the transaction in cents. payment_method - The CreditCard or the Spreedly payment method token. options - A hash of options:

:store - Retain the payment method if the authorize
         succeeds.  Defaults to false.  (optional)
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 54
def authorize(money, payment_method, options = {})
  request = build_transaction_request(money, payment_method, options)
  commit("gateways/#{@options[:gateway_token]}/authorize.xml", request)
end
capture(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 59
def capture(money, authorization, options = {})
  request = build_xml_request('transaction') do |doc|
    add_invoice(doc, money, options)
  end

  commit("transactions/#{authorization}/capture.xml", request)
end
find(transaction_token) click to toggle source

Public: Get the transaction with the given token.

# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 116
def find(transaction_token)
  commit("transactions/#{transaction_token}.xml", nil, :get)
end
Also aliased as: status
purchase(money, payment_method, options = {}) click to toggle source

Public: Run a purchase transaction.

money - The monetary amount of the transaction in cents. payment_method - The CreditCard or Check or the Spreedly payment method token. options - A hash of options:

:store - Retain the payment method if the purchase
         succeeds.  Defaults to false.  (optional)
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 42
def purchase(money, payment_method, options = {})
  request = build_transaction_request(money, payment_method, options)
  commit("gateways/#{options[:gateway_token] || @options[:gateway_token]}/purchase.xml", request)
end
refund(money, authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 67
def refund(money, authorization, options = {})
  request = build_xml_request('transaction') do |doc|
    add_invoice(doc, money, options)
    add_extra_options(:gateway_specific_fields, doc, options)
  end

  commit("transactions/#{authorization}/credit.xml", request)
end
scrub(transcript) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 126
def scrub(transcript)
  transcript.
    gsub(%r((Authorization: Basic )\w+), '\1[FILTERED]').
    gsub(%r((<number>).+(</number>)), '\1[FILTERED]\2').
    gsub(%r((<verification_value>).+(</verification_value>)), '\1[FILTERED]\2').
    gsub(%r((<payment_method_token>).+(</payment_method_token>)), '\1[FILTERED]\2')
end
status(transaction_token)
Alias for: find
store(credit_card, options = {}) click to toggle source

Public: Store a credit card in the Spreedly vault and retain it.

credit_card - The CreditCard to store options - A standard ActiveMerchant options hash

# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 101
def store(credit_card, options = {})
  retain = (options.has_key?(:retain) ? options[:retain] : true)
  save_card(retain, credit_card, options)
end
supports_scrubbing?() click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 122
def supports_scrubbing?
  true
end
unstore(authorization, options = {}) click to toggle source

Public: Redact the CreditCard in Spreedly. This wipes the sensitive

payment information from the card.

credit_card - The CreditCard to store options - A standard ActiveMerchant options hash

# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 111
def unstore(authorization, options = {})
  commit("payment_methods/#{authorization}/redact.xml", '', :put)
end
verify(payment_method, options = {}) click to toggle source

Public: Determine whether a credit card is chargeable card and available for purchases.

payment_method - The CreditCard or the Spreedly payment method token. options - A hash of options:

:store - Retain the payment method if the verify
         succeeds.  Defaults to false.  (optional)
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 86
def verify(payment_method, options = {})
  if payment_method.is_a?(String)
    verify_with_token(payment_method, options)
  else
    MultiResponse.run do |r|
      r.process { save_card(options[:store], payment_method, options) }
      r.process { verify_with_token(r.authorization, options) }
    end
  end
end
void(authorization, options = {}) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 76
def void(authorization, options = {})
  commit("transactions/#{authorization}/void.xml", '')
end

Private Instance Methods

add_bank_account(doc, bank_account, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 212
def add_bank_account(doc, bank_account, options)
  doc.bank_account do
    doc.first_name(bank_account.first_name)
    doc.last_name(bank_account.last_name)
    doc.bank_routing_number(bank_account.routing_number)
    doc.bank_account_number(bank_account.account_number)
    doc.bank_account_type(bank_account.account_type)
    doc.bank_account_holder_type(bank_account.account_holder_type)
  end
end
add_credit_card(doc, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 194
def add_credit_card(doc, credit_card, options)
  doc.credit_card do
    doc.number(credit_card.number)
    doc.verification_value(credit_card.verification_value)
    doc.first_name(credit_card.first_name)
    doc.last_name(credit_card.last_name)
    doc.month(credit_card.month)
    doc.year(credit_card.year)
    doc.email(options[:email])
    doc.address1(options[:billing_address].try(:[], :address1))
    doc.address2(options[:billing_address].try(:[], :address2))
    doc.city(options[:billing_address].try(:[], :city))
    doc.state(options[:billing_address].try(:[], :state))
    doc.zip(options[:billing_address].try(:[], :zip))
    doc.country(options[:billing_address].try(:[], :country))
  end
end
add_extra_options(type, doc, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 223
def add_extra_options(type, doc, options)
  doc.send(type) do
    extra_options_to_doc(doc, options[type])
  end
end
add_invoice(doc, money, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 169
def add_invoice(doc, money, options)
  doc.amount amount(money) unless money.nil?
  doc.currency_code(options[:currency] || currency(money) || default_currency)
  doc.order_id(options[:order_id])
  doc.ip(options[:ip]) if options[:ip]
  doc.description(options[:description]) if options[:description]

  doc.merchant_name_descriptor(options[:merchant_name_descriptor]) if options[:merchant_name_descriptor]
  doc.merchant_location_descriptor(options[:merchant_location_descriptor]) if options[:merchant_location_descriptor]
end
add_payment_method(doc, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 180
def add_payment_method(doc, payment_method, options)
  doc.retain_on_success(true) if options[:store]

  if payment_method.is_a?(String)
    doc.payment_method_token(payment_method)
  elsif payment_method.is_a?(CreditCard)
    add_credit_card(doc, payment_method, options)
  elsif payment_method.is_a?(Check)
    add_bank_account(doc, payment_method, options)
  else
    raise TypeError, 'Payment method not supported'
  end
end
authorize_with_token(money, payment_method_token, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 151
def authorize_with_token(money, payment_method_token, options)
  request = build_transaction_request(money, payment_method_token, options)
  commit("gateways/#{@options[:gateway_token]}/authorize.xml", request)
end
build_transaction_request(money, payment_method, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 161
def build_transaction_request(money, payment_method, options)
  build_xml_request('transaction') do |doc|
    add_invoice(doc, money, options)
    add_payment_method(doc, payment_method, options)
    add_extra_options(:gateway_specific_fields, doc, options)
  end
end
build_xml_request(root, &block) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 274
def build_xml_request(root, &block)
  builder = Nokogiri::XML::Builder.new
  builder.__send__(root, &block)
  builder.to_xml
end
childnode_to_response(response, node, childnode) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 256
def childnode_to_response(response, node, childnode)
  node_name = node.name.downcase
  childnode_name = childnode.name.downcase
  composed_name = "#{node_name}_#{childnode_name}"

  childnodes_present = !childnode.elements.empty?

  if childnodes_present && composed_name == 'payment_method_data'
    response[composed_name.to_sym] = Hash.from_xml(childnode.to_s).values.first
  elsif childnodes_present && node_name == 'gateway_specific_response_fields'
    response[node_name.to_sym] = {
      childnode_name => Hash.from_xml(childnode.to_s).values.first
    }
  else
    response[composed_name.to_sym] = childnode.text
  end
end
commit(relative_url, request, method = :post, authorization_field = :token) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 280
def commit(relative_url, request, method = :post, authorization_field = :token)
  begin
    raw_response = ssl_request(method, "#{live_url}/#{relative_url}", request, headers)
  rescue ResponseError => e
    raw_response = e.response.body
  end

  response_from(raw_response, authorization_field)
end
extra_options_to_doc(doc, value) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 229
def extra_options_to_doc(doc, value)
  return doc.text value unless value.kind_of? Hash

  value.each do |k, v|
    doc.send(k) do
      extra_options_to_doc(doc, v)
    end
  end
end
headers() click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 302
def headers
  {
    'Authorization' => ('Basic ' + Base64.strict_encode64("#{@options[:login]}:#{@options[:password]}").chomp),
    'Content-Type' => 'text/xml'
  }
end
parse(xml) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 239
def parse(xml)
  response = {}

  doc = Nokogiri::XML(xml)
  doc.root.xpath('*').each do |node|
    if node.elements.empty?
      response[node.name.downcase.to_sym] = node.text
    else
      node.elements.each do |childnode|
        childnode_to_response(response, node, childnode)
      end
    end
  end

  response
end
purchase_with_token(money, payment_method_token, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 146
def purchase_with_token(money, payment_method_token, options)
  request = build_transaction_request(money, payment_method_token, options)
  commit("gateways/#{options[:gateway_token] || @options[:gateway_token]}/purchase.xml", request)
end
response_from(raw_response, authorization_field) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 290
def response_from(raw_response, authorization_field)
  parsed = parse(raw_response)
  options = {
    authorization: parsed[authorization_field],
    test: (parsed[:on_test_gateway] == 'true'),
    avs_result: { code: parsed[:response_avs_code] },
    cvv_result: parsed[:response_cvv_code]
  }

  Response.new(parsed[:succeeded] == 'true', parsed[:message] || parsed[:error], parsed, options)
end
save_card(retain, credit_card, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 136
def save_card(retain, credit_card, options)
  request = build_xml_request('payment_method') do |doc|
    add_credit_card(doc, credit_card, options)
    add_extra_options(:data, doc, options)
    doc.retained(true) if retain
  end

  commit('payment_methods.xml', request, :post, :payment_method_token)
end
verify_with_token(payment_method_token, options) click to toggle source
# File lib/active_merchant/billing/gateways/spreedly_core.rb, line 156
def verify_with_token(payment_method_token, options)
  request = build_transaction_request(nil, payment_method_token, options)
  commit("gateways/#{@options[:gateway_token]}/verify.xml", request)
end