class Spreedly::Environment

Attributes

base_url[R]
currency_code[R]
key[R]

Public Class Methods

gateway_options() click to toggle source
# File lib/spreedly/environment.rb, line 125
def self.gateway_options
  self.new("", "").gateway_options
end
new(environment_key, access_secret, options={}) click to toggle source
# File lib/spreedly/environment.rb, line 12
def initialize(environment_key, access_secret, options={})
  @key, @access_secret = environment_key, access_secret
  @base_url = options[:base_url] || "https://core.spreedly.com"
  @currency_code = options[:currency_code] || 'USD'
end
receiver_options() click to toggle source
# File lib/spreedly/environment.rb, line 134
def self.receiver_options
  self.new("", "").receiver_options
end

Public Instance Methods

add_credit_card(options) click to toggle source
# File lib/spreedly/environment.rb, line 150
def add_credit_card(options)
  api_post(add_payment_method_url, add_credit_card_body(options), false)
end
add_gateway(gateway_type, credentials = {}) click to toggle source
# File lib/spreedly/environment.rb, line 138
def add_gateway(gateway_type, credentials = {})
  body = add_gateway_body(gateway_type, credentials)
  xml_doc = ssl_post(add_gateway_url, body, headers)
  Gateway.new(xml_doc)
end
add_receiver(receiver_type, host_names = nil, credentials = []) click to toggle source
# File lib/spreedly/environment.rb, line 144
def add_receiver(receiver_type, host_names = nil, credentials = [])
  body = add_receiver_body(receiver_type, host_names, credentials)
  xml_doc = ssl_post(add_receiver_url, body, headers)
  Receiver.new(xml_doc)
