unionpay_app 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7afd21bfe81de80f0153d31d23e192cd803a5617
4
+ data.tar.gz: 23a2262bf8d3bca7699c8fde77e2734d1d92e288
5
+ SHA512:
6
+ metadata.gz: 7e3d9713349471d32f03cc0bdaeb99272d5087b6e6dce2e0b962ba45a5744d0911c6dc86352adad500d0b22e7822eb06bbf5c9e2e3b5359006850bcdff40e471
7
+ data.tar.gz: 9a7d7ab2af53d3ebc0c04946500c4269eed618b3fd39d4be2c4831810eb31e800c4445d6dfa8f5a0f0198ede1055e8bc28094df6b585719ff670fda9edd493ce
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "typhoeus", "~> 0.6.9"
4
+
5
+ # Specify your gem's dependencies in alipay.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ unionpay_app (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ethon (0.7.3)
10
+ ffi (>= 1.3.0)
11
+ fakeweb (1.3.0)
12
+ ffi (1.9.8)
13
+ rake (10.4.2)
14
+ typhoeus (0.6.9)
15
+ ethon (>= 0.7.1)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bundler (~> 1.3)
22
+ fakeweb
23
+ rake
24
+ typhoeus (~> 0.6.9)
25
+ unionpay_app!
data/README.md ADDED
@@ -0,0 +1,12 @@
1
+ 该gem是一个银联手机控件支付接口
2
+ 1. #银联签名 返回hash {time: "交易时间", sign: "post给银联的hash"}
3
+ UnionpayApp::Service.sign txtAmt, orderId
4
+ params = UnionpayApp::Service.sign("100", "20150415122801272")
5
+ 2. #发送给银联post请求 换取tn值
6
+ UnionpayApp::Service.post UnionpayApp::Service.sign(txtAmt, orderId)[:sign]
7
+ UnionpayApp::Service.post(params)
8
+ 3. #银联验签:当请求银联获取tn后,或者请求银联查询交易后,或者当交易完成后,银联通过backUrl、frontUrl返回你结果时要进行验签
9
+ UnionpayApp::Service.verify params
10
+ 4. #查询交易是否成功 传入参数order_id
11
+ UnionpayApp::Service.query order_id, UnionpayApp::Service.sign(txtAmt, orderId)[:time]
12
+ UnionpayApp::Service.query "20150415122801272", params[:time]
@@ -0,0 +1,97 @@
1
+ require 'digest'
2
+ require 'openssl'
3
+ require "base64"
4
+ require 'open-uri'
5
+ require 'typhoeus'
6
+
7
+ module UnionpayApp
8
+ module Service
9
+ #银联支付签名
10
+ def self.sign txtAmt, orderId
11
+ union_params = {
12
+ :version => "5.0.0",
13
+ :encoding => "utf-8",
14
+ :certId => UnionpayApp.cert_id,
15
+ :txnType => '01',
16
+ :txnSubType => "01",
17
+ :bizType => "000201",
18
+ :channelType => "08",
19
+ :frontUrl => UnionpayApp.front_url,
20
+ :backUrl => UnionpayApp.back_url,
21
+ :accessType => "0",
22
+ :merId => UnionpayApp.mer_id,
23
+ :orderId => orderId, #商户订单号
24
+ :txnTime => Time.now.strftime("%Y%m%d%H%M%S"), #订单发送时间
25
+ :txnAmt => txtAmt, #以分为单位
26
+ :currencyCode => '156',
27
+ :signMethod => '01',
28
+ }
29
+ data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
30
+ sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
31
+ {time: union_params[:txnTime], sign: union_params.merge(signature: sign)}
32
+ end
33
+
34
+ def self.post union_params
35
+ request = Typhoeus::Request.new(UnionpayApp.uri, method: :post, params: union_params[:sign], ssl_verifypeer: false, headers: {'Content-Type' =>'application/x-www-form-urlencoded'} )
36
+ request.run
37
+ if request.response.success?
38
+ tn = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['tn']
39
+ else
40
+ tn = ""
41
+ end
42
+ end
43
+
44
+ #银联支付验签
45
+ def self.verify params
46
+ if unionpay_get_public_key_by_cert_id params['certId']
47
+ public_key = unionpay_get_public_key_by_cert_id params['certId']
48
+ signature_str = params['signature']
49
+ p = params.reject{|k, v| k == "signature"}.sort.map{|key, value| "#{key}=#{value}" }.join('&')
50
+ signature = Base64.decode64(signature_str)
51
+ data = Digest::SHA1.hexdigest(p)
52
+ key = OpenSSL::PKey::RSA.new public_key
53
+ digest = OpenSSL::Digest::SHA256.new
54
+ key.verify digest, signature, data
55
+ else
56
+ false
57
+ end
58
+ end
59
+
60
+ # 银联支付 根据证书id返回公钥
61
+ def self.get_public_key_by_cert_id cert_id
62
+ certificate = OpenSSL::X509::Certificate.new(UnionpayApp.cer) #读取cer文件
63
+ certificate.serial.to_s == cert_id ? certificate.public_key.to_s : nil #php 返回的直接是cer文件 UnionpayApp.cer
64
+ end
65
+
66
+ def self.query order_id, txnTime
67
+ union_params = {
68
+ :version => '5.0.0', #版本号
69
+ :encoding => 'utf-8', #编码方式
70
+ :certId => UnionpayApp.cert_id, #证书ID
71
+ :signMethod => '01', #签名方法
72
+ :txnType => '00', #交易类型
73
+ :txnSubType => '00', #交易子类
74
+ :bizType => '000000', #业务类型
75
+ :accessType => '0', #接入类型
76
+ :channelType => '07', #渠道类型
77
+ :orderId => order_id, #请修改被查询的交易的订单号
78
+ :merId => UnionpayApp.mer_id, #商户代码,请修改为自己的商户号
79
+ :txnTime => txnTime, #请修改被查询的交易的订单发送时间
80
+ }
81
+ data = Digest::SHA1.hexdigest(union_params.sort.map{|key, value| "#{key}=#{value}" }.join('&'))
82
+ sign = Base64.encode64(OpenSSL::PKey::RSA.new(UnionpayApp.private_key).sign('sha1', data.force_encoding("utf-8"))).gsub("\n", "")
83
+ request = Typhoeus::Request.new(UnionpayApp.uri, method: :post, params: union_params.merge(signature: sign), ssl_verifypeer: false, headers: {'Content-Type' =>'application/x-www-form-urlencoded'} )
84
+ request.run
85
+ if request.response.success?
86
+ code = Hash[*request.response.body.split("&").map{|a| a.gsub("==", "@@").split("=")}.flatten]['origRespCode']
87
+ elsif request.response.timed_out?
88
+ code = "got a time out"
89
+ elsif request.response.code == 0
90
+ code = request.response.return_message
91
+ else
92
+ code = request.response.code.to_s
93
+ end
94
+ end
95
+
96
+ end
97
+ end
@@ -0,0 +1,3 @@
1
+ module UnionpayApp
2
+ VERSION = "0.6.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'unionpay_app/service'
2
+
3
+ module UnionpayApp
4
+ class << self
5
+ attr_accessor :front_url
6
+ attr_accessor :back_url
7
+ attr_accessor :mer_id
8
+ attr_accessor :uri
9
+ attr_accessor :private_key
10
+ attr_accessor :cer
11
+ attr_accessor :cert_id
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unionpay_app/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unionpay_app"
8
+ spec.version = UnionpayApp::VERSION
9
+ spec.authors = ["Yohansun"]
10
+ spec.email = ["yohansun@qq.com"]
11
+ spec.description = %q{An unofficial simple unionpay_app gem}
12
+ spec.summary = %q{An unofficial simple unionpay_app gem}
13
+ spec.homepage = "https://github.com/Yohansun/unionpay_app"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "fakeweb"
24
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unionpay_app
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Yohansun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: fakeweb
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: An unofficial simple unionpay_app gem
56
+ email:
57
+ - yohansun@qq.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - Gemfile.lock
64
+ - README.md
65
+ - lib/unionpay_app.rb
66
+ - lib/unionpay_app/service.rb
67
+ - lib/unionpay_app/version.rb
68
+ - unionpay_app.gemspec
69
+ homepage: https://github.com/Yohansun/unionpay_app
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.2.2
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: An unofficial simple unionpay_app gem
93
+ test_files: []