Class: SagePay::Server::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/sage_pay/server/notification.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Notification

Returns a new instance of Notification.



145
146
147
148
149
150
151
# File 'lib/sage_pay/server/notification.rb', line 145

def initialize(attributes = {})
  attributes.each do |k, v|
    # We're only providing readers, not writers, so we have to directly
    # set the instance variable.
    instance_variable_set("@#{k}", v)
  end
end

Instance Attribute Details

#address_resultObject (readonly)

Returns the value of attribute address_result.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def address_result
  @address_result
end

#address_statusObject (readonly)

Returns the value of attribute address_status.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def address_status
  @address_status
end

#avs_cv2Object (readonly)

Returns the value of attribute avs_cv2.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def avs_cv2
  @avs_cv2
end

#card_typeObject (readonly)

Returns the value of attribute card_type.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def card_type
  @card_type
end

#cavvObject (readonly)

Returns the value of attribute cavv.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def cavv
  @cavv
end

#cv2_resultObject (readonly)

Returns the value of attribute cv2_result.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def cv2_result
  @cv2_result
end

#gift_aidObject (readonly)

Returns the value of attribute gift_aid.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def gift_aid
  @gift_aid
end

#last_4_digitsObject (readonly)

Returns the value of attribute last_4_digits.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def last_4_digits
  @last_4_digits
end

#payer_statusObject (readonly)

Returns the value of attribute payer_status.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def payer_status
  @payer_status
end

#post_code_resultObject (readonly)

Returns the value of attribute post_code_result.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def post_code_result
  @post_code_result
end

#statusObject (readonly)

Returns the value of attribute status.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def status
  @status
end

#status_detailObject (readonly)

Returns the value of attribute status_detail.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def status_detail
  @status_detail
end

#threed_secure_statusObject (readonly)

Returns the value of attribute threed_secure_status.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def threed_secure_status
  @threed_secure_status
end

#tx_auth_noObject (readonly)

Returns the value of attribute tx_auth_no.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def tx_auth_no
  @tx_auth_no
end

#tx_typeObject (readonly)

Returns the value of attribute tx_type.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def tx_type
  @tx_type
end

#vendor_tx_codeObject (readonly)

Returns the value of attribute vendor_tx_code.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def vendor_tx_code
  @vendor_tx_code
end

#vps_protocolObject (readonly)

Returns the value of attribute vps_protocol.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def vps_protocol
  @vps_protocol
end

#vps_signatureObject (readonly)

Returns the value of attribute vps_signature.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def vps_signature
  @vps_signature
end

#vps_tx_idObject (readonly)

Returns the value of attribute vps_tx_id.



4
5
6
# File 'lib/sage_pay/server/notification.rb', line 4

def vps_tx_id
  @vps_tx_id
end

Class Method Details

