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
TEST_OPTIONS =
{
  merchant_id: '2000132',
  hash_key: '5294y06JbISpM5x9',
  hash_iv: 'v77hoKGq4kWxNNIS'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/allpay/client.rb', line 20

def initialize options = {}
  @options = {mode: :production}.merge!(options)
  case @options[:mode]
  when :production
    option_required! :merchant_id, :hash_key, :hash_iv
  when :test
    @options = TEST_OPTIONS.merge(options)
  else
    raise InvalidMode, %Q{option :mode is either :test or :production}
  end
  @options.freeze
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/allpay/client.rb', line 18

def options
  @options
end

Instance Method Details

#api_hostObject



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

def api_host
  case @options[:mode]
  when :production then PRODUCTION_API_HOST
  when :test then TEST_API_HOST
  end
end

#generate_checkout_params(overwrite_params = {}) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/allpay/client.rb', line 60

def generate_checkout_params overwrite_params = {}
  generate_params({
    MerchantTradeDate: Time.now.strftime('%Y/%m/%d %H:%M:%S'),
    MerchantTradeNo: SecureRandom.hex(4),
    PaymentType: 'aio'
  }.merge!(overwrite_params))
end

#generate_params(overwrite_params = {}) ⇒ Object



53
54
55
56
57
58
# File 'lib/allpay/client.rb', line 53

def generate_params overwrite_params = {}
  result = overwrite_params.clone
  result[:MerchantID] = @options[:merchant_id]
  result[:CheckMacValue] = make_mac(result)
  result
end

#make_mac(params = {}) ⇒ Object



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

def make_mac params = {}
  raw = params.sort_by{|k,v|k.downcase}.map!{|k,v| "#{k}=#{v}"}.join('&')
  padded = "HashKey=#{@options[:hash_key]}&#{raw}&HashIV=#{@options[: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



84
85
86
87
88
89
# File 'lib/allpay/client.rb', line 84

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



73
74
75
76
77
78
79
80
81
82
# File 'lib/allpay/client.rb', line 73

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



68
69
70
71
# File 'lib/allpay/client.rb', line 68

def request path, params = {}
  api_url = URI.join(api_host, path)
  Net::HTTP.post_form api_url, generate_params(params)
end

#verify_mac(params = {}) ⇒ Object



47
48
49
50
51
# File 'lib/allpay/client.rb', line 47

def verify_mac params = {}
  stringified_keys = params.stringify_keys
  check_mac_value = stringified_keys.delete('CheckMacValue')
  make_mac(stringified_keys) == check_mac_value
end