unionpei 1.0.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 873151ea6fe19398b75fb59859966a9b24cff017f2b993a6cdee337275b24553
4
- data.tar.gz: 988869dfbec8acb49da6fa1497aa0907e68a4beea06aded21f85c379ffba7263
3
+ metadata.gz: 1e6b75a75c7873af619848b433d7f80bc086e110fd675130a529fdc87e38fcdb
4
+ data.tar.gz: 8d85162ba956cd54db133f1383f1c27354053d243fe4bc0d0105ce46b9283b63
5
5
  SHA512:
6
- metadata.gz: 8bd6c723b2e5bba4745f4daade5d185128bc8a53d8958b0e9e37caf8aad27b5834bc6af347b74f53362f57a41a47d2c5c7907daabd7abb6e156436495b55d86d
7
- data.tar.gz: 769762c3bcffd2b3e8bb9839c9183f7cf98f2129bfd28995777c32a23bf31e044f999e5da80a9e3f69052eabfbbfdb55c12cda6b0829bfc91ba0db2c7e925f45
6
+ metadata.gz: d7a5e4a06edf18f83a62d13a2974434b3fdbbc52d83125c94c2dd0f50a8061c3b9ac4ac74cfd90e10ea4216c543df7d68dec96d383f4fb742ed3eb65de53fe32
7
+ data.tar.gz: d743aa56571ab9bad9e23b2b87f63a41185da98ab126e3988f0f3924e03f6ad82dea18125267c0972af8f440dc640f7c46476a9258c35e07536ab34ae8c9f288
@@ -0,0 +1,45 @@
1
+ name: Ruby Gem
2
+
3
+ on:
4
+ push:
5
+ branches: [ "master" ]
6
+ pull_request:
7
+ branches: [ "master" ]
8
+
9
+ jobs:
10
+ build:
11
+ name: Build + Publish
12
+ runs-on: ubuntu-latest
13
+ permissions:
14
+ contents: read
15
+ packages: write
16
+
17
+ steps:
18
+ - uses: actions/checkout@v3
19
+ - name: Set up Ruby 2.6
20
+ uses: actions/setup-ruby@v1
21
+ with:
22
+ ruby-version: 2.6.x
23
+
24
+ - name: Publish to GPR
25
+ run: |
26
+ mkdir -p $HOME/.gem
27
+ touch $HOME/.gem/credentials
28
+ chmod 0600 $HOME/.gem/credentials
29
+ printf -- "---\n:github: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
30
+ gem build *.gemspec
31
+ gem push --KEY github --host https://rubygems.pkg.github.com/${OWNER} *.gem
32
+ env:
33
+ GEM_HOST_API_KEY: "Bearer ${{secrets.GITHUB_TOKEN}}"
34
+ OWNER: ${{ github.repository_owner }}
35
+
36
+ - name: Publish to RubyGems
37
+ run: |
38
+ mkdir -p $HOME/.gem
39
+ touch $HOME/.gem/credentials
40
+ chmod 0600 $HOME/.gem/credentials
41
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
42
+ gem build *.gemspec
43
+ gem push *.gem
44
+ env:
45
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_AUTH_TOKEN}}"
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ .idea
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in alipay.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ UnionPei - 非官方银联支付(UnionPay)SDK
2
+ --------
3
+
4
+ # Install
5
+
6
+ ```ruby
7
+ gem 'unionpei'
8
+
9
+ ```
10
+
11
+ # 快速开始(rails)
12
+
13
+ ## 指定读取设定的配置文件
14
+
15
+ ```ruby
16
+ # config/initializers/unionpei.rb
17
+ UnionPei.configure do |config|
18
+ if Rails.env.production?
19
+ config.acp_sdk_config_path = Rails.root.join('safe/unionpay/acp_production_sdk.ini')
20
+ end
21
+ end
22
+
23
+ ```
24
+
25
+ ## 生成支付的页面
26
+ ```ruby
27
+ # 参数可参考相关文档,或者源码中payment.rb
28
+
29
+ class PaymentsController < ApplicationController
30
+
31
+ # B2C 支付
32
+ def union_b2c_pay
33
+ UnionPei::Payment.b2c({})
34
+ render html: UnionPei::Payment.b2c.html_safe
35
+ end
36
+
37
+ # B2B 支付
38
+ def union_b2b_pay
39
+ UnionPei::Payment.b2b({})
40
+ render html: UnionPei::Payment.b2b.html_safe
41
+ end
42
+
43
+ # 查询订单详情
44
+ def query_trans
45
+ UnionPei::Payment.query_trans({})
46
+ end
47
+ end
48
+ ```
49
+
50
+ # 参考文档
51
+
52
+ - 银联测试参数:https://open.unionpay.com/tjweb/user/mchTest/param
53
+ - 测试说明:https://open.unionpay.com/tjweb/support/faq/mchlist?id=516
54
+ - 证书说明:https://open.unionpay.com/tjweb/support/faq/mchlist?id=21
55
+ - API文档:https://open.unionpay.com/tjweb/acproduct/APIList?apiservId=448&acpAPIId=754&bussType=0
56
+ - SDK下载:https://open.unionpay.com/tjweb/support/faq/mchlist?id=38
57
+ - 测试卡信息:https://open.unionpay.com/tjweb/support/faq/mchlist?id=4
58
+ - B2B:https://open.unionpay.com/tjweb/acproduct/list?apiSvcId=452&index=999
59
+
60
+ # 免责声明
61
+
62
+ 本Gem对以下非官方代码进行封装和改造:
63
+
64
+ https://open.unionpay.com/tjweb/support/faq/mchlist?id=38
65
+
66
+ 代码仅供参考学习,生产环境请自行封装代码。
67
+
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new do |t|
7
+ t.libs << 'test'
8
+ t.test_files = FileList['test/**/*_test.rb']
9
+ end
10
+
11
+ task default: :test
@@ -0,0 +1,25 @@
1
+ -----BEGIN CERTIFICATE-----
2
+ MIIEPzCCAyegAwIBAgIFEDl2NhIwDQYJKoZIhvcNAQEFBQAwWDELMAkGA1UEBhMC
3
+ Q04x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97KJz/SPt2JAYnZOlIDqWwp9/Afvt0/MDr8y0PK
20
+ Q9c6eqIzBx7a9LpUTUl5u1jS+xSDZ/KF2lXnjwaFp7jICLWEMlBstCoogi7KwH9A
21
+ LpJP7/dj9g==
22
+ -----END CERTIFICATE-----
Binary file
data/install.sh ADDED
@@ -0,0 +1,2 @@
1
+ gem build unionpei.gemspec
2
+ gem install unionpei-*.gem
@@ -1,4 +1,4 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'openssl'
4
4
  require 'base64'
