lib/alipay/service.rb in alipay-0.5.0 vs lib/alipay/service.rb in alipay-0.6.0.beta1
- old
+ new
@@ -1,7 +1,6 @@
require 'cgi'
-require 'open-uri'
module Alipay
module Service
GATEWAY_URL = 'https://mapi.alipay.com/gateway.do'
@@ -16,11 +15,11 @@
'payment_type' => '1'
}.merge(Utils.stringify_keys(options))
check_required_options(options, CREATE_PARTNER_TRADE_BY_BUYER_REQUIRED_OPTIONS)
- "#{GATEWAY_URL}?#{query_string(options)}"
+ request_uri(options).to_s
end
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 )
# alipaydualfun
def self.trade_create_by_buyer_url(options = {})
@@ -32,11 +31,11 @@
'payment_type' => '1'
}.merge(Utils.stringify_keys(options))
check_required_options(options, TRADE_CREATE_BY_BUYER_REQUIRED_OPTIONS)
- "#{GATEWAY_URL}?#{query_string(options)}"
+ request_uri(options).to_s
end
CREATE_DIRECT_PAY_BY_USER_REQUIRED_OPTIONS = %w( service partner _input_charset out_trade_no subject payment_type seller_email )
# direct
def self.create_direct_pay_by_user_url(options)
@@ -52,11 +51,11 @@
if options['total_fee'].nil? and (options['price'].nil? || options['quantity'].nil?)
warn("Ailpay Warn: total_fee or (price && quantiry) must have one")
end
- "#{GATEWAY_URL}?#{query_string(options)}"
+ request_uri(options).to_s
end
CREATE_REFUND_URL_REQUIRED_OPTIONS = %w( batch_no data notify_url )
# 支付宝即时到帐批量退款有密接口(此为异步接口,有密指通过此接口打开 url 后需要用户输入支付宝的支付密码进行退款)
def self.create_refund_url(options)
@@ -77,11 +76,11 @@
'refund_date' => Time.now.strftime('%Y-%m-%d %H:%M:%S'), # 申请退款时间
'batch_num' => data.size, # 总笔数
'detail_data' => detail_data # 转换后的单笔数据集字符串
}.merge(options)
- "#{GATEWAY_URL}?#{query_string(options)}"
+ request_uri(options).to_s
end
CREATE_FOREX_SINGLE_REFUND_URL_REQUIRED_OPTIONS = %w( out_return_no out_trade_no return_amount currency reason )
# 支付宝境外收单单笔退款接口
# out_return_no 退款流水单号
@@ -96,11 +95,11 @@
'gmt_return' => Time.now.strftime('%Y%m%d%H%M%S')
}.merge(Utils.stringify_keys(options))
check_required_options(options, CREATE_FOREX_SINGLE_REFUND_URL_REQUIRED_OPTIONS)
- "#{GATEWAY_URL}?#{query_string(options)}"
+ request_uri(options).to_s
end
SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_OPTIONS = %w( service partner _input_charset trade_no logistics_name )
def self.send_goods_confirm_by_platform(options)
options = {
@@ -113,11 +112,11 @@
if options['transport_type'].nil? and options['create_transport_type'].nil?
warn("Ailpay Warn: transport_type or create_transport_type must have one")
end
- open("#{GATEWAY_URL}?#{query_string(options)}").read
+ Net::HTTP.get(request_uri(options))
end
CREATE_FOREX_TRADE_REQUIRED_OPTIONS = %w(service partner _input_charset notify_url subject out_trade_no currency total_fee)
def self.create_forex_trade(options)
options = {
@@ -127,11 +126,11 @@
'seller_email' => Alipay.seller_email
}.merge(Utils.stringify_keys(options))
check_required_options(options, CREATE_FOREX_TRADE_REQUIRED_OPTIONS)
- "#{GATEWAY_URL}?#{query_string(options)}"
+ request_uri(options).to_s
end
CLOSE_TRADE_REQUIRED_OPTIONS = %w( service partner _input_charset)
CLOSE_TRADE_REQUIRED_OPTIONAL_OPTIONS = %w( trade_no out_order_no )
def self.close_trade(options)
@@ -142,11 +141,11 @@
}.merge(Utils.stringify_keys(options))
check_required_options(options, CLOSE_TRADE_REQUIRED_OPTIONS)
check_optional_options(options, CLOSE_TRADE_REQUIRED_OPTIONAL_OPTIONS)
- open("#{GATEWAY_URL}?#{query_string(options)}").read
+ Net::HTTP.get(request_uri(options))
end
SINGLE_TRADE_QUERY_OPTIONS = %w( service partner _input_charset)
SINGLE_TRADE_QUERY_OPTIONAL_OPTIONS = %w( trade_no out_trade_no )
def self.single_trade_query(options)
@@ -157,16 +156,21 @@
}.merge(Utils.stringify_keys(options))
check_required_options(options, SINGLE_TRADE_QUERY_OPTIONS)
check_optional_options(options, SINGLE_TRADE_QUERY_OPTIONAL_OPTIONS)
- open("#{GATEWAY_URL}?#{query_string(options)}").read
+ Net::HTTP.get(request_uri(options))
end
- def self.query_string(options)
- options.merge('sign_type' => 'MD5', 'sign' => Alipay::Sign.generate(options)).map do |key, value|
- "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
- end.join('&')
+ def self.request_uri(options)
+ uri = URI(GATEWAY_URL)
+ uri.query = URI.encode_www_form(sign_params(options))
+ uri
+ end
+
+ def self.sign_params(params)
+ params = params.merge('sign_type' => Alipay.sign_type) if params['sign_type'].nil?
+ params.merge('sign' => Alipay::Sign.generate(params))
end
def self.check_required_options(options, names)
return if !Alipay.debug_mode?