cmbpay 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1033ee7980717b1e21d499b08a77fdb87fa567231e247c38c53c315ad3daecd3
4
+ data.tar.gz: 72429abbbe182af66ef9d83e5744583c49176787ca0b8c7da16db148bd66e1f3
5
+ SHA512:
6
+ metadata.gz: 4d69f0baa0a2b3e3f531f996947c9715f21c8b40422be55ea03ab95a0c1b94bded45a09b6df71005cdb8e9a78fe65765527d29c6b1c6deb89ba4b983d9ab3397
7
+ data.tar.gz: 07d46ae6f13ac29414f040ab481a6c1bb8fef9c4e987c824aee8a96ca6d932817c3213512128c8df97f23e5a25b2df175a47162496421db65781b92f7e26fd84
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .*.sw[a-z]
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at zouchao2008@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in cmbpay.gemspec
6
+ gemspec
@@ -0,0 +1,51 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cmbpay (0.1.0)
5
+ rest-client (>= 2.0.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ domain_name (0.5.20180417)
12
+ unf (>= 0.0.5, < 1.0.0)
13
+ http-cookie (1.0.3)
14
+ domain_name (~> 0.5)
15
+ mime-types (3.1)
16
+ mime-types-data (~> 3.2015)
17
+ mime-types-data (3.2016.0521)
18
+ netrc (0.11.0)
19
+ rake (10.5.0)
20
+ rest-client (2.0.2)
21
+ http-cookie (>= 1.0.2, < 2.0)
22
+ mime-types (>= 1.16, < 4.0)
23
+ netrc (~> 0.8)
24
+ rspec (3.5.0)
25
+ rspec-core (~> 3.5.0)
26
+ rspec-expectations (~> 3.5.0)
27
+ rspec-mocks (~> 3.5.0)
28
+ rspec-core (3.5.4)
29
+ rspec-support (~> 3.5.0)
30
+ rspec-expectations (3.5.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.5.0)
33
+ rspec-mocks (3.5.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.5.0)
36
+ rspec-support (3.5.0)
37
+ unf (0.1.4)
38
+ unf_ext
39
+ unf_ext (0.0.7.5)
40
+
41
+ PLATFORMS
42
+ ruby
43
+
44
+ DEPENDENCIES
45
+ bundler (~> 1.16)
46
+ cmbpay!
47
+ rake (~> 10.0)
48
+ rspec (~> 3.0)
49
+
50
+ BUNDLED WITH
51
+ 1.16.0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 zouchao
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,59 @@
1
+ # Cmbpay
2
+ 非官方sdk, 开发文档详见: [http://openhome.cmbchina.com/pay/Default.aspx][1]
3
+
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'cmbpay'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install cmbpay
19
+
20
+ ## Usage
21
+
22
+ ### config
23
+
24
+ Create config/initializers/cmbpay.rb and put following configurations into it.
25
+ ```ruby
26
+ # required
27
+ Cmbpay.merchant_no = 'YOUR_MERCHANT_NO'
28
+ Cmbpay.branch_no = 'YOUR_BRANCH_NO' # usually bank city area code
29
+ Cmbpay.key = 'YOUR_KEY'
30
+ Cmbpay.debug_mode = true # default is `true`
31
+ Cmbpay.sandbox_mode = false # cmbchina test environment, default is `false`
32
+ ```
33
+
34
+ ### APIs
35
+ Check official document for detailed request params and return fields
36
+
37
+ #### MB_APPQRPay
38
+ ```ruby
39
+ # required fields
40
+ params = {
41
+ :orderNo => "123124",
42
+ :amount => "0.01",
43
+ :payNoticeUrl => "http://baidu.com",
44
+ :clientIP => "127.0.0.1"
45
+ }
46
+ r = Cmbpay::Service.mb_appqrpay params
47
+ # => {
48
+ # :method => :post,
49
+ # :url => "http://121.15.180.66:801/netpayment/BaseHttp.dll?MB_APPQRPay",
50
+ # :payload => {
51
+ # :charset => "utf-8",
52
+ # :jsonRequestData => "{\"version\":\"1.0\",\"signType\":\"SHA-256\",\"reqData\":{\"merchantNo\":\"YOUR_MERCHANT_NO\",\"branchNo\":\"YOUR_BRANCH_NO\",\"dateTime\":\"20180808110251\",\"date\":\"20180808\",\"orderNo\":\"123124\",\"amount\":\"0.01\",\"payNoticeUrl\":\"http://baidu.com\",\"clientIP\":\"127.0.0.1\"},\"sign\":\"50b51645e1b9201007511c5b11a8671c6ad8dde2eccb940e65da1ced414147dd\"}"
53
+ # }
54
+ # }
55
+ ```
56
+
57
+
58
+
59
+ [1]: http://openhome.cmbchina.com/pay/Default.aspx
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cmbpay"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cmbpay/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "cmbpay"
7
+ spec.version = Cmbpay::VERSION
8
+ spec.authors = ["zouchao"]
9
+ spec.email = ["zouchao2008@gmail.com"]
10
+
11
+ spec.summary = %q{An unofficial simple cmb netpayment gem}
12
+ spec.description = %q{An unofficial simple cmb netpayment gem, copied from alipay}
13
+ spec.homepage = "https://github.com/zouchao/cmbpay"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ spec.bindir = "bin"
23
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency "rest-client", '>= 2.0.0'
27
+
28
+ spec.add_development_dependency "bundler", "~> 1.16"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ end
@@ -0,0 +1,20 @@
1
+ require "cmbpay/version"
2
+ require "Cmbpay/service"
3
+ require "Cmbpay/utils"
4
+ require "Cmbpay/sign"
5
+
6
+ module Cmbpay
7
+ @sign_type = 'SHA-256'
8
+ @sandbox_mode = false
9
+ @debug_mode = true
10
+ @extra_rest_client_options = {}
11
+
12
+ class << self
13
+ attr_accessor :merchant_no, :branch_no, :key, :sign_type, :extra_rest_client_options, :sandbox_mode, :debug_mode
14
+
15
+
16
+ def debug_mode?
17
+ !!@debug_mode
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,232 @@
1
+ require 'json'
2
+ require 'rest_client'
3
+ module Cmbpay
4
+ module Service
5
+ SANDBOX_MB_APPQRPAY_GATEWAY_URL = 'http://121.15.180.66:801/netpayment/BaseHttp.dll?MB_APPQRPay'
6
+ MB_APPQRPAY_GATEWAY_URL = 'https://netpay.cmbchina.com/netpayment/BaseHttp.dll?MB_APPQRPay'
7
+
8
+ SANDBOX_PUBLIC_URL = 'http://121.15.180.66:801/NetPayment_dl/BaseHttp.dll' # 公共apiurl(测试环境)
9
+ SANDBOX_DO_BUSINESS_GATEWAY_URL = 'http://121.15.180.72/CmbBank_B2B/UI/NetPay/DoBusiness.ashx'
10
+ SANDBOX_QUERY_SETTLED_ORDER_BY_MERCHANT_DATE_GATEWAY_URL = "#{SANDBOX_PUBLIC_URL}?QuerySettledOrderByMerchantDate"
11
+ SANDBOX_QUERY_ACCOUNTlIST_GATEWAY_URL = "#{SANDBOX_PUBLIC_URL}?QueryAccountList"
12
+ SANDBOX_QUERY_SINGLE_ORDER_GATEWAY_URL = "#{SANDBOX_PUBLIC_URL}?QuerySingleOrder"
13
+ SANDBOX_DO_REFUND_GATEWAY_URL = "#{SANDBOX_PUBLIC_URL}?DoRefund"
14
+ SANDBOX_QUERY_REFUND_BY_DATE_GATEWAY_URL = "#{SANDBOX_PUBLIC_URL}?QueryRefundByDate"
15
+ SANDBOX_QUERY_SETTLED_REFUND_GATEWAY_URL = "#{SANDBOX_PUBLIC_URL}?QuerySettledRefund"
16
+
17
+ PUBLIC_URL = "https://payment.ebank.cmbchina.com/NetPayment/BaseHttp.dll" # 公共apiurl(生产环境)
18
+ DO_BUSINESS_GATEWAY_URL = 'https://b2b.cmbchina.com/CmbBank_B2B/UI/NetPay/DoBusiness.ashx'
19
+ QUERY_SETTLED_ORDER_BY_MERCHANT_DATE_GATEWAY_URL = "#{PUBLIC_URL}?QuerySettledOrderByMerchantDate"
20
+ QUERY_ACCOUNTlIST_GATEWAY_URL = "#{PUBLIC_URL}?QueryAccountList"
21
+ QUERY_SINGLE_ORDER_GATEWAY_URL = "#{PUBLIC_URL}?QuerySingleOrder"
22
+ DO_REFUND_GATEWAY_URL = "#{PUBLIC_URL}?DoRefund"
23
+ QUERY_REFUND_BY_DATE_GATEWAY_URL = "#{PUBLIC_URL}?QueryRefundByDate"
24
+ QUERY_SETTLED_REFUND_GATEWAY_URL = "#{PUBLIC_URL}?QuerySettledRefund"
25
+
26
+ MB_APPQRPAY_REQUIRED_REQ_DATA = %w( orderNo amount payNoticeUrl clientIP )
27
+ # 二维码支付API
28
+ def self.mb_appqrpay(req_data, options = {})
29
+ req_data = Utils.stringify_keys(req_data)
30
+ check_required_params(req_data, MB_APPQRPAY_REQUIRED_REQ_DATA)
31
+
32
+ params = {
33
+ 'charset' => 'utf-8',
34
+ 'jsonRequestData' => {
35
+ 'reqData' => {
36
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
37
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
38
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
39
+ 'date' => Time.now.strftime('%Y%m%d'),
40
+ }.merge(req_data)
41
+ }
42
+ }
43
+
44
+ request_args(get_gateway_url(__method__), sign_params(params, options))
45
+ end
46
+
47
+ # 查询招行公钥API
48
+ def self.do_business(req_data={}, options = {})
49
+ req_data = Utils.stringify_keys(req_data)
50
+ params = {
51
+ 'jsonRequestData' => {
52
+ 'charset' => 'utf-8',
53
+ 'reqData' => {
54
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
55
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
56
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
57
+ 'txCode' => 'FBPK'
58
+ }.merge(req_data)
59
+ }
60
+ }
61
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
62
+ end
63
+
64
+
65
+ QUERY_SETTLED_ORDER_BY_MERCHANT_DATE_REQUIRED_REQ_DATA = %w( beginDate endDate operatorNo)
66
+ # 按商户日期查询已结账订单API
67
+ def query_settled_order_by_merchant_date(req_data, options = {})
68
+ req_data = Utils.stringify_keys(req_data)
69
+ check_required_params(req_data, QUERY_SETTLED_ORDER_BY_MERCHANT_DATE_REQUIRED_REQ_DATA)
70
+ params = {
71
+ 'jsonRequestData' => {
72
+ 'charset' => 'utf-8',
73
+ 'reqData' => {
74
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
75
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
76
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
77
+ }.merge(req_data)
78
+ }
79
+ }
80
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
81
+ end
82
+
83
+
84
+ QUERY_ACCOUNTlIST_REQUIRED_REQ_DATA = %w( date operatorNo )
85
+ # 查询入账明细API
86
+ def query_accountList(req_data, options = {})
87
+ req_data = Utils.stringify_keys(req_data)
88
+ check_required_params(req_data, QUERY_ACCOUNTlIST_REQUIRED_REQ_DATA)
89
+ params = {
90
+ 'jsonRequestData' => {
91
+ 'charset' => 'utf-8',
92
+ 'reqData' => {
93
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
94
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
95
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
96
+ }.merge(req_data)
97
+ }
98
+ }
99
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
100
+ end
101
+
102
+ QUERY_SINGLE_ORDER_REQUIRED_REQ_DATA = %w( type date )
103
+ # 查询单笔订单API
104
+ def self.query_single_order(req_data, options = {})
105
+ req_data = Utils.stringify_keys(req_data)
106
+ check_required_params(req_data, QUERY_SINGLE_ORDER_REQUIRED_REQ_DATA)
107
+ if req_data['type'] == 'A'
108
+ warn("Cmbpay Warn: must have bankSerialNo if type is A") unless req_data.keys.include? 'bankSerialNo'
109
+ else
110
+ warn("Cmbpay Warn: must have orderNo if type is B") unless req_data.keys.include? 'orderNo'
111
+ end
112
+
113
+ params = {
114
+ 'charset' => 'utf-8',
115
+ 'jsonRequestData' => {
116
+ 'charset' => 'utf-8',
117
+ 'reqData' => {
118
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
119
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
120
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
121
+ }.merge(req_data)
122
+ }
123
+ }
124
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
125
+ end
126
+
127
+ DO_REFUND_REQUIRED_REQ_DATA = %w( date orderNo amount operatorNo pwd )
128
+ # 退款API
129
+ def do_refund(req_data, options = {})
130
+ req_data = Utils.stringify_keys(req_data)
131
+ check_required_params(req_data, DO_REFUND_REQUIRED_REQ_DATA)
132
+ params = {
133
+ 'jsonRequestData' => {
134
+ 'charset' => 'utf-8',
135
+ 'reqData' => {
136
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
137
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
138
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
139
+ }.merge(req_data)
140
+ }
141
+ }
142
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
143
+ end
144
+
145
+ QUERY_REFUND_BY_DATE_REQUIRED_REQ_DATA = %w( beginDate endDate )
146
+ # 按退款日期查询退款API
147
+ def query_refund_by_date(req_data, options = {})
148
+ req_data = Utils.stringify_keys(req_data)
149
+ check_required_params(req_data, QUERY_REFUND_BY_DATE_REQUIRED_REQ_DATA)
150
+ params = {
151
+ 'jsonRequestData' => {
152
+ 'charset' => 'utf-8',
153
+ 'reqData' => {
154
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
155
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
156
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
157
+ }.merge(req_data)
158
+ }
159
+ }
160
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
161
+ end
162
+
163
+ QUERY_SETTLED_REFUND_REQUIRED_REQ_DATA = %w( type date )
164
+ # 已处理退款查询API
165
+ def query_settled_refund(req_data, options = {})
166
+ req_data = Utils.stringify_keys(req_data)
167
+ check_required_params(req_data, QUERY_SETTLED_REFUND_REQUIRED_REQ_DATA)
168
+ params = {
169
+ 'jsonRequestData' => {
170
+ 'charset' => 'utf-8',
171
+ 'reqData' => {
172
+ 'merchantNo' => options.delete(:merchantNo) || Cmbpay.merchant_no,
173
+ 'branchNo' => options.delete(:branchNo) || Cmbpay.branch_no,
174
+ 'dateTime' => Time.now.strftime('%Y%m%d%H%M%S'),
175
+ }.merge(req_data)
176
+ }
177
+ }
178
+ request_remote(get_gateway_url(__method__), sign_params(params, options))
179
+ end
180
+
181
+ class << self
182
+ private
183
+
184
+ def get_gateway_url(method)
185
+ prefix = Cmbpay.sandbox_mode ? 'SANDBOX_' : ''
186
+ self.const_get("#{prefix}#{method.upcase}_GATEWAY_URL")
187
+ end
188
+
189
+ def request_args(gateway_url, params, options = {})
190
+ gateway_url = options.delete(:gateway_url) || gateway_url
191
+ params['jsonRequestData'] = JSON.generate params['jsonRequestData']
192
+ {
193
+ method: options[:method] || :post,
194
+ url: gateway_url,
195
+ payload: params,
196
+ }
197
+ end
198
+
199
+ def request_remote(gateway_url, params, options = {})
200
+ options = Cmbpay.extra_rest_client_options.merge(options)
201
+ gateway_url = options.delete(:gateway_url) || gateway_url
202
+ params['jsonRequestData'] = JSON.generate params['jsonRequestData']
203
+
204
+ RestClient::Request.execute(
205
+ {
206
+ method: options[:method] || :post,
207
+ url: gateway_url,
208
+ payload: params,
209
+ headers: { content_type: 'application/x-www-form-urlencoded' }
210
+ }.merge(options)
211
+ ).body
212
+ end
213
+
214
+ def sign_params(params, options = {})
215
+ options['key'] ||= Cmbpay.key
216
+ params['jsonRequestData'].merge!(
217
+ 'version' => '1.0',
218
+ 'signType' => 'SHA-256',
219
+ 'sign' => Sign.generate(params['jsonRequestData']['reqData'], options)
220
+ )
221
+ params
222
+ end
223
+
224
+ def check_required_params(options, names)
225
+ return unless Cmbpay.debug_mode?
226
+ names.each do |name|
227
+ warn("Cmbpay Warn: missing required option: #{name}") unless options.has_key?(name)
228
+ end
229
+ end
230
+ end
231
+ end
232
+ end
@@ -0,0 +1,41 @@
1
+ require 'digest'
2
+
3
+ module Cmbpay
4
+ module Sign
5
+ def self.generate(params, options={})
6
+ key = options.delete(:key)
7
+
8
+ query = params.sort.map do |k, v|
9
+ "#{k}=#{v}"
10
+ end.compact.join('&')
11
+ p query
12
+ Digest::SHA256.hexdigest("#{query}&#{key || Cmbpay.key}")
13
+ end
14
+
15
+ CMBPAY_RSA_PUBLIC_KEY = <<-EOF
16
+ -----BEGIN PUBLIC KEY-----
17
+ MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDZs4l8Ez3F4MG0kF7RRSL+pn8M
18
+ mxVE3nfdXzjx6d3rH8IfDbNvNRLS0X0b5iJnPyFO8sbbUo1Im4zX0M8XA0xnnviG
19
+ yn5E6occiyUXJRgokphWb5BwaYdVhnLldctdimHoJTk3NFEQFav3guygR54i3tym
20
+ rDc8lWtuG8EczVu8FwIDAQAB
21
+ -----END PUBLIC KEY-----
22
+ EOF
23
+
24
+ def self.verify?(params, options = {})
25
+ params = Utils.stringify_keys(params)
26
+
27
+ sign_type = params.delete('sign_type')
28
+ sign = params.delete('sign')
29
+ string = params_to_string(params['noticeData'])
30
+ public_key = options['public_key'] || CMBPAY_RSA_PUBLIC_KEY
31
+
32
+ RSA.verify?(public_key, string, sign)
33
+ end
34
+
35
+ def self.params_to_string(params)
36
+ params.sort.map { |item| item.join('=') }.join('&')
37
+ end
38
+
39
+ end
40
+ end
41
+
@@ -0,0 +1,11 @@
1
+ module Cmbpay
2
+ module Utils
3
+ def self.stringify_keys(hash)
4
+ new_hash = {}
5
+ hash.each do |key, value|
6
+ new_hash[(key.to_s rescue key) || key] = value
7
+ end
8
+ new_hash
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Cmbpay
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmbpay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - zouchao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-08-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: An unofficial simple cmb netpayment gem, copied from alipay
70
+ email:
71
+ - zouchao2008@gmail.com
72
+ executables:
73
+ - console
74
+ - setup
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".gitignore"
79
+ - ".rspec"
80
+ - ".travis.yml"
81
+ - CODE_OF_CONDUCT.md
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - cmbpay.gemspec
90
+ - lib/cmbpay.rb
91
+ - lib/cmbpay/service.rb
92
+ - lib/cmbpay/sign.rb
93
+ - lib/cmbpay/utils.rb
94
+ - lib/cmbpay/version.rb
95
+ homepage: https://github.com/zouchao/cmbpay
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.7.4
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: An unofficial simple cmb netpayment gem
119
+ test_files: []