Class: BaseService

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

Overview

Popbill API BaseService class

Constant Summary collapse

ServiceID_REAL =
"POPBILL"
ServiceID_TEST =
"POPBILL_TEST"
ServiceURL_REAL =
"https://popbill.linkhub.co.kr"
ServiceURL_TEST =
"https://popbill-test.linkhub.co.kr"
ServiceURL_Static_REAL =
"https://static-popbill.linkhub.co.kr"
ServiceURL_Static_TEST =
"https://static-popbill-test.linkhub.co.kr"
ServiceURL_GA_REAL =
"https://ga-popbill.linkhub.co.kr"
ServiceURL_GA_TEST =
"https://ga-popbill-test.linkhub.co.kr"
POPBILL_APIVersion =
"1.0"
BOUNDARY =
"==POPBILL_RUBY_SDK=="

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#ipRestrictOnOffObject

Returns the value of attribute ipRestrictOnOff.



22
23
24
# File 'lib/popbill.rb', line 22

def ipRestrictOnOff
  @ipRestrictOnOff
end

#isTestObject

Returns the value of attribute isTest.



22
23
24
# File 'lib/popbill.rb', line 22

def isTest
  @isTest
end

#linkhubObject

Returns the value of attribute linkhub.



22
23
24
# File 'lib/popbill.rb', line 22

def linkhub
  @linkhub
end

#scopesObject

Returns the value of attribute scopes.



22
23
24
# File 'lib/popbill.rb', line 22

def scopes
  @scopes
end

#token_tableObject

Returns the value of attribute token_table.



22
23
24
# File 'lib/popbill.rb', line 22

def token_table
  @token_table
end

#useGAIPObject

Returns the value of attribute useGAIP.



22
23
24
# File 'lib/popbill.rb', line 22

def useGAIP
  @useGAIP
end

#useLocalTimeYNObject

Returns the value of attribute useLocalTimeYN.



22
23
24
# File 'lib/popbill.rb', line 22

def useLocalTimeYN
  @useLocalTimeYN
end

#useStaticIPObject

Returns the value of attribute useStaticIP.



22
23
24
# File 'lib/popbill.rb', line 22

def useStaticIP
  @useStaticIP
end

Class Method Details

.instance(linkID, secretKey) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/popbill.rb', line 26

def instance(linkID, secretKey)
  @instance ||= new
  @instance.token_table = {}
  @instance.linkhub = Linkhub.instance(linkID, secretKey)
  @instance.scopes = ["member"]
  @instance.ipRestrictOnOff = true
  @instance.useStaticIP = false
  @instance.useGAIP = false
  @instance.useLocalTimeYN = true

  return @instance
end

Instance Method Details

#addScope(scopeValue) ⇒ Object

add Service Scope array



44
45
46
# File 'lib/popbill.rb', line 44

def addScope(scopeValue)
  @scopes.push(scopeValue)
end

#checkID(idValue) ⇒ Object

check Popbill Member ID



431
432
433
# File 'lib/popbill.rb', line 431

def checkID(idValue)
  http_response = httpget("/IDCheck?ID=" + idValue, "", "")
end

#checkIsMember(corpNum, linkID) ⇒ Object

Check is Partner’s Popbill Member



484
485
486
487
488
489
# File 'lib/popbill.rb', line 484

def checkIsMember(corpNum, linkID)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  http_response = httpget("/Join?CorpNum=" + corpNum + "&LID=" + linkID, "", "")
end

#getAccessURL(corpNum, userID) ⇒ Object

팝빌 로그인 URL



464
465
466
467
468
469
470
471
# File 'lib/popbill.rb', line 464

def getAccessURL(corpNum, userID)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  response = httpget("/?TG=LOGIN", corpNum, userID)
  response['url']
end

#getBalance(corpNum) ⇒ Object

Get Popbill Member’s Remain Point



387
388
389
390
391
392
393
394
395
396
397
# File 'lib/popbill.rb', line 387

def getBalance(corpNum)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  begin
    @linkhub.getBalance(getSession_Token(corpNum), getServiceID(), @useStaticIP, @useGAIP)
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end

#getChargeURL(corpNum, userID) ⇒ Object

팝빌 연동회원 포인트 충전 URL



474
475
476
477
478
479
480
481
# File 'lib/popbill.rb', line 474

