Class: SpreedlyCore::Transaction

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

Overview

Abstract class for all the different spreedly transactions

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 = {}) ⇒ Transaction

Returns a new instance of Transaction.



28
29
30
31
32
33
# File 'lib/spreedly-core-ruby/transactions.rb', line 28

def initialize(attrs={})
  super
  if(valid_signature?)
    verified!
  end
end

Instance Attribute Details

#amountObject (readonly)

Returns the value of attribute amount.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def amount
  @amount
end

#checkout_urlObject (readonly)

Returns the value of attribute checkout_url.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def checkout_url
  @checkout_url
end

#created_atObject (readonly)

Returns the value of attribute created_at.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def created_at
  @created_at
end

#currency_codeObject (readonly)

Returns the value of attribute currency_code.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def currency_code
  @currency_code
end

#gateway_tokenObject (readonly)

Returns the value of attribute gateway_token.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def gateway_token
  @gateway_token
end

#messageObject (readonly)

Returns the value of attribute message.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def message
  @message
end

#on_test_gatewayObject (readonly)

Returns the value of attribute on_test_gateway.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def on_test_gateway
  @on_test_gateway
end

#responseObject (readonly)

Returns the value of attribute response.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def response
  @response
end

#signedObject (readonly)

Returns the value of attribute signed.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def signed
  @signed
end

#stateObject (readonly)

Returns the value of attribute state.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def state
  @state
end

#succeededObject (readonly) Also known as: succeeded?

Returns the value of attribute succeeded.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def succeeded
  @succeeded
end

#tokenObject (readonly)

Returns the value of attribute token.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def token
  @token
end

#transaction_typeObject (readonly)

Returns the value of attribute transaction_type.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def transaction_type
  @transaction_type
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



4
5
6
# File 'lib/spreedly-core-ruby/transactions.rb', line 4

def updated_at
  @updated_at
end

Class Method Details

.find(token) ⇒ Object

Lookup the transaction by its token. Returns the correct subclass



17
18
19
20
21
22
23
24
25
26
# File 'lib/spreedly-core-ruby/transactions.rb', line 17

def self.find(token)
  return nil if token.nil?
  verify_get("/transactions/#{token}.xml", :has_key => "transaction") do |response|
    attrs = response.parsed_response["transaction"]
    klass = @@transaction_type_to_class[attrs["transaction_type"]] || self
    klass.new(attrs).tap do |transaction|
      transaction.verified!
    end
  end
end

.handles(transaction_type) ⇒ Object

Breaks enacapsulation a bit, but allow subclasses to register the ‘transaction_type’ they handle.



11
12
13
14
# File 'lib/spreedly-core-ruby/transactions.rb', line 11

def self.handles(transaction_type)
  @@transaction_type_to_class ||= {}
  @@transaction_type_to_class[transaction_type] = self
end

Instance Method Details

#pending?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/spreedly-core-ruby/transactions.rb', line 43

def pending?
  state == "pending"
end

#valid_signature?Boolean

Returns:

  • (Boolean)


47
48
49
50
51
52
53
54
# File 'lib/spreedly-core-ruby/transactions.rb', line 47

def valid_signature?
  return false unless signed
  fields = signed["fields"]
  data = fields.collect do |field|
    self.instance_variable_get("@#{field}")
  end.join("|")
  (OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new(signed["algorithm"], SpreedlyCore::Base.secret, data)) == signed["signature"])
end

#verified!Object



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

def verified!
  @verified = true
end

#verified?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/spreedly-core-ruby/transactions.rb', line 39

def verified?
  @verified
end