Skip to content

Commit b57c6aa

Browse files
committed
init alipay
0 parents  commit b57c6aa

17 files changed

+322
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
*.gem
2+
*.rbc
3+
.bundle
4+
.config
5+
.yardoc
6+
Gemfile.lock
7+
InstalledFiles
8+
_yardoc
9+
coverage
10+
doc/
11+
lib/bundler/man
12+
pkg
13+
rdoc
14+
spec/reports
15+
test/tmp
16+
test/version_tmp
17+
tmp

Gemfile

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source 'https://rubygems.org'
2+
3+
# Specify your gem's dependencies in alipay.gemspec
4+
gemspec

LICENSE.txt

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Copyright (c) 2013 Rei
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.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Alipay
2+
3+
TODO: Write a gem description
4+
5+
## Installation
6+
7+
Add this line to your application's Gemfile:
8+
9+
gem 'alipay'
10+
11+
And then execute:
12+
13+
$ bundle
14+
15+
Or install it yourself as:
16+
17+
$ gem install alipay
18+
19+
## Usage
20+
21+
TODO: Write usage instructions here
22+
23+
## Contributing
24+
25+
1. Fork it
26+
2. Create your feature branch (`git checkout -b my-new-feature`)
27+
3. Commit your changes (`git commit -am 'Add some feature'`)
28+
4. Push to the branch (`git push origin my-new-feature`)
29+
5. Create new Pull Request

Rakefile

+9
Original file line numberDiff line numberDiff line change
@@ -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

alipay.gemspec

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# coding: utf-8
2+
lib = File.expand_path('../lib', __FILE__)
3+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4+
require 'alipay/version'
5+
6+
Gem::Specification.new do |spec|
7+
spec.name = "alipay"
8+
spec.version = Alipay::VERSION
9+
spec.authors = ["Rei"]
10+
spec.email = ["[email protected]"]
11+
spec.description = %q{TODO: Write a gem description}
12+
spec.summary = %q{TODO: Write a gem summary}
13+
spec.homepage = ""
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 "fakeweb"
24+
end

lib/alipay.rb

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
require "alipay/version"
2+
require 'alipay/utils'
3+
require 'alipay/sign'
4+
require 'alipay/service'
5+
require 'alipay/notify'
6+
7+
module Alipay
8+
class << self
9+
attr_accessor :pid
10+
attr_accessor :key
11+
attr_accessor :seller_email
12+
end
13+
end

lib/alipay/notify.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module Alipay
2+
class Notify
3+
def self.verify?(params)
4+
params = Utils.symbolize_keys(params)
5+
open("http://notify.alipay.com/trade/notify_query.do?partner=#{Alipay.pid}&notify_id=#{CGI.escape params[:notify_id].to_s}").read == 'true'
6+
end
7+
end
8+
end

lib/alipay/service.rb

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
require 'cgi'
2+
require 'open-uri'
3+
4+
module Alipay
5+
module Service
6+
GATEWAY_URL = 'https://mapi.alipay.com/gateway.do'
7+
8+
CREATE_PARTNER_TRADE_BY_BUYER_REQUIRED_OPTIONS = %w( service partner _input_charset out_trade_no subject payment_type logistics_type logistics_fee logistics_payment seller_email price quantity )
9+
# alipayescow
10+
def self.create_partner_trade_by_buyer_url(options)
11+
options = {
12+
:service => 'create_partner_trade_by_buyer',
13+
:_input_charset => 'utf-8',
14+
:partner => Alipay.pid,
15+
:seller_email => Alipay.seller_email,
16+
:payment_type => '1'
17+
}.merge(Utils.symbolize_keys(options))
18+
19+
check_required_options(options, TRADE_CREATE_BY_BUYER_REQUIRED_OPTIONS)
20+
21+
"#{GATEWAY_URL}?#{query_string(options)}"
22+
end
23+
24+
TRADE_CREATE_BY_BUYER_REQUIRED_OPTIONS = %w( service partner _input_charset out_trade_no subject payment_type logistics_type logistics_fee logistics_payment seller_email price quantity )
25+
# alipaydualfun
26+
def self.trade_create_by_buyer_url(options = {})
27+
options = {
28+
:service => 'trade_create_by_buyer',
29+
:_input_charset => 'utf-8',
30+
:partner => Alipay.pid,
31+
:seller_email => Alipay.seller_email,
32+
:payment_type => '1'
33+
}.merge(Utils.symbolize_keys(options))
34+
35+
check_required_options(options, TRADE_CREATE_BY_BUYER_REQUIRED_OPTIONS)
36+
37+
"#{GATEWAY_URL}?#{query_string(options)}"
38+
end
39+
40+
SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_OPTIONS = %w( service partner _input_charset trade_no logistics_name )
41+
def self.send_goods_confirm_by_platform(options)
42+
options = {
43+
:service => 'send_goods_confirm_by_platform',
44+
:partner => Alipay.pid,
45+
:_input_charset => 'utf-8'
46+
}.merge(Utils.symbolize_keys(options))
47+
48+
check_required_options(options, SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_OPTIONS)
49+
50+
if options[:transport_type].nil? && options[:create_transport_type].nil?
51+
warn("Ailpay Warn: transport_type or create_transport_type must have one")
52+
end
53+
54+
open("#{GATEWAY_URL}?#{query_string(options)}").read
55+
end
56+
57+
def self.query_string(options)
58+
options.merge(:sign_type => 'MD5', :sign => Alipay::Sign.generate(options)).map do |key, value|
59+
"#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
60+
end.join('&')
61+
end
62+
63+
def self.check_required_options(options, names)
64+
names.each do |name|
65+
warn("Ailpay Warn: missing required option: #{name}") unless options.has_key?(name.to_sym)
66+
end
67+
end
68+
end
69+
end