def getChargeURL(corpNum, userID)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  response = httpget("/?TG=CHRG", corpNum, userID)
  response['url']
end

#getContactInfo(corpNum, contactID, userID = "") ⇒ Object



491
492
493
494
495
496
497
498
499
# File 'lib/popbill.rb', line 491

def getContactInfo(corpNum, contactID, userID  = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  postData = {}
  postData["id"] = contactID

  httppost("/Contact", corpNum, postData.to_json, "", userID)
end

#getCorpInfo(corpNum, userID = "") ⇒ Object

Get Corp Info



527
528
529
530
531
532
# File 'lib/popbill.rb', line 527

def getCorpInfo(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httpget("/CorpInfo", corpNum, userID)
end

#getPartnerBalance(corpNum) ⇒ Object

Get Linkhub Partner’s Remain Point



400
401
402
403
404
405
406
407
408
409
410
# File 'lib/popbill.rb', line 400

def getPartnerBalance(corpNum)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  begin
    @linkhub.getPartnerBalance(getSession_Token(corpNum), getServiceID(), @useStaticIP, @useGAIP)
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end

#getPartnerURL(corpNum, togo) ⇒ Object

파트너 포인트 충전 URL - 2017/08/29 추가



413
414
415
416
417
418
419
420
421
422
423
# File 'lib/popbill.rb', line 413

def getPartnerURL(corpNum, togo)
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  begin
    @linkhub.getPartnerURL(getSession_Token(corpNum), getServiceID(), togo, @useStaticIP, @useGAIP)
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end

#getPaymentURL(corpNum, userID = "") ⇒ Object

연동회원 포인트 결제내역 팝업 URL



446
447
448
449
450
451
452
# File 'lib/popbill.rb', line 446

def getPaymentURL(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  response = httpget("/?TG=PAYMENT", corpNum, userID)
  response['url']
end

#getPopbillURL(corpNum, togo, userID = "") ⇒ Object

Get Pobill SSO URL



436
437
438
439
440
441
442
443
# File 'lib/popbill.rb', line 436

def getPopbillURL(corpNum, togo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  response = httpget("/?TG=" + togo, corpNum, userID)
  response['url']
end

#getServiceIDObject



79
80
81
# File 'lib/popbill.rb', line 79

def getServiceID()
  return @isTest ? BaseService::ServiceID_TEST : BaseService::ServiceID_REAL
end

#getServiceURLObject



68
69
70
71
72
73
74
75
76
77
# File 'lib/popbill.rb', line 68

def getServiceURL()
  if @useGAIP
      return @isTest ? BaseService::ServiceURL_GA_TEST : BaseService::ServiceURL_GA_REAL
  elsif @useStaticIP
    return @isTest ? BaseService::ServiceURL_Static_TEST : BaseService::ServiceURL_Static_REAL
  else
    return @isTest ? BaseService::ServiceURL_TEST : BaseService::ServiceURL_REAL
  end

end

#getSession_Token(corpNum) ⇒ Object

Get Session Token by checking token-cached hash or token Request



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
# File 'lib/popbill.rb', line 84

def getSession_Token(corpNum)
  targetToken = nil
  refresh = false

  # check already cached CorpNum's SessionToken
  if @token_table.key?(corpNum.to_sym)
    targetToken = @token_table[corpNum.to_sym]
  end

  if targetToken.nil?
    refresh = true
  else
    # Token's expireTime must use parse() because time format is hh:mm:ss.SSSZ
    expireTime = DateTime.parse(targetToken['expiration'])
    serverUTCTime = DateTime.strptime(@linkhub.getTime(@useStaticIP, @useGAIP, @useLocalTimeYN))
    refresh = expireTime < serverUTCTime
  end

  if refresh
    begin
      # getSessionToken from Linkhub
      targetToken = @linkhub.getSessionToken(
          @isTest ? ServiceID_TEST : ServiceID_REAL, corpNum, @scopes, @ipRestrictOnOff ? "" : "*", @useStaticIP, @useGAIP, @useLocalTimeYN)

    rescue LinkhubException => le
      raise PopbillException.new(le.code, le.message)
    end
    # append token to cache hash
    @token_table[corpNum.to_sym] = targetToken
  end

  targetToken['session_token']
end

#getUseHistoryURL(corpNum, userID = "") ⇒ Object

연동회원 포인트 사용내역 팝업 URL



455
456
457
458
459
460
461
# File 'lib/popbill.rb', line 455

def getUseHistoryURL(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  response = httpget("/?TG=USEHISTORY", corpNum, userID)
  response['url']
end

#gzip_parse(target) ⇒ Object

end of getSession_Token



120
121
122
123
124
# File 'lib/popbill.rb', line 120

def gzip_parse (target)
  sio = StringIO.new(target)
  gz = Zlib::GzipReader.new(sio)
  gz.read()
end

#httpget(url, corpNum, userID = '') ⇒ Object

Popbill API http Get Request Func



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
# File 'lib/popbill.rb', line 127

def httpget(url, corpNum, userID = '')
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Accept-Encoding" => "gzip,deflate",
  }

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  uri = URI(getServiceURL() + url)

  request = Net::HTTP.new(uri.host, 443)
  request.use_ssl = true

  Net::HTTP::Get.new(uri)

  res = request.get(uri.request_uri, headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end
end

#httppost(url, corpNum, postData, action = '', userID = '', contentsType = '') ⇒ Object

Request HTTP Post



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
200
201
202
203
204
205
206
207
208
# File 'lib/popbill.rb', line 165

def httppost(url, corpNum, postData, action = '', userID = '', contentsType = '')

  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Accept-Encoding" => "gzip,deflate",
  }

  if contentsType == ''
    headers["Content-Type"] = "application/json; charset=utf8"
  else
    headers["Content-Type"] = contentsType
  end

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  if action.to_s != ''
    headers["X-HTTP-Method-Override"] = action
  end

  uri = URI(getServiceURL() + url)

  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, postData, headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end
end

#httppostbulk(url, corpNum, postData, submitID, action = '', userID = '') ⇒ Object

end of httppost



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
249
250
251
252
# File 'lib/popbill.rb', line 212

def httppostbulk(url, corpNum, postData, submitID, action = '', userID = '')
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Accept-Encoding" => "gzip,deflate",
  }

  headers["Content-Type"] = "application/json; charset=utf8"
  headers["x-pb-message-digest"] = Base64.strict_encode64(Digest::SHA1.digest(postData))
  headers["x-pb-submit-id"] = submitID

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  if action.to_s != ''
    headers["X-HTTP-Method-Override"] = action
  end

  uri = URI(getServiceURL() + url)

  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, postData, headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end
end

#httppostfile(url, corpNum, form, files, userID, isBinary = false) ⇒ Object

Request HTTP Post File



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
293
294
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
# File 'lib/popbill.rb', line 255

def httppostfile(url, corpNum, form, files, userID, isBinary = false)
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Content-Type" => "multipart/form-data;boundary=" + BaseService::BOUNDARY,
      "Accept-Encoding" => "gzip,deflate",
      "Connection" => "Keep-Alive"
  }

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  post_body = []

  if form.to_s != ''
    post_body << "--#{BaseService::BOUNDARY}\r\n"
    post_body << "Content-Disposition: form-data; name=\"form\"\r\n"
    post_body << "Content-Type: Application/json;\r\n\r\n"
    post_body << form.to_json + "\r\n"
  end
  if isBinary
    files.each do |fileData|
      fileName = fileData["fileName"]
      post_body << "\r\n--#{BaseService::BOUNDARY}\r\n"
      post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{fileName}\"\r\n"
      post_body << "Content-Type: Application/octet-stream\r\n\r\n"
      post_body << fileData["fileData"].force_encoding("UTF-8")
    end
  else
    files.each do |filePath|
      begin
        fileName = File.basename(filePath)
        post_body << "\r\n--#{BaseService::BOUNDARY}\r\n"
        post_body << "Content-Disposition: form-data; name=\"file\"; filename=\"#{fileName}\"\r\n"
        post_body << "Content-Type: Application/octet-stream\r\n\r\n"
        post_body << File.read(filePath)
      rescue
        raise PopbillException.new(-99999999, "Failed to reading filedata from filepath")
      end
    end
  end

  post_body << "\r\n--#{BaseService::BOUNDARY}--\r\n"
  # Add the file Data

  uri = URI(getServiceURL() + url)

  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, post_body.join.encode("UTF-8"), headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end

end

#httppostfiles(url, corpNum, form, files, userID) ⇒ Object



325
326
327
328
329
330
331
332
333
334
335
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
383
384
# File 'lib/popbill.rb', line 325

def httppostfiles(url, corpNum, form, files, userID)
  headers = {
      "x-pb-version" => BaseService::POPBILL_APIVersion,
      "Content-Type" => "multipart/form-data;boundary=" + BaseService::BOUNDARY,
      "Accept-Encoding" => "gzip,deflate",
      "Connection" => "Keep-Alive"
  }

  if corpNum.to_s != ''
    headers["Authorization"] = "Bearer " + getSession_Token(corpNum)
  end

  if userID.to_s != ''
    headers["x-pb-userid"] = userID
  end

  post_body = []

  if form.to_s != ''
    post_body << "--#{BaseService::BOUNDARY}\r\n"
    post_body << "Content-Disposition: form-data; name=\"form\"\r\n"
    post_body << "Content-Type: Application/json; charset=euc-kr\r\n\r\n"
    post_body << form.to_json
  end

  files.each do |filePath|
    begin
      fileName = File.basename(filePath)
      post_body << "--#{BaseService::BOUNDARY}\r\n"
      post_body << "Content-Disposition: form-data; name=\"Filedata\"; filename=\"#{fileName}\"\r\n"
      post_body << "Content-Type: Application/octet-stream\r\n\r\n"
      post_body << File.read(filePath)
    rescue
      raise PopbillException.new(-99999999, "Failed to reading filedata from filepath")
    end
  end

  post_body << "\r\n\r\n--#{BaseService::BOUNDARY}--\r\n"
  # Add the file Data

  uri = URI(getServiceURL() + url)

  https = Net::HTTP.new(uri.host, 443)
  https.use_ssl = true
  Net::HTTP::Post.new(uri)

  res = https.post(uri.request_uri, post_body.join.encode("UTF-8"), headers)

  if res.code == "200"
    if res.header['Content-Encoding'].eql?('gzip')
      JSON.parse(gzip_parse(res.body))
    else
      JSON.parse(res.body)
    end
  else
    raise PopbillException.new(JSON.parse(res.body)["code"],
                               JSON.parse(res.body)["message"])
  end

end

#joinMember(joinInfo) ⇒ Object

Join Popbill Member



426
427
428
# File 'lib/popbill.rb', line 426

def joinMember(joinInfo)
  httppost("/Join", "", joinInfo.to_json, "", "")
end

#listContact(corpNum, userID = "") ⇒ Object

Get list Corp Contact



502
503
504
505
506
507
# File 'lib/popbill.rb', line 502

def listContact(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httpget("/IDs", corpNum, userID)
end

#registContact(corpNum, contactInfo, userID = "") ⇒ Object

Regist Contact



519
520
521
522
523
524
# File 'lib/popbill.rb', line 519

def registContact(corpNum, contactInfo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httppost("/IDs/New", corpNum, contactInfo.to_json, "", userID)
end

#setIpRestrictOnOff(value) ⇒ Object



52
53
54
# File 'lib/popbill.rb', line 52

def setIpRestrictOnOff(value)
  @ipRestrictOnOff = value
end

#setIsTest(testValue) ⇒ Object



48
49
50
# File 'lib/popbill.rb', line 48

def setIsTest(testValue)
  @isTest = testValue
end

#setUseGAIP(value) ⇒ Object



60
61
62
# File 'lib/popbill.rb', line 60

def setUseGAIP(value)
  @useGAIP = value
end

#setUseLocalTimeYN(value) ⇒ Object



64
65
66
# File 'lib/popbill.rb', line 64

def setUseLocalTimeYN(value)
  @useLocalTimeYN = value
end

#setUseStaticIP(value) ⇒ Object



56
57
58
# File 'lib/popbill.rb', line 56

def setUseStaticIP(value)
  @useStaticIP = value
end

#updateContact(corpNum, contactInfo, userID = "") ⇒ Object

Update Contact Info



510
511
512
513
514
515
516
# File 'lib/popbill.rb', line 510

def updateContact(corpNum, contactInfo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end

  httppost("/IDs", corpNum, contactInfo.to_json, "", userID)
end

#updateCorpInfo(corpNum, corpInfo, userID = "") ⇒ Object

Update Corp Info



535
536
537
538
539
540
# File 'lib/popbill.rb', line 535

def updateCorpInfo(corpNum, corpInfo, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httppost("/CorpInfo", corpNum, corpInfo.to_json, "", userID)
end