cybersource_rest_client 0.0.10 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/lib/AuthenticationSDK/authentication/http/GetSignatureParameter.rb +64 -0
  3. data/lib/AuthenticationSDK/authentication/http/HttpSignatureHeader.rb +52 -0
  4. data/lib/AuthenticationSDK/authentication/jwt/JwtToken.rb +62 -0
  5. data/lib/AuthenticationSDK/authentication/payloadDigest/digest.rb +10 -0
  6. data/lib/AuthenticationSDK/core/Authorization.rb +24 -0
  7. data/lib/AuthenticationSDK/core/ITokenGeneration.rb +4 -0
  8. data/lib/AuthenticationSDK/core/Logger.rb +26 -0
  9. data/lib/AuthenticationSDK/core/MerchantConfig.rb +181 -0
  10. data/{AuthenticationSDK → lib/AuthenticationSDK}/resource/TRRReports.json +0 -0
  11. data/{AuthenticationSDK → lib/AuthenticationSDK}/resource/cybs.yml +0 -0
  12. data/{AuthenticationSDK → lib/AuthenticationSDK}/resource/request.json +0 -0
  13. data/{AuthenticationSDK → lib/AuthenticationSDK}/resource/request_capture.json +0 -0
  14. data/{AuthenticationSDK → lib/AuthenticationSDK}/resource/testrest.p12 +0 -0
  15. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/Authorization_spec.rb +0 -0
  16. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/MerchantConfigData.rb +0 -0
  17. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/MerchantConfig_spec.rb +0 -0
  18. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/PostRequestData.json +0 -0
  19. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/PutRequestData.json +0 -0
  20. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/ResponseCodeMessage_spec.rb +0 -0
  21. data/{AuthenticationSDK → lib/AuthenticationSDK}/spec/spec_helper.rb +0 -0
  22. data/lib/AuthenticationSDK/util/ApiException.rb +19 -0
  23. data/lib/AuthenticationSDK/util/Cache.rb +36 -0
  24. data/lib/AuthenticationSDK/util/Constants.rb +138 -0
  25. data/lib/AuthenticationSDK/util/PropertiesUtil.rb +19 -0
  26. data/lib/AuthenticationSDK/util/Utility.rb +32 -0
  27. data/lib/cybersource_rest_client/api_client.rb +11 -11
  28. data/lib/cybersource_rest_client.rb +13 -0
  29. metadata +27 -29
  30. data/AuthenticationSDK/AuthenticationSDK-0.0.1.gem +0 -0
  31. data/AuthenticationSDK/Cybersource.gemspec +0 -25
  32. data/AuthenticationSDK/authentication/http/GetSignatureParameter.rb +0 -64
  33. data/AuthenticationSDK/authentication/http/HttpSignatureHeader.rb +0 -52
  34. data/AuthenticationSDK/authentication/jwt/JwtToken.rb +0 -62
  35. data/AuthenticationSDK/authentication/payloadDigest/digest.rb +0 -10
  36. data/AuthenticationSDK/core/Authorization.rb +0 -24
  37. data/AuthenticationSDK/core/ITokenGeneration.rb +0 -4
  38. data/AuthenticationSDK/core/Logger.rb +0 -26
  39. data/AuthenticationSDK/core/MerchantConfig.rb +0 -181
  40. data/AuthenticationSDK/util/ApiException.rb +0 -19
  41. data/AuthenticationSDK/util/Cache.rb +0 -36
  42. data/AuthenticationSDK/util/Constants.rb +0 -138
  43. data/AuthenticationSDK/util/PropertiesUtil.rb +0 -20
  44. data/AuthenticationSDK/util/Utility.rb +0 -31
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4a56275fc06161b39c6a32b3900000f7df9eb898c8b9ccfc8e5bf1ecb4a52c91
4
- data.tar.gz: 5ca0894ea7d9cfae7c927db5e13b074d1e98561f4282d5191076924617546212
3
+ metadata.gz: a8735c72e139b8c81a915d7f82fd194189f36bb3206de4a20edd098e538999cd
4
+ data.tar.gz: c85d359997ede2deed769d1dd349730581ca21992c4ebf621c4b93e7b79b3204
5
5
  SHA512:
