unionpay_open 0.1.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: 99d503550ffdbcc797d456b6734bb50f684aa152
4
+ data.tar.gz: a2b87cae3d08f36cdf9c0e2dd33baf6259f4ecf0
5
+ SHA512:
6
+ metadata.gz: f903bff832867341d5c30440810dc9b7419fcaea2496ed7ed2d7f60ede1a4ca5e42d04d7602f65c793e04843229f6d0319e03ad3a9f911c6756d6410691c05dd
7
+ data.tar.gz: 95c411087b4dc9a61341b3a7dee587a5d6ed90c9d35883bf6c1d87a0d90185ceef627caa71c4ee7e03435bb50252bd2c806d2c9eb50802bc5351a5ffc6012854
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.1
4
+ before_install: gem install bundler -v 1.10.5
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,19 @@
1
+ source 'http://ruby.taobao.org/'
2
+ #source 'https://rubygems.org'
3
+
4
+
5
+ gem 'i18n'
6
+ gem 'activesupport'
7
+ gem 'faraday'
8
+
9
+
10
+ group :development do
11
+ gem 'pry'
12
+ gem 'pry-coolline'
13
+ gem 'pry-byebug'
14
+ gem 'pry-stack_explorer'
15
+ end
16
+
17
+
18
+ # Specify your gem's dependencies in unionpay_open.gemspec
19
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Yanying Wang
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,110 @@
1
+ # UnionpayOpen
2
+ http://open.unionpay.com
3
+
4
+ Ruby wapper for Unionpay(银联支付)'s Open API.
5
+
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'unionpay_open'
13
+ # or
14
+ gem 'unionpay_open', :git => 'git://github.com/yanyingwang/unionpay_open.git'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install unionpay_open
24
+
25
+ ## Usage
26
+
27
+
28
+ ### 配置
29
+ ```ruby
30
+ UnionpayOpen.config do |unionpay|
31
+ unionpay.pfx_file = 'tmp/acp.pfx'
32
+ unionpay.pfx_file_password = '000000'
33
+ unionpay.ca_file = 'tmp/acp.cer'
34
+ unionpay.merchant_no = '111111111111111'
35
+
36
+ # unionpay.env = 'production' # 默认值为'development',其可自动识别rack环境。
37
+ # 如: ENV['RACK_ENV']如果为'production',此gem则会自动加载为production环境。
38
+ # 可通过`UnionpayOpen.env`来检查确定Gem当前使用的环境。
39
+ # 此值的设定,主要影响`UnionpayOpen.endpoint`的值,即请求的银联网关地址。
40
+
41
+ end
42
+
43
+ ```
44
+
45
+
46
+ ### 查看
47
+ 配置载入完成后,可以通过如下命令查看配置文件内容:
48
+ ```ruby
49
+ UnionpayOpen.env
50
+ UnionpayOpen.endpoint
51
+
52
+ UnionpayOpen.pfx_file
53
+ UnionpayOpen.merchant_no
54
+
55
+ UnionpayOpen.pkcs12
56
+ UnionpayOpen.pkcs12.to_s
57
+ UnionpayOpen.pkcs12.certificate.serial.to_s
58
+
59
+ UnionpayOpen.x509_certificate.public_key.to_s
60
+ UnionpayOpen.x509_certificate.serial.to_s
61
+ ```
62
+
63
+ ### 单位转换
64
+ 银联接口总额使用分做单位,可以如如下方法转换:
65
+ ```ruby
66
+ # 10000分 = 100元
67
+ UnionpayOpen.fen2yuan(10000)
68
+
69
+
70
+ # 100.1元 = 10010分
71
+ UnionpayOpen.yuan2fen(100.1)
72
+ ```
73
+
74
+ ### 前台交易请求地址
75
+
76
+ ```ruby
77
+ response = UnionpayOpen::Wap.front_trans_req( front_url: 'http://test.com',
78
+ order_id: "abcd1qaz",
79
+ txn_amt: "10000" )
80
+ # response 正常情况将输入银联请求结果,为HTML页面代码。
81
+ ```
82
+
83
+ **注意**:
84
+
85
+ 1. 方法名支持按照ruby规范,用snake方式(下划线连接单词)书写,Gem会在内部自动进行转换以适应银联接口。
86
+
87
+ 2. 调用Gem时传入的参数默认只需包含必要字段(如front_trans_req只需front_url, order_id, txn_amt),其他字段如果也作为参数传入,将覆盖UnionpayOpen内部已经默认定义的值。
88
+
89
+ 3. debug需要,可在方法后添加一个block,来查看请求到银联的参数,如下示例代码:
90
+
91
+ ```ruby
92
+ # 查看其他字段默认值:
93
+ UnionpayOpen::Wap.front_trans_req {}
94
+
95
+ # 查看请求到银联接口的全部参数:
96
+ UnionpayOpen::Wap.front_trans_req( front_url: 'http://test.com',
97
+ order_id: "abcd1qaz",
98
+ txn_amt: "10000" ) {}
99
+ ```
100
+
101
+
102
+ ## Development
103
+
104
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
105
+
106
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/unionpay_open. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pry"
5
+ require "unionpay_open"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ Pry.start
12
+
13
+ # require "irb"
14
+ # IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,46 @@
1
+ require "unionpay_open/version"
2
+ require "unionpay_open/base"
3
+ require "unionpay_open/wap"
4
+ require 'active_support/core_ext/class/attribute_accessors'
5
+ require 'active_support/inflector'
6
+ require 'openssl'
7
+ require 'base64'
8
+ require 'faraday'
9
+
10
+
11
+
12
+ module UnionpayOpen
13
+ cattr_accessor :pfx_file, :pfx_file_password, :ca_file, :merchant_no, :pkcs12, :x509_certificate, :env, :endpoint
14
+ ENDPOINT_DEV = 'https://101.231.204.80:5000/gateway/api/'
15
+ ENDPOINT_PRO = 'https://gateway.95516.com/gateway/api/'
16
+ @@env = ENV['RACK_ENV']; @@env ||= 'development'
17
+ @@endpoint = ( @@env == "production" ? ENDPOINT_PRO : ENDPOINT_DEV )
18
+
19
+ class << self
20
+ def config
21
+ yield(self) if block_given?
22
+
23
+ @@endpoint = ENDPOINT_PRO if @@env == "production"
24
+ @@x509_certificate = OpenSSL::X509::Certificate.new(File.read(@@ca_file)) if @@ca_file
25
+ if @@pfx_file and @@pfx_file_password
26
+ @@pkcs12 = OpenSSL::PKCS12.new( File.read(@@pfx_file), @@pfx_file_password )
27
+ end
28
+ end
29
+
30
+ def fen2yuan(amount) # 10fen = 0.1yuan; 100fen = 1.0yuan
31
+ value = "00" + amount.to_s
32
+ [ value[0..-3], value[-2..-1] ].join('.').to_f
33
+ end
34
+
35
+ def yuan2fen(amount) # 100yuan = 10000fen
36
+ value = amount.to_s.split('.')
37
+
38
+ return (value.first + "00").to_i if value.size == 1
39
+
40
+ a, b = value.first, value.last
41
+ (b += "0") if b.size == 1
42
+ (a + b[0..1]).to_i
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,25 @@
1
+ module UnionpayOpen
2
+ Base = Class.new do
3
+ class << self
4
+
5
+ def faraday
6
+ Faraday.new(@@endpoint, :ssl => {:verify => false})
7
+ end
8
+
9
+ def sign(data)
10
+ Base64.strict_encode64(
11
+ @@pkcs12.key.sign(OpenSSL::Digest::SHA1.new,
12
+ Digest::SHA1.hexdigest(data)) )
13
+ end
14
+
15
+ def global_fixed_params
16
+ { version: '5.0.0',
17
+ encoding: 'UTF-8',
18
+ txnTime: Time.now.strftime("%Y%m%d%H%M%S"),
19
+ certId: @@pkcs12.certificate.serial.to_s,
20
+ merId: @@merchant_no }
21
+ end
22
+
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,3 @@
1
+ module UnionpayOpen
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,28 @@
1
+ module UnionpayOpen
2
+ class Wap < Base
3
+ class << self
4
+ @@wap_fixed_params = { signMethod: '01',
5
+ txnType: '01',
6
+ txnSubType: '01',
7
+ bizType: '000201',
8
+ channelType: '08',
9
+ accessType: "0",
10
+ currencyCode: "156" }
11
+
12
+ def front_trans_req(**params)
13
+ path = __method__.to_s.camelize(:lower) + '.do'
14
+
15
+ camelized_params = params.map{ |k, v| [k.to_s.camelize(:lower).to_sym, v]}.to_h
16
+ wap_params = global_fixed_params.merge(@@wap_fixed_params).merge(camelized_params)
17
+
18
+ metadata = wap_params.sort.map{ |k, v| "#{k}=#{v}" }.join('&')
19
+ wap_params[:signature] = sign(metadata)
20
+
21
+ return wap_params if block_given?
22
+ response = faraday.post(path, wap_params)
23
+ response.body
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,40 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'unionpay_open/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "unionpay_open"
8
+ spec.version = UnionpayOpen::VERSION
9
+ spec.authors = ["YanyingWang"]
10
+ spec.email = ["yanyingwang1@gmail.com"]
11
+
12
+ spec.summary = %q{Unionpay(银联支付) Open API in Ruby.}
13
+ spec.description = %q{Ruby wapper for Unionpay's Open API.}
14
+ spec.homepage = "https://github.com/yanyingwang/unionpay_open"
15
+ spec.license = "MIT"
16
+
17
+ spec.required_ruby_version = '>= 2.1.5'
18
+
19
+
20
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
21
+ # delete this section to allow pushing this gem to any host.
22
+ #if spec.respond_to?(:metadata)
23
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
24
+ #else
25
+ #raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
26
+ #end
27
+
28
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency "bundler", "~> 1.10"
34
+ spec.add_dependency "rake", "~> 10.0"
35
+ spec.add_dependency "rspec"
36
+ spec.add_dependency 'i18n'
37
+ spec.add_dependency 'activesupport'
38
+ spec.add_dependency 'faraday'
39
+
40
+ end
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unionpay_open
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - YanyingWang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-30 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.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: i18n
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Ruby wapper for Unionpay's Open API.
98
+ email:
99
+ - yanyingwang1@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/unionpay_open.rb
115
+ - lib/unionpay_open/base.rb
116
+ - lib/unionpay_open/version.rb
117
+ - lib/unionpay_open/wap.rb
118
+ - unionpay_open.gemspec
119
+ homepage: https://github.com/yanyingwang/unionpay_open
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: 2.1.5
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.4.8
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Unionpay(银联支付) Open API in Ruby.
143
+ test_files: []
144
+ has_rdoc: