Class: PaiementCic

Inherits:
Object
  • Object
show all
Defined in:
lib/paiement_cic.rb,
lib/paiement_cic/version.rb

Defined Under Namespace

Modules: FormHelper

Constant Summary collapse

VERSION =
"0.2"
@@version =

clé extraite grâce à extract2HmacSha1.html fourni par le Crédit Mutuel

"3.0"
@@hmac_key =

clé extraite grâce à extract2HmacSha1.html fourni par le Crédit Mutuel

"ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
@@target_url =
"https://paiement.creditmutuel.fr/test/paiement.cgi"
@@tpe =
"123456"
@@societe =
"masociete"
@@url_ok =
""

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config(amount_in_cents, reference) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/paiement_cic.rb', line 41

def self.config(amount_in_cents, reference)
  oa = ActiveSupport::OrderedHash.new
  oa["version"]     = "3.0"
  oa["TPE"]         = tpe
  oa["date"]        = Time.now.strftime(date_format)
  oa["montant"]     =  ("%.2f" % amount_in_cents) + "EUR"
  oa["reference"]   = reference
  oa["texte-libre"] = ""
  oa["lgue"]      = "FR"
  oa["societe"]     = societe
  oa["mail"]        = ""
  oa
end

.date_formatObject



37
38
39
# File 'lib/paiement_cic.rb', line 37

def self.date_format
  "%d/%m/%Y:%H:%M:%S"
end

.mac_string(params) ⇒ Object



55
56
57
58
# File 'lib/paiement_cic.rb', line 55

def self.mac_string params
  hmac_key = PaiementCic.new
  mac_string = [hmac_key.tpe, params["date"], params['montant'], params['reference'], params['texte-libre'], hmac_key.version, params['code-retour'], params['cvx'], params['vld'], params['brand'], params['status3ds'], params['numauto'], params['motifrefus'], params['originecb'], params['bincb'], params['hpancb'], params['ipclient'], params['originetr'], params['veres'], params['pares']].join('*') + "*"
end

.verify_hmac(params) ⇒ Object



60
61
62
63
64
65
# File 'lib/paiement_cic.rb', line 60

def self.verify_hmac params
  hmac_key = PaiementCic.new
  mac_string = [hmac_key.tpe, params["date"], params['montant'], params['reference'], params['texte-libre'], hmac_key.version, params['code-retour'], params['cvx'], params['vld'], params['brand'], params['status3ds'], params['numauto'], params['motifrefus'], params['originecb'], params['bincb'], params['hpancb'], params['ipclient'], params['originetr'], params['veres'], params['pares']].join('*') + "*"

  hmac_key.valid_hmac?(mac_string, params['MAC'])
end

Instance Method Details

#computeHMACSHA1(data) ⇒ Object

Return the HMAC for a data string



73
74
75
# File 'lib/paiement_cic.rb', line 73

def computeHMACSHA1(data)
	hmac_sha1(usable_key(self), data).downcase
end

#hmac_sha1(key, data) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/paiement_cic.rb', line 77

def hmac_sha1(key, data)
	length = 64

	if (key.length > length) 
		key = [Digest::SHA1.hexdigest(key)].pack("H*")
	end

	key  = key.ljust(length, 0.chr)
	ipad = ''.ljust(length, 54.chr)
	opad = ''.ljust(length, 92.chr)

	k_ipad = key ^ ipad
	k_opad = key ^ opad

	#Digest::SHA1.hexdigest(k_opad + [Digest::SHA1.hexdigest(k_ipad + sData)].pack("H*"))
  OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new("sha1"), key, data)
end

#valid_hmac?(mac_string, sent_mac) ⇒ Boolean

Check if the HMAC matches the HMAC of the data string

Returns:

  • (Boolean)


68
69
70
# File 'lib/paiement_cic.rb', line 68

def valid_hmac?(mac_string, sent_mac)
	computeHMACSHA1(mac_string) == sent_mac.downcase
end