6
- metadata.gz: 576ee6017fdd0a015fd169cf2ed23fa5f7661c09d9e170feabbdf06c381775b6a0d6472c912019c27d1ab503648b46b120a142a54d2ab7ccd83e0fa6b76a0f4d
7
- data.tar.gz: a0653272e74b479f74e9423f842708acf4c6a89bcdd069ebc75869e4cc382cc95d94e23726e25c96f94760717f650184057271fe90017201e4e686c9105db5fa
6
+ metadata.gz: ff7bec1dfd8d017f81730fa7432c5aa372582a288085af7e041ea79e108c401bd14419d664119ecec11988542cbf1980da7f0b740868c9504210f105bc12a108
7
+ data.tar.gz: 1c65b4018234fecd0be94afa1cee3bf74b3350647ca597a963252bb56111c221090238e6dcb0a316db38ed7d11231b82b5adbec4fce633f534e59e24e7d56e25
@@ -0,0 +1,64 @@
1
+ require_relative '../../util/Constants.rb'
2
+ require_relative '.././payloadDigest/digest.rb'
3
+ require 'openssl'
4
+ # This function returns value for paramter Signature which is then passed to Signature header
5
+ # paramter 'Signature' is calucated based on below key values and then signed with SECRET KEY -
6
+ # host: Sandbox (apitest.cybersource.com) or Production (api.cybersource.com) hostname
7
+ # date: "HTTP-date" format as defined by RFC7231.
8
+ # (request-target): Should be in format of httpMethod: path
9
+ # Example: "post /pts/v2/payments"
10
+ # Digest: Only needed for POST calls.
11
+ # digestString = BASE64( HMAC-SHA256 ( Payload ));
12
+ # Digest: “SHA-256=“ + digestString;
13
+ # v-c-merchant-id: set value to Cybersource Merchant ID
14
+ # This ID can be found on EBC portal*/
15
+ class SignatureParameter
16
+ def generateSignatureParameter(merchantconfig_obj, gmtdatetime, log_obj)
17
+ request_type = merchantconfig_obj.requestType.upcase
18
+ merchantSecretKey = merchantconfig_obj.merchantSecretKey
19
+ signatureString = Constants::HOST + ': ' + merchantconfig_obj.requestHost
20
+ signatureString << "\n"+ Constants::DATE + ': ' + gmtdatetime
21
+ signatureString << "\n(request-target): "
22
+ if request_type == Constants::GET_REQUEST_TYPE || request_type == Constants::DELETE_REQUEST_TYPE
23
+ targetUrl=gettargetUrlForGetDelete(request_type,merchantconfig_obj)
24
+ signatureString << targetUrl + "\n"
25
+ elsif request_type == Constants::POST_REQUEST_TYPE || request_type == Constants::PUT_REQUEST_TYPE || request_type == Constants::PATCH_REQUEST_TYPE
26
+ targetUrl=gettargetUrlForPutPost(request_type,merchantconfig_obj)
27
+ signatureString << targetUrl + "\n"
28
+ payload = merchantconfig_obj.requestJsonData
29
+ digest = DigestGeneration.new.generateDigest(payload, log_obj)
30
+ digest_payload = Constants::SHA256 + digest
31
+ signatureString << Constants::DIGEST + ': ' + digest_payload + "\n"
32
+ end
33
+ signatureString << Constants::V_C_MERCHANT_ID + ': ' + merchantconfig_obj.merchantId
34
+ encodedSignatureString = signatureString.force_encoding(Encoding::UTF_8)
35
+ decodedKey = Base64.decode64(merchantSecretKey)
36
+ base64EncodedSignature = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', decodedKey, encodedSignatureString))
37
+ return base64EncodedSignature
38
+ rescue StandardError => err
39
+ log_obj.logger.error(err.message)
40
+ log_obj.logger.error(err.backtrace)
41
+ puts 'Check log for more details.'
42
+ exit!
43
+ end
44
+ def gettargetUrlForGetDelete(request_type, merchantconfig_obj)
45
+ targetUrlForGetDelete = ''
46
+ if request_type == Constants::DELETE_REQUEST_TYPE
47
+ targetUrlForGetDelete = Constants::DELETE_REQUEST_TYPE_LOWER + ' ' + merchantconfig_obj.requestTarget
48
+ elsif request_type == Constants::GET_REQUEST_TYPE
49
+ targetUrlForGetDelete = Constants::GET_REQUEST_TYPE_LOWER + ' ' + merchantconfig_obj.requestTarget
50
+ end
51
+ return targetUrlForGetDelete
52
+ end
53
+ def gettargetUrlForPutPost(request_type, merchantconfig_obj)
54
+ targetUrlForPutPost = ''
55
+ if request_type == Constants::POST_REQUEST_TYPE
56
+ targetUrlForPutPost = Constants::POST_REQUEST_TYPE_LOWER + ' ' + merchantconfig_obj.requestTarget
57
+ elsif request_type == Constants::PUT_REQUEST_TYPE
58
+ targetUrlForPutPost = Constants::PUT_REQUEST_TYPE_LOWER + ' ' + merchantconfig_obj.requestTarget
59
+ elsif request_type == Constants::PATCH_REQUEST_TYPE
60
+ targetUrlForPutPost = Constants::PATCH_REQUEST_TYPE_LOWER + ' ' + merchantconfig_obj.requestTarget
61
+ end
62
+ return targetUrlForPutPost
63
+ end
64
+ end
@@ -0,0 +1,52 @@
1
+ require 'base64'
2
+ require_relative '../../core/ITokenGeneration.rb'
3
+ require_relative '../../util/Constants.rb'
4
+ require_relative '../../util/ApiException.rb'
5
+ require_relative '.././payloadDigest/digest.rb'
6
+ require_relative '.././http/GetSignatureParameter.rb'
7
+ public
8
+ # SignatureHeader return SignatureHeader Value that contains following paramters
9
+ # * keyid -- Merchant ID obtained from EBC portal
10
+ # * algorithm -- Should have value as "HmacSHA256"
11
+ # * headers -- List of all header name passed in the Signature paramter below
12
+ # String getHeaders = "host date (request-target)" + " " + "v-c-merchant-id";
13
+ # String postHeaders = "host date (request-target) digest v-c-merchant-id";
14
+ # Note: Digest is not passed for GET calls
15
+ # * signature -- Signature header has paramter called signature
16
+ # Paramter 'Signature' must contain all the paramters mentioned in header above in given order
17
+ class GenerateHttpSignature
18
+ # Generates Signature based on the requestType
19
+ def getToken(merchantconfig_obj, gmtdatetime, log_obj)
20
+ request_type = merchantconfig_obj.requestType.upcase
21
+ signatureHeaderValue =''
22
+ signatureHeaderValue << Constants::KEY_ID + merchantconfig_obj.merchantKeyId + "\""
23
+ # Algorithm should be always HmacSHA256 for http signature
24
+ signatureHeaderValue << ', ' + Constants::ALGORITHM + Constants::SIGNATURE_ALGORITHM + "\""
25
+ # Headers - list is choosen based on HTTP method
26
+ signatureheader=getsignatureHeader(request_type)
27
+ signatureHeaderValue << ', ' + Constants::HEADERS_PARAM + signatureheader + "\""
28
+ # Get Value for parameter 'Signature' to be passed to Signature Header
29
+ signature_value = SignatureParameter.new.generateSignatureParameter(merchantconfig_obj, gmtdatetime, log_obj)
30
+ signatureHeaderValue << ', ' + Constants::SIGNATURE_PARAM + signature_value + "\""
31
+ return signatureHeaderValue
32
+ rescue StandardError => err
33
+ ApiException.new.apiexception(err,log_obj)
34
+ exit!
35
+ end
36
+ def getsignatureHeader(request_type)
37
+ headers = ''
38
+ if request_type == Constants::POST_REQUEST_TYPE
39
+ headers = 'host date (request-target) digest ' + Constants::V_C_MERCHANT_ID
40
+ elsif request_type == Constants::GET_REQUEST_TYPE || request_type == Constants::DELETE_REQUEST_TYPE
41
+ headers = 'host date (request-target)' + ' ' + Constants::V_C_MERCHANT_ID
42
+ elsif request_type == Constants::PUT_REQUEST_TYPE
43
+ headers = 'host date (request-target) digest ' + Constants::V_C_MERCHANT_ID
44
+ elsif request_type == Constants::PATCH_REQUEST_TYPE
45
+ headers = 'host date (request-target) digest ' + Constants::V_C_MERCHANT_ID
46
+ else
47
+ raise StandardError.new(Constants::ERROR_PREFIX + Constants::INVALID_REQUEST_TYPE_METHOD)
48
+ end
49
+ return headers
50
+ end
51
+ implements TokenInterface
52
+ end
@@ -0,0 +1,62 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'jwt'
4
+ require 'json'
5
+ require 'active_support'
6
+ require_relative '../../core/ITokenGeneration.rb'
7
+ require_relative '../../util/Constants.rb'
8
+ require_relative '../../util/ApiException.rb'
9
+ require_relative '../../util/Cache.rb'
10
+ require_relative '../../authentication/payloadDigest/digest.rb'
11
+ public
12
+ #JWT Token-generated based on the Request type
13
+ class GenerateJwtToken
14
+ def getToken(merchantconfig_obj,gmtDatetime,log_obj)
15
+ jwtBody = ''
16
+ request_type = merchantconfig_obj.requestType.upcase
17
+ filePath = merchantconfig_obj.keysDirectory + '/' + merchantconfig_obj.keyFilename + '.p12'
18
+ if (!File.exist?(filePath))
19
+ raise Constants::ERROR_PREFIX + Constants::FILE_NOT_FOUND + File.expand_path(filePath)
20
+ end
21
+ p12File = File.binread(filePath)
22
+ jwtBody=getJwtBody(request_type, gmtDatetime, merchantconfig_obj, log_obj)
23
+ claimSet = JSON.parse(jwtBody)
24
+ p12FilePath = OpenSSL::PKCS12.new(p12File, merchantconfig_obj.keyPass)
25
+ # Generating certificate.
26
+ cacheObj = ActiveSupport::Cache::MemoryStore.new
27
+ x5Cert = Cache.new.fetchCachedCertificate(filePath, p12File, merchantconfig_obj.keyPass, cacheObj)
28
+ # Generating Public key.
29
+ publicKey = OpenSSL::PKey::RSA.new(p12FilePath.key.public_key)
30
+ #Generating Private Key
31
+ privateKey = OpenSSL::PKey::RSA.new(p12FilePath.key)
32
+ # JWT token-Generates using RS256 algorithm only
33
+ x5clist = [x5Cert]
34
+ customHeaders = {}
35
+ customHeaders['v-c-merchant-id'] = merchantconfig_obj.keyAlias
36
+ customHeaders['x5c'] = x5clist
37
+ # Generating JWT token
38
+ token = JWT.encode(claimSet, privateKey, 'RS256', customHeaders)
39
+ return token
40
+ rescue StandardError => err
41
+ if err.message.include? 'PKCS12_parse: mac verify failure'
42
+ ApiException.new.customerror(Constants::ERROR_PREFIX + Constants::INCORRECT_KEY_PASS,log_obj)
43
+ exit!
44
+ else
45
+ ApiException.new.apiexception(err,log_obj)
46
+ exit!
47
+ end
48
+ end
49
+ def getJwtBody(request_type, gmtDatetime, merchantconfig_obj,log_obj)
50
+ if request_type == Constants::POST_REQUEST_TYPE || request_type == Constants::PUT_REQUEST_TYPE || request_type == Constants::PATCH_REQUEST_TYPE
51
+ payload = merchantconfig_obj.requestJsonData
52
+ # Note: Digest is not passed for GET calls
53
+ digest = DigestGeneration.new.generateDigest(payload, log_obj)
54
+ jwtBody = "{\n \"digest\":\"" + digest + "\", \"digestAlgorithm\":\"SHA-256\", \"iat\":\"" + gmtDatetime + "\"}"
55
+ elsif request_type == Constants::GET_REQUEST_TYPE || request_type == Constants::DELETE_REQUEST_TYPE
56
+ jwtBody = "{\n \"iat\":\"" + gmtDatetime + "\"\n} \n\n"
57
+ else
58
+ raise StandardError.new(Constants::ERROR_PREFIX + Constants::INVALID_REQUEST_TYPE_METHOD)
59
+ end
60
+ end
61
+ implements TokenInterface
62
+ end
@@ -0,0 +1,10 @@
1
+ require 'digest'
2
+ public
3
+ # This method return Digest value which is SHA-256
4
+ # hash of payload that is BASE64 encoded
5
+ class DigestGeneration
6
+ def generateDigest(payload, log_obj)
7
+ digest = Digest::SHA256.base64digest(payload)
8
+ return digest
9
+ end
10
+ end
@@ -0,0 +1,24 @@
1
+ require_relative '../authentication/http/HttpSignatureHeader.rb'
2
+ require_relative '../authentication/jwt/JwtToken.rb'
3
+ require_relative '../util/Constants.rb'
4
+ require_relative '../util/ApiException.rb'
5
+ public
6
+ # This function calls for the generation of Signature message depending on the authentication type.
7
+ class Authorization
8
+ def getToken(merchantconfig_obj, gmtdatetime, log_obj)
9
+ authenticationType = merchantconfig_obj.authenticationType.upcase
10
+ if merchantconfig_obj.requestType.to_s.empty?
11
+ raise StandardError.new(Constants::ERROR_PREFIX + Constants::REQUEST_TYPE_EMPTY)
12
+ end
13
+ if authenticationType == Constants::AUTH_TYPE_HTTP
14
+ token = GenerateHttpSignature.new.getToken(merchantconfig_obj, gmtdatetime, log_obj)
15
+ elsif authenticationType == Constants::AUTH_TYPE_JWT
16
+ token = GenerateJwtToken.new.getToken(merchantconfig_obj, gmtdatetime, log_obj)
17
+ elsif authenticationType != Constants::AUTH_TYPE_HTTP || authenticationType != Constants::AUTH_TYPE_JWT
18
+ raise StandardError.ner(Constants::ERROR_PREFIX + Constants::AUTH_ERROR)
19
+ end
20
+ rescue StandardError => err
21
+ ApiException.new.apiexception(err,log_obj)
22
+ exit!
23
+ end
24
+ end
@@ -0,0 +1,4 @@
1
+ require 'interface'
2
+ TokenInterface = interface {
3
+ required_methods :getToken
4
+ }
@@ -0,0 +1,26 @@
1
+ require_relative '../util/ApiException.rb'
2
+ require 'logger'
3
+ public
4
+ # Logger Class
5
+ class Log
6
+ def initialize(logDirectory, logFilename, logSize, enableLog)
7
+ #log
8
+ if enableLog == true
9
+ filename = logDirectory + '/' + logFilename + '.log'
10
+ datetime = DateTime.now
11
+ if File.exist?(filename) && File.size(filename) >= logSize
12
+ updatedFileName = logDirectory + '/' + logFilename + '_' + datetime.strftime('%Y%m%d%H%M%S') + '.log'
13
+ File.rename(filename, updatedFileName)
14
+ end
15
+ @logger = Logger.new(STDOUT)
16
+ @logger = Logger.new(filename, logSize)
17
+ @logger.datetime_format = datetime.strftime('%Y-%m-%d %H:%M:%S')
18
+ else
19
+ @logger = Logger.new(false)
20
+ end
21
+ rescue StandardError => err
22
+ puts err
23
+ exit!
24
+ end
25
+ attr_accessor :logger
26
+ end
@@ -0,0 +1,181 @@
1
+ require_relative './Logger.rb'
2
+ require_relative '../util/Constants.rb'
3
+ require_relative '../util/ApiException.rb'
4
+ public
5
+ # This fuction has all the merchantConfig properties getters and setters methods
6
+ class Merchantconfig
7
+ def initialize(cybsPropertyObj)
8
+ # Common Parameters
9
+ @merchantId = cybsPropertyObj['merchantID']
10
+ @runEnvironment = cybsPropertyObj['runEnvironment']
11
+ @authenticationType = cybsPropertyObj['authenticationType']
12
+ @logDirectory = cybsPropertyObj['logDirectory']
13
+ @logSize = cybsPropertyObj['logSize']
14
+ @enableLog = cybsPropertyObj['enableLog']
15
+ @proxyAddress = cybsPropertyObj['proxyAddress']
16
+ @proxyPort = cybsPropertyObj['proxyPort']
17
+ @getId = ''
18
+ @requestHost = ''
19
+ @requestTarget = ''
20
+ @requestJsonData = ''
21
+ # HTTP Parameters
22
+ @merchantSecretKey = cybsPropertyObj['merchantsecretKey']
23
+ @merchantKeyId = cybsPropertyObj['merchantKeyId']
24
+ # JWT Parameters
25
+ @keysDirectory = cybsPropertyObj['keysDirectory']
26
+ @keyAlias = cybsPropertyObj['keyAlias']
27
+ @keyPass = cybsPropertyObj['keyPass']
28
+ @keyFilename = cybsPropertyObj['keyFilename']
29
+ @logFilename = cybsPropertyObj['logFilename']
30
+ validateMerchantDetails()
31
+ logAllProperties(cybsPropertyObj)
32
+ end
33
+ #fall back logic
34
+ def validateMerchantDetails()
35
+ logmessage=''
36
+ if @enableLog.to_s.empty?
37
+ @enableLog = true
38
+ elsif @enableLog.instance_of? Fixnum
39
+ @enableLog = @enableLog.to_s
40
+ end
41
+ if @logSize.to_s.empty?
42
+ @logSize = Constants::DEFAULT_LOG_SIZE
43
+ elsif !@logSize.instance_of? Fixnum
44
+ @logSize=@logSize.to_i
45
+ end
46
+ if @logDirectory.to_s.empty? || !Dir.exist?(@logDirectory)
47
+ @logDirectory = Constants::DEFAULT_LOG_DIRECTORY
48
+ unless Dir.exist?(@logDirectory)
49
+ Dir.mkdir(Constants::DEFAULT_LOG_DIRECTORY)
50
+ end
51
+ logmessage = Constants::WARNING_PREFIX + Constants::INVALID_LOG_DIRECTORY + File.expand_path(@logDirectory)
52
+ end
53
+ if @logFilename.to_s.empty?
54
+ @logFilename = Constants::DEFAULT_LOGFILE_NAME
55
+ elsif !@logFilename.instance_of? String
56
+ @logFilename=@logFilename.to_s
57
+ end
58
+ @log_obj = Log.new @logDirectory,@logFilename,@logSize,@enableLog
59
+ @log_obj.logger.info('START> =======================================')
60
+ if !logmessage.to_s.empty?
61
+ ApiException.new.apiwarning(logmessage,log_obj)
62
+ end
63
+ if @merchantId.to_s.empty?
64
+ err = raise StandardError.new(Constants::ERROR_PREFIX + Constants::MERCHANT_ID_NULL)
65
+ ApiException.new.apiexception(err,log_obj)
66
+ elsif !@merchantId.instance_of? String
67
+ @merchantId=@merchantId.to_s
68
+ end
69
+ if @authenticationType.to_s.empty?
70
+ err = raise StandardError.new(Constants::ERROR_PREFIX + Constants::AUTH_TYPE_MANDATORY)
71
+ ApiException.new.apiexception(err,log_obj)
72
+ end
73
+ if !@authenticationType.instance_of? String
74
+ err = raise StandardError.new(Constants::ERROR_PREFIX+ Constants::AUTH_ERROR)
75
+ ApiException.new.apiexception(err,log_obj)
76
+ end
77
+ if !@runEnvironment.to_s.empty?
78
+ if !@runEnvironment.instance_of? String
79
+ @requestHost = @runEnvironment.to_s
80
+ elsif @runEnvironment.upcase == Constants::RUN_ENV_PROD
81
+ @requestHost = Constants::PRODUCTION_URL
82
+ elsif @runEnvironment.upcase == Constants::RUN_ENV_SANDBOX
83
+ @requestHost = Constants::SANDBOX_URL
84
+ else
85
+ @requestHost = @runEnvironment
86
+ end
87
+ elsif @runEnvironment.to_s.empty?
88
+ err = raise StandardError.new(Constants::ERROR_PREFIX + Constants::RUN_ENVIRONMENT)
89
+ ApiException.new.apiexception(err,log_obj)
90
+ end
91
+ if @authenticationType.upcase == Constants::AUTH_TYPE_JWT
92
+ if @keyAlias.to_s.empty?
93
+ @keyAlias = @merchantId
94
+ ApiException.new.apiwarning(Constants::WARNING_PREFIX + Constants::KEY_ALIAS_NULL_EMPTY, log_obj)
95
+ elsif !@keyAlias.instance_of? String
96
+ @keyAlias=@keyAlias.to_s
97
+ elsif @keyAlias != @merchantId
98
+ @keyAlias = @merchantId
99
+ ApiException.new.apiwarning(Constants::WARNING_PREFIX + Constants::INCORRECT_KEY_ALIAS, log_obj)
100
+ end
101
+ if @keyPass.to_s.empty?
102
+ @keyPass = @merchantId
103
+ ApiException.new.apiwarning(Constants::WARNING_PREFIX + Constants::KEY_PASS_NULL, log_obj)
104
+ elsif !@keyPass.instance_of? String
105
+ @keyPass=@keyPass.to_s
106
+ end
107
+ if @keysDirectory.to_s.empty?
108
+ @keysDirectory = Constants::DEFAULT_KEY_DIRECTORY
109
+ ApiException.new.apiwarning(Constants::WARNING_PREFIX + Constants::KEY_DIRECTORY_EMPTY + @keysDirectory, log_obj)
110
+ elsif !@keysDirectory.instance_of? String
111
+ @keysDirectory=@keysDirectory.to_s
112
+ end
113
+ if @keyFilename.to_s.empty?
114
+ @keyFilename = @merchantId
115
+ ApiException.new.apiwarning(Constants::WARNING_PREFIX + Constants::KEY_FILE_NAME_NULL_EMPTY, log_obj)
116
+ elsif !@keyFilename.instance_of? String
117
+ @keyFilename=@keyFilename.to_s
118
+ end
119
+ end
120
+ if @authenticationType.upcase == Constants::AUTH_TYPE_HTTP
121
+ if @merchantKeyId.to_s.empty?
122
+ err = raise StandardError.new(Constants::ERROR_PREFIX+ Constants::MERCHANT_KEY_ID_MANDATORY)
123
+ ApiException.new.apiexception(err,log_obj)
124
+ elsif !@merchantKeyId.instance_of? String
125
+ @merchantKeyId=@merchantKeyId.to_s
126
+ end
127
+ if @merchantSecretKey.to_s.empty?
128
+ err = raise StandardError.new(Constants::ERROR_PREFIX+ Constants::MERCHANT_SECRET_KEY_MANDATORY)
129
+ ApiException.new.apiexception(err,log_obj)
130
+ elsif !@merchantSecretKey.instance_of? String
131
+ @merchantSecretKey=@merchantSecretKey.to_s
132
+ end
133
+ end
134
+ if !@proxyAddress.instance_of? String
135
+ @proxyAddress=@proxyAddress.to_s
136
+ end
137
+ if !@proxyPort.instance_of? String
138
+ @proxyPort=@proxyPort.to_s
139
+ end
140
+ end
141
+ def logAllProperties(propertyObj)
142
+ merchantConfig = ''
143
+ hiddenProperties = (Constants::HIDDEN_MERCHANT_PROPERTIES).split(',')
144
+ hiddenPropArray = Array.new
145
+ hiddenProperties.each do |value|
146
+ hiddenPropArray << value.strip
147
+ end
148
+ hiddenPropArray.each do |prop|
149
+ propertyObj.each do |key, value|
150
+ if key == prop
151
+ propertyObj.delete(key)
152
+ end
153
+ end
154
+ end
155
+ @log_obj.logger.info('MERCHCFG >' + propertyObj.to_s)
156
+ end
157
+
158
+ # getter and setter methods
159
+ attr_accessor :merchantId
160
+ attr_accessor :merchantSecretKey
161
+ attr_accessor :merchantKeyId
162
+ attr_accessor :authenticationType
163
+ attr_accessor :keysDirectory
164
+ attr_accessor :requestHost
165
+ attr_accessor :keyAlias
166
+ attr_accessor :keyPass
167
+ attr_accessor :keyFilename
168
+ attr_accessor :requestJsonData
169
+ attr_accessor :requestUrl
170
+ attr_accessor :requestType
171
+ attr_accessor :getId
172
+ attr_accessor :logDirectory
173
+ attr_accessor :logFilename
174
+ attr_accessor :enableLog
175
+ attr_accessor :logSize
176
+ attr_accessor :logger
177
+ attr_accessor :proxyAddress
178
+ attr_accessor :proxyPort
179
+ attr_accessor :requestTarget
180
+ attr_accessor :log_obj
181
+ end
@@ -0,0 +1,19 @@
1
+ public
2
+ class ApiException
3
+ def apiexception(err,log_Obj)
4
+ log_Obj.logger.error(err.message)
5
+ if !err.backtrace.to_s.empty?
6
+ log_Obj.logger.error(err.backtrace)
7
+ end
8
+ log_Obj.logger.info('END> =======================================')
9
+ puts 'Check log for more details.'
10
+ end
11
+ def apiwarning(message,log_Obj)
12
+ log_Obj.logger.warn(message)
13
+ end
14
+ def customerror(message,log_Obj)
15
+ log_Obj.logger.error(message)
16
+ log_Obj.logger.info('END> =======================================')
17
+ puts 'Check log for more details.'
18
+ end
19
+ end
@@ -0,0 +1,36 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ public
4
+ # P12 file certificate Cache
5
+ class Cache
6
+ def fetchCachedCertificate(filePath, p12File, keyPass, cacheObj)
7
+ certCache = cacheObj.read('certiFromP12File')
8
+ cachedLastModifiedTimeStamp = cacheObj.read('certificateLastModifiedTimeStamp')
9
+ if File.exist?(filePath)
10
+ currentFileLastModifiedTime = File.mtime(filePath)
11
+ if certCache.to_s.empty? || cachedLastModifiedTimeStamp.to_s.empty?
12
+ certificateFromP12File = getCertificate(p12File, keyPass, cacheObj, currentFileLastModifiedTime)
13
+ return certificateFromP12File
14
+ elsif currentFileLastModifiedTime > cachedLastModifiedTimeStamp
15
+ # Function call to read the file and put values to new cache
16
+ certificateFromP12File = getCertificate(p12File, keyPass, cacheObj, currentFileLastModifiedTime)
17
+ return certificateFromP12File
18
+ else
19
+ return certCache
20
+ end
21
+ else
22
+ raise Constants::ERROR_PREFIX + Constants::FILE_NOT_FOUND + filePath
23
+ end
24
+ end
25
+
26
+ def getCertificate(p12File, keyPass, cacheObj, currentFileLastModifiedTime)
27
+ p12FilePath = OpenSSL::PKCS12.new(p12File, keyPass)
28
+ # Generating certificate from p12File.
29
+ x5CertPem = OpenSSL::X509::Certificate.new(p12FilePath.certificate)
30
+ # Converting Certificate format from PEM TO DER to remove header and footer of the certificate.
31
+ x5CertDer = Base64.strict_encode64(x5CertPem.to_der)
32
+ cacheObj.write('certiFromP12File', x5CertDer)
33
+ cacheObj.write('certificateLastModifiedTimeStamp', currentFileLastModifiedTime)
34
+ return x5CertDer
35
+ end
36
+ end