Class: AllpayWebService::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(merchant_id:, hash_key:, hash_iv:) ⇒ Client

Returns a new instance of Client.



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

def initialize merchant_id:, hash_key:, hash_iv:
  raise_argument_error(msg: :missing_parameter, field: :merchant_id) if merchant_id.nil?
  raise_argument_error(msg: :parameter_should_be, field: :merchant_id, data: "String") unless merchant_id.is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :merchant_id) if merchant_id.empty?
  raise_argument_error(msg: :reach_max_length, field: :merchant_id, length: 10) if merchant_id.size > 10

  raise_argument_error(msg: :missing_parameter, field: :hash_key) if hash_key.nil?
  raise_argument_error(msg: :parameter_should_be, field: :hash_key, data: "String") unless hash_key.is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :hash_key) if hash_key.empty?

  raise_argument_error(msg: :missing_parameter, field: :hash_iv) if hash_iv.nil?
  raise_argument_error(msg: :parameter_should_be, field: :hash_iv, data: "String") unless hash_iv.is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :hash_iv) if hash_iv.empty?

  @merchant_id = merchant_id
  @hash_key = hash_key
  @hash_iv = hash_iv
end

Instance Attribute Details

#hash_ivObject

Returns the value of attribute hash_iv.



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

def hash_iv
  @hash_iv
end

#hash_keyObject

Returns the value of attribute hash_key.



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

def hash_key
  @hash_key
end

#merchant_idObject

Returns the value of attribute merchant_id.



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

def merchant_id
  @merchant_id
end

Instance Method Details

#create_trade(args = {}) ⇒ Object

Create trade



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/allpay/client.rb', line 50

