bifubao 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5c77b9007d4215a0b337a19570b5e419f717f1ce
4
+ data.tar.gz: e338ace28085b88e8789739d1d1a885a38a06e3b
5
+ SHA512:
6
+ metadata.gz: c095cf8e57ffcc4d011ff20c730e4e15a03a78e0cf725c3013127615c58fabba39e0d01f596529e6b606f5320c28844e2c6391b31ef48ffe61596e60310c126b
7
+ data.tar.gz: 8daf799098ec912b7645bfc61176acf73d542d1d496ba665d9f8a93fd001cf26275bf700d4721a43422ef2969ab5c7920c9ca680a731a3c77f061e1bd693ac6c
@@ -0,0 +1,16 @@
1
+ .DS_Store
2
+
3
+ *.sw[op]
4
+ *~
5
+ .project
6
+ /TAGS
7
+
8
+ .idea
9
+
10
+ capybara-*.html
11
+ .rspec
12
+ /coverage/
13
+
14
+ .vagrant
15
+
16
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in alipay.gemspec
4
+ gemspec
@@ -0,0 +1,23 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ bifubao (0.0.1)
5
+ rest-client (~> 1)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ mime-types (2.3)
11
+ netrc (0.7.7)
12
+ rake (10.3.2)
13
+ rest-client (1.7.2)
14
+ mime-types (>= 1.16, < 3.0)
15
+ netrc (~> 0.7)
16
+
17
+ PLATFORMS
18
+ ruby
19
+
20
+ DEPENDENCIES
21
+ bifubao!
22
+ bundler (~> 1)
23
+ rake (~> 10)
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Jasl
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,66 @@
1
+ # Bifubao
2
+
3
+ A simple bifubao ruby gem, without unnecessary magic or wraper, it's directly facing how bifubao api works.
4
+
5
+ Please read bifubao official document first: <http://wiki.bifubao.com/twiki/bin/view/Main/WebHome> .
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'bifubao'
13
+ ```
14
+
15
+ or development version
16
+
17
+ ```ruby
18
+ gem 'bifubao', :github => 'jasl/bifubao'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ ```sh
24
+ $ bundle
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ ### Config
30
+
31
+ ```ruby
32
+ Bifubao.private_key = YOUR_PRIVATE_KEY
33
+ Bifubao.app_hash_id = YOUR_HASH_ID
34
+ ```
35
+
36
+ ### Create external
37
+
38
+ Official API document: <http://wiki.bifubao.com/twiki/bin/view/Main/V1OrderCreateexternal>
39
+
40
+ ```ruby
41
+ options = {
42
+ :external_order_id => 'YOUR_ORDER_ID', # 20130801000001
43
+ :display_name => 'YOUR_ORDER_SUBJECCT', # Test Good
44
+ :price_cny => 100
45
+ }
46
+
47
+ Bifubao::Service.create_external(options) # returns json contains result
48
+ ```
49
+
50
+ ### Verify notify
51
+
52
+ TODO
53
+
54
+ ## Contributing
55
+
56
+ Bug report or pull request are welcome.
57
+
58
+ ### Make a pull request
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
65
+
66
+ Please write unit test with your code if necessary.
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "test"
6
+ t.test_files = FileList['test/**/*_test.rb']
7
+ end
8
+
9
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'bifubao/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "bifubao"
8
+ spec.version = Bifubao::VERSION
9
+ spec.authors = ["Jasl"]
10
+ spec.email = ["jasl9187@hotmail.com"]
11
+ spec.description = %q{An unofficial bifubao ruby gem}
12
+ spec.summary = %q{An unofficial bifubao ruby gem}
13
+ spec.homepage = "https://github.com/jasl/bifubao"
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_runtime_dependency "rest-client", "~> 1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1"
24
+ spec.add_development_dependency "rake" , "~> 10"
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'openssl'
2
+
3
+ require 'bifubao/version'
4
+ require 'bifubao/utils'
5
+ require 'bifubao/sign'
6
+
7
+ require 'bifubao/service'
8
+
9
+ module Bifubao
10
+ class << self
11
+ attr_accessor :private_key
12
+ attr_accessor :app_hash_id
13
+
14
+ OFFICIAL_PUBLIC_KEY_PEM = "-----BEGIN PUBLIC KEY-----\n" +
15
+ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqUSnx8dqJ0UC0jvFTEdL\n" +
16
+ "gde7BSmKi8GzDnxvu/AMQw7TG3pRKAAKQJRYUSqpgMyOwUSrv3yfu3gBJwufjWJz\n" +
17
+ "Kgtm8D9TOoYnZMJm4x5Lv9/EpYEg0zrAsmU/6rZJ9mYRaNPrt811Thju0/19fa77\n" +
18
+ "XnsQ78UmvV4zCePkKAArO70SsU/hf1SinDX//t0a3/UOk0DhKoJZpzjb5mb+dcXM\n" +
19
+ "GOJKpAONDGDK2UE1W67HmIG72b/R/G8CAFYbw4MGCjb0/Ee6obcAGK3Cj1JcuHjH\n" +
20
+ "NzymBH0NuDvyz7fJuTg9Eplnh1blNeCJoG/vv7VLZNKetTMTx+H2X534RUQ4XheX\n" +
21
+ "4QIDAQAB\n" +
22
+ "-----END PUBLIC KEY-----"
23
+
24
+ OFFICIAL_PUBLIC_KEY = OpenSSL::PKey::RSA.new(OFFICIAL_PUBLIC_KEY_PEM)
25
+ end
26
+ end
@@ -0,0 +1,38 @@
1
+ require 'cgi'
2
+ require 'json'
3
+ require 'rest-client'
4
+
5
+ module Bifubao
6
+ module Service
7
+ API_HOST = "https://api.bifubao.com/#{Bifubao::API_VERSION}"
8
+
9
+ CREATE_EXTERNAL_REQUIRED_OPTIONS = %w(external_order_id price_btc price_cny display_name)
10
+
11
+ def self.create_external(options)
12
+ options = {
13
+ '_app_hash_id_' => Bifubao.app_hash_id,
14
+ '_time_' => Time.now.to_i,
15
+ 'price_btc' => 0,
16
+ 'price_cny' => 0,
17
+ '_sign_algo_' => 'sha1'
18
+ }.merge(Utils.stringify_keys(options))
19
+
20
+ check_required_options(options, CREATE_EXTERNAL_REQUIRED_OPTIONS)
21
+ if options['price_btc'] + options['price_cny'] == 0 || options['price_btc'] * options['price_cny'] != 0
22
+ raise "Bifubao error: price_btc and price_cny should only one above 0"
23
+ end
24
+
25
+ JSON.parse RestClient.post("#{API_HOST}/order/createexternal/", sign_query(options))
26
+ end
27
+
28
+ def self.sign_query(options)
29
+ options.merge('_signature_' => Bifubao::Sign.generate(options))
30
+ end
31
+
32
+ def self.check_required_options(options, names)
33
+ names.each do |name|
34
+ raise "Bifubao error: missing required option: #{name}" unless options.has_key?(name)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,25 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+
4
+ module Bifubao
5
+ module Sign
6
+ def self.to_sign_data(params)
7
+ params.sort.map do |key, value|
8
+ "#{key}#{value}"
9
+ end.join('')
10
+ end
11
+
12
+ def self.generate(params)
13
+ Base64.encode64(Bifubao.private_key.sign(OpenSSL::Digest::SHA1.new, to_sign_data(params)))
14
+ end
15
+
16
+ def self.verify?(params)
17
+ params = Bifubao::Utils.stringify_keys(params)
18
+ params.delete '_signature_sha1_'
19
+
20
+ sign = params.delete('_signature_')
21
+
22
+ Bifubao::OFFICIAL_PUBLIC_KEY.verify(OpenSSL::Digest::SHA1.new, Base64.decode64(sign), to_sign_data(params))
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,11 @@
1
+ module Bifubao
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,4 @@
1
+ module Bifubao
2
+ VERSION = "0.0.1"
3
+ API_VERSION = 'v00001'
4
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class Bifubao::ServiceTest < Test::Unit::TestCase
4
+ def test_create_external
5
+ options = {
6
+ external_order_id: SecureRandom.uuid,
7
+ price_cny: 1,
8
+ display_name: 'foo'
9
+ }
10
+
11
+ assert_nothing_raised do
12
+ Bifubao::Service.create_external(options)
13
+ end
14
+ end
15
+
16
+ def test_create_external_with_cny_and_btc
17
+ options = {
18
+ external_order_id: SecureRandom.uuid,
19
+ price_cny: 1,
20
+ price_btc: 1,
21
+ display_name: 'foo'
22
+ }
23
+
24
+ assert_raise RuntimeError do
25
+ Bifubao::Service.create_external(options)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class Bifubao::SignTest < Test::Unit::TestCase
4
+ def test_verify_sign
5
+ # TODO: pending
6
+ end
7
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class Bifubao::UtilsTest < Test::Unit::TestCase
4
+ def test_stringify_keys
5
+ hash = { 'a' => 1, :b => 2 }
6
+ assert_equal({ 'a' => 1, 'b' => 2 }.sort, Bifubao::Utils.stringify_keys(hash).sort)
7
+ end
8
+ end
@@ -0,0 +1,36 @@
1
+ require 'test/unit'
2
+ require 'bifubao'
3
+
4
+ # cheat from https://github.com/bifubao/app_demo/blob/master/pay_redirect.php
5
+ DEMO_APP_HASH_ID = '4e589c175d6a483562e1c50f54351250367d4253ed146d411dbe612eb63b52d3'
6
+ DEMO_PRIVATE_KEY_PEM = "-----BEGIN PRIVATE KEY-----\n" +
7
+ "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDUoRth9cZtSOx+\n" +
8
+ "7MWFCRJAXSZAOA9kDzJ65yuc6KpkwKKR1yS416yGlYfbgABAcUv8D8O4/NgjaFA2\n" +
9
+ "m84SQaCYalmCD8cxuq2n0KIEM8z/9U/SqMIg71GKdFKePJDUpEsdwgJ8UwQq1dAb\n" +
10
+ "z3j3VJcp7+JxlsCSWkwJz31Uc74OxU81TAji0hZPPBhb4zEdGCfhTjGfrVaBVbSv\n" +
11
+ "AdyFcBjDpDxx7LRH1Ao1TchGIxlb+ekisQhaeIE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
+ "fXMCv7b6Pa9V8M+imY1q3ZDMLARtMukhuDmRaUG7To3HIPbkXizsuJP7AoGASM9j\n" +
27
+ "faAiKipU+bivqq5jN/+Sm/EiJQgRlUG6pPgNIl2Yax2rae/K0Qxe99GMRsfM98/Q\n" +
28
+ "6uF7MQOnVgbq9NdwWguSjKlJW+vO06ItT7BQIGC2mSuhGfaQ2tumWNahFIMimYFF\n" +
29
+ "ygLNJ/bMOHYxabn1xLMjBC+wN37mMF/XwssR2bcCgYEAzDHa2JcmEfZngYXqFID0\n" +
30
+ "upEybl1tPFfxjAukBuCuG4Y8wdyCqHxIrU42rhJEbw4Db/zyD4/i3dilyuN7mhk8\n" +
31
+ "6Ekaty5YiG7y3yKjQlkim4DWCc5YBSLmCSKnbnKViEnqlCw4x0+2HjYLalORwYyH\n" +
32
+ "Dtrdb07S7MpHL04pohQ7H94=\n" +
33
+ "-----END PRIVATE KEY-----"
34
+
35
+ Bifubao.private_key = OpenSSL::PKey::RSA.new(DEMO_PRIVATE_KEY_PEM)
36
+ Bifubao.app_hash_id = DEMO_APP_HASH_ID
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bifubao
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jasl
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-07-22 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: '1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1'
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'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1'
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'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10'
55
+ description: An unofficial bifubao ruby gem
56
+ email:
57
+ - jasl9187@hotmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - bifubao.gemspec
69
+ - lib/bifubao.rb
70
+ - lib/bifubao/service.rb
71
+ - lib/bifubao/sign.rb
72
+ - lib/bifubao/utils.rb
73
+ - lib/bifubao/version.rb
74
+ - test/alipay/service_test.rb
75
+ - test/alipay/sign_test.rb
76
+ - test/alipay/utils_test.rb
77
+ - test/test_helper.rb
78
+ homepage: https://github.com/jasl/bifubao
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.2.2
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: An unofficial bifubao ruby gem
102
+ test_files:
103
+ - test/alipay/service_test.rb
104
+ - test/alipay/sign_test.rb
105
+ - test/alipay/utils_test.rb
106
+ - test/test_helper.rb