Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add query interface

  • Loading branch information...
commit d86c08610aa1c4f2961257a4d8d4859db988a599 1 parent 473d1ab
Zhang Yuanyi authored
View
2  lib/tenpay.rb
@@ -1,5 +1,7 @@
require 'tenpay/config'
require 'tenpay/request'
require 'tenpay/response'
+require 'tenpay/query'
+require 'tenpay/query_response'
Tenpay::Config.load_config
View
54 lib/tenpay/query.rb
@@ -0,0 +1,54 @@
+require 'digest/md5'
+require 'cgi'
+require 'iconv'
+require 'net/http'
+require 'uri'
+require 'hpricot'
+
+module Tenpay
+ class Query
+ GATEWAY_URL = "http://mch.tenpay.com/cgi-bin/cfbi_query_order_v3.cgi"
+
+ def initialize(order_id, date, attach=nil, charset='UTF-8')
+ @cmdno = 1
+ @date = date.strftime("%Y%m%d")
+ @spid = Tenpay::Config.spid
+ @key = Tenpay::Config.key
+
+ @order_id = order_id.to_i
+ @attach = attach || 'nil'
+ @charset = charset
+ end
+
+ def response
+ @response ||= QueryResponse.new(Net::HTTP.get(URI.parse("#{GATEWAY_URL}?#{params}")))
+ end
+
+ def transaction_id
+ @transaction_id ||= "%s%s%010d" % [@spid, @date, @order_id]
+ end
+
+ def sign
+ @sign ||= Digest::MD5.hexdigest(sign_params).upcase
+ end
+
+ def sign_params
+ @sign_params ||= generate_sign_params
+ end
+
+ private
+ def params
+ @params ||= generate_params
+ end
+
+ def generate_params
+ "cmdno=2&date=#{@date}&bargainor_id=#{@spid}&transaction_id=#{transaction_id}&sp_billno=#{@order_id}&attach=#{@attach}&" +
+ "output_xml=1&charset=#{@charset}&sign=#{sign}"
+ end
+
+ def generate_sign_params
+ "attach=#{@attach}&bargainor_id=#{@spid}&charset=#{@charset}&cmdno=2&date=#{@date}&output_xml=1&" +
+ "sp_billno=#{@order_id}&transaction_id=#{transaction_id}&key=#{@key}"
+ end
+ end
+end
View
65 lib/tenpay/query_response.rb
@@ -0,0 +1,65 @@
+module Tenpay
+ class QueryResponse
+ def initialize(response)
+ @body = response
+ end
+
+ def valid?
+ sign == Digest::MD5.hexdigest(sign_params).upcase
+ end
+
+ def successful?
+ valid? && pay_result == '0'
+ end
+
+ def doc
+ @doc ||= Hpricot(@body)
+ end
+
+ def attach
+ @attach ||= (doc / 'attach').inner_text
+ end
+
+ def cmdno
+ @cmdno ||= (doc / 'cmdno').inner_text
+ end
+
+ def date
+ @date ||= (doc / 'date').inner_text
+ end
+
+ def fee_type
+ @fee_type ||= (doc / 'fee_type').inner_text
+ end
+
+ def pay_info
+ @pay_info ||= (doc / 'pay_info').inner_text
+ end
+
+ def pay_result
+ @pay_result ||= (doc / 'pay_result').inner_text
+ end
+
+ def order_id
+ @order_id ||= (doc / 'sp_billno').inner_text
+ end
+
+ def total_fee
+ @total_fee ||= (doc / 'total_fee').inner_text
+ end
+
+ def transaction_id
+ @transaction_id ||= (doc / 'transaction_id').inner_text
+ end
+
+ def sign
+ @sign ||= (doc / 'sign').inner_text
+ end
+ private
+ def sign_params
+ "attach=#{attach}&bargainor_id=#{Tenpay::Config.spid}&cmdno=#{cmdno}&date=#{date}&fee_type=#{fee_type}" +
+ "&pay_info=#{pay_info}&pay_result=#{pay_result}&sp_billno=#{order_id}&total_fee=#{total_fee}&" +
+ "transaction_id=#{transaction_id}&key=#{Tenpay::Config.key}"
+ end
+ end
+end
View
80 spec/query_spec.rb
@@ -0,0 +1,80 @@
+ENV["RAILS_ENV"] = "test"
+require 'spec'
+require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
+require "tenpay"
+require 'net/http'
+
+describe Tenpay::Request do
+ before do
+ @query = Tenpay::Query.new(1, Date.new(2009, 9, 12))
+ @sign_params = "attach=nil&bargainor_id=1900000109&charset=UTF-8&cmdno=2&date=20090912&output_xml=1&" +
+ "sp_billno=1&transaction_id=1900000109200909120000000001&key=8934e7d15453e97507ef794cf7b0519d"
+ @sign = Digest::MD5.hexdigest(@sign_params).upcase
+ @params = "cmdno=2&date=20090912&bargainor_id=1900000109&transaction_id=1900000109200909120000000001" +
+ "&sp_billno=1&attach=nil&output_xml=1&charset=UTF-8&sign=#{@sign}"
+ end
+
+ it "should return a Query Response object." do
+ @uri = mock('uri')
+ URI.should_receive(:parse).with("#{Tenpay::Query::GATEWAY_URL}?#{@params}").and_return(@uri)
+ Net::HTTP.should_receive(:get).with(@uri)
+ Tenpay::QueryResponse.stub!(:new).and_return(mock('response'))
+ @query.response
+ end
+
+ it "should construct sign params properly." do
+ @query.sign_params.should == @sign_params
+ end
+end
+
+describe Tenpay::QueryResponse do
+ before do
+ @valid_response = VALID_XML
+ @invalid_response = ''
+ end
+
+ it "should be invalid with invalid response" do
+ @response = Tenpay::QueryResponse.new(@invalid_response)
+ @response.should_not be_valid
+ end
+
+ it "should be valid with valid response" do
+ @response = Tenpay::QueryResponse.new(@valid_response)
+ @response.attach.should == 'a'
+ @response.cmdno.should == '2'
+ @response.date.should == '20090315'
+ @response.fee_type.should == '1'
+ @response.pay_info.should == 'test'
+ @response.pay_result.should == '2006'
+ @response.order_id.should == '56212087'
+ @response.total_fee.should == '3000'
+ @response.transaction_id.should == '1900000108200903150056212087'
+ @response.sign.should == 'A8D50F1DC2E2E4CC83191AE39C702C27'
+
+ @response.should be_valid
+ end
+
+ VALID_XML = %(<?xml version="1.0" encoding="GB2312" ?>
+ <root>
+ <attach>a</attach>
+ <bank_type />
+ <bargainor_id>1900000108</bargainor_id>
+ <bill_no />
+ <charset>GB2312</charset>
+ <cmdno>2</cmdno>
+ <date>20090315</date>
+ <dump_html_file />
+ <fee_type>1</fee_type>
+ <output_xml>1</output_xml>
+ <pay_info>test</pay_info>
+ <pay_result>2006</pay_result>
+ <retcode>00</retcode>
+ <retmsg>交易成功</retmsg>
+ <return_url>http://192.168.1.129/bank/cft_return.aspx</return_url>
+ <sign>A8D50F1DC2E2E4CC83191AE39C702C27</sign>
+ <sp_billno>56212087</sp_billno>
+ <total_fee>3000</total_fee>
+ <transaction_id>1900000108200903150056212087</transaction_id>
+ </root>
+ )
+end
View
2  spec/request_spec.rb
@@ -6,7 +6,7 @@
describe Tenpay::Request do
before do
@request = Tenpay::Request.new('test product', 1,
- 4500, 'http://return', 'order_id=1')
+ 4500, 'http://return', '', 'order_id=1')
today = Date.today
@date = "%d%02d%02d" % [today.year, today.month, today.day]
@params = "cmdno=1&date=#{@date}&bargainor_id=1900000109&" +
View
1  uninstall.rb
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.