activemerchant_alipay 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 [Lkagva Ochirkhuyag]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ h1. Activemerchant integration for Alipay
2
+
3
+ This plugin is an active_merchant integration for chinese payment platform Alipay
4
+
5
+ h2. Install
6
+
7
+ Install as a gem:
8
+
9
+ <pre><code>
10
+ sudo gem install activemerchant
11
+ sudo gem install activemerchant_alipay
12
+ </code></pre>
13
+
14
+ h2. Use
15
+
16
+
17
+ Copyright (c) 2012 [Lkhagva Ochirkhuyag], released under the MIT license
@@ -0,0 +1,25 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'jeweler'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test activemerchant_alipay.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ Jeweler::Tasks.new do |gemspec|
17
+ gemspec.name = "activemerchant_alipay"
18
+ gemspec.summary = "Activemerchant integration for Alipay"
19
+ gemspec.description = "Activemerchant patch for china online payment platform alipay"
20
+ gemspec.email = "ochkoo@gmail.com"
21
+ gemspec.homepage = "http://github.com/ochko/activemerchant_alipay"
22
+ gemspec.authors = ["Lkhagva Ochirkhuyag"]
23
+ gemspec.add_dependency 'activemerchant', '>= 1.20.0'
24
+ end
25
+ Jeweler::GemcutterTasks.new
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "activemerchant_alipay"
8
+ s.version = "0.0.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Lkhagva Ochirkhuyag"]
12
+ s.date = "2012-02-22"
13
+ s.description = "Activemerchant patch for china online payment platform alipay"
14
+ s.email = "ochkoo@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "README.textile"
17
+ ]
18
+ s.files = [
19
+ "MIT-LICENSE",
20
+ "README.textile",
21
+ "Rakefile",
22
+ "VERSION",
23
+ "activemerchant_alipay.gemspec",
24
+ "init.rb",
25
+ "lib/active_merchant/billing/integrations/alipay.rb",
26
+ "lib/active_merchant/billing/integrations/alipay/common.rb",
27
+ "lib/active_merchant/billing/integrations/alipay/helper.rb",
28
+ "lib/active_merchant/billing/integrations/alipay/notification.rb",
29
+ "lib/active_merchant/billing/integrations/alipay/return.rb",
30
+ "lib/activemerchant_alipay.rb",
31
+ "test/activemerchant_alipay_test.rb",
32
+ "test/test_helper.rb"
33
+ ]
34
+ s.homepage = "http://github.com/ochko/activemerchant_alipay"
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = "1.8.15"
37
+ s.summary = "Activemerchant integration for Alipay"
38
+
39
+ if s.respond_to? :specification_version then
40
+ s.specification_version = 3
41
+
42
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
43
+ s.add_runtime_dependency(%q<activemerchant>, [">= 1.20.0"])
44
+ else
45
+ s.add_dependency(%q<activemerchant>, [">= 1.20.0"])
46
+ end
47
+ else
48
+ s.add_dependency(%q<activemerchant>, [">= 1.20.0"])
49
+ end
50
+ end
51
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # Include hook code here
2
+ require 'activemerchant_alipay'
@@ -0,0 +1,39 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ module Integrations #:nodoc:
4
+ module Alipay
5
+ autoload :Common, File.dirname(__FILE__) + '/alipay/common'
6
+ autoload :Helper, File.dirname(__FILE__) + '/alipay/helper'
7
+ autoload :Return, File.dirname(__FILE__) + '/alipay/return'
8
+ autoload :Notification, File.dirname(__FILE__) + '/alipay/notification'
9
+
10
+ # Set this if you want to change the Alipay test url
11
+ mattr_accessor :test_url
12
+
13
+ # Overwrite this if you want to change the Alipay production url
14
+ mattr_accessor :production_url
15
+ self.production_url = 'https://www.alipay.com/cooperate/gateway.do'
16
+
17
+ def self.service_url
18
+ mode = ActiveMerchant::Billing::Base.integration_mode
19
+ case mode
20
+ when :production
21
+ self.production_url
22
+ when :test
23
+ self.test_url || self.production_url
24
+ else
25
+ raise StandardError, "Integration mode set to an invalid value: #{mode}"
26
+ end
27
+ end
28
+
29
+ def self.notification(post, options = {})
30
+ Notification.new(post)
31
+ end
32
+
33
+ def self.return(query_string, options = {})
34
+ Return.new(query_string)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require 'digest/md5'
2
+ require 'cgi'
3
+
4
+ module ActiveMerchant #:nodoc:
5
+ module Billing #:nodoc:
6
+ module Integrations #:nodoc:
7
+ module Alipay
8
+ module Common
9
+ def signed?
10
+ method = @params.delete("sign_type")
11
+ sign = @params.delete("sign")
12
+
13
+ if 'MD5' == method && sign.present?
14
+ sign.downcase == signature(@params)
15
+ else
16
+ false
17
+ end
18
+ end
19
+
20
+ def signature(params)
21
+ query = params.sort.collect do |s|
22
+ "#{s[0]}=#{s[0] == "notify_id" ? s[1] : CGI.unescape(s[1])}"
23
+ end
24
+ Digest::MD5.hexdigest(query.join("&") + KEY)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,87 @@
1
+ require 'cgi'
2
+ require 'digest/md5'
3
+
4
+ module ActiveMerchant #:nodoc:
5
+ module Billing #:nodoc:
6
+ module Integrations #:nodoc:
7
+ module Alipay
8
+ class Helper < ActiveMerchant::Billing::Integrations::Helper
9
+ include Common
10
+
11
+ CREATE_DIRECT_PAY_BY_USER = 'create_direct_pay_by_user'
12
+ CREATE_PARTNER_TRADE_BY_BUYER = 'create_partner_trade_by_buyer'
13
+ TRADE_CREATE_BY_BUYER = 'trade_create_by_buyer'
14
+ CREATE_FOREIGN_TRADE = 'create_forex_trade'
15
+
16
+ def initialize(order, account, options = {})
17
+ super
18
+ end
19
+
20
+ def sign
21
+ add_field('sign', signature(@fields))
22
+ add_field('sign_type', 'MD5')
23
+ nil
24
+ end
25
+
26
+ ###################################################
27
+ # common
28
+ ###################################################
29
+ mapping :account, 'partner'
30
+ mapping :order, 'out_trade_no'
31
+ mapping :seller, :email => 'seller_email',
32
+ :id => 'seller_id'
33
+ mapping :buyer, :email => 'buyer_email',
34
+ :id => 'buyer_id'
35
+ mapping :notify_url, 'notify_url'
36
+ mapping :return_url, 'return_url'
37
+ mapping :show_url, 'show_url'
38
+ mapping :body, 'body'
39
+ mapping :subject, 'subject'
40
+ mapping :charset, '_input_charset'
41
+ mapping :service, 'service'
42
+ mapping :payment_type, 'payment_type'
43
+ mapping :extra_common_param, 'extra_common_param'
44
+ mapping :currency, 'currency'
45
+
46
+ #################################################
47
+ # create direct pay by user
48
+ #################################################
49
+ mapping :total_fee, 'total_fee'
50
+ mapping :paymethod, 'paymethod'
51
+ mapping :defaultbank, 'defaultbank'
52
+ mapping :royalty, :type => 'royalty_type',
53
+ :parameters => 'royalty_parameters'
54
+ mapping :it_b_pay, 'it_b_pay'
55
+
56
+ #################################################
57
+ # create partner trade by buyer and trade create by user
58
+ #################################################
59
+ mapping :price, 'price'
60
+ mapping :quantity, 'quantity'
61
+ mapping :discount, 'discount'
62
+ ['', '_1', '_2', '_3'].each do |postfix|
63
+ self.class_eval <<-EOF
64
+ mapping :logistics#{postfix}, :type => 'logistics_type#{postfix}',
65
+ :fee => 'logistics_fee#{postfix}',
66
+ :payment => 'logistics_payment#{postfix}'
67
+ EOF
68
+ end
69
+ mapping :receive, :name => 'receive_name',
70
+ :address => 'receive_address',
71
+ :zip => 'receive_zip',
72
+ :phone => 'receive_phone',
73
+ :mobile => 'receive_mobile'
74
+ mapping :t_b_pay, 't_b_pay'
75
+ mapping :t_s_send_1, 't_s_send_1'
76
+ mapping :t_s_send_2, 't_s_send_2'
77
+
78
+ #################################################
79
+ # create partner trade by buyer
80
+ #################################################
81
+ mapping :agent, 'agent'
82
+ mapping :buyer_msg, 'buyer_msg'
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,87 @@
1
+ require 'cgi'
2
+
3
+ module ActiveMerchant #:nodoc:
4
+ module Billing #:nodoc:
5
+ module Integrations #:nodoc:
6
+ module Alipay
7
+ class Notification < ActiveMerchant::Billing::Integrations::Notification
8
+ include Common
9
+
10
+ def complete?
11
+ trade_status == "TRADE_FINISHED"
12
+ end
13
+
14
+ def success?
15
+ trade_status == 'TRADE_SUCCESS'
16
+ end
17
+
18
+ def pending?
19
+ trade_status == 'WAIT_BUYER_PAY'
20
+ end
21
+
22
+ def acknowledge
23
+ raise StandardError.new("Faulty alipay response: #{params}") unless signed?
24
+ true
25
+ end
26
+
27
+ def status
28
+ trade_status
29
+ end
30
+
31
+ ['extra_common_param', 'notify_type', 'notify_id', 'out_trade_no', 'trade_no', 'payment_type', 'subject', 'body',
32
+ 'seller_email', 'seller_id', 'buyer_email', 'buyer_id', 'logistics_type', 'logistics_payment',
33
+ 'receive_name', 'receive_address', 'receive_zip', 'receive_phone', 'receive_mobile'].each do |param|
34
+ self.class_eval <<-EOF
35
+ def #{param}
36
+ params['#{param}']
37
+ end
38
+ EOF
39
+ end
40
+
41
+ ['price', 'discount', 'quantity', 'total_fee', 'coupon_discount', 'logistics_fee'].each do |param|
42
+ self.class_eval <<-EOF
43
+ def #{param}
44
+ params['#{param}']
45
+ end
46
+ EOF
47
+ end
48
+
49
+ ['trade_status', 'refund_status', 'logistics_status'].each do |param|
50
+ self.class_eval <<-EOF
51
+ def #{param}
52
+ params['#{param}']
53
+ end
54
+ EOF
55
+ end
56
+
57
+ ['notify_time', 'gmt_create', 'gmt_payment', 'gmt_close', 'gmt_refund', 'gmt_send_goods', 'gmt_logistics_modify'].each do |param|
58
+ self.class_eval <<-EOF
59
+ def #{param}
60
+ Time.parse params['#{param}']
61
+ end
62
+ EOF
63
+ end
64
+
65
+ ['use_coupon', 'is_total_fee_adjust'].each do |param|
66
+ self.class_eval <<-EOF
67
+ def #{param}?
68
+ 'T' == params['#{param}']
69
+ end
70
+ EOF
71
+ end
72
+
73
+ private
74
+
75
+ # Take the posted data and move the relevant data into a hash
76
+ def parse(post)
77
+ @raw = post
78
+ for line in post.split('&')
79
+ key, value = *line.scan( %r{^(\w+)\=(.*)$} ).flatten
80
+ params[key] = CGI.unescape(value || '')
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,41 @@
1
+ module ActiveMerchant #:nodoc:
2
+ module Billing #:nodoc:
3
+ module Integrations #:nodoc:
4
+ module Alipay
5
+ class Return < ActiveMerchant::Billing::Integrations::Return
6
+ include Common
7
+
8
+ def success?
9
+ @params['is_success'] == 'T'
10
+ end
11
+
12
+ def message
13
+ @message
14
+ end
15
+
16
+ def order
17
+ @params["out_trade_no"]
18
+ end
19
+
20
+ def amount
21
+ @params["total_fee"]
22
+ end
23
+
24
+ def notify_id
25
+ @params["notify_id"]
26
+ end
27
+
28
+ def acknowledge
29
+ unless signed?
30
+ @message = "Alipay Error: ILLEGAL_SIGN"
31
+ return false
32
+ end
33
+
34
+ true
35
+ end
36
+
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1 @@
1
+ require 'active_merchant/billing/integrations/alipay'
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class ActivemerchantAlipay < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activemerchant_alipay
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Lkhagva Ochirkhuyag
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-02-22 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activemerchant
16
+ requirement: &70157393443860 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 1.20.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70157393443860
25
+ description: Activemerchant patch for china online payment platform alipay
26
+ email: ochkoo@gmail.com
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files:
30
+ - README.textile
31
+ files:
32
+ - MIT-LICENSE
33
+ - README.textile
34
+ - Rakefile
35
+ - VERSION
36
+ - activemerchant_alipay.gemspec
37
+ - init.rb
38
+ - lib/active_merchant/billing/integrations/alipay.rb
39
+ - lib/active_merchant/billing/integrations/alipay/common.rb
40
+ - lib/active_merchant/billing/integrations/alipay/helper.rb
41
+ - lib/active_merchant/billing/integrations/alipay/notification.rb
42
+ - lib/active_merchant/billing/integrations/alipay/return.rb
43
+ - lib/activemerchant_alipay.rb
44
+ - test/activemerchant_alipay_test.rb
45
+ - test/test_helper.rb
46
+ homepage: http://github.com/ochko/activemerchant_alipay
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.15
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Activemerchant integration for Alipay
70
+ test_files: []