Class: Allpay::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/allpay.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/allpay.rb', line 15

def initialize(options = {})

  if options[:merchant_id].to_i == 2000132
    @merchant_id = 2000132
    @key = "ejCk326UnaZWKisg"
    @iv = "q9jcZX8Ib9LM8wYk"
    @api = 'pay-stage.allpay.com.tw'
  else
    @merchant_id = options[:merchant_id]
    @key = options[:key]
    @iv = options[:iv]
    @api = 'pay.allpay.com.tw'
  end
end

Instance Attribute Details

#all_ivObject

Returns the value of attribute all_iv.



13
14
15
# File 'lib/allpay.rb', line 13

def all_iv
  @all_iv
end

#all_keyObject

Returns the value of attribute all_key.



13
14
15
# File 'lib/allpay.rb', line 13

def all_key
  @all_key
end

#apiObject

Returns the value of attribute api.



13
14
15
# File 'lib/allpay.rb', line 13

def api
  @api
end

#httpObject

Returns the value of attribute http.



13
14
15
# File 'lib/allpay.rb', line 13

def http
  @http
end

#ivObject

Returns the value of attribute iv.



13
14
15
# File 'lib/allpay.rb', line 13

def iv
  @iv
end

#keyObject

Returns the value of attribute key.



13
14
15
# File 'lib/allpay.rb', line 13

def key
  @key
end

#merchant_idObject

Returns the value of attribute merchant_id.



13
14
15
# File 'lib/allpay.rb', line 13

def merchant_id
  @merchant_id
end

#restObject

Returns the value of attribute rest.



13
14
15
# File 'lib/allpay.rb', line 13

def rest
  @rest
end

Class Method Details

.decrypt_data(key, iv, data) ⇒ Object



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

def self.decrypt_data(key, iv, data)
  cipher = OpenSSL::Cipher::AES128.new(:CBC).decrypt
  cipher.key = key
  cipher.iv = iv
  Client.parse_xml(cipher.update(Base64.decode64(data.gsub(" ","+"))) + cipher.final)
end

.encrypt_data(key, iv, data) ⇒ Object



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

def self.encrypt_data(key, iv, data)
  data = Gyoku.xml({"Root" => { "Data" => data}})
  cipher = OpenSSL::Cipher::AES128.new(:CBC).encrypt
  cipher.key = key
  cipher.iv = iv

  Base64.encode64(cipher.update(data) + cipher.final).gsub("\n","")
end

.parse_xml(data) ⇒ Object



74
75
76
# File 'lib/allpay.rb', line 74

def self.parse_xml(data)
  Nori.new.parse(data)["Root"]["Data"]
end

Instance Method Details

#get_vaccount(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/allpay.rb', line 30

def get_vaccount(options = {})
  has_key?
  enc_data = Client.encrypt_data(@key, @iv, {
    'MerchantID' => merchant_id,
    'MerchantTradeNo' => options[:merchant_trade_no] ||  SecureRandom.hex(10),
    'MerchantTradeDate' => options[:merchant_trade_date] || Time.now.strftime('%Y/%m/%d %H:%M:%S'),
    'TradeAmount' => options[:trade_amount] || 0,
    'ExpireDate' => options[:expire_date] || '7',
    'BankName' => options[:bank_name] || "CHINATRUST",
    'ReplyURL' => options[:reply_url] || "",
    'Remark' => options[:remark] || "",
  })
  # p enc_data
  begin
    result = Client.parse_xml(get('/payment/Srv/gateway', {"MerchantID" => merchant_id, "PaymentType" => "vAccount", "XMLData" => enc_data}))
  rescue => e
    response = e
  end

  [result["RtnCode"].to_i, result]
end

#get_vaccount_callback(xmldata) ⇒ Object



52
53
54
55
56
# File 'lib/allpay.rb', line 52

def get_vaccount_callback(xmldata)
  has_key?
  result = Client.decrypt_data(@key, @iv, xmldata)
  [result["RtnCode"].to_i, result]
end