def create_trade args = {}
  raise_argument_error(msg: :parameter_should_be, field: "Parameter", data: "Hash") unless args.is_a? Hash

  # filter arguments by accept keys
  accept_keys = [
    :ServiceURL, :MerchantTradeNo, :MerchantTradeDate, :TotalAmount, :TradeDesc, :CardNo,
    :CardValidMM, :CardValidYY, :CardCVV2, :UnionPay, :Installment, :ThreeD, :CharSet,
    :Enn, :BankOnly, :Redeem, :PhoneNumber, :AddMember, :CName, :Email, :Remark, :PlatformID
  ]
  args = args.filter(accept_keys)

  raise_argument_error(msg: :missing_parameter, field: :ServiceURL) if args[:ServiceURL].nil?
  raise_argument_error(msg: :parameter_should_be, field: :ServiceURL, data: "String") unless args[:ServiceURL].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :ServiceURL) if args[:ServiceURL].empty?

  raise_argument_error(msg: :missing_parameter, field: :MerchantTradeNo) if args[:MerchantTradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :MerchantTradeNo, data: "String") unless args[:MerchantTradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :MerchantTradeNo) if args[:MerchantTradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :MerchantTradeNo, length: 20) if args[:MerchantTradeNo].size > 20

  raise_argument_error(msg: :missing_parameter, field: :MerchantTradeDate) if args[:MerchantTradeDate].nil?
  raise_argument_error(msg: :parameter_should_be, field: :MerchantTradeDate, data: "String") unless args[:MerchantTradeDate].is_a? String
  raise_argument_error(msg: :wrong_data_format, field: :MerchantTradeDate) unless /\A\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2}\z/.match(args[:MerchantTradeDate])

  raise_argument_error(msg: :missing_parameter, field: :TotalAmount) if args[:TotalAmount].nil?
  raise_argument_error(msg: :parameter_should_be, field: :TotalAmount, data: "Integer") unless args[:TotalAmount].is_a? Integer
  raise_argument_error(msg: :parameter_should_be, field: :TotalAmount, data: "greater than 0") if args[:TotalAmount] <= 0

  raise_argument_error(msg: :missing_parameter, field: :TradeDesc) if args[:TradeDesc].nil?
  raise_argument_error(msg: :parameter_should_be, field: :TradeDesc, data: "String") unless args[:TradeDesc].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :TradeDesc) if args[:TradeDesc].empty?
  raise_argument_error(msg: :reach_max_length, field: :TradeDesc, length: 200) if args[:TradeDesc].size > 200

  raise_argument_error(msg: :missing_parameter, field: :CardNo) if args[:CardNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :CardNo, data: "Integer") unless args[:CardNo].is_a? Integer

  raise_argument_error(msg: :missing_parameter, field: :CardValidMM) if args[:CardValidMM].nil?
  raise_argument_error(msg: :parameter_should_be, field: :CardValidMM, data: "String") unless args[:CardValidMM].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :CardValidMM) if args[:CardValidMM].empty?
  raise_argument_error(msg: :reach_max_length, field: :CardValidMM, length: 2) if args[:CardValidMM].size > 2

  raise_argument_error(msg: :missing_parameter, field: :CardValidYY) if args[:CardValidYY].nil?
  raise_argument_error(msg: :parameter_should_be, field: :CardValidYY, data: "String") unless args[:CardValidYY].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :CardValidYY) if args[:CardValidYY].empty?
  raise_argument_error(msg: :reach_max_length, field: :CardValidYY, length: 2) if args[:CardValidYY].size > 2

  if args.has_key? :CardCVV2
    raise_argument_error(msg: :parameter_should_be, field: :CardCVV2, data: "Integer") unless args[:CardCVV2].is_a? Integer
  end

  # NOTE: Web Service 版本都帶 0。
  args.delete :UnionPay if args.has_key? :UnionPay
  # if args.has_key? :UnionPay
  #   raise_argument_error(msg: :parameter_should_be, field: :UnionPay, data: "Integer") unless args[:UnionPay].is_a? Integer
  # end

  if args.has_key? :Installment
    raise_argument_error(msg: :parameter_should_be, field: :Installment, data: "Integer") unless args[:Installment].is_a? Integer
  end

  if args.has_key? :ThreeD
    raise_argument_error(msg: :parameter_should_be, field: :ThreeD, data: "Integer") unless args[:ThreeD].is_a? Integer
    raise_argument_error(msg: :parameter_should_be, field: :ThreeD, data: ThreeD.readable_keys) unless ThreeD.values.include? args[:ThreeD]
  end

  if args.has_key? :CharSet
    raise_argument_error(msg: :parameter_should_be, field: :CharSet, data: "String") unless args[:CharSet].is_a? String
    raise_argument_error(msg: :parameter_should_be, field: :CharSet, data: CharSet.readable_keys) unless CharSet.values.include? args[:CharSet]
  end

  if args.has_key? :Enn
    raise_argument_error(msg: :parameter_should_be, field: :Enn, data: "String") unless args[:Enn].is_a? String
    raise_argument_error(msg: :parameter_should_be, field: :Enn, data: English.readable_keys) unless English.values.include? args[:Enn]
  end

  if args.has_key? :BankOnly
    raise_argument_error(msg: :parameter_should_be, field: :BankOnly, data: "String") unless args[:BankOnly].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :BankOnly, length: 120) if args[:BankOnly].size > 120
  end

  # NOTE: 目前 Web Service 是背景處理授權,尚未開放紅利折抵。
  args.delete :Redeem if args.has_key? :Redeem
  # if args.has_key? :Redeem
  #   raise_argument_error(msg: :parameter_should_be, field: :Redeem, data: "String") unless args[:Redeem].is_a? String
  #   raise_argument_error(msg: :reach_max_length, field: :Redeem, length: 1) if args[:Redeem].size > 1
  # end

  if args.has_key? :PhoneNumber
    raise_argument_error(msg: :parameter_should_be, field: :PhoneNumber, data: "String") unless args[:PhoneNumber].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :PhoneNumber, length: 10) if args[:PhoneNumber].size > 10
  end

  if args.has_key? :AddMember
    raise_argument_error(msg: :parameter_should_be, field: :AddMember, data: "String") unless args[:AddMember].is_a? String
    raise_argument_error(msg: :parameter_should_be, field: :AddMember, data: AddMember.readable_keys) unless AddMember.values.include? args[:AddMember]
  end

  if args[:AddMember] == AddMember::YES
    raise_argument_error(msg: :missing_parameter, field: :CName) if args[:CName].nil?
    raise_argument_error(msg: :parameter_should_be, field: :CName, data: "String") unless args[:CName].is_a? String
    raise_argument_error(msg: :cannot_be_empty, field: :CName) if args[:CName].empty?
    raise_argument_error(msg: :reach_max_length, field: :CName, length: 60) if args[:CName].size > 60
  end

  if args.has_key? :Email
    raise_argument_error(msg: :parameter_should_be, field: :Email, data: "String") unless args[:Email].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :Email, length: 100) if args[:Email].size > 100
  end

  if args.has_key? :Remark
    raise_argument_error(msg: :parameter_should_be, field: :Remark, data: "String") unless args[:Remark].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :Remark, length: 200) if args[:Remark].size > 200
  end

  if args.has_key? :PlatformID
    raise_argument_error(msg: :parameter_should_be, field: :PlatformID, data: "String") unless args[:PlatformID].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :PlatformID, length: 9) if args[:PlatformID].size > 9
  end

  service_url = args[:ServiceURL]
  args.delete :ServiceURL

  data = {
    MerchantID: @merchant_id,
    TradeDesc: "",
    CardCVV2: "",
    UnionPay: 0,
    Installment: 0,
    ThreeD: 0,
    CharSet: "utf-8",
    Enn: "",
    BankOnly: "",
    Redeem: "",
    PhoneNumber: "",
    AddMember: "0",
    CName: "",
    Email: "",
    Remark: "",
    PlatformID: ""
  }.merge(args)
  data[:TradeDesc] = CGI.escape(data[:TradeDesc])[0...200]
  data[:CardValidMM] = data[:CardValidMM].to_s.rjust(2, "0")
  data[:Remark] = CGI.escape(data[:Remark])[0...200]

  post_data = build_post_data trade_type: :create_trade, args: data

  response = request(service_url: service_url, data: post_data, is_soap_request: true)

  parse trade_type: :create_trade, response: response
