tencentpay 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in tencentpay.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/tencentpay.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'yaml'
2
+ require 'digest/md5'
3
+ require 'cgi'
4
+ require 'net/http'
5
+ require 'uri'
6
+ require 'nokogiri'
7
+
8
+ require "tencentpay/version"
9
+ require "tencentpay/config"
@@ -0,0 +1,14 @@
1
+ module Tencentpay
2
+ module Config
3
+ class << self
4
+ attr_reader :bargainor_id, :key
5
+
6
+ def load_config
7
+ filename = "#{Rails.root}/config/tencentpay.yml"
8
+ config = YAML.load(File.open(filename))
9
+ @bargainor_id, @key = config[RAILS_ENV]['bargainor_id'], config[RAILS_ENV]['key']
10
+ raise "Please configure your Tencentpay settings in #{filename}." unless @@spid && @@key
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,38 @@
1
+ module Tencentpay
2
+ class Query
3
+ # GATEWAY_URL = "http://mch.tenpay.com/cgi-bin/cfbi_query_order_v3.cgi"
4
+ #
5
+ # def initialize(sp_billno, date, attach = nil, charset = 'UTF-8')
6
+ # @cmdno = 2
7
+ # @date = date.strftime("%Y%m%d")
8
+ #
9
+ # @bargainor_id = Tenpay::Config.bargainor_id
10
+ # @key = Tenpay::Config.key
11
+ #
12
+ # @sp_billno = sp_billno.to_i
13
+ # @transaction_id = "%s%s%010d" % [@bargainor_id, @date, @sp_billno]
14
+ #
15
+ # @attach = attach || 'nil'
16
+ # @output_xml = '1'
17
+ # @charset = charset
18
+ # end
19
+ #
20
+ # def sign_params
21
+ # "attach=#{@attach}&bargainor_id=#{@bargainor_id}&charset=#{@charset}&cmdno=#{@cmdno}&date=#{@date}&" +
22
+ # "output_xml=#{@output_xml}&sp_billno=#{@sp_billno}&transaction_id=#{@transaction_id}&key=#{@key}"
23
+ # end
24
+ #
25
+ # def params
26
+ # "cmdno=#{@cmdno}&date=#{@date}&bargainor_id=#{@bargainor_id}&transaction_id=#{@transaction_id}&sp_billno=#{@sp_billno}&" +
27
+ # "attach=#{@attach}&output_xml=#{@output_xml}&charset=#{@charset}&sign=#{sign}"
28
+ # end
29
+ #
30
+ # def sign
31
+ # Digest::MD5.hexdigest(sign_params).upcase
32
+ # end
33
+ #
34
+ # def response
35
+ # @response ||= QueryResponse.new(Net::HTTP.get(URI.parse("#{GATEWAY_URL}?#{params}")))
36
+ # end
37
+ end
38
+ end
@@ -0,0 +1,65 @@
1
+ module Tencentpay
2
+ class QueryResponse #
3
+ # def initialize(response)
4
+ # @body = response
5
+ # end
6
+ #
7
+ # def valid?
8
+ # sign == Digest::MD5.hexdigest(sign_params).upcase
9
+ # end
10
+ #
11
+ # def successful?
12
+ # valid? && pay_result == '0'
13
+ # end
14
+ #
15
+ # def doc
16
+ # @doc ||= Hpricot(@body)
17
+ # end
18
+ #
19
+ # def attach
20
+ # @attach ||= (doc / 'attach').inner_text
21
+ # end
22
+ #
23
+ # def cmdno
24
+ # @cmdno ||= (doc / 'cmdno').inner_text
25
+ # end
26
+ #
27
+ # def date
28
+ # @date ||= (doc / 'date').inner_text
29
+ # end
30
+ #
31
+ # def fee_type
32
+ # @fee_type ||= (doc / 'fee_type').inner_text
33
+ # end
34
+ #
35
+ # def pay_info
36
+ # @pay_info ||= (doc / 'pay_info').inner_text
37
+ # end
38
+ #
39
+ # def pay_result
40
+ # @pay_result ||= (doc / 'pay_result').inner_text
41
+ # end
42
+ #
43
+ # def order_id
44
+ # @order_id ||= (doc / 'sp_billno').inner_text
45
+ # end
46
+ #
47
+ # def total_fee
48
+ # @total_fee ||= (doc / 'total_fee').inner_text
49
+ # end
50
+ #
51
+ # def transaction_id
52
+ # @transaction_id ||= (doc / 'transaction_id').inner_text
53
+ # end
54
+ #
55
+ # def sign
56
+ # @sign ||= (doc / 'sign').inner_text
57
+ # end
58
+ # private
59
+ # def sign_params
60
+ # "attach=#{attach}&bargainor_id=#{Tenpay::Config.spid}&cmdno=#{cmdno}&date=#{date}&fee_type=#{fee_type}" +
61
+ # "&pay_info=#{pay_info}&pay_result=#{pay_result}&sp_billno=#{order_id}&total_fee=#{total_fee}&" +
62
+ # "transaction_id=#{transaction_id}&key=#{Tenpay::Config.key}"
63
+ # end
64
+ end
65
+ end
@@ -0,0 +1,51 @@
1
+ module Tencentpay
2
+ class Request
3
+ GATEWAY_URL = "http://service.tenpay.com/cgi-bin/v3.0/payservice.cgi"
4
+
5
+ def initialize(desc, sp_billno, total_fee, return_url, bank_type = 0, spbill_create_ip = '', attach = nil)
6
+ @cmdno = 1
7
+ @date = Date.today.strftime("%Y%m%d")
8
+ @bank_type = bank_type
9
+ @desc = desc
10
+
11
+ @bargainor_id = Tenpay::Config.bargainor_id
12
+ @key = Tenpay::Config.key
13
+
14
+ @sp_billno = sp_billno.to_i
15
+ @transaction_id = "%s%s%010d" % [@bargainor_id, @date, @sp_billno]
16
+
17
+ @total_fee = total_fee.to_i
18
+ @fee_type = 1
19
+
20
+ @return_url = return_url
21
+ @attach = attach || ''
22
+
23
+ @spbill_create_ip = (RAILS_ENV == 'production' ? spbill_create_ip : '')
24
+ @cs = "utf-8"
25
+ end
26
+
27
+ def sign
28
+ @sign ||= Digest::MD5.hexdigest(sign_params).lowcase
29
+ end
30
+
31
+ def sign_params
32
+ str = "cmdno=#{@cmdno}&date=#{@date}&bargainor_id=#{@bargainor_id}&transaction_id=#{@transaction_id}" +
33
+ "&sp_billno=#{@sp_billno}&total_fee=#{@total_fee}&fee_type=#{@fee_type}" +
34
+ "&return_url=#{@return_url}&attach=#{CGI.escape(@attach)}"
35
+ str << "&spbill_create_ip=#{@spbill_create_ip}" if @spbill_create_ip.present?
36
+ str << "&key=#{@key}"
37
+ end
38
+
39
+ def params
40
+ str = "cmdno=#{@cmdno}&date=#{@date}&bargainor_id=#{@bargainor_id}&transaction_id=#{@transaction_id}" +
41
+ "&sp_billno=#{@sp_billno}&total_fee=#{@total_fee}&fee_type=#{@fee_type}" +
42
+ "&return_url=#{@return_url}&attach=#{CGI.escape(@attach)}&desc=#{CGI.escape(@desc)}"
43
+ str << "&spbill_create_ip=#{@spbill_create_ip}" if @spbill_create_ip.present?
44
+ str
45
+ end
46
+
47
+ def url
48
+ "#{GATEWAY_URL}?#{params}&sign=#{sign}"
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,49 @@
1
+ module Tencentpay
2
+ class Response
3
+ attr_reader :sp_billno, :total_fee, :attach
4
+
5
+ def initialize(params)
6
+ @cmdno = params[:cmdno] || ''
7
+ @pay_result = params[:pay_result] || ''
8
+ @date = params[:date] || ''
9
+ @transaction_id = params[:transaction_id] || ''
10
+ @sp_billno = (params[:sp_billno] || '').to_i
11
+ @total_fee = params[:total_fee] || ''
12
+ @fee_type = params[:fee_type] || ''
13
+ @attach = params[:attach] || ''
14
+
15
+ @sign = params[:sign] || ''
16
+
17
+ @bargainor_id = Tenpay::Config.bargainor_id
18
+ @key = Tenpay::Config.key
19
+ end
20
+
21
+ def successful?
22
+ @pay_result == '0' && valid_sign?
23
+ end
24
+
25
+ def valid_sign?
26
+ @sign == Digest::MD5.hexdigest(sign_params).lowcase
27
+ end
28
+
29
+ def sign_params
30
+ "cmdno=#{@cmdno}&pay_result=#{@pay_result}&date=#{@date}&transaction_id=#{@transaction_id}" +
31
+ "&sp_billno=#{@sp_billno}&total_fee=#{@total_fee}&fee_type=#{@fee_type}" +
32
+ "&attach=#{CGI.escape(@attach)}&key=#{@key}"
33
+ end
34
+
35
+ def self.show_html(show_url)
36
+ strHtml = <<-EOF
37
+ <html>
38
+ <head>
39
+ <meta name="TENCENT_ONLINE_PAYMENT" content="China TENCENT">
40
+ <script language="javascript">
41
+ window.location.href='#{show_url}';
42
+ </script>
43
+ </head>
44
+ <body></body>
45
+ </html>
46
+ EOF
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Tencentpay
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "tencentpay/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "tencentpay"
7
+ s.version = Tencentpay::VERSION
8
+ s.authors = ["liguang"]
9
+ s.email = ["lg2046@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{财付通接口}
12
+ s.description = %q{财付通接口 }
13
+
14
+ s.rubyforge_project = "tencentpay"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ # specify any dependencies here; for example:
22
+ # s.add_development_dependency "rspec"
23
+ s.add_runtime_dependency "nokogiri"
24
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: tencentpay
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - liguang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-04 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: nokogiri
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ description: !binary |
35
+ 6LSi5LuY6YCa5o6l5Y+jIA==
36
+
37
+ email:
38
+ - lg2046@gmail.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - .gitignore
47
+ - Gemfile
48
+ - Rakefile
49
+ - lib/tencentpay.rb
50
+ - lib/tencentpay/config.rb
51
+ - lib/tencentpay/query.rb
52
+ - lib/tencentpay/query_response.rb
53
+ - lib/tencentpay/request.rb
54
+ - lib/tencentpay/response.rb
55
+ - lib/tencentpay/version.rb
56
+ - tencentpay.gemspec
57
+ homepage: ""
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options: []
62
+
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: tencentpay
86
+ rubygems_version: 1.8.13
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: !binary |
90
+ 6LSi5LuY6YCa5o6l5Y+j
91
+
92
+ test_files: []
93
+