2013-06-05 15:22:41 +02:00
|
|
|
require 'paiement_cic/tpe'
|
|
|
|
require 'paiement_cic/railtie' if defined?(Rails)
|
|
|
|
|
2009-08-17 10:37:35 +02:00
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
class String
|
|
|
|
def ^(other)
|
|
|
|
raise ArgumentError, "Can't bitwise-XOR a String with a non-String" \
|
|
|
|
unless other.kind_of? String
|
|
|
|
raise ArgumentError, "Can't bitwise-XOR strings of different length" \
|
|
|
|
unless self.length == other.length
|
2012-01-30 09:39:50 +01:00
|
|
|
result = (0..self.length-1).collect { |i| self[i].ord ^ other[i].ord }
|
2009-08-17 10:37:35 +02:00
|
|
|
result.pack("C*")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
module PaiementCic
|
|
|
|
API_VERSION = "3.0"
|
|
|
|
DATE_FORMAT = "%d/%m/%Y:%H:%M:%S"
|
2013-06-05 10:28:56 +02:00
|
|
|
|
2013-06-11 11:51:22 +02:00
|
|
|
END_POINTS = {
|
|
|
|
cic: {
|
|
|
|
production: 'https://ssl.paiement.cic-banques.fr/paiement.cgi',
|
|
|
|
test: 'https://ssl.paiement.cic-banques.fr/test/paiement.cgi'
|
|
|
|
},
|
|
|
|
cm: {
|
|
|
|
production: 'https://paiement.creditmutuel.fr/paiement.cgi',
|
|
|
|
test: 'https://paiement.creditmutuel.fr/test/paiement.cgi'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
DEFAULT_BANK = :cm
|
|
|
|
DEFAULT_ENV = :test
|
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
class << self
|
2013-06-11 11:51:22 +02:00
|
|
|
attr_accessor :hmac_key, :tpe, :societe
|
2013-06-05 15:22:41 +02:00
|
|
|
attr_writer :target_url
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
def configure(&block)
|
|
|
|
yield(self) if block_given?
|
2013-06-05 10:28:56 +02:00
|
|
|
end
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-11 11:51:22 +02:00
|
|
|
def bank
|
|
|
|
@bank || DEFAULT_BANK
|
|
|
|
end
|
|
|
|
|
|
|
|
def bank=(value)
|
|
|
|
raise UnknownBankError unless END_POINTS.keys.include?(value.to_sym)
|
|
|
|
@bank = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def env
|
|
|
|
@env || DEFAULT_ENV
|
|
|
|
end
|
|
|
|
|
|
|
|
def env=(value)
|
|
|
|
raise UnknownEnvError unless END_POINTS.first.last.include?(value.to_sym)
|
|
|
|
@env = value
|
|
|
|
end
|
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
def target_url
|
2013-06-11 11:51:22 +02:00
|
|
|
@target_url || END_POINTS[self.bank][self.env]
|
2013-06-05 15:22:41 +02:00
|
|
|
end
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
def hmac_sha1(key, data)
|
|
|
|
length = 64
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
if (key.length > length)
|
|
|
|
key = [Digest::SHA1.hexdigest(key)].pack("H*")
|
|
|
|
end
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
key = key.ljust(length, 0.chr)
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
k_ipad = key ^ ''.ljust(length, 54.chr)
|
|
|
|
k_opad = key ^ ''.ljust(length, 92.chr)
|
2009-12-21 11:24:45 +01:00
|
|
|
|
2013-06-05 15:22:41 +02:00
|
|
|
Digest::SHA1.hexdigest(k_opad + [Digest::SHA1.hexdigest(k_ipad + data)].pack("H*"))
|
2013-06-05 10:28:56 +02:00
|
|
|
end
|
|
|
|
end
|
2013-06-11 11:51:22 +02:00
|
|
|
|
|
|
|
class UnknownBankError < Exception; end
|
|
|
|
class UnknownEnvError < Exception; end
|
2009-12-21 11:24:45 +01:00
|
|
|
end
|