end

#do_action(args = {}) ⇒ Object

Execute action for trade



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# File 'lib/allpay/client.rb', line 336

def do_action args = {}
  raise_argument_error(msg: :parameter_should_be, field: "Parameter", data: "Hash") unless args.is_a? Hash

  # filter arguments by accept keys
  accept_keys = [
    :ServiceURL, :MerchantTradeNo, :TradeNo, :Action, :TotalAmount, :PlatformID
  ]
  args = args.filter(accept_keys)

  raise_argument_error(msg: :missing_parameter, field: :ServiceURL) if args[:ServiceURL].nil?
  raise_argument_error(msg: :parameter_should_be, field: :ServiceURL, data: "String") unless args[:ServiceURL].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :ServiceURL) if args[:ServiceURL].empty?

  raise_argument_error(msg: :missing_parameter, field: :MerchantTradeNo) if args[:MerchantTradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :MerchantTradeNo, data: "String") unless args[:MerchantTradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :MerchantTradeNo) if args[:MerchantTradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :MerchantTradeNo, length: 20) if args[:MerchantTradeNo].size > 20

  raise_argument_error(msg: :missing_parameter, field: :TradeNo) if args[:TradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :TradeNo, data: "String") unless args[:TradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :TradeNo) if args[:TradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :TradeNo, length: 20) if args[:TradeNo].size > 20

  raise_argument_error(msg: :missing_parameter, field: :Action) if args[:Action].nil?
  raise_argument_error(msg: :parameter_should_be, field: :Action, data: "String") unless args[:Action].is_a? String
  raise_argument_error(msg: :parameter_should_be, field: :Action, data: ActionType.readable_keys) unless ActionType.values.include? args[:Action]

  raise_argument_error(msg: :missing_parameter, field: :TotalAmount) if args[:TotalAmount].nil?
  raise_argument_error(msg: :parameter_should_be, field: :TotalAmount, data: "Integer") unless args[:TotalAmount].is_a? Integer
  raise_argument_error(msg: :parameter_should_be, field: :TotalAmount, data: "greater than 0") if args[:TotalAmount] <= 0

  if args.has_key? :PlatformID
    raise_argument_error(msg: :parameter_should_be, field: :PlatformID, data: "String") unless args[:PlatformID].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :PlatformID, length: 9) if args[:PlatformID].size > 9
  end

  service_url = args[:ServiceURL]
  args.delete :ServiceURL

  data = {}.merge(args)

  post_data = build_post_data trade_type: :do_action, args: data

  response = request service_url: service_url, data: post_data, platform_id: args[:PlatformID]

  parse trade_type: :do_action, response: response
end

#query_trade(args = {}) ⇒ Object

Query trade information



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/allpay/client.rb', line 295

def query_trade args = {}
  raise_argument_error(msg: :parameter_should_be, field: "Parameter", data: "Hash") unless args.is_a? Hash

  # filter arguments by accept keys
  accept_keys = [
    :ServiceURL, :MerchantTradeNo, :PlatformID
  ]
  args = args.filter(accept_keys)

  raise_argument_error(msg: :missing_parameter, field: :ServiceURL) if args[:ServiceURL].nil?
  raise_argument_error(msg: :parameter_should_be, field: :ServiceURL, data: "String") unless args[:ServiceURL].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :ServiceURL) if args[:ServiceURL].empty?

  raise_argument_error(msg: :missing_parameter, field: :MerchantTradeNo) if args[:MerchantTradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :MerchantTradeNo, data: "String") unless args[:MerchantTradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :MerchantTradeNo) if args[:MerchantTradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :MerchantTradeNo, length: 20) if args[:MerchantTradeNo].size > 20

  if args.has_key? :PlatformID
    raise_argument_error(msg: :parameter_should_be, field: :PlatformID, data: "String") unless args[:PlatformID].is_a? String
    raise_argument_error(msg: :cannot_be_empty, field: :PlatformID) if args[:PlatformID].nil? or args[:PlatformID].empty?
    raise_argument_error(msg: :reach_max_length, field: :PlatformID, length: 9) if args[:PlatformID].size > 9
  end

  trade_type = args[:PlatformID].nil? ? :query_trade : :platform_query_trade

  service_url = args[:ServiceURL]
  args.delete :ServiceURL

  data = {
    PlatformID: ""
  }.merge(args)

  post_data = build_post_data trade_type: trade_type, args: data

  response = request(service_url: service_url, data: post_data, is_soap_request: true)

  parse trade_type: :query_trade, response: response