lib/alipay/sign.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Alipay
2+
module Sign
3+
def self.generate(params)
4+
query = params.sort.map do |key, value|
5+
"#{key}=#{value}"
6+
end.join('&')
7+
8+
Digest::MD5.hexdigest("#{query}#{Alipay.key}")
9+
end
10+
11+
def self.verify?(params)
12+
params = Utils.symbolize_keys(params)
13+
params.delete(:sign_type)
14+
sign = params.delete(:sign)
15+
16+
generate(params) == sign
17+
end
18+
end
19+
end

lib/alipay/utils.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Alipay
2+
module Utils
3+
def self.symbolize_keys(hash)
4+
new_hash = {}
5+
hash.each do |key, value|
6+
new_hash[(key.to_sym rescue key) || key] = value
7+
end
8+
new_hash
9+
end
10+
end
11+
end

lib/alipay/version.rb

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module Alipay
2+
VERSION = "0.0.1"
3+
end

test/alipay/notify_test.rb

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
require 'test_helper'
2+
3+
class Alipay::NotifyTest < Test::Unit::TestCase
4+
def test_verify_notify
5+
FakeWeb.register_uri(:get, "http://notify.alipay.com/trade/notify_query.do?partner=#{Alipay.pid}&notify_id=true_id", :body => "true")
6+
FakeWeb.register_uri(:get, "http://notify.alipay.com/trade/notify_query.do?partner=#{Alipay.pid}&notify_id=fake_id", :body => "false")
7+
8+
assert Alipay::Notify.verify?(:notify_id => 'true_id')
9+
assert !Alipay::Notify.verify?(:notify_id => 'fake_id')
10+
end
11+
end

test/alipay/service_test.rb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
require 'test_helper'
2+
3+
class Alipay::ServiceTest < Test::Unit::TestCase
4+
def test_generatecreate_partner_trade_by_buyer_url
5+
options = {
6+
:out_trade_no => '1',
7+
:subject => 'test',
8+
:logistics_type => 'POST',
9+
:logistics_fee => '0',
10+
:logistics_payment => 'SELLER_PAY',
11+
:price => '0.01',
12+
:quantity => 1
13+
}
14+
assert_not_nil Alipay::Service.create_partner_trade_by_buyer_url(options)
15+
end
16+
17+
def test_should_generate_trade_create_by_buyer_url
18+
options = {
19+
:out_trade_no => '1',
20+
:subject => 'test',
21+
:logistics_type => 'POST',
22+
:logistics_fee => '0',
23+
:logistics_payment => 'SELLER_PAY',
24+
:price => '0.01',
25+
:quantity => 1
26+
}
27+
assert_not_nil Alipay::Service.trade_create_by_buyer_url(options)
28+
end
29+
30+
def test_should_send_goods_confirm_by_platform
31+
body = <<-EOF
32+
<?xml version="1.0" encoding="utf-8"?>
33+
<alipay>
34+
<is_success>T</is_success>
35+
</alipay>
36+
EOF
37+
FakeWeb.register_uri(
38+
:get,
39+
%r|https://mapi\.alipay\.com/gateway\.do.*|,
40+
:body => body
41+
)
42+
43+
assert_equal body, Alipay::Service.send_goods_confirm_by_platform(
44+
:trade_no => 'trade_no_id',
45+
:logistics_name => 'writings.io',
46+
:transport_type => 'POST'
47+
)
48+
end
49+
end

test/alipay/sign_test.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require 'test_helper'
2+
3+
class Alipay::SignTest < Test::Unit::TestCase
4+
def setup
5+
@params = {
6+
:service => 'test',
7+
:partner => '123'
8+
}
9+
@sign = Digest::MD5.hexdigest("partner=123&service=test#{Alipay.key}")
10+
end
11+
12+
def test_generate_sign
13+
assert_equal @sign, Alipay::Sign.generate(@params)
14+
end
15+
16+
def test_verify_sign
17+
assert Alipay::Sign.verify?(@params.merge(:sign => @sign))
18+
end
19+
end

test/alipay/utils_test.rb

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'test_helper'
2+
3+
class Alipay::UtilsTest < Test::Unit::TestCase
4+
def test_symbolize_keys
5+
hash = { 'a' => 1, :b => 2 }
6+
assert_equal({ :a => 1, :b => 2 }.sort, Alipay::Utils.symbolize_keys(hash).sort)
7+
end
8+
end

test/test_helper.rb

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
require 'test/unit'
2+
require 'alipay'
3+
require 'fakeweb'
4+
5+
Alipay.pid = 'pid'
6+
Alipay.key = 'key'
7+
Alipay.seller_email = '[email protected]'

0 commit comments

Comments
 (0)