OSDN Git Service

Merge branch '727_fix_product_minus_price' of /home/git/repositories/saas-ec-si/elecoma
[elecoma/elecoma.git] / lib / veritrans.rb
1 class Veritrans
2   @@proxy_command = "(cd #{RAILS_ROOT}/script/veritrans; perl proxy.pl)"
3   cattr_accessor :proxy_command
4   @@service_type = 'bsx1'
5   cattr_accessor :service_type
6
7   attr_accessor :config
8   def initialize(proxy_command=@@proxy_command)
9     @proxy_command = proxy_command || "(cd #{RAILS_ROOT}/script/veritrans; perl proxy.pl)"
10     @config = {}
11   end
12
13   def request(command, params)
14     yaml = {
15       'command' => command,
16       'params' => params.stringify_keys
17     }.to_yaml
18     chunk = nil
19     IO.popen(@proxy_command, "r+") {|io|
20       io.puts(yaml)
21       io.close_write
22       chunk = io.read # 向こうで close しないと無限に待つ
23     }
24     Response.new(YAML.load(chunk))
25   end
26
27   # 与信
28   def authonly(params)
29     auth('authonly', params)
30   end
31
32   # 不明
33   def mauthonly(params)
34     auth('mauthonly', params)
35   end
36
37   # 与信+売上
38   def authcapture(params)
39     auth('authcapture', params)
40   end
41
42   # 不明
43   def mauthcapture(params)
44     auth('mauthcapture', params)
45   end
46
47   # 売上
48   def postauth(params)
49     p = build_params(params, [:order_id, :amount])
50     request('postauth', p)
51   end
52
53   # キャンセル
54   def void(params)
55     p = build_params(params, [:order_id, :txn_type])
56     request('payrequest', p)
57   end
58
59   # 返品
60   def return(params)
61     p = build_params(params, [:order_id, :txn_type])
62     request('payrequest', p)
63   end
64
65   # 確認?
66   def payrequest(params)
67     p = build_params(params, [:order_id, :amount, :note])
68     request('payrequest', p)
69   end
70
71   # 再試行
72   def retry(params)
73     p = build_params(params, [:order_id])
74     request('retry', p)
75   end
76
77   # 検索
78   def query(params)
79     p = build_params(params, [])
80     request('query', p)
81   end
82
83   # 複数検索
84   def query_orders(params)
85     p = build_params(params, [:start_time, :end_time])
86     request('query-orders', p)
87   end
88
89   # jpo-info に入れる値
90   def self.bunkatsu(n)
91     n == 1 and return '10'
92     '61C%02d' % n
93   end
94
95   private
96
97   def auth(command, params)
98     p = build_params(params, [:order_id, :amount, :card_number, :card_exp])
99     request(command, p)
100   end
101
102   def build_params(params, required)
103     params = params.inject({}) do |hash, pair|
104       k = pair[0].to_s.gsub(/_/, '-')
105       hash[k] = pair[1]
106       hash
107     end
108     if @@service_type
109       params['service-type'] ||= @@service_type
110     end
111     missing = required.select do |name|
112       name = name.to_s.gsub(/_/, '-')
113       params[name].nil?
114     end
115     unless missing.empty?
116       raise ArgumentError, "parameter(s) required: %s" % missing.join(", ")
117     end
118     params
119   end
120
121   class Response
122     def initialize(hash)
123       @h = hash
124     end
125
126     def success?
127       @h['MStatus'] == 'success'
128     end
129
130     def message
131       message = @h['aux-msg']
132       if message.blank? || !success?
133         message = @h['MErrMsg']
134       end
135       message && message.toutf8
136     end
137
138     def card_number
139       n = @h['card-number'].to_s
140       if n
141         n[0..1] + '*' * 10 + n[2..5]
142       end
143     end
144
145     def [](key)
146       key = translate_key(key)
147       @h[key]
148     end
149
150     def paid_amount
151       s = @h['paid-amount']
152       s && s.split(/ /)[1].to_i
153     end
154
155     def []=(key, value)
156       key = translate_key(key)
157       @h[key] = value
158     end
159
160     def method_missing(name, *args)
161       self[name]
162     end
163
164     private
165
166     def translate_key(key)
167       if key.is_a?(Symbol)
168         key = key.to_s.gsub('_', '-')
169       end
170       key
171     end
172   end
173
174 end