end

#resend_otp(args = {}) ⇒ Object

Resend otp code



251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/allpay/client.rb', line 251

def resend_otp args = {}
  raise_argument_error(msg: :parameter_should_be, field: "Parameter", data: "Hash") unless args.is_a? Hash

  # filter arguments by accept keys
  accept_keys = [
    :ServiceURL, :MerchantTradeNo, :TradeNo, :PlatformID
  ]
  args = args.filter(accept_keys)

  raise_argument_error(msg: :missing_parameter, field: :ServiceURL) if args[:ServiceURL].nil?
  raise_argument_error(msg: :parameter_should_be, field: :ServiceURL, data: "String") unless args[:ServiceURL].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :ServiceURL) if args[:ServiceURL].empty?

  raise_argument_error(msg: :missing_parameter, field: :MerchantTradeNo) if args[:MerchantTradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :MerchantTradeNo, data: "String") unless args[:MerchantTradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :MerchantTradeNo) if args[:MerchantTradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :MerchantTradeNo, length: 20) if args[:MerchantTradeNo].size > 20

  raise_argument_error(msg: :missing_parameter, field: :TradeNo) if args[:TradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :TradeNo, data: "String") unless args[:TradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :TradeNo) if args[:TradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :TradeNo, length: 20) if args[:TradeNo].size > 20

  if args.has_key? :PlatformID
    raise_argument_error(msg: :parameter_should_be, field: :PlatformID, data: "String") unless args[:PlatformID].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :PlatformID, length: 9) if args[:PlatformID].size > 9
  end

  service_url = args[:ServiceURL]
  args.delete :ServiceURL

  data = {
    MerchantID: @merchant_id,
    PlatformID: ""
  }.merge(args)

  post_data = build_post_data trade_type: :resend_otp, args: data

  response = request(service_url: service_url, data: post_data, is_soap_request: true)

  parse trade_type: :resend_otp, response: response
end

#verify_order_by_otp(args = {}) ⇒ Object

Verify order by otp code



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/allpay/client.rb', line 202

def verify_order_by_otp args = {}
  raise_argument_error(msg: :parameter_should_be, field: "Parameter", data: "Hash") unless args.is_a? Hash

  # filter argumentss by accept keys
  accept_keys = [
    :ServiceURL, :MerchantTradeNo, :TradeNo, :OtpCode, :PlatformID
  ]
  args = args.filter(accept_keys)

  raise_argument_error(msg: :missing_parameter, field: :ServiceURL) if args[:ServiceURL].nil?
  raise_argument_error(msg: :parameter_should_be, field: :ServiceURL, data: "String") unless args[:ServiceURL].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :ServiceURL) if args[:ServiceURL].empty?

  raise_argument_error(msg: :missing_parameter, field: :MerchantTradeNo) if args[:MerchantTradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :MerchantTradeNo, data: "String") unless args[:MerchantTradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :MerchantTradeNo) if args[:MerchantTradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :MerchantTradeNo, length: 20) if args[:MerchantTradeNo].size > 20

  raise_argument_error(msg: :missing_parameter, field: :TradeNo) if args[:TradeNo].nil?
  raise_argument_error(msg: :parameter_should_be, field: :TradeNo, data: "String") unless args[:TradeNo].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :TradeNo) if args[:TradeNo].empty?
  raise_argument_error(msg: :reach_max_length, field: :TradeNo, length: 20) if args[:TradeNo].size > 20

  raise_argument_error(msg: :missing_parameter, field: :OtpCode) if args[:OtpCode].nil?
  raise_argument_error(msg: :parameter_should_be, field: :OtpCode, data: "String") unless args[:OtpCode].is_a? String
  raise_argument_error(msg: :cannot_be_empty, field: :OtpCode) if args[:OtpCode].empty?
  raise_argument_error(msg: :reach_max_length, field: :OtpCode, length: 10) if args[:OtpCode].size > 10

  if args.has_key? :PlatformID
    raise_argument_error(msg: :parameter_should_be, field: :PlatformID, data: "String") unless args[:PlatformID].is_a? String
    raise_argument_error(msg: :reach_max_length, field: :PlatformID, length: 9) if args[:PlatformID].size > 9
  end

  service_url = args[:ServiceURL]
  args.delete :ServiceURL

  data = {
    MerchantID: @merchant_id,
    PlatformID: ""
  }.merge(args)

  post_data = build_post_data trade_type: :verify_order_by_otp, args: data

  response = request(service_url: service_url, data: post_data, is_soap_request: true)

  parse trade_type: :verify_order_by_otp, response: response
end