midpay_alipay 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format d
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.3@midpay-alipay --create
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'http://ruby.taobao.org'
2
+ gemspec
3
+ group :test do
4
+ gem 'rspec', '~> 2.11'
5
+ gem "rack-test", require: "rack/test"
6
+ gem 'simplecov'
7
+ gem 'simplecov-rcov'
8
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 xixilive
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,69 @@
1
+ # Midpay::Alipay
2
+
3
+ Payment strategy for Alipay(支付宝 https://alipay.com) base on Midpay Middleware.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'midpay_alipay'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install midpay-alipay
18
+
19
+ ## Usage
20
+
21
+ In your application initialize
22
+
23
+ ```ruby
24
+ require 'midpay_alipay'
25
+ use Midpay::Strategies::Alipay, APPID, APPSECRET, :request_params_proc => {|params|
26
+ order = Order.find(params['order_id'])
27
+ order.to_alipay_params
28
+ }
29
+ ```
30
+
31
+ In your routes:
32
+
33
+ ```ruby
34
+ map '/midpay/alipay/callback' => 'payment#callback'
35
+ ```
36
+
37
+
38
+ Type following URL in your browser
39
+
40
+ ```
41
+ http://DOMAIN.COM/midpay/alipay?order_id=123
42
+ ```
43
+
44
+ And your broswer will navigate to Alipay Casher page,
45
+
46
+ Then, handle the callback phase in your controller
47
+
48
+ ```ruby
49
+ def callback
50
+ strategy = request.env['midpay.strategy']
51
+ callback_data = request.env['midpay.callback'] # A Midpay::PaymentInfo object
52
+ if callback_data.success?
53
+ order = Order.find(callback_data.raw_data[:out_trade_no])
54
+ order.paid!
55
+ end
56
+
57
+ p callback_data.to_hash
58
+ p callback_data.to_json
59
+ # ....
60
+ end
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork it
66
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
67
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
68
+ 4. Push to the branch (`git push origin my-new-feature`)
69
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module Midpay
2
+ module Alipay
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,52 @@
1
+ require 'midpay'
2
+ module Midpay
3
+ module Strategies
4
+ class Alipay
5
+ require 'uri'
6
+ require 'net/http'
7
+
8
+ GATEWAY = "https://www.alipay.com/cooperate/gateway.do"
9
+ NOTIFY_VERIFY_GATEWAY = "http://notify.alipay.com/trade/notify_query.do"
10
+
11
+ include ::Midpay::Strategy
12
+
13
+ set :sign_type, "MD5"
14
+ set :_input_charset, "utf-8"
15
+
16
+ def request_phase response
17
+ response.write("You are being redirected to Alipay......")
18
+ response.redirect ali_request_url
19
+ end
20
+
21
+ def callback_phase pi
22
+ raise Midpay::Errors::InvalidSignature.new unless request_params[:sign] == request_params.sign(:sign_type){|hsh| ali_sign_str(hsh) }
23
+ pi.extra = { :notify_verified => notify_verified?(request_params[:notify_id]) }
24
+ pi.raw_data = request_params.symbolize_keys
25
+ pi.success = (pi.raw_data['is_success'] == 'T' || pi.raw_data['trade_status'] == "TRADE_FINISHED")
26
+ end
27
+
28
+ def ali_request_url
29
+ GATEWAY + "?" + ali_request_params.to_query
30
+ end
31
+
32
+ def ali_request_params
33
+ params = request_data
34
+ params.merge_if!(arguments.merge(partner: options.app_key, seller_id: options.app_key, notify_url: callback_url, return_url: callback_url))
35
+ params.sign!(:sign, :sign_type) do |hsh|
36
+ ali_sign_str(hsh)
37
+ end
38
+ end
39
+
40
+ def ali_sign_str hash
41
+ hash.reject{|k,v| [:sign_type, :sign].include?(k.to_sym) || v.to_s.empty? }.collect{|k,v| "#{k}=#{v}" }.sort.join("&") + options.app_secret
42
+ end
43
+
44
+ def notify_verified? id
45
+ url = NOTIFY_VERIFY_GATEWAY + "?service=notify_verify&partner=#{options.app_key}&notify_id=#{id}"
46
+ body = Net::HTTP.get_response(URI.parse(url)).body.gsub(/\s+/,'').downcase rescue nil
47
+ body == 'true'
48
+ end
49
+ end
50
+ end
51
+ end
52
+ ::Midpay[:alipay] = ::Midpay::Strategies::Alipay
@@ -0,0 +1,5 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+
4
+ require 'midpay/alipay/version'
5
+ require 'midpay/strategies/alipay'
@@ -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 'midpay/alipay/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "midpay_alipay"
8
+ spec.version = Midpay::Alipay::VERSION
9
+ spec.authors = ["xixilive"]
10
+ spec.email = ["xixilive@gmail.com"]
11
+ spec.description = %q{Payment strategy for Alipay(https://alipay.com) base on Midpay Middleware.}
12
+ spec.summary = %q{Payment strategy for Alipay(https://alipay.com) base on Midpay Middleware.}
13
+ spec.homepage = "https://github.com/xixilive/midpay_alipay"
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_dependency "midpay"
22
+ spec.add_development_dependency "bundler", "~> 1.3"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ describe Midpay::Strategies::Alipay do
4
+ include Rack::Test::Methods
5
+
6
+ let(:inner_app){
7
+ lambda { |env| [200, {"Content-Type" => "text/html"}, ["body"]] }
8
+ }
9
+
10
+ let(:app){
11
+ Midpay::Strategies::Alipay.new(inner_app,
12
+ :app_key => "APPKEY",
13
+ :app_secret => "APPSECRET",
14
+ :request_params_proc => Proc.new{|params|
15
+ {
16
+ :service => "trade_create_by_buyer",
17
+ :seller_email => "seller@email.com",
18
+ :out_trade_no => params['order_id'],
19
+ :subject => "subject",
20
+ :payment_type => '1',
21
+ :logistics_type=>"EXPRESS",
22
+ :logistics_fee => 0,#delivery_amount,
23
+ :logistics_payment => "BUYER_PAY",
24
+ :price => 1, #item_amount,
25
+ :quantity=> 1,
26
+ :body => "body",
27
+ :discount=> 0, #-discount,
28
+ :total_fee=> 1, #total_price,
29
+ :show_url=> "http://127.0.0.1",
30
+ :receive_name => "recipient",
31
+ :receive_address => "address",
32
+ :receive_zip => "post_code",
33
+ :receive_phone => "phone"
34
+ }
35
+ }
36
+ )
37
+ }
38
+
39
+ let(:query){
40
+ 'body=%E5%86%9C%E4%BA%BA%E7%89%B9%E4%BE%9B-%E6%96%B0%E6%98%A5%E7%A4%BC%E7%9B%92%E7%B3%BB%E5%88%97(%E6%B7%B1%E8%89%B2%E7%B3%BB%C2%B7%E5%86%AC%E4%BB%A4%E8%A1%A5%E7%9B%8A%E9%BB%91%E5%93%81%E8%87%BB%E9%80%89)&buyer_email=hxplove01%40126.com&buyer_id=2088702028771600&discount=0.00&gmt_create=2013-07-19+16%3A18%3A25&gmt_logistics_modify=2013-07-19+16%3A18%3A25&gmt_payment=2013-07-19+16%3A21%3A19&is_success=T&is_total_fee_adjust=N&logistics_fee=0.00&logistics_payment=BUYER_PAY&logistics_type=EXPRESS&notify_id=RqPnCoPT3K9%252Fvwbh3I72LfQlir9TXixOvcNML3lAnszC8nxIDtv6GPlhvNbRfSNHf4aS&notify_time=2013-07-19+16%3A21%3A25&notify_type=trade_status_sync&out_trade_no=51da5b6bbcc126678b000006&payment_type=1&price=0.01&quantity=1&receive_address=%E5%8C%97%E4%BA%AC%2C+%E5%8C%97%E4%BA%AC%E5%B8%82%2C+%E6%9C%9D%E9%98%B3%E5%8C%BA%2C+%E5%8C%97%E4%BA%AC%E6%9C%9D%E9%98%B3%E5%8C%BA%E5%9B%A2%E7%BB%93%E6%B9%96%E5%8C%97%E9%87%8C8%E5%8F%B7%E6%A5%BC5%E9%97%A8103%E5%AE%A4%2C+100011%2C+%E4%B8%A5%E6%96%87%2C+13671297531&receive_name=%E4%B8%A5%E6%96%87&receive_phone=13671297531&receive_zip=100011&seller_actions=SEND_GOODS&seller_email=info%40baoshutech.com&seller_id=2088401331137232&subject=%E5%86%9C%E4%BA%BA%E7%89%B9%E4%BE%9B-%E6%96%B0%E6%98%A5%E7%A4%BC%E7%9B%92%E7%B3%BB%E5%88%97(%E6%B7%B1%E8%89%B2%E7%B3%BB%C2%B7%E5%86%AC%E4%BB%A4%E8%A1%A5%E7%9B%8A%E9%BB%91%E5%93%81%E8%87%BB%E9%80%89)&total_fee=0.01&trade_no=2013071958435860&trade_status=WAIT_SELLER_SEND_GOODS&use_coupon=N&sign=402b1a2b5a3b29430379b9961130a1bd&sign_type=MD5'
41
+ }
42
+
43
+ let(:query_data){
44
+ Hash[::Rack::Utils.unescape(query).split("&").collect{|i| i.split("=")}]
45
+ }
46
+
47
+ it 'on request phase' do
48
+ get '/midpay/alipay?order_id=823456781235689'
49
+ expect(last_response.headers['Location']).to eq("https://www.alipay.com/cooperate/gateway.do?_input_charset=utf-8&body=body&discount=0&logistics_fee=0&logistics_payment=BUYER_PAY&logistics_type=EXPRESS&notify_url=http%3A%2F%2Fexample.org%2Fmidpay%2Falipay%2Fcallback&out_trade_no=823456781235689&partner=APPKEY&payment_type=1&price=1&quantity=1&receive_address=address&receive_name=recipient&receive_phone=phone&receive_zip=post_code&return_url=http%3A%2F%2Fexample.org%2Fmidpay%2Falipay%2Fcallback&seller_email=seller%40email.com&seller_id=APPKEY&service=trade_create_by_buyer&show_url=http%3A%2F%2F127.0.0.1&sign=a76efa33d1bbbd4275422323173ae142&sign_type=MD5&subject=subject&total_fee=1")
50
+ end
51
+
52
+ it 'on callback phase' do
53
+ get "/midpay/alipay/callback?#{query}"
54
+ expect(last_request.env['midpay.strategy'].class).to eq(app.class)
55
+ expect(last_request.env['midpay.callback'].pay).to eq("alipay")
56
+ expect(last_request.env['midpay.callback'].raw_data).to eq(query_data)
57
+ expect(last_request.env['midpay.callback'].success?).to be_true
58
+ end
59
+ end
@@ -0,0 +1,37 @@
1
+ # encoding:utf-8
2
+ require 'rspec'
3
+ require 'rspec/autorun'
4
+ require 'rack/test'
5
+ require 'midpay_alipay'
6
+
7
+ if %W[on yes true 1].include?(ENV['RCOV'])
8
+ require 'simplecov'
9
+ require 'simplecov-rcov'
10
+ class SimpleCov::Formatter::MergedFormatter
11
+ def format(result)
12
+ SimpleCov::Formatter::HTMLFormatter.new.format(result)
13
+ SimpleCov::Formatter::RcovFormatter.new.format(result)
14
+ end
15
+ end
16
+ SimpleCov.formatter = SimpleCov::Formatter::MergedFormatter
17
+
18
+ SimpleCov.start "test_frameworks" do
19
+ #add_filter ''
20
+ end
21
+ end
22
+
23
+ Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
24
+
25
+ RSpec.configure do |c|
26
+ c.before(:suite) do
27
+
28
+ end
29
+
30
+ c.before(:each) do
31
+
32
+ end
33
+
34
+ c.after(:suite) do
35
+
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: midpay_alipay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - xixilive
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: midpay
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
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: '1.3'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
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
+ description: Payment strategy for Alipay(https://alipay.com) base on Midpay Middleware.
63
+ email:
64
+ - xixilive@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - .rspec
71
+ - .rvmrc
72
+ - Gemfile
73
+ - LICENSE.txt
74
+ - README.md
75
+ - Rakefile
76
+ - lib/midpay/alipay/version.rb
77
+ - lib/midpay/strategies/alipay.rb
78
+ - lib/midpay_alipay.rb
79
+ - midpay_alipay.gemspec
80
+ - spec/midpay/strategies/alipay_spec.rb
81
+ - spec/spec_helper.rb
82
+ homepage: https://github.com/xixilive/midpay_alipay
83
+ licenses:
84
+ - MIT
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ! '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 1.8.25
104
+ signing_key:
105
+ specification_version: 3
106
+ summary: Payment strategy for Alipay(https://alipay.com) base on Midpay Middleware.
107
+ test_files:
108
+ - spec/midpay/strategies/alipay_spec.rb
109
+ - spec/spec_helper.rb