Permalink
Browse files

move common code

  • Loading branch information...
1 parent 153f837 commit 37c2aac4d71ed72fdf59a709b43a730579704e68 @ochko committed Feb 21, 2012
@@ -2,9 +2,10 @@ module ActiveMerchant #:nodoc:
module Billing #:nodoc:
module Integrations #:nodoc:
module Alipay
- autoload 'Helper', File.dirname(__FILE__) + '/alipay/helper'
- autoload 'Return', File.dirname(__FILE__) + '/alipay/return'
- autoload 'Notification', File.dirname(__FILE__) + '/alipay/notification'
+ autoload :Common, File.dirname(__FILE__) + '/alipay/common'
+ autoload :Helper, File.dirname(__FILE__) + '/alipay/helper'
+ autoload :Return, File.dirname(__FILE__) + '/alipay/return'
+ autoload :Notification, File.dirname(__FILE__) + '/alipay/notification'
mattr_accessor :service_url
self.service_url = 'https://www.alipay.com/cooperate/gateway.do'
@@ -0,0 +1,30 @@
+require 'digest/md5'
+require 'cgi'
+
+module ActiveMerchant #:nodoc:
+ module Billing #:nodoc:
+ module Integrations #:nodoc:
+ module Alipay
+ module Common
+ def signed?
+ method = @params.delete("sign_type")
+ sign = @params.delete("sign")
+
+ if 'MD5' == method && sign.present?
+ sign.downcase == signature(@params)
+ else
+ false
+ end
+ end
+
+ def signature(params)
+ query = params.sort.collect do |s|
+ "#{s[0]}=#{s[0] == "notify_id" ? s[1] : CGI.unescape(s[1])}"
+ end
+ Digest::MD5.hexdigest(query.join("&") + KEY)
+ end
+ end
+ end
+ end
+ end
+end
@@ -6,11 +6,23 @@ module Billing #:nodoc:
module Integrations #:nodoc:
module Alipay
class Helper < ActiveMerchant::Billing::Integrations::Helper
+ include Common
+
CREATE_DIRECT_PAY_BY_USER = 'create_direct_pay_by_user'
CREATE_PARTNER_TRADE_BY_BUYER = 'create_partner_trade_by_buyer'
TRADE_CREATE_BY_BUYER = 'trade_create_by_buyer'
CREATE_FOREIGN_TRADE = 'create_forex_trade'
+ def initialize(order, account, options = {})
+ super
+ end
+
+ def sign
+ add_field('sign', signature(@fields))
+ add_field('sign_type', 'MD5')
+ nil
+ end
+
###################################################
# common
###################################################
@@ -68,19 +80,6 @@ class Helper < ActiveMerchant::Billing::Integrations::Helper
#################################################
mapping :agent, 'agent'
mapping :buyer_msg, 'buyer_msg'
-
- def initialize(order, account, options = {})
- super
- end
-
- def sign
- add_field('sign',
- Digest::MD5.hexdigest((@fields.sort.collect{|s|s[0]+"="+CGI.unescape(s[1])}).join("&")+KEY)
- )
- add_field('sign_type', 'MD5')
- nil
- end
-
end
end
end
@@ -6,29 +6,29 @@ module Billing #:nodoc:
module Integrations #:nodoc:
module Alipay
class Notification < ActiveMerchant::Billing::Integrations::Notification
- include Sign
+ include Common
def complete?
trade_status == "TRADE_FINISHED"
end
def success?
- trade_status == 'TRADE_FINISHED' || trade_status == 'TRADE_SUCCESS'
+ trade_status == 'TRADE_SUCCESS'
end
def pending?
trade_status == 'WAIT_BUYER_PAY'
end
- def status
- trade_status
- end
-
def acknowledge
- raise StandardError.new("Faulty alipay result: ILLEGAL_SIGN") unless verify_sign
+ raise StandardError.new("Faulty alipay response: #{params}") unless signed?
true
end
+ def status
+ trade_status
+ end
+
['extra_common_param', 'notify_type', 'notify_id', 'out_trade_no', 'trade_no', 'payment_type', 'subject', 'body',
'seller_email', 'seller_id', 'buyer_email', 'buyer_id', 'logistics_type', 'logistics_payment',
'receive_name', 'receive_address', 'receive_zip', 'receive_phone', 'receive_mobile'].each do |param|
@@ -5,7 +5,15 @@ module Billing #:nodoc:
module Integrations #:nodoc:
module Alipay
class Return < ActiveMerchant::Billing::Integrations::Return
- include Sign
+ include Common
+
+ def success?
+ @params['is_success'] == 'T'
+ end
+
+ def message
+ @message
+ end
def order
@params["out_trade_no"]
@@ -19,27 +27,15 @@ def notify_id
@params["notify_id"]
end
- def initialize(query_string)
- super
- end
-
- def success?
- @params['is_success'] == 'T'
- end
-
- def signed
- unless @params["sign"] && verify_sign
+ def acknowledge
+ unless signed?
@message = "Alipay Error: ILLEGAL_SIGN"
return false
end
true
end
- def message
- @message
- end
-
end
end
end
@@ -1,26 +0,0 @@
-require 'digest/md5'
-require 'cgi'
-
-module ActiveMerchant #:nodoc:
- module Billing #:nodoc:
- module Integrations #:nodoc:
- module Alipay
- module Sign
- def verify_sign
- sign_type = @params.delete("sign_type")
- sign = @params.delete("sign")
-
- md5_string = @params.sort.collect do |s|
- unless s[0] == "notify_id"
- s[0]+"="+CGI.unescape(s[1])
- else
- s[0]+"="+s[1]
- end
- end
- Digest::MD5.hexdigest(md5_string.join("&")+KEY) == sign.downcase
- end
- end
- end
- end
- end
-end

0 comments on commit 37c2aac

Please sign in to comment.