Module: NewAlipay

Defined in:
lib/new_alipay.rb,
lib/new_alipay/version.rb,
lib/new_alipay/batch_trans.rb

Defined Under Namespace

Modules: BatchTrans

Constant Summary collapse

VERSION =
"0.2.3"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.account_nameObject

Returns the value of attribute account_name.



10
11
12
# File 'lib/new_alipay.rb', line 10

def 
  @account_name
end

.alipay_public_key_pathObject

Returns the value of attribute alipay_public_key_path.



11
12
13
# File 'lib/new_alipay.rb', line 11

def alipay_public_key_path
  @alipay_public_key_path
end

.keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/new_alipay.rb', line 10

def key
  @key
end

.partnerObject

Returns the value of attribute partner.



10
11
12
# File 'lib/new_alipay.rb', line 10

def partner
  @partner
end

.rsa_private_key_pathObject

Returns the value of attribute rsa_private_key_path.



11
12
13
# File 'lib/new_alipay.rb', line 11

def rsa_private_key_path
  @rsa_private_key_path
end

.seller_emailObject

Returns the value of attribute seller_email.



10
11
12
# File 'lib/new_alipay.rb', line 10

def seller_email
  @seller_email
end

Class Method Details

.mobile_trade_create(config) ⇒ Object

移动支付:创建支付订单



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/new_alipay.rb', line 16

def mobile_trade_create(config)
  parameters = {
      "partner" => "\"#{self.partner}\"",
      "seller_id" => "\"#{self.seller_email}\"",
      "out_trade_no" => "\"#{config[:out_trade_no]}\"",
      "subject" => "\"#{config[:subject]}\"",
      "body" => "\"#{config[:body]}\"",
      "total_fee" => "\"#{config[:total_fee]}\"",
      "notify_url" => "\"#{config[:notify_url]}\"",
      "service" => "\"mobile.securitypay.pay\"",
      "payment_type" => "1",
      "_input_charset" => "\"utf-8\"",
      "it_b_pay" => "30m",
      "return_url" => "\"m.alipay.com\""
  }
  signing_str = parameters.inject([]) { |memo, (key, v)| memo << "#{key}=#{v}"; memo }.join("&")
  rsa_sign(signing_str)
end

.rsa_sign(signing_str) ⇒ Object

rsa签名



50
51
52
53
54
55
56
57
58
59
# File 'lib/new_alipay.rb', line 50

def rsa_sign(signing_str)
  #读取私钥文件
  private_key_content = File.read(self.rsa_private_key_path || "./config/alipay/quick/key/rsa_private_key.pem")

  private_key = OpenSSL::PKey::RSA.new private_key_content
  digest = OpenSSL::Digest::SHA1.new
  sign = Base64::encode64(private_key.sign(digest, signing_str))
  signing_str+= "&sign=\"#{url_encode(sign)}\""
  signing_str+ "&sign_type=\"RSA\""
end

.url_encode(s) ⇒ Object



61
62
63
# File 'lib/new_alipay.rb', line 61

def url_encode(s)
  URI.escape(s.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

.verify_rsa?(params) ⇒ Boolean

验证是否签名成功

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/new_alipay.rb', line 36

def verify_rsa?(params)

  sym_key_params = params.inject({}) { |memo, (key, v)| memo[key.to_s.to_sym]=v; memo }
  org_sign = sym_key_params[:sign]
  sym_key_params = sym_key_params.reject { |key, v| [:sign_type, :sign].include? key }
  sym_key_params = sym_key_params.inject([]) { |memo, (key, v)| memo << "#{key}=#{v}"; memo }
  sym_key_params = sym_key_params.sort! { |m, n| m.to_s <=> n.to_s }
  signing_str = sym_key_params.join("&")

  rsa = OpenSSL::PKey::RSA.new File.read(self.alipay_public_key_path || "./config/alipay/quick/key/alipay_public_key.pem")
  rsa.verify('sha1', Base64::decode64(org_sign), signing_str)
end