Module: AlipayDualfun

Defined in:
lib/alipay_dualfun.rb

Constant Summary collapse

GATEWAY_URL =
'https://mapi.alipay.com/gateway.do'
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 )
SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_OPTIONS =
%w( service partner _input_charset trade_no logistics_name )

Class Method Summary collapse

Class Method Details

.check_required_options(options, names) ⇒ Object



29
30
31
32
33
# File 'lib/alipay_dualfun.rb', line 29

def self.check_required_options(options, names)
  names.each do |name|
    warn("Ailpay Warn: missing required option: #{name}") unless options.has_key?(name)
  end
end

.generate_sign(params) ⇒ Object



49
50
51
52
53
54
# File 'lib/alipay_dualfun.rb', line 49

def self.generate_sign(params)
  query = params.sort.map do |key, value|
    "#{key}=#{value}"
  end.join('&')
  Digest::MD5.hexdigest("#{query}#{@secret_key}")
end

.notify_verify?(params) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
72
# File 'lib/alipay_dualfun.rb', line 65

def self.notify_verify?(params)
  if verify_sign?(params)
    params = stringify_keys(params)
    open("https://mapi.alipay.com/gateway.do?service=notify_verify&partner=#{@partner}&notify_id=#{CGI.escape params['notify_id'].to_s}").read == 'true'
  else
    false
  end
end

.query_string(options) ⇒ Object



43
44
45
46
47
# File 'lib/alipay_dualfun.rb', line 43

def self.query_string(options)
   options.merge('sign_type' => 'MD5', 'sign' => generate_sign(options)).map do |key, value|
     "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
   end.join('&')
end

.send_goods_confirm_by_platform(options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/alipay_dualfun.rb', line 75

def self.send_goods_confirm_by_platform(options)
  options = {
    'service'        => 'send_goods_confirm_by_platform',
    'transport_type' => 'DIRECT',
    '_input_charset' => 'utf-8'
  }.merge(stringify_keys(options))

  check_required_options(options, SEND_GOODS_CONFIRM_BY_PLATFORM_REQUIRED_OPTIONS)
  open("#{GATEWAY_URL}?#{query_string(options)}").read
end

.stringify_keys(hash) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/alipay_dualfun.rb', line 35

def self.stringify_keys(hash)
  new_hash = {}
  hash.each do |key, value|
    new_hash[(key.to_s rescue key) || key] = value
  end
  new_hash
end

.trade_create_by_buyer_url(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/alipay_dualfun.rb', line 10

def self.trade_create_by_buyer_url(options = {})
  options = {
    'service'        => 'trade_create_by_buyer',
    '_input_charset' => 'utf-8',
    'payment_type'   => '1',
    'logistics_type'    => 'DIRECT',
    'logistics_fee'     => '0',
    'logistics_payment' => 'SELLER_PAY'
  }.merge(stringify_keys(options))

  @secret_key = options['key']
  @partner = options['partner']
  options.delete('key')

  check_required_options(options, TRADE_CREATE_BY_BUYER_REQUIRED_OPTIONS)

  "#{GATEWAY_URL}?#{query_string(options)}"
end

.verify_sign?(params) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
# File 'lib/alipay_dualfun.rb', line 57

def self.verify_sign?(params)
  params = stringify_keys(params)
  params.delete('sign_type')
  sign = params.delete('sign')

  generate_sign(params) == sign
end