Class: SageParty::Transaction

Inherits:
Object
  • Object
show all
Includes:
PartyResource
Defined in:
lib/sage_party.rb

Constant Summary collapse

URLS =
{:simulator => 'https://test.sagepay.com/simulator/VSPServerGateway.asp?Service=VendorRegisterTx',
:test => 'https://test.sagepay.com/gateway/service/vspserver-register.vsp',
:live => 'https://live.sagepay.com/gateway/service/vspserver-register.vsp'}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find(vendor_id, sage_id) ⇒ Transaction

Find a stored transaction

Returns:



47
48
49
50
51
# File 'lib/sage_party.rb', line 47

def find(vendor_id, sage_id)
  transaction = get(vendor_id)
  return missing_transaction if transaction.nil? || transaction.vps_tx_id != sage_id
  transaction
end

.register_tx(data) ⇒ Transaction

Register a new transaction with SagePay

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/sage_party.rb', line 35

def register_tx(data)
  response = raw_register(data)
  hash = {}
  response.split("\r\n").each do |line|
    line = line.split("=", 2)
    hash[line.first] = line.last
  end
  self.new(hash.merge({:id => data[:VendorTxCode], :vendor_name => data[:Vendor]}))
end

.sage_pay_server(server) ⇒ Object

Define which Sage server to use

Parameters:

  • server (Symbol)

    One of :live, :test, or :simulator



29
30
31
# File 'lib/sage_party.rb', line 29

def sage_pay_server(server)
  PartyResource::Connector.add(:sage_party, {:base_uri => URLS[server.to_sym]})
end

Instance Method Details

#==(other) ⇒ Boolean

Test transaction equality

Returns:

  • (Boolean)


77
78
79
# File 'lib/sage_party.rb', line 77

def ==(other)
  properties_equal?(other) && self.exists? == other.exists?
end

#exists?Boolean

Detrmine if this transaction exists

Returns:

  • (Boolean)


82
83
84
# File 'lib/sage_party.rb', line 82

def exists?
  !@not_found
end

#merge!(data) ⇒ Transaction

Merge in transaction stage two data

Returns:



88
89
90
91
92
93
# File 'lib/sage_party.rb', line 88

def merge!(data)
  data = data.with_indifferent_access
  data.delete(:SecurityKey)
  populate_properties(data)
  self
end

#responseString

Return HTTP response to return to SagePay server

Returns:

  • (String)


66
67
68
69
70
71
72
73
# File 'lib/sage_party.rb', line 66

def response
  return format_response(:invalid, 'Transaction not found') unless exists?
  return format_response(:invalid, 'Security check failed') unless signature_ok?
  return format_response(:error, 'Sage Pay reported an error') if status == 'ERROR'
  return format_response(:invalid, 'Unexpected status') if %w{AUTHENTICATED REGISTERED}.include?(status)
  return format_response(:invalid, "Invalid status: #{status}") unless %w{OK NOTAUTHED ABORT REJECTED}.include?(status)
  format_response(:ok)
end

#signature_ok?Boolean

Check if transaction data matches its signature

Returns:

  • (Boolean)


96
97
98
# File 'lib/sage_party.rb', line 96

def signature_ok?
  generate_md5 == vps_signature
end