Class: Onepay::AlipayResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/onepay/response/alipay.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ AlipayResponse

Returns a new instance of AlipayResponse.



8
9
10
11
12
# File 'lib/onepay/response/alipay.rb', line 8

def initialize(params)
  @params = params
  @order_id = params['out_trade_no']
  @total_fee = params['total_fee'].to_f
end

Instance Attribute Details

#order_idObject (readonly)

Returns the value of attribute order_id.



6
7
8
# File 'lib/onepay/response/alipay.rb', line 6

def order_id
  @order_id
end

#total_feeObject (readonly)

Returns the value of attribute total_fee.



6
7
8
# File 'lib/onepay/response/alipay.rb', line 6

def total_fee
  @total_fee
end

Instance Method Details

#fail_responseObject



36
37
38
# File 'lib/onepay/response/alipay.rb', line 36

def fail_response
  'fail'
end

#success_responseObject



32
33
34
# File 'lib/onepay/response/alipay.rb', line 32

def success_response
  'success'
end

#successful?Boolean

Returns:

  • (Boolean)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/onepay/response/alipay.rb', line 14

def successful?
  new_hash = {}
  @params.each do |key, value|
    new_hash[(key.to_s rescue key) || key] = URI.decode(value)
  end

  sign = new_hash.delete('sign')
  new_hash.delete('sign_type')
  new_hash.delete('action')
  new_hash.delete('controller')
  params_string = params_to_string(new_hash)

  p params_string

  rsa = OpenSSL::PKey::RSA.new(Onepay.config['alipay']['public_key'])
  @params['trade_status'] == 'TRADE_SUCCESS' && rsa.verify('sha256', Base64.strict_decode64(sign), params_string)
end