alipay_mini 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +52 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +34 -0
- data/LICENSE +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/alipay_mini.gemspec +28 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/alipay_mini/api/alipay_base.rb +31 -0
- data/lib/alipay_mini/api/system_oauth_token.rb +28 -0
- data/lib/alipay_mini/api/user_info_share.rb +19 -0
- data/lib/alipay_mini/config.rb +57 -0
- data/lib/alipay_mini/request.rb +38 -0
- data/lib/alipay_mini/sign/rsa2.rb +24 -0
- data/lib/alipay_mini/sign.rb +41 -0
- data/lib/alipay_mini/utils.rb +20 -0
- data/lib/alipay_mini/version.rb +3 -0
- data/lib/alipay_mini.rb +43 -0
- metadata +121 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 82877c7848d2ea38add95626da34d271b4dd7aab9d65d30eca5f098960b88663
|
|
4
|
+
data.tar.gz: 12ec68458d3b6e0f1efb54ea7b84f9467e74e5d2446dfc3c0ccfadde1d2920ca
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 598028509fd7f45f3f58f5825bc11c108a87f6b73427acf7a56d83dbe9cc43483b77f56b4737b39df53915a527899cea36df3ab77fc93a782caa9b26f2cd1a41
|
|
7
|
+
data.tar.gz: 76c81ca22bd870038655cea0b0d5c1b4768e499247a069726d015d127d12a0c2074a66a18d096fc248d3e8f668a68eca6bd09b5f814e6f7d8f94d30fa3f9ea75
|
data/.gitignore
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
*.gem
|
|
2
|
+
*.rbc
|
|
3
|
+
/.config
|
|
4
|
+
/coverage/
|
|
5
|
+
/InstalledFiles
|
|
6
|
+
/pkg/
|
|
7
|
+
/spec/reports/
|
|
8
|
+
/spec/examples.txt
|
|
9
|
+
/test/tmp/
|
|
10
|
+
/test/version_tmp/
|
|
11
|
+
/tmp/
|
|
12
|
+
|
|
13
|
+
# Used by dotenv library to load environment variables.
|
|
14
|
+
# .env
|
|
15
|
+
|
|
16
|
+
## Specific to RubyMotion:
|
|
17
|
+
.dat*
|
|
18
|
+
.repl_history
|
|
19
|
+
build/
|
|
20
|
+
*.bridgesupport
|
|
21
|
+
build-iPhoneOS/
|
|
22
|
+
build-iPhoneSimulator/
|
|
23
|
+
|
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
|
25
|
+
#
|
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
|
29
|
+
#
|
|
30
|
+
# vendor/Pods/
|
|
31
|
+
|
|
32
|
+
## Documentation cache and generated files:
|
|
33
|
+
/.yardoc/
|
|
34
|
+
/_yardoc/
|
|
35
|
+
/doc/
|
|
36
|
+
/rdoc/
|
|
37
|
+
|
|
38
|
+
## Environment normalization:
|
|
39
|
+
/.bundle/
|
|
40
|
+
/vendor/bundle
|
|
41
|
+
/lib/bundler/man/
|
|
42
|
+
|
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
45
|
+
# Gemfile.lock
|
|
46
|
+
# .ruby-version
|
|
47
|
+
# .ruby-gemset
|
|
48
|
+
|
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
50
|
+
.rvmrc
|
|
51
|
+
|
|
52
|
+
.idea/
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -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 lanyuejin1108@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
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
alipay_mini (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://gems.ruby-china.com/
|
|
8
|
+
specs:
|
|
9
|
+
addressable (2.5.2)
|
|
10
|
+
public_suffix (>= 2.0.2, < 4.0)
|
|
11
|
+
crack (0.4.3)
|
|
12
|
+
safe_yaml (~> 1.0.0)
|
|
13
|
+
hashdiff (0.3.7)
|
|
14
|
+
minitest (5.11.3)
|
|
15
|
+
public_suffix (3.0.3)
|
|
16
|
+
rake (10.5.0)
|
|
17
|
+
safe_yaml (1.0.4)
|
|
18
|
+
webmock (3.4.2)
|
|
19
|
+
addressable (>= 2.3.6)
|
|
20
|
+
crack (>= 0.3.2)
|
|
21
|
+
hashdiff
|
|
22
|
+
|
|
23
|
+
PLATFORMS
|
|
24
|
+
ruby
|
|
25
|
+
|
|
26
|
+
DEPENDENCIES
|
|
27
|
+
alipay_mini!
|
|
28
|
+
bundler (~> 1.16)
|
|
29
|
+
minitest (~> 5.0)
|
|
30
|
+
rake (~> 10.0)
|
|
31
|
+
webmock
|
|
32
|
+
|
|
33
|
+
BUNDLED WITH
|
|
34
|
+
1.16.1
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018
|
|
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,39 @@
|
|
|
1
|
+
# AlipayMini
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/alipay_mini`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'alipay_mini'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install alipay_mini
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
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).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/alipay_mini. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
36
|
+
|
|
37
|
+
## Code of Conduct
|
|
38
|
+
|
|
39
|
+
Everyone interacting in the AlipayMini project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/alipay_mini/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/alipay_mini.gemspec
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require "alipay_mini/version"
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "alipay_mini"
|
|
8
|
+
spec.version = AlipayMini::VERSION
|
|
9
|
+
spec.authors = ["renyijiu"]
|
|
10
|
+
spec.email = ["lanyuejin1108@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{An unofficial simple alipay mini program gem}
|
|
13
|
+
spec.description = %q{An unofficial simple alipay mini program gem}
|
|
14
|
+
spec.homepage = "https://github.com/renyijiu/alipay_mini"
|
|
15
|
+
spec.license = "MIT"
|
|
16
|
+
|
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
|
19
|
+
end
|
|
20
|
+
spec.bindir = "exe"
|
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
22
|
+
spec.require_paths = ["lib"]
|
|
23
|
+
|
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
27
|
+
spec.add_development_dependency "webmock"
|
|
28
|
+
end
|
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "alipay_mini"
|
|
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__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
module AlipayMini
|
|
2
|
+
module Api
|
|
3
|
+
class AlipayBase
|
|
4
|
+
|
|
5
|
+
def method
|
|
6
|
+
raise ArrgumentError, "please set the correct api method name"
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def timestamp
|
|
10
|
+
Time.now.strftime("%Y-%m-%d %H:%M:%S")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def base_params
|
|
14
|
+
{
|
|
15
|
+
app_id: AlipayMini.config[:app_id],
|
|
16
|
+
format: AlipayMini.config[:format],
|
|
17
|
+
charset: AlipayMini.config[:charset],
|
|
18
|
+
sign_type: AlipayMini.config[:sign_type],
|
|
19
|
+
version: AlipayMini.config[:version],
|
|
20
|
+
method: method,
|
|
21
|
+
timestamp: timestamp
|
|
22
|
+
}
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def sign_params(params)
|
|
26
|
+
params.merge(sign: AlipayMini::Sign.generate(params))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module AlipayMini
|
|
2
|
+
module Api
|
|
3
|
+
class SystemOauthToken < AlipayBase
|
|
4
|
+
|
|
5
|
+
# when grant_type is 'authorization_code', code is the auth_code
|
|
6
|
+
# when grant_type is 'refresh_token', code is the refresh_token
|
|
7
|
+
def get(grant_type, code)
|
|
8
|
+
case grant_type
|
|
9
|
+
when 'authorization_code'
|
|
10
|
+
params = base_params.merge(grant_type: grant_type, code: code)
|
|
11
|
+
when 'refresh_token'
|
|
12
|
+
params = base_params.merge(grant_type: grant_type, refresh_token: code)
|
|
13
|
+
else
|
|
14
|
+
raise ArgumentError, 'grant type not valid, allow type "authorization_code", "refresh_token"'
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
AlipayMini::Request.get(method, sign_params(params))
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def method
|
|
23
|
+
"alipay.system.oauth.token"
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module AlipayMini
|
|
2
|
+
module Api
|
|
3
|
+
class UserInfoShare < AlipayBase
|
|
4
|
+
|
|
5
|
+
def get(access_token)
|
|
6
|
+
params = base_params.merge(auth_token: access_token)
|
|
7
|
+
|
|
8
|
+
AlipayMini::Request.get(method, sign_params(params))
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
private
|
|
12
|
+
|
|
13
|
+
def method
|
|
14
|
+
"alipay.user.info.share"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require "singleton"
|
|
2
|
+
|
|
3
|
+
module AlipayMini
|
|
4
|
+
class Config
|
|
5
|
+
include Singleton
|
|
6
|
+
|
|
7
|
+
# api request url
|
|
8
|
+
attr_accessor :url
|
|
9
|
+
|
|
10
|
+
# mini program app id
|
|
11
|
+
attr_accessor :app_id
|
|
12
|
+
|
|
13
|
+
# format, default is 'JSON', only support 'JSON'
|
|
14
|
+
attr_accessor :format
|
|
15
|
+
|
|
16
|
+
# charset, default is 'utf-8'
|
|
17
|
+
attr_accessor :charset
|
|
18
|
+
|
|
19
|
+
# sign type, default is 'RSA2', support 'RSA2' or 'RSA'
|
|
20
|
+
attr_accessor :sign_type
|
|
21
|
+
|
|
22
|
+
# version, now is '1.0'
|
|
23
|
+
attr_accessor :version
|
|
24
|
+
|
|
25
|
+
# app private key, use for sign
|
|
26
|
+
attr_accessor :private_key
|
|
27
|
+
|
|
28
|
+
# alipay public key, use for verify
|
|
29
|
+
attr_accessor :public_key
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def configuration
|
|
33
|
+
@config ||= {}.tap do |config|
|
|
34
|
+
config[:url] = url if url
|
|
35
|
+
config[:app_id] = app_id if app_id
|
|
36
|
+
config[:private_key] = format_private_key(private_key) if private_key
|
|
37
|
+
config[:public_key] = format_public_key(public_key) if public_key
|
|
38
|
+
|
|
39
|
+
config[:version] = '1.0'
|
|
40
|
+
config[:format] = format || 'JSON'
|
|
41
|
+
config[:charset] = charset || 'utf-8'
|
|
42
|
+
config[:sign_type] = sign_type || 'RSA2'
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def format_private_key(key)
|
|
49
|
+
"-----BEGIN RSA PRIVATE KEY-----\n#{key}\n-----END RSA PRIVATE KEY-----\n"
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def format_public_key(key)
|
|
53
|
+
"-----BEGIN PUBLIC KEY-----\n#{key}\n-----END PUBLIC KEY-----\n"
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end
|
|
57
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "json"
|
|
3
|
+
|
|
4
|
+
module AlipayMini
|
|
5
|
+
module Request
|
|
6
|
+
class << self
|
|
7
|
+
def get(method, params)
|
|
8
|
+
uri = URI(AlipayMini.config[:url])
|
|
9
|
+
uri.query = URI.encode_www_form(params)
|
|
10
|
+
|
|
11
|
+
res = Net::HTTP.get_response(uri)
|
|
12
|
+
|
|
13
|
+
format_res(method, res)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
protected
|
|
17
|
+
|
|
18
|
+
def format_res(method, res)
|
|
19
|
+
return [false, "Network error"] unless res.is_a?(Net::HTTPSuccess)
|
|
20
|
+
|
|
21
|
+
res = JSON.parse(res.body)
|
|
22
|
+
method_key = "#{method.split('.').join('_')}_response"
|
|
23
|
+
method_key = "error_response" if res.has_key?("error_response")
|
|
24
|
+
|
|
25
|
+
body = res.fetch(method_key, nil)
|
|
26
|
+
return [false, "Response body is empty"] if body.nil?
|
|
27
|
+
|
|
28
|
+
sign_params = {sign: res.fetch("sign", nil), sign_type: AlipayMini.config[:sign_type]}
|
|
29
|
+
verify = AlipayMini::Sign.verify?(body.merge(sign_params))
|
|
30
|
+
return [false, 'Sign error'] unless verify
|
|
31
|
+
|
|
32
|
+
# 支付宝部分返回值与文档不一致,采取code判断,默认请求成功
|
|
33
|
+
code = body.fetch("code", "10000")
|
|
34
|
+
[code == "10000", body]
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require "openssl"
|
|
2
|
+
require "base64"
|
|
3
|
+
|
|
4
|
+
module AlipayMini
|
|
5
|
+
module Sign
|
|
6
|
+
class RSA2
|
|
7
|
+
|
|
8
|
+
def self.sign(key, string)
|
|
9
|
+
pkey = OpenSSL::PKey::RSA.new(key)
|
|
10
|
+
digest = OpenSSL::Digest::SHA256.new
|
|
11
|
+
|
|
12
|
+
Base64.strict_encode64(pkey.sign(digest, string))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.verify?(key, string, sign)
|
|
16
|
+
pkey = OpenSSL::PKey::RSA.new(key)
|
|
17
|
+
digest = OpenSSL::Digest::SHA256.new
|
|
18
|
+
|
|
19
|
+
pkey.verify(digest, Base64.strict_decode64(sign), string)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
|
|
2
|
+
module AlipayMini
|
|
3
|
+
module Sign
|
|
4
|
+
def self.generate(params, options = {})
|
|
5
|
+
params = Utils.stringify_keys(params)
|
|
6
|
+
|
|
7
|
+
sign_type = options[:sign_type] || AlipayMini.config[:sign_type]
|
|
8
|
+
key = options[:private_key] || AlipayMini.config[:private_key]
|
|
9
|
+
|
|
10
|
+
string = Utils.params_to_string(params)
|
|
11
|
+
|
|
12
|
+
case sign_type
|
|
13
|
+
when 'RSA2'
|
|
14
|
+
RSA2.sign(key, string)
|
|
15
|
+
else
|
|
16
|
+
raise ArgumentError, "invalid sign_type #{sign_type}, allowed type: 'RSA2'"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.verify?(params, options = {})
|
|
21
|
+
params = Utils.stringify_keys(params)
|
|
22
|
+
|
|
23
|
+
sign_type = params.delete('sign_type')
|
|
24
|
+
sign = params.delete('sign')
|
|
25
|
+
public_key = options[:public_key] || AlipayMini.config[:public_key]
|
|
26
|
+
|
|
27
|
+
# 转义正斜杠,防止验签无法通过
|
|
28
|
+
string = params.to_json.gsub('/', '\/')
|
|
29
|
+
|
|
30
|
+
case sign_type
|
|
31
|
+
when 'RSA2'
|
|
32
|
+
RSA2.verify?(public_key, string, sign)
|
|
33
|
+
else
|
|
34
|
+
false
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
module AlipayMini
|
|
3
|
+
module Utils
|
|
4
|
+
def self.stringify_keys(hash)
|
|
5
|
+
if hash.is_a? ::Hash
|
|
6
|
+
return hash.inject({}) do |memo, (k, v)|
|
|
7
|
+
memo.tap { |m| m[k.to_s] = self.stringify_keys(v) }
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
hash
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.params_to_string(params)
|
|
15
|
+
params.sort.map{ |item| item.join('=') }.join('&')
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
end
|
|
20
|
+
end
|
data/lib/alipay_mini.rb
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "alipay_mini/version"
|
|
2
|
+
require "alipay_mini/config"
|
|
3
|
+
require "alipay_mini/utils"
|
|
4
|
+
require "alipay_mini/sign/rsa2"
|
|
5
|
+
require "alipay_mini/sign"
|
|
6
|
+
require "alipay_mini/request"
|
|
7
|
+
require "alipay_mini/api/alipay_base"
|
|
8
|
+
require "alipay_mini/api/system_oauth_token"
|
|
9
|
+
require "alipay_mini/api/user_info_share"
|
|
10
|
+
|
|
11
|
+
module AlipayMini
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
def configure
|
|
15
|
+
config = Config.instance
|
|
16
|
+
yield config
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def config
|
|
20
|
+
Config.instance.configuration
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# alipay.system.oauth.token api
|
|
24
|
+
#
|
|
25
|
+
# @params grant_type String 'authorization_code', 'refresh_token'
|
|
26
|
+
# @params code String auth_code or refresh_token
|
|
27
|
+
#
|
|
28
|
+
# @return Array [flag<Boolean>, res<Hash>]
|
|
29
|
+
def system_oauth_token(grant_type, code)
|
|
30
|
+
AlipayMini::Api::SystemOauthToken.new.get(grant_type, code)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# alipay.user.info.share api
|
|
34
|
+
#
|
|
35
|
+
# @params access_token String access_token
|
|
36
|
+
#
|
|
37
|
+
# @return Array [flag<Boolean>, res<Hash>]
|
|
38
|
+
def user_info_share(access_token)
|
|
39
|
+
AlipayMini::Api::UserInfoShare.new.get(access_token)
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: alipay_mini
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- renyijiu
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-08-21 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.16'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.16'
|
|
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: :development
|
|
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: minitest
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '5.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '5.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: webmock
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
description: An unofficial simple alipay mini program gem
|
|
70
|
+
email:
|
|
71
|
+
- lanyuejin1108@gmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".gitignore"
|
|
77
|
+
- ".travis.yml"
|
|
78
|
+
- CODE_OF_CONDUCT.md
|
|
79
|
+
- Gemfile
|
|
80
|
+
- Gemfile.lock
|
|
81
|
+
- LICENSE
|
|
82
|
+
- README.md
|
|
83
|
+
- Rakefile
|
|
84
|
+
- alipay_mini.gemspec
|
|
85
|
+
- bin/console
|
|
86
|
+
- bin/setup
|
|
87
|
+
- lib/alipay_mini.rb
|
|
88
|
+
- lib/alipay_mini/api/alipay_base.rb
|
|
89
|
+
- lib/alipay_mini/api/system_oauth_token.rb
|
|
90
|
+
- lib/alipay_mini/api/user_info_share.rb
|
|
91
|
+
- lib/alipay_mini/config.rb
|
|
92
|
+
- lib/alipay_mini/request.rb
|
|
93
|
+
- lib/alipay_mini/sign.rb
|
|
94
|
+
- lib/alipay_mini/sign/rsa2.rb
|
|
95
|
+
- lib/alipay_mini/utils.rb
|
|
96
|
+
- lib/alipay_mini/version.rb
|
|
97
|
+
homepage: https://github.com/renyijiu/alipay_mini
|
|
98
|
+
licenses:
|
|
99
|
+
- MIT
|
|
100
|
+
metadata: {}
|
|
101
|
+
post_install_message:
|
|
102
|
+
rdoc_options: []
|
|
103
|
+
require_paths:
|
|
104
|
+
- lib
|
|
105
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
106
|
+
requirements:
|
|
107
|
+
- - ">="
|
|
108
|
+
- !ruby/object:Gem::Version
|
|
109
|
+
version: '0'
|
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
111
|
+
requirements:
|
|
112
|
+
- - ">="
|
|
113
|
+
- !ruby/object:Gem::Version
|
|
114
|
+
version: '0'
|
|
115
|
+
requirements: []
|
|
116
|
+
rubyforge_project:
|
|
117
|
+
rubygems_version: 2.7.6
|
|
118
|
+
signing_key:
|
|
119
|
+
specification_version: 4
|
|
120
|
+
summary: An unofficial simple alipay mini program gem
|
|
121
|
+
test_files: []
|