.from_params(params, signature_verification_details = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sage_pay/server/notification.rb', line 10

def self.from_params(params, signature_verification_details = nil)
  key_converter = {
    "VPSProtocol"    => :vps_protocol,
    "TxType"         => :tx_type,
    "VendorTxCode"   => :vendor_tx_code,
    "VPSTxId"        => :vps_tx_id,
    "Status"         => :status,
    "StatusDetail"   => :status_detail,
    "TxAuthNo"       => :tx_auth_no,
    "AVSCV2"         => :avs_cv2,
    "AddressResult"  => :address_result,
    "PostCodeResult" => :post_code_result,
    "CV2Result"      => :cv2_result,
    "GiftAid"        => :gift_aid,
    "3DSecureStatus" => :threed_secure_status,
    "CAVV"           => :cavv,
    "AddressStatus"  => :address_status,
    "PayerStatus"    => :payer_status,
    "CardType"       => :card_type,
    "Last4Digits"    => :last_4_digits,
    "VPSSignature"   => :vps_signature
  }

  match_converter = {
    "NOTPROVIDED" => :not_provided,
    "NOTCHECKED"  => :not_checked,
    "MATCHED"     => :matched,
    "NOTMATCHED"  => :not_matched
  }

  true_false_converter = {
    "0" => false,
    "1" => true
  }

  value_converter = {
    :tx_type => {
      "PAYMENT"      => :payment,
      "DEFERRED"     => :deferred,
      "AUTHENTICATE" => :authenticate
    },
    :status => {
      "OK"            => :ok,
      "NOTAUTHED"     => :not_authed,
      "ABORT"         => :abort,
      "REJECTED"      => :rejected,
      "AUTHENTICATED" => :authenticated,
      "REGISTERED"    => :registered,
      "ERROR"         => :error
    },
    :avs_cv2 => {
      "ALL MATCH"                => :all_match,
      "SECURITY CODE MATCH ONLY" => :security_code_match_only,
      "ADDRESS MATCH ONLY"       => :address_match_only,
      "NO DATA MATCHES"          => :no_data_matches,
      "DATA NOT CHECKED"         => :data_not_checked
    },
    :address_result   => match_converter,
    :post_code_result => match_converter,
    :cv2_result       => match_converter,
    :gift_aid         => true_false_converter,
    :threed_secure_status => {
      "OK"           => :ok,
      "NOTCHECKED"   => :not_checked,
      "NOTAVAILABLE" => :not_available,
      "NOTAUTHED"    => :not_authed,
      "INCOMPLETE"   => :incomplete,
      "ERROR"        => :error
    },
    :address_status => {
      "NONE"        => :none,
      "CONFIRMED"   => :confirmed,
      "UNCONFIRMED" => :unconfirmed
    },
    :payer_status => {
      "VERIFIED"   => :verified,
      "UNVERIFIED" => :unverified
    },
    :card_type => {
      "VISA"    => :visa,
      "MC"      => :mastercard,
      "DELTA"   => :visa_delta,
      "SOLO"    => :solo,
      "MAESTRO" => :maestro,
      "UKE"     => :visa_electron,
      "AMEX"    => :american_express,
      "DC"      => :diners,
      "JCB"     => :jcb,
      "LASER"   => :laser,
      "PAYPAL"  => :paypal
    }
  }

  attributes = {}
  params.each do |key, value|
    unless value.nil?
      converted_key = key_converter[key]
      converted_value = value_converter[converted_key].nil? ? value : value_converter[converted_key][value]

      attributes[converted_key] = converted_value
    end
  end

  if signature_verification_details.nil? && block_given?
    signature_verification_details = yield(attributes)
  end

  unless signature_verification_details.nil?
    # We need to calculate the VPS signature from the values passed in as
    # additional params from the original registration and notification.
    fields_used_in_signature = [
      params["VPSTxId"],
      params["VendorTxCode"],
      params["Status"],
      params["TxAuthNo"],
      signature_verification_details.vendor,
      params["AVSCV2"],
      signature_verification_details.security_key,
      params["AddressResult"],
      params["PostCodeResult"],
      params["CV2Result"],
      params["GiftAid"],
      params["3DSecureStatus"],
      params["CAVV"],
      params["AddressStatus"],
      params["PayerStatus"],
      params["CardType"],
      params["Last4Digits"]
    ]
    attributes[:calculated_hash] = Digest::MD5.hexdigest(fields_used_in_signature.join).upcase
  end

  new(attributes)
end

Instance Method Details

#address_matched?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/sage_pay/server/notification.rb', line 161

def address_matched?
  address_result == :matched
end

#avs_cv2_matched?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/sage_pay/server/notification.rb', line 157

def avs_cv2_matched?
  avs_cv2 == :all_match
end

#cv2_matched?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/sage_pay/server/notification.rb', line 169

def cv2_matched?
  cv2_result == :matched
end

#ok?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/sage_pay/server/notification.rb', line 153

def ok?
  status == :ok
end

#post_code_matched?Boolean

Returns:

  • (Boolean)


165
166
167
# File 'lib/sage_pay/server/notification.rb', line 165

def post_code_matched?
  post_code_result == :matched
end

#response(redirect_url) ⇒ Object



181
182
183
184
185
186
187
# File 'lib/sage_pay/server/notification.rb', line 181

def response(redirect_url)
  if valid_signature?
    SagePay::Server::NotificationResponse.new(:status => :ok, :redirect_url => redirect_url)
  else
    SagePay::Server::NotificationResponse.new(:status => :invalid, :redirect_url => redirect_url, :status_detail => "Signature did not match our expectations")
  end.response
end

#threed_secure_status_ok?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/sage_pay/server/notification.rb', line 173

def threed_secure_status_ok?
  threed_secure_status == :ok
end

#valid_signature?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/sage_pay/server/notification.rb', line 177

def valid_signature?
  @calculated_hash == vps_signature
end