qtpay 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/lib/qtpay/service.rb +106 -0
- data/lib/qtpay/sign.rb +32 -0
- data/lib/qtpay/utils.rb +21 -0
- data/lib/qtpay/version.rb +3 -0
- data/lib/qtpay.rb +28 -0
- data/qtpay.gemspec +25 -0
- metadata +118 -0
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
.idea
|
7
|
+
Gemfile.lock
|
8
|
+
InstalledFiles
|
9
|
+
_yardoc
|
10
|
+
coverage
|
11
|
+
doc/
|
12
|
+
lib/bundler/man
|
13
|
+
pkg
|
14
|
+
rdoc
|
15
|
+
spec/reports
|
16
|
+
test/tmp
|
17
|
+
test/version_tmp
|
18
|
+
tmp
|
19
|
+
|
20
|
+
.DS_Store
|
21
|
+
.ruby-gemset
|
22
|
+
.ruby-version
|
data/Gemfile
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
module Qtpay
|
2
|
+
module Service
|
3
|
+
|
4
|
+
# ==== params ====
|
5
|
+
# (required)
|
6
|
+
# out_user: customer uniq id
|
7
|
+
#
|
8
|
+
# (optional)
|
9
|
+
# mobile: customer mobile number
|
10
|
+
# weixin_openid: WeiXin open id
|
11
|
+
# mchnt_id: merchant id
|
12
|
+
# out_mchnt: developer defined merchant id
|
13
|
+
# expires: token expires time in seconds (default 86400)
|
14
|
+
|
15
|
+
def self.get_user_token(params, options = {})
|
16
|
+
make_request(:get, get_user_token_url(params, options))
|
17
|
+
end
|
18
|
+
|
19
|
+
# ==== params ====
|
20
|
+
# (required)
|
21
|
+
# total_amt: total payment amount in cents
|
22
|
+
# out_sn: order serial number, must be uniq in all requests
|
23
|
+
#
|
24
|
+
# (optional)
|
25
|
+
# mchnt_id: merchant id
|
26
|
+
# out_mchnt: developer defined merchant id
|
27
|
+
# token: auth token created in get_user_token
|
28
|
+
# qrcode:
|
29
|
+
# openid: openid when creating qrcode
|
30
|
+
# expire_time: order expires time, format YYYY-mm-dd HH:MM:SS
|
31
|
+
|
32
|
+
def self.create_pre_order(params, options = {})
|
33
|
+
make_request(:post, create_pre_order_url(params, options))
|
34
|
+
end
|
35
|
+
|
36
|
+
GET_USER_TOKEN_REQUIRED_PARAMS = %w( out_user )
|
37
|
+
def self.get_user_token_url(params, options = {})
|
38
|
+
params = Utils.stringify_keys(params)
|
39
|
+
check_required_params(params, GET_USER_TOKEN_REQUIRED_PARAMS)
|
40
|
+
|
41
|
+
params = {
|
42
|
+
'caller' => 'server',
|
43
|
+
'app_code' => options[:app_code] || Qtpay.app_code,
|
44
|
+
}.merge(params)
|
45
|
+
|
46
|
+
request_uri('/auth/v1/token', params, options)
|
47
|
+
end
|
48
|
+
|
49
|
+
CREATE_PRE_ORDER_REQUIRED_PARAMS = %w( total_amt out_sn )
|
50
|
+
def self.create_pre_order_url(params, options = {})
|
51
|
+
params = Utils.stringify_keys(params)
|
52
|
+
check_required_params(params, CREATE_PRE_ORDER_REQUIRED_PARAMS)
|
53
|
+
|
54
|
+
params = {
|
55
|
+
'caller' => 'server',
|
56
|
+
'app_code' => options[:app_code] || Qtpay.app_code,
|
57
|
+
}.merge(params)
|
58
|
+
|
59
|
+
request_uri('/order/v1/pre_create', params, options)
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.request_uri(path, params, options = {})
|
63
|
+
uri = URI("#{Qtpay.gateway_url}#{path}")
|
64
|
+
uri.query = URI.encode_www_form(sign_params(params, options))
|
65
|
+
uri
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.sign_params(params, options = {})
|
69
|
+
params.merge(
|
70
|
+
'sign' => Qtpay::Sign.generate(params, options)
|
71
|
+
)
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.check_required_params(params, names)
|
75
|
+
return if !Qtpay.debug_mode?
|
76
|
+
|
77
|
+
names.each do |name|
|
78
|
+
warn("Qtpay Warn: missing required option: #{name}") unless params.has_key?(name)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def self.make_request(request_type, url)
|
83
|
+
request_type = case request_type
|
84
|
+
when :get
|
85
|
+
:get_response
|
86
|
+
when :post
|
87
|
+
:post_form
|
88
|
+
else
|
89
|
+
request_type
|
90
|
+
end
|
91
|
+
|
92
|
+
if request_type == :get_response
|
93
|
+
res = Net::HTTP.send(request_type, url)
|
94
|
+
else
|
95
|
+
res = Net::HTTP.send(request_type, url, {})
|
96
|
+
end
|
97
|
+
if res.respond_to?(:body)
|
98
|
+
JSON.parse(res.body)
|
99
|
+
else # unknown error
|
100
|
+
res
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
data/lib/qtpay/sign.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Qtpay
|
2
|
+
module Sign
|
3
|
+
def self.generate(params, options = {})
|
4
|
+
params = Utils.stringify_keys(params)
|
5
|
+
key = options[:server_key] || Qtpay.server_key
|
6
|
+
string = params_to_string(params)
|
7
|
+
|
8
|
+
sign(key, string)
|
9
|
+
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.verify?(params, options = {})
|
13
|
+
params = Utils.stringify_keys(params)
|
14
|
+
|
15
|
+
sign_type = params.delete('sign_type')
|
16
|
+
sign = params.delete('sign')
|
17
|
+
string = params_to_string(params)
|
18
|
+
|
19
|
+
key = options[:server_key] || Qtpay.server_key
|
20
|
+
|
21
|
+
sign == sign(key, string)
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.sign(key, string)
|
25
|
+
Digest::MD5.hexdigest("#{string}#{key}")
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.params_to_string(params)
|
29
|
+
params.sort.map { |item| item.join('=') }.join('&')
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/qtpay/utils.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
module Qtpay
|
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
|
+
|
11
|
+
def self.to_cents(dollars)
|
12
|
+
(dollars.to_d * 100).ceil
|
13
|
+
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.to_dollars(cents)
|
17
|
+
cents.to_d / 100.0
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
21
|
+
end
|
data/lib/qtpay.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'cgi'
|
3
|
+
require 'qtpay/version'
|
4
|
+
require 'qtpay/utils'
|
5
|
+
require 'qtpay/sign'
|
6
|
+
require 'qtpay/service'
|
7
|
+
|
8
|
+
module Qtpay
|
9
|
+
@debug_mode = true
|
10
|
+
@env = 'sandbox'
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :app_code, :server_key, :env, :debug_mode
|
14
|
+
|
15
|
+
def debug_mode?
|
16
|
+
!!@debug_mode
|
17
|
+
end
|
18
|
+
|
19
|
+
def gateway_url
|
20
|
+
if @env == 'live'
|
21
|
+
'https://qtapi.qfpay.com'
|
22
|
+
else
|
23
|
+
'https://qtsandbox.qfpay.com'
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
data/qtpay.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qtpay/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "qtpay"
|
8
|
+
spec.version = Qtpay::VERSION
|
9
|
+
spec.authors = ["April Tsang"]
|
10
|
+
spec.email = ["april@tofugear.com"]
|
11
|
+
spec.description = %q{An unofficial simple qtpay gem}
|
12
|
+
spec.summary = %q{An unofficial simple qtpay gem}
|
13
|
+
spec.homepage = "https://github.com/tofugear/qtpay"
|
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 "minitest"
|
24
|
+
spec.add_development_dependency "fakeweb"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qtpay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- April Tsang
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2015-12-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: minitest
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: fakeweb
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: An unofficial simple qtpay gem
|
79
|
+
email:
|
80
|
+
- april@tofugear.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- lib/qtpay.rb
|
88
|
+
- lib/qtpay/service.rb
|
89
|
+
- lib/qtpay/sign.rb
|
90
|
+
- lib/qtpay/utils.rb
|
91
|
+
- lib/qtpay/version.rb
|
92
|
+
- qtpay.gemspec
|
93
|
+
homepage: https://github.com/tofugear/qtpay
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
post_install_message:
|
97
|
+
rdoc_options: []
|
98
|
+
require_paths:
|
99
|
+
- lib
|
100
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
101
|
+
none: false
|
102
|
+
requirements:
|
103
|
+
- - ! '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
none: false
|
108
|
+
requirements:
|
109
|
+
- - ! '>='
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 1.8.23
|
115
|
+
signing_key:
|
116
|
+
specification_version: 3
|
117
|
+
summary: An unofficial simple qtpay gem
|
118
|
+
test_files: []
|