@@ -8,99 +8,94 @@ require_relative 'sdk_util'
8
8
 
9
9
  module UnionPei
10
10
  class AcpService
11
- def AcpService.sign(req, certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
11
+ def self.sign(req, certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
12
12
  SDKUtil.buildSignature(req, certPath, certPwd)
13
13
  end
14
14
 
15
- def AcpService.signByCertInfo(req, certPath, certPwd)
15
+ def self.signByCertInfo(req, certPath, certPwd)
16
16
  SDKUtil.buildSignature(req, certPath, certPwd)
17
17
  end
18
18
 
19
- def AcpService.signBySecureKey(req, secureKey)
19
+ def self.signBySecureKey(req, secureKey)
20
20
  SDKUtil.buildSignature(req, nil, nil, secureKey)
21
21
  end
22
22
 
23
- def AcpService.validate(resp)
23
+ def self.validate(resp)
24
24
  SDKUtil.verify(resp)
25
25
  end
26
26
 
27
- def AcpService.validateBySecureKey(resp, secureKey)
27
+ def self.validateBySecureKey(resp, secureKey)
28
28
  SDKUtil.verifyBySecureKey(resp, secureKey)
29
29
  end
30
30
 
31
- def AcpService.post(params, url)
31
+ def self.post(params, url)
32
32
  content = SDKUtil.createLinkString(params, false, true)
33
33
  respString = SDKUtil.post(url, content)
34
- resp = SDKUtil.parseQString(respString)
35
- return resp
34
+ SDKUtil.parseQString(respString)
36
35
  end
37
36
 
38
- def AcpService.createAutoFormHtml(params, reqUrl)
39
- return SDKUtil.createAutoFormHtml(params, reqUrl)
37
+ def self.createAutoFormHtml(params, reqUrl)
38
+ SDKUtil.createAutoFormHtml(params, reqUrl)
40
39
  end
41
40
 
42
- def AcpService.getCustomerInfo(customerInfo)
43
- if(customerInfo == nil or customerInfo.length == 0)
44
- return ""
45
- end
46
- return Base.encode64("{" + SDKUtil.createLinkString(customerInfo,false,false)+"}").gsub(/\n|\r/, '')
41
+ def self.getCustomerInfo(customerInfo)
42
+ return '' if customerInfo.nil? || customerInfo.length.zero?
43
+
44
+ Base.encode64("{#{SDKUtil.createLinkString(customerInfo, false, false)}}").gsub(/\n|\r/, '')
47
45
  end
48
46
 
49
- def AcpService.getCustomerInfoWithEncrypt(customerInfo)
50
- if(customerInfo == nil or customerInfo.length == 0)
51
- return ""
52
- end
47
+ def self.getCustomerInfoWithEncrypt(customerInfo)
48
+ return '' if customerInfo.nil? || customerInfo.length.zero?
49
+
53
50
  encryptedInfo = {}
54
- for key in customerInfo.keys
55
- if (key == 'phoneNo' or key == 'cvn2' or key == 'expired')
56
- encryptedInfo[key] = customerInfo.delete(key)
57
- end
51
+ customerInfo.each_key do |key|
52
+ encryptedInfo[key] = customerInfo.delete(key) if (key == 'phoneNo') || (key == 'cvn2') || (key == 'expired')
58
53
  end
59
- if (encryptedInfo.length > 0)
54
+ if encryptedInfo.length.positive?
60
55
  encryptedInfo = SDKUtil.createLinkString(encryptedInfo, false, false)
61
56
  encryptedInfo = AcpService.encryptData(encryptedInfo, SDKConfig.instance.encryptCertPath)
62
57
  customerInfo['encryptedInfo'] = encryptedInfo
63
58
  end
64
- return Base64.encode64("{" + SDKUtil.createLinkString(customerInfo,false,false)+"}").gsub(/\n|\r/, '')
59
+ Base64.encode64("{#{SDKUtil.createLinkString(customerInfo, false, false)}}").gsub(/\n|\r/, '')
65
60
  end
66
61
 
67
- def AcpService.parseCustomerInfo(customerInfostr, certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
62
+ def self.parseCustomerInfo(customerInfostr, certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
68
63
  customerInfostr = Base64.decode64(customerInfostr)
69
- customerInfostr = customerInfostr[1, customerInfostr.length-1]
64
+ customerInfostr = customerInfostr[1, customerInfostr.length - 1]
70
65
  customerInfo = SDKUtil.parseQString(customerInfostr)
71
66
  if customerInfo['encryptedInfo']
72
67
  encryptedInfoStr = customerInfo.delete('encryptedInfo')
73
68
  encryptedInfoStr = AcpService.decryptData(encryptedInfoStr, certPath, certPwd)
74
69
  encryptedInfo = SDKUtil.parseQString(encryptedInfoStr)
75
- for key in encryptedInfo.keys
70
+ encryptedInfo.each_key do |key|
76
71
  customerInfo[key] = encryptedInfo[key]
77
72
  end
78
73
  end
79
- return customerInfo
74
+ customerInfo
80
75
  end
81
76
 
82
- def AcpService.getEncryptCertId
83
- return CertUtil.getEncryptCertId
77
+ def self.getEncryptCertId
78
+ CertUtil.getEncryptCertId
84
79
  end
85
80
 
86
- def AcpService.encryptData(data, certPath=SDKConfig.instance.encryptCertPath)
87
- return SDKUtil.encryptPub(data, certPath)
81
+ def self.encryptData(data, certPath = SDKConfig.instance.encryptCertPath)
82
+ SDKUtil.encryptPub(data, certPath)
88
83
  end
89
84
 
90
- def AcpService.decryptData(data, certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
91
- return SDKUtil.decryptPri(data, certPath, certPwd)
85
+ def self.decryptData(data, certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
86
+ SDKUtil.decryptPri(data, certPath, certPwd)
92
87
  end
93
88
 
94
- def AcpService.deCodeFileContent(params, fileDirectory)
95
- return SDKUtil.deCodeFileContent(params, fileDirectory)
89
+ def self.deCodeFileContent(params, fileDirectory)
90
+ SDKUtil.deCodeFileContent(params, fileDirectory)
96
91
  end
97
92
 
98
- def AcpService.enCodeFileContent(path)
99
- return SDKUtil.enCodeFileContent(path)
93
+ def self.enCodeFileContent(path)
94
+ SDKUtil.enCodeFileContent(path)
100
95
  end
101
96
 
102
- def AcpService.updateEncryptCert(params)
103
- return SDKUtil.getEncryptCert(params)
97
+ def self.updateEncryptCert(params)
98
+ SDKUtil.getEncryptCert(params)
104
99
  end
105
100
  end
106
101
  end
@@ -1,37 +1,31 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
2
 
3
3
  require 'openssl'
4
4
  require 'base64'
5
5
  require_relative 'log_util'
6
6
  require_relative 'sdk_config'
7
7
 
8
-
9
8
  module UnionPei
10
- UNIONPAY_CNNAME = "中国银联股份有限公司"
9
+ UNIONPAY_CNNAME = '中国银联股份有限公司'
11
10
 
12
11
  class Cert
13
12
  attr_accessor :cert, :certId, :key
14
- @certId
15
- @key
16
- @cert
17
13
  end
18
14
 
19
15
  class CertUtil
20
-
21
16
  @@signCerts = {}
22
17
  @@encryptCert = {}
23
- @@verifyCerts = {} #5.0.0验签证书,key是certId
24
- @@verifyCerts5_1_0 = {} #5.1.0验签证书,key是base64的证书内容
18
+ @@verifyCerts = {} # 5.0.0验签证书,key是certId
19
+ @@verifyCerts5_1_0 = {} # 5.1.0验签证书,key是base64的证书内容
25
20
  @@middleCert = nil
26
21
  @@rootCert = nil
27
22
 
28
- private
29
- def CertUtil.initSignCert(certPath, certPwd)
23
+ def self.initSignCert(certPath, certPwd)
30
24
  if !certPath || !certPwd
31
- LogUtil.info("signCertPath or signCertPwd is none, exit initSignCert")
25
+ LogUtil.info('signCertPath or signCertPwd is none, exit initSignCert')
32
26
  return
33
27
  end
34
- LogUtil.info("读取签名证书……")
28
+ LogUtil.info('读取签名证书……')
35
29
  cert = Cert.new
36
30
  file = IO.binread(certPath)
37
31
  p12 = OpenSSL::PKCS12.new(file, certPwd)
@@ -39,89 +33,76 @@ module UnionPei
39
33
  cert.cert = p12.certificate
40
34
  cert.key = p12.key
41
35
  @@signCerts[certPath] = cert
42
- LogUtil.info("签名证书读取成功,序列号:" + cert.certId)
43
- end
44
-
45
- def CertUtil.initEncryptCert(certPath=SDKConfig.instance.encryptCertPath)
46
- if !certPath
47
- LogUtil.info("encryptCertPath is none, exit initEncryptCert")
48
- return
49
- end
50
- LogUtil.info("读取加密证书……")
51
- cert = Cert.new
52
- file = IO.binread(certPath)
53
- x509Cert = OpenSSL::X509::Certificate.new(file)
54
- cert.cert = x509Cert
55
- cert.certId = x509Cert.serial.to_s
56
- cert.key = x509Cert.public_key
57
- @@encryptCert[certPath] = cert
58
- LogUtil.info("加密证书读取成功,序列号:" + cert.certId)
36
+ LogUtil.info("签名证书读取成功,序列号:#{cert.certId}")
59
37
  end
60
38
 
61
- def CertUtil.initRootCert()
62
- if @@rootCert
39
+ def self.initEncryptCert(certPath = SDKConfig.instance.encryptCertPath)
40
+ unless certPath
41
+ LogUtil.info('encryptCertPath is none, exit initEncryptCert')
63
42
  return
64
43
  end
65
- if !SDKConfig.instance.rootCertPath
66
- LogUtil.info("rootCertPath is none, exit initRootCert")
44
+ LogUtil.info('读取加密证书……')
45
+ cert = Cert.new
46
+ file = IO.binread(certPath)
47
+ x509Cert = OpenSSL::X509::Certificate.new(file)
48
+ cert.cert = x509Cert
49
+ cert.certId = x509Cert.serial.to_s
50
+ cert.key = x509Cert.public_key
51
+ @@encryptCert[certPath] = cert
52
+ LogUtil.info("加密证书读取成功,序列号:#{cert.certId}")
53
+ end
54
+
55
+ def self.initRootCert
56
+ return if @@rootCert
57
+
58
+ unless SDKConfig.instance.rootCertPath
59
+ LogUtil.info('rootCertPath is none, exit initRootCert')
67
60
  return
68
61
  end
69
- LogUtil.info("start initRootCert")
62
+ LogUtil.info('start initRootCert')
70
63
  file = IO.binread(SDKConfig.instance.rootCertPath)
71
64
  x509Cert = OpenSSL::X509::Certificate.new(file)
72
65
  @@rootCert = x509Cert
73
- LogUtil.info("initRootCert succeed")
66
+ LogUtil.info('initRootCert succeed')
74
67
  end
75
68
 
76
- def CertUtil.initMiddleCert()
77
- if @@middleCert
78
- return
79
- end
80
- if !SDKConfig.instance.middleCertPath
81
- LogUtil.info("middleCertPath is none, exit initMiddleCert")
69
+ def self.initMiddleCert
70
+ return if @@middleCert
71
+
72
+ unless SDKConfig.instance.middleCertPath
73
+ LogUtil.info('middleCertPath is none, exit initMiddleCert')
82
74
  return
83
75
  end
84
- LogUtil.info("start initMiddleCert")
76
+ LogUtil.info('start initMiddleCert')
85
77
  file = IO.binread(SDKConfig.instance.middleCertPath)
86
78
  x509Cert = OpenSSL::X509::Certificate.new(file)
87
79
  @@middleCert = x509Cert
88
- LogUtil.info("initMiddleCert succeed")
80
+ LogUtil.info('initMiddleCert succeed')
89
81
  end
90
82
 
91
- public
92
- def CertUtil.getSignPriKey(certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
93
- if !@@signCerts[certPath]
94
- CertUtil.initSignCert(certPath, certPwd)
95
- end
83
+ def self.getSignPriKey(certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
84
+ CertUtil.initSignCert(certPath, certPwd) unless @@signCerts[certPath]
96
85
  @@signCerts[certPath].key
97
86
  end
98
87
 
99
- def CertUtil.getSignCertId(certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
100
- if !@@signCerts[certPath]
101
- CertUtil.initSignCert(certPath, certPwd)
102
- end
88
+ def self.getSignCertId(certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
89
+ CertUtil.initSignCert(certPath, certPwd) unless @@signCerts[certPath]
103
90
  @@signCerts[certPath].certId
104
91
  end
105
92
 
106
- def CertUtil.getEncryptKey(certPath=SDKConfig.instance.encryptCertPath)
107
- if !@@encryptCert[certPath]
108
- CertUtil.initEncryptCert(certPath)
109
- end
93
+ def self.getEncryptKey(certPath = SDKConfig.instance.encryptCertPath)
94
+ CertUtil.initEncryptCert(certPath) unless @@encryptCert[certPath]
110
95
  @@encryptCert[certPath].key
111
96
  end
112
97
 
113
- def CertUtil.getEncryptCertId(certPath=SDKConfig.instance.encryptCertPath)
114
- if !@@encryptCert[certPath]
115
- CertUtil.initEncryptCert(certPath)
116
- end
98
+ def self.getEncryptCertId(certPath = SDKConfig.instance.encryptCertPath)
99
+ CertUtil.initEncryptCert(certPath) unless @@encryptCert[certPath]
117
100
  @@encryptCert[certPath].certId
118
101
  end
119
102
 
120
- def CertUtil.verifyAndGetVerifyKey(certBase64String)
103
+ def self.verifyAndGetVerifyKey(certBase64String)
104
+ return @@verifyCerts5_1_0[certBase64String].key if @@verifyCerts5_1_0[certBase64String]
121
105
 
122
- if @@verifyCerts5_1_0[certBase64String]
123
- return @@verifyCerts5_1_0[certBase64String].key
124
- end
125
106
  initMiddleCert
126
107
  initRootCert
127
108
 
@@ -137,31 +118,31 @@ module UnionPei
137
118
  store.add_cert(x509Cert)
138
119
  store.add_cert(@@middleCert)
139
120
  store.add_cert(@@rootCert)
140
- if !store.verify(x509Cert)
141
- LogUtil.error("validate signPubKeyCert by cert chain failed, error=" + store.error + ", error string=" + store.error_string)
121
+ unless store.verify(x509Cert)
122
+ LogUtil.error("validate signPubKeyCert by cert chain failed, error=#{store.error}, error string=#{store.error_string}")
142
123
  return nil
143
124
  end
144
125
 
145
126
  sSubject = x509Cert.subject.to_s
146
- ss = sSubject.split("@")
127
+ ss = sSubject.split('@')
147
128
  if ss.length <= 2
148
- LogUtil.error("error sSubject: " + sSubject)
129
+ LogUtil.error("error sSubject: #{sSubject}")
149
130
  return nil
150
131
  end
151
- cn = ss[2];
132
+ cn = ss[2]
152
133
  if SDKConfig.instance.ifValidateCNName
153
134
  if UNIONPAY_CNNAME != cn
154
- LogUtil.error("cer owner is not CUP:" + cn)
135
+ LogUtil.error("cer owner is not CUP:#{cn}")
155
136
  return nil
156
- elsif UNIONPAY_CNNAME != cn and cn != "00040000:SIGN" #测试环境目前是00040000:SIGN
157
- LogUtil.error("cer owner is not CUP:" + cn)
137
+ elsif (UNIONPAY_CNNAME != cn) && (cn != '00040000:SIGN') # 测试环境目前是00040000:SIGN
138
+ LogUtil.error("cer owner is not CUP:#{cn}")
158
139
  return nil
159
140
  end
160
141
  end
161
142
 
162
- LogUtil.info("validate signPubKeyCert by cert succeed: " + certBase64String)
163
- @@verifyCerts5_1_0[certBase64String] = cert;
164
- return @@verifyCerts5_1_0[certBase64String].key
143
+ LogUtil.info("validate signPubKeyCert by cert succeed: #{certBase64String}")
144
+ @@verifyCerts5_1_0[certBase64String] = cert
145
+ @@verifyCerts5_1_0[certBase64String].key
165
146
 
166
147
  # 用bc的jar用中级证书验证可以单独验时间,然后再用中级证书验一下,但为了和谐统一,目前改store验证书链验证了。
167
148
  # if Time.new<x509Cert.not_before or Time.new>x509Cert.not_after
@@ -176,19 +157,17 @@ module UnionPei
176
157
  # end
177
158
  end
178
159
 
179
- def CertUtil.getDecryptPriKey(certPath=SDKConfig.instance.signCertPath, certPwd=SDKConfig.instance.signCertPwd)
180
- if !@@signCerts[certPath]
181
- CertUtil.initSignCert(certPath, certPwd)
182
- end
160
+ def self.getDecryptPriKey(certPath = SDKConfig.instance.signCertPath, certPwd = SDKConfig.instance.signCertPwd)
161
+ CertUtil.initSignCert(certPath, certPwd) unless @@signCerts[certPath]
183
162
  @@signCerts[certPath].key
184
163
  end
185
164
 
186
- def CertUtil.resetEncryptCertPublicKey()
165
+ def self.resetEncryptCertPublicKey
187
166
  @@encryptCert = {}
188
167
  CertUtil.initEncryptCert
189
168
  end
190
169
 
191
- def CertUtil.getX509Cert(strCert)
170
+ def self.getX509Cert(strCert)
192
171
  OpenSSL::X509::Certificate.new(strCert)
193
172
  end
194
173
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UnionPei
4
+ class Configuration
5
+ attr_accessor :acp_sdk_config_path
6
+
7
+ def initialize
8
+ @acp_sdk_config_path = default_acp_sdk_path
9
+ end
10
+
11
+ def default_acp_sdk_path
12
+ "#{File.dirname(__FILE__)}/acp_sdk.ini"
13
+ end
14
+ end
15
+ end