chinapay 1.2.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: 32dcd035384323bb5646d4a37f2635ac426b17e0
4
+ data.tar.gz: 89f50fc420ca21a46def55a8362bd57816e86a02
5
+ SHA512:
6
+ metadata.gz: 8e4c1733418a8766c10ea61e38cc4189b1ce2e73f26c0fa5327c47cb8f7146439ce39c978a72c097b34464d024d1100e3e0a51c0269990ac8282adc1ff48f7cd
7
+ data.tar.gz: 6ec78921f1715ba8e02c763a842094759b1056657e15e4772469d158463b3438962ff766db810f8e3f46d4f81a140870b35e565a6b007bc78e66a9df73f951f7
data/.DS_Store ADDED
Binary file
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in tencentpay.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 lg2046
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.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Tencentpay
2
+
3
+ 财付通SDK
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'chinapay', :git => 'git://github.com/lg2046/chinapay.git
10
+
11
+ And then execute:
12
+
13
+ $ bundle or bundle update
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chinapay
18
+
19
+ ## Usage
20
+
21
+ 充值:
22
+ @request = Chinapay.build_request(:alipay, //支持["alipay", "tenpay", "1002", "1038" 数字为财付通的网银直联代码]
23
+ '测试充值',
24
+ 100, //人民币元为单位
25
+ 'http://www.example.com/transactions/notify', //回调url
26
+ current_user.id.to_s, //附加数据
27
+ )
28
+ redirect_to @request.url
29
+
30
+ response = Chinapay.build_response(params)
31
+ if response
32
+ response.successful?
33
+ puts response.success_response(option_redirect_url)
34
+ end
35
+
36
+ 充值成功后回调:
37
+ tencent_response = Tencentpay::Response.new(params) tencent_response为一hash值 inspect即可
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/chinapay.gemspec ADDED
@@ -0,0 +1,18 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/chinapay/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["lg2046"]
6
+ gem.email = ["lg2046@gmail.com"]
7
+ gem.description = %q{支持支付宝 财付通的ruby 即时到账}
8
+ gem.summary = %q{支持支付宝 财付通的ruby 即时到账}
9
+ gem.homepage = "http://github.com/lg2046"
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "chinapay"
15
+ gem.require_paths = ["lib"]
16
+
17
+ gem.version = Chinapay::VERSION
18
+ end
data/chinapay.yml ADDED
@@ -0,0 +1,10 @@
1
+ ---
2
+ development:
3
+ tencent:
4
+ parter: 121111XXXX
5
+ key: XXXXXXXXXXXXXXXXXX
6
+ seller: 121111XXXX
7
+ alipay:
8
+ parter: 12111XXXX
9
+ key: XXXXXXX
10
+ seller: XXXXX@XXXXX.com
data/lib/.DS_Store ADDED
Binary file
data/lib/chinapay.rb ADDED
@@ -0,0 +1,36 @@
1
+ require "chinapay/version"
2
+ require 'yaml'
3
+ require 'digest/md5'
4
+ require 'cgi'
5
+
6
+ require File.dirname(__FILE__) + '/chinapay/config'
7
+
8
+ require File.dirname(__FILE__) + '/chinapay/request/tencent'
9
+ require File.dirname(__FILE__) + '/chinapay/request/alipay'
10
+ require File.dirname(__FILE__) + '/chinapay/response/tencent'
11
+ require File.dirname(__FILE__) + '/chinapay/response/alipay'
12
+
13
+ module Chinapay
14
+
15
+ ADAPTER_TYPES = ["alipay", "tenpay", "1002", "1038", "1003", "1052", "1005", "1020", "1024", "1022", "1006", "1008", "1004", "1021", "1009", "1025", "1027", "1032"]
16
+
17
+ def self.build_request(adapter, title, total_fee, notify_url, attach)
18
+ adapter = adapter.to_s
19
+
20
+ raise "Not vaid chinapay adapter, it should be in #{ADAPTER_TYPES}" unless ADAPTER_TYPES.include?(adapter)
21
+
22
+ return Chinapay::Request::Alipay.new(title, total_fee, notify_url, attach, nil) if adapter == "alipay"
23
+ return Chinapay::Request::Tencent.new(title, total_fee, notify_url, attach, '0') if adapter == "tenpay"
24
+ Chinapay::Request::Tencent.new(title, total_fee, notify_url, attach, adapter)
25
+ end
26
+
27
+ def self.build_response(params)
28
+ return Chinapay::Response::Tencent.new(params) if params["sp_billno"].present?
29
+ return Chinapay::Response::Alipay.new(params) if params["out_trade_no"].present?
30
+ nil
31
+ end
32
+ end
33
+
34
+ if defined?(Rails)
35
+ require File.dirname(__FILE__) + '/chinapay/railtie'
36
+ end
Binary file
@@ -0,0 +1,27 @@
1
+ module Chinapay
2
+
3
+ class << self; attr_accessor :config; end
4
+
5
+ # 配置采用统一的 parter_id key seller
6
+ def self.load_config
7
+ begin
8
+ # filename = "#{Rails.root}/config/chinapay.yml"
9
+ filename = File.expand_path(Rails.root.to_s + "/config/chinapay.yml")
10
+ @config = YAML.load(File.open(filename))[Rails.env]
11
+ rescue Exception => e
12
+ puts <<-EXAMPLE
13
+ ---
14
+ development:
15
+ tencent:
16
+ parter: 121111XXXX
17
+ key: XXXXXXXXXXXXXXXXXX
18
+ seller: 121111XXXX
19
+ alipay:
20
+ parter: 12111XXXX
21
+ key: XXXXXXX
22
+ seller: XXXXX@XXXXX.com
23
+ EXAMPLE
24
+ raise "Please configure your Chinapay settings in #{filename}."
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,8 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Tencentpay
3
+ class Railtie < ::Rails::Railtie
4
+ initializer "加载rails的配置文件" do
5
+ Chinapay.load_config
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,43 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Chinapay
3
+ module Request
4
+ class Alipay
5
+ GATEWAY_URL = "https://mapi.alipay.com/gateway.do?"
6
+
7
+ def initialize(title, total_fee, notify_url, attach, bank_type)
8
+ @title = title
9
+ @total_fee = total_fee
10
+ @notify_url = notify_url
11
+ @attach = attach
12
+ end
13
+
14
+
15
+ def url
16
+ sign_parameters = {
17
+ :service => "create_direct_pay_by_user",
18
+ :partner => Chinapay.config["alipay"]["parter"],
19
+ :_input_charset => "utf-8",
20
+ :return_url => @notify_url,
21
+ :notify_url => @notify_url,
22
+ :out_trade_no => Time.now.to_i + Time.now.tv_usec * 1000 + $$,
23
+ :subject => @title,
24
+ :payment_type => 1,
25
+ :seller_email => Chinapay.config["alipay"]["seller"],
26
+ :total_fee => @total_fee,
27
+ :extra_common_param => @attach
28
+ }
29
+
30
+ url_parameters = sign_parameters.merge(:sign_type => "MD5", :sign => md5_sort(sign_parameters)).map {|k, v| "#{k}=#{CGI.escape(v.to_s)}"}.join('&')
31
+ "https://mapi.alipay.com/gateway.do?" + url_parameters
32
+ end
33
+
34
+
35
+
36
+ private
37
+ def md5_sort(hash)
38
+ Digest::MD5.hexdigest(hash.keys.sort {|va,kb| va.to_s <=> kb.to_s}.map {|k| "#{k}=#{hash[k]}"}.join('&') + Chinapay.config["alipay"]["key"]).downcase
39
+ end
40
+
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,53 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Chinapay
3
+ module Request
4
+ class Tencent
5
+ GATEWAY_URL = "https://www.tenpay.com/cgi-bin/v1.0/pay_gate.cgi"
6
+
7
+ def initialize(title, total_fee, notify_url, attach, bank_type)
8
+ @cmdno = 1
9
+ @date = Date.today.strftime("%Y%m%d")
10
+ @bank_type = bank_type
11
+ @desc = title || "即时到账充值"
12
+
13
+ @bargainor_id = Chinapay.config["tencent"]["parter"]
14
+ @key = Chinapay.config["tencent"]["key"]
15
+
16
+ @sp_billno = Time.now.to_i + Time.now.tv_usec * 1000 + $$
17
+ @transaction_id = "#{@bargainor_id}#{@date}#{@sp_billno}"
18
+
19
+ @total_fee = (total_fee * 100).to_i
20
+ @fee_type = 1
21
+
22
+ @return_url = notify_url
23
+ @attach = attach || ''
24
+
25
+ @spbill_create_ip = '127.0.0.1'
26
+ @cs = "utf-8"
27
+ end
28
+
29
+ def sign
30
+ @sign ||= Digest::MD5.hexdigest(sign_params).downcase
31
+ end
32
+
33
+ def sign_params
34
+ "cmdno=#{@cmdno}&date=#{@date}&bargainor_id=#{@bargainor_id}&transaction_id=#{@transaction_id}" +
35
+ "&sp_billno=#{@sp_billno}&total_fee=#{@total_fee}&fee_type=#{@fee_type}" +
36
+ "&return_url=#{@return_url}&attach=#{CGI.escape(@attach)}" +
37
+ "&spbill_create_ip=#{@spbill_create_ip}&key=#{@key}"
38
+ end
39
+
40
+ def params
41
+ "cmdno=#{@cmdno}&date=#{@date}&bank_type=#{@bank_type}&desc=#{CGI.escape(@desc)}" +
42
+ "&purchaser_id=&bargainor_id=#{@bargainor_id}&transaction_id=#{@transaction_id}" +
43
+ "&sp_billno=#{@sp_billno}&total_fee=#{@total_fee}&fee_type=#{@fee_type}" +
44
+ "&return_url=#{CGI.escape(@return_url)}&attach=#{CGI.escape(@attach)}" +
45
+ "&spbill_create_ip=#{@spbill_create_ip}&cs=#{@cs}"
46
+ end
47
+
48
+ def url
49
+ "#{GATEWAY_URL}?#{params}&sign=#{sign}"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,34 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Chinapay
3
+ module Response
4
+ class Alipay
5
+ attr_reader :trade_no, :total_fee, :attach, :pay_type
6
+
7
+ def initialize(params)
8
+ @params = params
9
+ @trade_no = params["out_trade_no"]
10
+ @total_fee = params["total_fee"].to_f
11
+ @attach = params["extra_common_param"]
12
+
13
+ @pay_type = "支付宝"
14
+ end
15
+
16
+ def successful?
17
+ (@params["trade_status"] == "TRADE_SUCCESS") && (md5_sort(filter_params(@params)) == @params.sign)
18
+ end
19
+
20
+ def success_response(redirect_url = "")
21
+ "success"
22
+ end
23
+
24
+ private
25
+ def filter_params(params)
26
+ params.dup.delete_if {|k,v| (k == "controller") || (k == "action") || (k == "sign") || (k == "sign_type") || v.blank? }
27
+ end
28
+
29
+ def md5_sort(hash)
30
+ Digest::MD5.hexdigest(hash.keys.sort.map {|k| "#{k}=#{hash[k]}"}.join('&') + Chinapay.config["alipay"]["key"]).downcase
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,58 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Chinapay
3
+ module Response
4
+ class Tencent
5
+ attr_reader :trade_no, :total_fee, :attach, :pay_type
6
+
7
+ def initialize(params)
8
+ @cmdno = params["cmdno"] || ''
9
+ @pay_result = params["pay_result"] || ''
10
+ @date = params["date"] || ''
11
+ @transaction_id = params["transaction_id"] || ''
12
+ @sp_billno = (params["sp_billno"] || '').to_i
13
+ @total_fee = params["total_fee"] || ''
14
+ @fee_type = params["fee_type"] || ''
15
+ @attach = params["attach"] || ''
16
+
17
+ @sign = params["sign"] || ''
18
+
19
+ @bargainor_id = Chinapay.config["tencent"]["parter"]
20
+ @key = Chinapay.config["tencent"]["key"]
21
+
22
+ @pay_type = "财付通"
23
+ end
24
+
25
+ def total_fee
26
+ @total_fee.to_f / 100
27
+ end
28
+
29
+ def trade_no
30
+ @sp_billno
31
+ end
32
+
33
+ def successful?
34
+ @pay_result == '0' && @sign.downcase == Digest::MD5.hexdigest(sign_params).downcase
35
+ end
36
+
37
+ def sign_params
38
+ "cmdno=#{@cmdno}&pay_result=#{@pay_result}&date=#{@date}&transaction_id=#{@transaction_id}" +
39
+ "&sp_billno=#{@sp_billno}&total_fee=#{@total_fee}&fee_type=#{@fee_type}" +
40
+ "&attach=#{CGI.escape(@attach)}&key=#{@key}"
41
+ end
42
+
43
+ def success_response(redirect_url = "")
44
+ strHtml = <<-EOF
45
+ <html>
46
+ <head>
47
+ <meta name="TENCENT_ONLINE_PAYMENT" content="China TENCENT">
48
+ <script language="javascript">
49
+ window.location.href='#{redirect_url}';
50
+ </script>
51
+ </head>
52
+ <body></body>
53
+ </html>
54
+ EOF
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,3 @@
1
+ module Chinapay
2
+ VERSION = "1.2.0"
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chinapay
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.2.0
5
+ platform: ruby
6
+ authors:
7
+ - lg2046
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-28 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: 支持支付宝 财付通的ruby 即时到账
14
+ email:
15
+ - lg2046@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - .DS_Store
21
+ - Gemfile
22
+ - LICENSE
23
+ - README.md
24
+ - Rakefile
25
+ - chinapay.gemspec
26
+ - chinapay.yml
27
+ - lib/.DS_Store
28
+ - lib/chinapay.rb
29
+ - lib/chinapay/.DS_Store
30
+ - lib/chinapay/config.rb
31
+ - lib/chinapay/railtie.rb
32
+ - lib/chinapay/request/alipay.rb
33
+ - lib/chinapay/request/tencent.rb
34
+ - lib/chinapay/response/alipay.rb
35
+ - lib/chinapay/response/tencent.rb
36
+ - lib/chinapay/version.rb
37
+ homepage: http://github.com/lg2046
38
+ licenses: []
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.2.0
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: 支持支付宝 财付通的ruby 即时到账
60
+ test_files: []