class SpreedlyCore::PaymentMethod

Attributes

address1[R]
address2[R]
card_type[R]
city[R]
country[R]
created_at[R]
data[R]
email[R]
errors[R]
first_name[R]
last_four_digits[R]
last_name[R]
month[R]
number[R]
payment_method_type[R]
phone_number[R]
state[R]
token[R]
updated_at[R]
verification_value[R]
year[R]
zip[R]

Public Class Methods

additional_required_cc_fields(*fields) click to toggle source

configure additional required fiels. Like :address1, :city, :state

# File lib/spreedly-core-ruby/payment_method.rb, line 9
def self.additional_required_cc_fields *fields
  @@additional_required_fields ||= Set.new
  @@additional_required_fields += fields
end
create(credit_card) click to toggle source
# File lib/spreedly-core-ruby/payment_method.rb, line 28
def self.create(credit_card)
  verify_post("/payment_methods.xml", :body => {:payment_method => { :credit_card => credit_card }}) do |response|
    AddPaymentMethodTransaction.new(response.parsed_response["transaction"])
  end
end
create_test_token(cc_overrides = {}) click to toggle source

Call spreedly to create a test token. pass_through_data will be added as the “data” field.

# File lib/spreedly-core-ruby/test_extensions.rb, line 44
def self.create_test_token(cc_overrides = {})
  card = {
    :first_name => "John",
    :last_name => "Foo",
    :card_type => :visa,
    :number => '4111111111111111',
    :verification_value => 123,
    :month => 4,
    :year => Time.now.year + 1
  }
  if cc_overrides.is_a?(Hash)
    overrides = cc_overrides[:credit_card] || cc_overrides["credit_card"] || cc_overrides
    card.merge!(overrides)
  end

  pm = PaymentMethod.create(card)
  pm.payment_method["token"]
end
find(token) click to toggle source

Lookup the PaymentMethod by token

# File lib/spreedly-core-ruby/payment_method.rb, line 20
def self.find(token)
  return nil if token.nil?
  verify_get("/payment_methods/#{token}.xml",
             :has_key => "payment_method") do |response|
    new(response.parsed_response["payment_method"])
  end
end
new(attrs={}) click to toggle source

Create a new PaymentMethod based on the attrs hash and then validate

Calls superclass method SpreedlyCore::Base::new
# File lib/spreedly-core-ruby/payment_method.rb, line 35
def initialize(attrs={})
  super(attrs)
  validate
end
reset_additional_required_cc_fields() click to toggle source

clear the configured additional required fields

# File lib/spreedly-core-ruby/payment_method.rb, line 15
def self.reset_additional_required_cc_fields
  @@additional_required_fields = Set.new
end
submit_url() click to toggle source

Returns the URL that CC data should be submitted to.

# File lib/spreedly-core-ruby/payment_method.rb, line 77
def self.submit_url
  Base.base_uri + '/payment_methods'
end

Public Instance Methods

authorize(amount, *args) click to toggle source

Make an authorize against payment method. You can then later capture against the authorize

# File lib/spreedly-core-ruby/payment_method.rb, line 60
def authorize(amount, *args)
  purchase_or_authorize(:authorize, amount, *args)
end
purchase(amount, *args) click to toggle source

Make a purchase against the payment method

# File lib/spreedly-core-ruby/payment_method.rb, line 55
def purchase(amount, *args)
  purchase_or_authorize(:purchase, amount, *args)
end
redact() click to toggle source

Redact the payment method

# File lib/spreedly-core-ruby/payment_method.rb, line 48
def redact
  self.class.verify_put("/payment_methods/#{token}/redact.xml", :body => {}, :has_key => "transaction") do |response|
    RedactTransaction.new(response.parsed_response["transaction"])
  end
end
retain() click to toggle source

Retain the payment method

# File lib/spreedly-core-ruby/payment_method.rb, line 41
def retain
  self.class.verify_put("/payment_methods/#{token}/retain.xml", :body => {}, :has_key => "transaction") do |response|
    RetainTransaction.new(response.parsed_response["transaction"])
  end
end
update(attributes) click to toggle source

Update the attributes of a payment method

# File lib/spreedly-core-ruby/payment_method.rb, line 65
def update(attributes)
  opts = {
    :headers => {"Content-Type" => "application/xml"},
    :body => attributes.to_xml(:root => "payment_method", :dasherize => false)
  }

  self.class.verify_put("/payment_methods/#{token}.xml", opts) do |response|
    PaymentMethod.new(response.parsed_response["payment_method"])
  end
end
valid?() click to toggle source
# File lib/spreedly-core-ruby/payment_method.rb, line 81
def valid?
  @errors.empty?
end

Protected Instance Methods

purchase_or_authorize(tran_type, amount, *args) click to toggle source
# File lib/spreedly-core-ruby/payment_method.rb, line 107
def purchase_or_authorize(tran_type, amount, *args)
  options = if(args.size == 1 && args.first.kind_of?(Hash))
    args.first
  else
    {
      :currency => args[0],
      :gateway_token => args[1],
      :ip_address => args[2]
    }
  end

  transaction_type = tran_type.to_s
  raise "Unknown transaction type" unless %w{purchase authorize}.include?(transaction_type)

  currency = (options[:currency] || "USD")
  gateway_token = (options[:gateway_token] || self.class.gateway_token)
  path = "/gateways/#{gateway_token}/#{transaction_type}.xml"
  data = {
    :transaction => {
      :transaction_type => transaction_type,
      :payment_method_token => token,
      :amount => amount,
      :currency_code => currency,
      :ip => options[:ip_address],
      :redirect_url => options[:redirect_url],
      :callback_url => options[:callback_url]
    }
  }
  self.class.verify_post(path, :body => data,
                         :has_key => "transaction") do |response|
    klass = SpreedlyCore.const_get("#{transaction_type.capitalize}Transaction")
    klass.new(response.parsed_response["transaction"])
  end
end
validate() click to toggle source

Validate additional cc fields like first_name, last_name, etc when configured to do so

# File lib/spreedly-core-ruby/payment_method.rb, line 89
def validate
  return if @has_been_validated
  @has_been_validated = true
  self.class.additional_required_cc_fields.each do |field|
    if instance_variable_get("@#{field}").blank?
      str_field = field.to_s
      friendly_name = if(str_field.respond_to?(:humanize))
        str_field.humanize
      else
        str_field.split("_").join(" ")
      end

      @errors << "#{friendly_name.capitalize} can't be blank"
    end
  end
  @errors = @errors.sort
end