Class: Allpay::Client

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

Constant Summary collapse

PRODUCTION_API_HOST =
'https://payment.allpay.com.tw'.freeze
TEST_API_HOST =
'http://payment-stage.allpay.com.tw'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
16
17
# File 'lib/allpay/client.rb', line 11

def initialize params = {}
  merchant_id = params[:merchant_id]
  hash_key = params[:hash_key]
  hash_iv = params[:hash_iv]
  mode = params[:mode]
  @merchant_id, @hash_key, @hash_iv, @mode = merchant_id, hash_key, hash_iv, mode
end

Instance Attribute Details

#hash_ivObject

Returns the value of attribute hash_iv.



9
10
11
# File 'lib/allpay/client.rb', line 9

def hash_iv
  @hash_iv
end

#hash_keyObject

Returns the value of attribute hash_key.



9
10
11
# File 'lib/allpay/client.rb', line 9

def hash_key
  @hash_key
end

#merchant_idObject

Returns the value of attribute merchant_id.



9
10
11
# File 'lib/allpay/client.rb', line 9

def merchant_id
  @merchant_id
end

#modeObject

Returns the value of attribute mode.



9
10
11
# File 'lib/allpay/client.rb', line 9

def mode
  @mode
end

Instance Method Details

#api_hostObject



19
20
21
22
23
24
25
# File 'lib/allpay/client.rb', line 19

def api_host
  case mode
  when :production then PRODUCTION_API_HOST
  when :test then TEST_API_HOST
  else raise '`mode` is either :test or :production (default)'
  end
end

#make_mac(params = {}) ⇒ Object



27
28
29
30
31
32
# File 'lib/allpay/client.rb', line 27

def make_mac params = {}
  raw = params.sort.map!{|k,v| "#{k}=#{v}"}.join('&')
  padded = "HashKey=#{@hash_key}&#{raw}&HashIV=#{@hash_iv}"
  url_encoded = CGI.escape(padded).downcase!
  Digest::MD5.hexdigest(url_encoded).upcase!
end

#query_period_credit_card_trade_info(merchant_trade_number) ⇒ Object



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

def query_period_credit_card_trade_info merchant_trade_number
  res = request '/Cashier/QueryPeriodCreditCardTradeInfo',
          MerchantTradeNo: merchant_trade_number,
          TimeStamp: Time.now.to_i
  JSON.parse(res.body)
end

#query_trade_info(merchant_trade_number, platform = nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
# File 'lib/allpay/client.rb', line 46

def query_trade_info merchant_trade_number, platform = nil
  params = {
    MerchantTradeNo: merchant_trade_number,
    TimeStamp: Time.now.to_i,
    PlatformID: platform
  }
  params.delete_if{ |k, v| v.nil? }
  res = request '/Cashier/QueryTradeInfo', params
  Hash[res.body.split('&').map!{|i| i.split('=')}]
end

#request(path, params = {}) ⇒ Object



39
40
41
42
43
44
# File 'lib/allpay/client.rb', line 39

def request path, params = {}
  params[:MerchantID] = @merchant_id
  params[:CheckMacValue] = make_mac(params)
  api_url = URI.join(api_host, path)
  Net::HTTP.post_form api_url, params
end

#verify_mac(params = {}) ⇒ Object



34
35
36
37
# File 'lib/allpay/client.rb', line 34

def verify_mac params = {}
  check_mac_value = params[:CheckMacValue]
  make_mac(params.reject{ |k,v| [:CheckMacValue, 'CheckMacValue'].include? k }) == check_mac_value
end