Class: SpreedlyCore::PaymentMethod

Inherits:
Base
  • Object
show all
Defined in:
lib/spreedly-core-ruby/payment_method.rb,
lib/spreedly-core-ruby/test_extensions.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

configure, environment_key, gateway_token, gateway_token=, verify_get, verify_options, verify_post, verify_put, verify_request

Constructor Details

#initialize(attrs = {}) ⇒ PaymentMethod

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



35
36
37
38
# File 'lib/spreedly-core-ruby/payment_method.rb', line 35

def initialize(attrs={})
  super(attrs)
  validate
end

Instance Attribute Details

#address1Object (readonly)

Returns the value of attribute address1.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def address1
  @address1
end

#address2Object (readonly)

Returns the value of attribute address2.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def address2
  @address2
end

#card_typeObject (readonly)

Returns the value of attribute card_type.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def card_type
  @card_type
end

#cityObject (readonly)

Returns the value of attribute city.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def city
  @city
end

#countryObject (readonly)

Returns the value of attribute country.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def country
  @country
end

#created_atObject (readonly)

Returns the value of attribute created_at.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def created_at
  @created_at
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def data
  @data
end

#emailObject (readonly)

Returns the value of attribute email.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def email
  @email
end

#errorsObject (readonly)

Returns the value of attribute errors.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def errors
  @errors
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def first_name
  @first_name
end

#last_four_digitsObject (readonly)

Returns the value of attribute last_four_digits.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def last_four_digits
  @last_four_digits
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def last_name
  @last_name
end

#monthObject (readonly)

Returns the value of attribute month.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def month
  @month
end

#numberObject (readonly)

Returns the value of attribute number.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def number
  @number
end

#payment_method_typeObject (readonly)

Returns the value of attribute payment_method_type.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def payment_method_type
  @payment_method_type
end

#phone_numberObject (readonly)

Returns the value of attribute phone_number.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def phone_number
  @phone_number
end

#stateObject (readonly)

Returns the value of attribute state.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def state
  @state
end

#tokenObject (readonly)

Returns the value of attribute token.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def token
  @token
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def updated_at
  @updated_at
end

#verification_valueObject (readonly)

Returns the value of attribute verification_value.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def verification_value
  @verification_value
end

#yearObject (readonly)

Returns the value of attribute year.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def year
  @year
end

#zipObject (readonly)

Returns the value of attribute zip.



3
4
5
# File 'lib/spreedly-core-ruby/payment_method.rb', line 3

def zip
  @zip
end

Class Method Details

.additional_required_cc_fields(*fields) ⇒ Object

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



9
10
11
12
# 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) ⇒ Object



28
29
30
31
32
# 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 = {}) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object

Lookup the PaymentMethod by token



20
21
22
23
24
25
26
# 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

.reset_additional_required_cc_fieldsObject

clear the configured additional required fields



15
16
17
# File 'lib/spreedly-core-ruby/payment_method.rb', line 15

def self.reset_additional_required_cc_fields
  @@additional_required_fields = Set.new
end

.submit_urlObject

Returns the URL that CC data should be submitted to.



77
78
79
# File 'lib/spreedly-core-ruby/payment_method.rb', line 77

def self.submit_url
  Base.base_uri + '/payment_methods'
end

Instance Method Details

#authorize(amount, *args) ⇒ Object

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



60
61
62
# File 'lib/spreedly-core-ruby/payment_method.rb', line 60

def authorize(amount, *args)
  purchase_or_authorize(:authorize, amount, *args)
end

#purchase(amount, *args) ⇒ Object

Make a purchase against the payment method



55
56
57
# File 'lib/spreedly-core-ruby/payment_method.rb', line 55

def purchase(amount, *args)
  purchase_or_authorize(:purchase, amount, *args)
end

#redactObject

Redact the payment method



48
49
50
51
52
# 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

#retainObject

Retain the payment method



41
42
43
44
45
# 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) ⇒ Object

Update the attributes of a payment method



65
66
67
68
69
70
71
72
73
74
# 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?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/spreedly-core-ruby/payment_method.rb', line 81

def valid?
  @errors.empty?
end