Class: Cybersourcery::CybersourceSigner

Inherits:
Object
  • Object
show all
Defined in:
lib/cybersourcery/cybersource_signer.rb

Defined Under Namespace

Classes: Signer

Constant Summary collapse

IGNORE_FIELDS =
%i[
  commit
  utf8
  authenticity_token
  action
  controller
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile, signer = Signer) ⇒ CybersourceSigner

Returns a new instance of CybersourceSigner.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cybersourcery/cybersource_signer.rb', line 19

def initialize(profile, signer = Signer)
  @profile              = profile
  @signer               = signer
  @signable_fields      = {
    access_key:           @profile.access_key,
    profile_id:           @profile.profile_id,
    payment_method:       @profile.payment_method,
    locale:               @profile.locale,
    transaction_type:     @profile.transaction_type,
    currency:             @profile.currency
  }
end

Instance Attribute Details

#form_fieldsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/cybersourcery/cybersource_signer.rb', line 51

def form_fields
  @form_fields ||= signable_fields.dup.merge(
    unsigned_field_names: @profile.unsigned_field_names.join(','),
    transaction_uuid:     SecureRandom.hex(16),
    reference_number:     SecureRandom.hex(16),
    signed_date_time:     time,
    signed_field_names:   nil # make sure it's in data.keys
  ).tap do |data|
    data[:signed_field_names] = data.keys.join(',')
  end
end

#profileObject

Returns the value of attribute profile.



6
7
8
# File 'lib/cybersourcery/cybersource_signer.rb', line 6

def profile
  @profile
end

#signable_fieldsObject (readonly)

Returns the value of attribute signable_fields.



9
10
11
# File 'lib/cybersourcery/cybersource_signer.rb', line 9

def signable_fields
  @signable_fields
end

#signerObject

Returns the value of attribute signer.



6
7
8
# File 'lib/cybersourcery/cybersource_signer.rb', line 6

def signer
  @signer
end

#timeObject



63
64
65
# File 'lib/cybersourcery/cybersource_signer.rb', line 63

def time
  @time ||= Time.now.utc.strftime("%Y-%m-%dT%H:%M:%SZ")
end

Class Method Details

.signature_message(hash, keys) ⇒ Object



67
68
69
# File 'lib/cybersourcery/cybersource_signer.rb', line 67

def self.signature_message(hash, keys)
  keys.map {|key| "#{key}=#{hash.fetch(key)}" }.join(',')
end

Instance Method Details

#add_and_sign_fields(params) ⇒ Object



32
33
34
35
# File 'lib/cybersourcery/cybersource_signer.rb', line 32

def add_and_sign_fields(params)
  add_signable_fields(params)
  signed_fields
end

#add_signable_fields(params) ⇒ Object



37
38
39
40
41
# File 'lib/cybersourcery/cybersource_signer.rb', line 37

def add_signable_fields(params)
  @signable_fields.merge! params.symbolize_keys.delete_if { |k,v|
    @profile.unsigned_field_names.include?(k) || IGNORE_FIELDS.include?(k)
  }
end

#signed_fieldsObject



43
44
45
46
47
48
49
# File 'lib/cybersourcery/cybersource_signer.rb', line 43

def signed_fields
  form_fields.tap do |data|
    signature_keys = data[:signed_field_names].split(',').map(&:to_sym)
    signature_message = self.class.signature_message(data, signature_keys)
    data[:signature]  = signer.signature(signature_message, profile.secret_key)
  end
end