end
authorize_on_gateway(gateway_token, payment_method_token, amount, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 47
def authorize_on_gateway(gateway_token, payment_method_token, amount, options = {})
  body = auth_purchase_body(amount, payment_method_token, options)
  api_post(authorize_url(gateway_token), body)
end
capture_transaction(authorization_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 61
def capture_transaction(authorization_token, options = {})
  api_post(capture_url(authorization_token), capture_body(options))
end
complete_transaction(transaction_token) click to toggle source
# File lib/spreedly/environment.rb, line 52
def complete_transaction(transaction_token)
  api_post(complete_transaction_url(transaction_token), '')
end
deliver_to_receiver(receiver_token, payment_method_token, receiver_options) click to toggle source
# File lib/spreedly/environment.rb, line 160
def deliver_to_receiver(receiver_token, payment_method_token, receiver_options)
  body = deliver_to_receiver_body(payment_method_token, receiver_options)
  api_post(deliver_to_receiver_url(receiver_token), body)
end
find_gateway(token) click to toggle source
# File lib/spreedly/environment.rb, line 37
def find_gateway(token)
  xml_doc = ssl_get(find_gateway_url(token), headers)
  Gateway.new(xml_doc)
end
find_payment_method(token) click to toggle source
# File lib/spreedly/environment.rb, line 22
def find_payment_method(token)
  xml_doc = ssl_get(find_payment_method_url(token), headers)
  PaymentMethod.new_from(xml_doc)
end
find_transaction(token) click to toggle source
# File lib/spreedly/environment.rb, line 27
def find_transaction(token)
  xml_doc = ssl_get(find_transaction_url(token), headers)
  Transaction.new_from(xml_doc)
end
find_transcript(transaction_token) click to toggle source
# File lib/spreedly/environment.rb, line 32
def find_transcript(transaction_token)
  transcript_url = find_transcript_url(transaction_token)
  ssl_raw_get(transcript_url, headers)
end
gateway_options() click to toggle source
# File lib/spreedly/environment.rb, line 120
def gateway_options
  xml_doc = ssl_options(gateway_options_url)
  GatewayClass.new_list_from(xml_doc)
end
list_gateways(since_token = nil) click to toggle source
# File lib/spreedly/environment.rb, line 110
def list_gateways(since_token = nil)
  xml_doc = ssl_get(list_gateways_url(since_token), headers)
  Gateway.new_list_from(xml_doc)
end
list_payment_methods(since_token = nil) click to toggle source
# File lib/spreedly/environment.rb, line 105
def list_payment_methods(since_token = nil)
  xml_doc = ssl_get(list_payment_methods_url(since_token), headers)
  PaymentMethod.new_list_from(xml_doc)
end
list_transactions(since_token = nil, payment_method_token = nil, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 100
def list_transactions(since_token = nil, payment_method_token = nil, options = {})
  xml_doc = ssl_get(list_transactions_url(since_token, payment_method_token, options), headers)
  Transaction.new_list_from(xml_doc)
end
purchase_on_gateway(gateway_token, payment_method_token, amount, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 42
def purchase_on_gateway(gateway_token, payment_method_token, amount, options = {})
  body = auth_purchase_body(amount, payment_method_token, options)
  api_post(purchase_url(gateway_token), body)
end
recache_payment_method(payment_method_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 84
def recache_payment_method(payment_method_token, options = {})
  body = recache_payment_method_body(options)
  xml_doc = ssl_put(recache_payment_method_url(payment_method_token), body, headers)
  RecacheSensitiveData.new_from(xml_doc)
end
receiver_options() click to toggle source
# File lib/spreedly/environment.rb, line 129
def receiver_options
  xml_doc = ssl_get(receiver_options_url, headers)
  ReceiverClass.new_list_from(xml_doc)
end
redact_gateway(gateway_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 90
def redact_gateway(gateway_token, options = {})
  xml_doc = ssl_put(redact_gateway_url(gateway_token), '', headers)
  Transaction.new_from(xml_doc)
end
redact_payment_method(payment_method_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 78
def redact_payment_method(payment_method_token, options = {})
  body = redact_payment_method_body(options)
  xml_doc = ssl_put(redact_payment_method_url(payment_method_token), body, headers)
  Transaction.new_from(xml_doc)
end
redact_receiver(receiver_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 95
def redact_receiver(receiver_token, options = {})
  xml_doc = ssl_put(redact_receiver_url(receiver_token), '', headers)
  Transaction.new_from(xml_doc)
end
refund_transaction(token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 69
def refund_transaction(token, options = {})
  api_post(refund_transaction_url(token), refund_body(options))
end
retain_payment_method(payment_method_token) click to toggle source
# File lib/spreedly/environment.rb, line 73
def retain_payment_method(payment_method_token)
  xml_doc = ssl_put(retain_payment_method_url(payment_method_token), '', headers)
  Transaction.new_from(xml_doc)
end
store_on_gateway(gateway_token, payment_method_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 115
def store_on_gateway(gateway_token, payment_method_token, options = {})
  body = store_body(payment_method_token, options)
  api_post(store_url(gateway_token), body)
end
transparent_redirect_form_action() click to toggle source
# File lib/spreedly/environment.rb, line 18
def transparent_redirect_form_action
  "#{base_url}/v1/payment_methods"
end
update_credit_card(credit_card_token, options) click to toggle source
# File lib/spreedly/environment.rb, line 154
def update_credit_card(credit_card_token, options)
  body = update_credit_card_body(options)
  xml_doc = ssl_put(update_payment_method_url(credit_card_token), body, headers)
  PaymentMethod.new_from(xml_doc)
end
verify_on_gateway(gateway_token, payment_method_token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 56
def verify_on_gateway(gateway_token, payment_method_token, options = {})
  body = verify_body(payment_method_token, options)
  api_post(verify_url(gateway_token), body)
end
void_transaction(token, options = {}) click to toggle source
# File lib/spreedly/environment.rb, line 65
def void_transaction(token, options = {})
  api_post(void_transaction_url(token), void_body(options))
end

Private Instance Methods

add_credentials_to_doc(doc, credentials) click to toggle source
# File lib/spreedly/environment.rb, line 256
def add_credentials_to_doc(doc, credentials)
  doc.credentials do
    credentials.each do |credential|
      doc.credential do
        add_to_doc(doc, credential, :name, :value, :safe)
      end
    end
  end
end
add_credit_card_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 266
def add_credit_card_body(options)
  build_xml_request('payment_method') do |doc|
    add_to_doc(doc, options, :data, :retained, :email)
    doc.credit_card do
      add_to_doc(doc, options, :number, :verification_value, :month, :full_name, :first_name, :last_name,
                 :year, :address1, :address2, :city, :state, :zip, :country, :phone_number,
                 :company, :eligible_for_card_updater)
    end
  end
end
add_extra_options_for_basic_ops(doc, options) click to toggle source
# File lib/spreedly/environment.rb, line 304
def add_extra_options_for_basic_ops(doc, options)
  add_gateway_specific_fields(doc, options)
  add_shipping_address_override(doc, options)
  add_to_doc(doc, options, :order_id, :description, :ip, :email, :merchant_name_descriptor,
                           :merchant_location_descriptor, :redirect_url, :callback_url,
                           :continue_caching, :attempt_3dsecure, :browser_info, :three_ds_version, :channel)
end
add_gateway_body(gateway_type, credentials) click to toggle source
# File lib/spreedly/environment.rb, line 234
def add_gateway_body(gateway_type, credentials)
  build_xml_request('gateway') do |doc|
    doc.gateway_type gateway_type
    add_to_doc(doc, credentials, *credentials.keys)
  end
end
add_gateway_specific_fields(doc, options) click to toggle source
# File lib/spreedly/environment.rb, line 312
def add_gateway_specific_fields(doc, options)
  return unless options[:gateway_specific_fields].kind_of?(Hash)
  doc << "<gateway_specific_fields>#{xml_for_hash(options[:gateway_specific_fields])}</gateway_specific_fields>"
end
add_receiver_body(receiver_type, host_names, credentials) click to toggle source
# File lib/spreedly/environment.rb, line 241
def add_receiver_body(receiver_type, host_names, credentials)
  build_xml_request('receiver') do |doc|
    doc.receiver_type receiver_type
    doc.hostnames(host_names) if host_names
    add_credentials_to_doc(doc, credentials) if credentials && !credentials.empty?
  end
end
add_shipping_address_override(doc, options) click to toggle source
# File lib/spreedly/environment.rb, line 317
def add_shipping_address_override(doc, options)
  return unless options[:shipping_address].kind_of?(Hash)
  doc.send(:shipping_address) do
    options[:shipping_address].each do |k, v|
      doc.send(k, v)
    end
  end
end
add_to_doc(doc, options, *attributes) click to toggle source
# File lib/spreedly/environment.rb, line 298
def add_to_doc(doc, options, *attributes)
  attributes.each do |attr|
    doc.send(attr, options[attr.to_sym]) if options[attr.to_sym] != nil
  end
end
api_post(url, body, talking_to_gateway = true) click to toggle source
# File lib/spreedly/environment.rb, line 341
def api_post(url, body, talking_to_gateway = true)
  xml_doc = ssl_post(url, body, headers, talking_to_gateway)
  Transaction.new_from(xml_doc)
end
auth_purchase_body(amount, payment_method_token, options) click to toggle source
# File lib/spreedly/environment.rb, line 173
def auth_purchase_body(amount, payment_method_token, options)
  build_xml_request('transaction') do |doc|
    doc.amount amount
    doc.currency_code(options[:currency_code] || currency_code)
    doc.payment_method_token(payment_method_token)
    add_to_doc(doc, options, :retain_on_success)
    add_to_doc(doc, options, :stored_credential_initiator)
    add_to_doc(doc, options, :stored_credential_reason_type)
    add_extra_options_for_basic_ops(doc, options)
  end
end
build_xml_request(root) { |doc| ... } click to toggle source
# File lib/spreedly/environment.rb, line 333
def build_xml_request(root)
  builder = Nokogiri::XML::Builder.new
  builder.__send__(root) do |doc|
    yield(doc)
  end
  builder.to_xml
end
capture_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 193
def capture_body(options)
  return '' if options.empty?

  build_xml_request('transaction') do |doc|
    add_to_doc(doc, options, :amount, :currency_code)
    add_extra_options_for_basic_ops(doc, options)
  end
end
deliver_to_receiver_body(payment_method_token, receiver_options) click to toggle source
# File lib/spreedly/environment.rb, line 285
def deliver_to_receiver_body(payment_method_token, receiver_options)
  build_xml_request('delivery') do |doc|
    doc.payment_method_token payment_method_token
    doc.url receiver_options[:url]
    doc.headers do
      doc.cdata receiver_options[:headers].map { |k, v| "#{k}: #{v}" }.join("\r\n")
    end
    doc.body do
      doc.cdata receiver_options[:body]
    end
  end
end
headers() click to toggle source
# File lib/spreedly/environment.rb, line 166
def headers
  {
    'Authorization' => ('Basic ' + Base64.strict_encode64("#{@key}:#{@access_secret}").chomp),
    'Content-Type' => 'text/xml'
  }
end
recache_payment_method_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 226
def recache_payment_method_body(options)
  build_xml_request('payment_method') do |doc|
    doc.credit_card do
      add_to_doc(doc, options, :verification_value)
    end
  end
end
redact_payment_method_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 219
def redact_payment_method_body(options)
  return '' if options.empty?
  build_xml_request('transaction') do |doc|
    add_to_doc(doc, options, :remove_from_gateway)
  end
end
refund_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 210
def refund_body(options)
  return '' if options.empty?

  build_xml_request('transaction') do |doc|
    add_to_doc(doc, options, :amount, :currency_code)
    add_extra_options_for_basic_ops(doc, options)
  end
end
store_body(payment_method_token, options) click to toggle source
# File lib/spreedly/environment.rb, line 249
def store_body(payment_method_token, options)
  build_xml_request('transaction') do |doc|
    doc.payment_method_token(payment_method_token)
    add_gateway_specific_fields(doc, options)
  end
end
update_credit_card_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 277
def update_credit_card_body(options)
  build_xml_request('payment_method') do |doc|
    add_to_doc(doc, options, :email, :month, :full_name, :first_name, :last_name, :year,
               :address1, :address2, :city, :state, :zip, :country, :phone_number,
               :eligible_for_card_updater)
  end
end
verify_body(payment_method_token, options) click to toggle source
# File lib/spreedly/environment.rb, line 185
def verify_body(payment_method_token, options)
  build_xml_request('transaction') do |doc|
    doc.payment_method_token(payment_method_token)
    add_to_doc(doc, options, :retain_on_success)
    add_extra_options_for_basic_ops(doc, options)
  end
end
void_body(options) click to toggle source
# File lib/spreedly/environment.rb, line 202
def void_body(options)
  return '' if options.empty?

  build_xml_request('transaction') do |doc|
    add_extra_options_for_basic_ops(doc, options)
  end
end
xml_for_hash(hash) click to toggle source
# File lib/spreedly/environment.rb, line 326
def xml_for_hash(hash)
  hash.map do |key, value|
    text = value.kind_of?(Hash) ? xml_for_hash(value) : value
    "<#{key}>#{text}</#{key}>"
  end.join
end