class BaseService

Popbill API BaseService class

Constants

BOUNDARY
POPBILL_APIVersion
ServiceID_REAL
ServiceID_TEST
ServiceURL_GA_REAL
ServiceURL_GA_TEST
ServiceURL_REAL
ServiceURL_TEST

Attributes

ipRestrictOnOff[RW]
isTest[RW]
linkhub[RW]
scopes[RW]
token_table[RW]
useStaticIP[RW]

Public Class Methods

instance(linkID, secretKey) click to toggle source
# File lib/popbill.rb, line 23
def instance(linkID, secretKey)
  @instance ||= new
  @instance.token_table = {}
  @instance.linkhub = Linkhub.instance(linkID, secretKey)
  @instance.scopes = ["member"]
  @instance.ipRestrictOnOff = true
  @instance.useStaticIP = false

  return @instance
end

Public Instance Methods

addScope(scopeValue) click to toggle source

add Service Scope array

# File lib/popbill.rb, line 39
def addScope(scopeValue)
  @scopes.push(scopeValue)
end
checkID(idValue) click to toggle source

check Popbill Member ID

# File lib/popbill.rb, line 366
def checkID(idValue)
  http_response = httpget("/IDCheck?ID=" + idValue, "", "")
end
checkIsMember(corpNum, linkID) click to toggle source

Check is Partner's Popbill Member

# File lib/popbill.rb, line 401
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) click to toggle source

팝빌 로그인 URL

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

  response = httpget("/?TG=LOGIN", corpNum, userID)
  response['url']
end
getBalance(corpNum) click to toggle source

Get Popbill Member's Remain Point

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

  begin
    @linkhub.getBalance(getSession_Token(corpNum), getServiceID(), @useStaticIP)
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end
getChargeURL(corpNum, userID) click to toggle source

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

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

  response = httpget("/?TG=CHRG", corpNum, userID)
  response['url']
end
getCorpInfo(corpNum, userID = "") click to toggle source

Get Corp Info

# File lib/popbill.rb, line 434
def getCorpInfo(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httpget("/CorpInfo", corpNum, userID)
end
getPartnerBalance(corpNum) click to toggle source

Get Linkhub Partner's Remain Point

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

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

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

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

  begin
    @linkhub.getPartnerURL(getSession_Token(corpNum), getServiceID(), togo, @useStaticIP)
  rescue LinkhubException => le
    raise PopbillException.new(le.code, le.message)
  end
end
getPopbillURL(corpNum, togo, userID = "") click to toggle source

Get Pobill SSO URL

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

  response = httpget("/?TG=" + togo, corpNum, userID)
  response['url']
end
getServiceID() click to toggle source
# File lib/popbill.rb, line 64
def getServiceID()
  return @isTest ? BaseService::ServiceID_TEST : BaseService::ServiceID_REAL
end
getServiceURL() click to toggle source
# File lib/popbill.rb, line 55
def getServiceURL()
  if @useStaticIP
    return @isTest ? BaseService::ServiceURL_GA_TEST : BaseService::ServiceURL_GA_REAL
  else
    return @isTest ? BaseService::ServiceURL_TEST : BaseService::ServiceURL_REAL
  end

end
getSession_Token(corpNum) click to toggle source

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

# File lib/popbill.rb, line 69
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))
    refresh = expireTime < serverUTCTime
  end

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

    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
gzip_parse(target) click to toggle source

end of getSession_Token

# File lib/popbill.rb, line 105
def gzip_parse (target)
  sio = StringIO.new(target)
  gz = Zlib::GzipReader.new(sio)
  gz.read()
end
httpget(url, corpNum, userID = '') click to toggle source

Popbill API http Get Request Func

# File lib/popbill.rb, line 112
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 = '') click to toggle source

Request HTTP Post

# File lib/popbill.rb, line 150
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
httppostfile(url, corpNum, form, files, userID) click to toggle source

Request HTTP Post File

# File lib/popbill.rb, line 199
def httppostfile(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;\r\n\r\n"
    post_body << form.to_json + "\r\n"
  end

  files.each do |filePath|
    begin
      fileName = File.basename(filePath)
      post_body << "--#{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

  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
httppostfiles(url, corpNum, form, files, userID) click to toggle source
# File lib/popbill.rb, line 260
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) click to toggle source

Join Popbill Member

# File lib/popbill.rb, line 361
def joinMember(joinInfo)
  httppost("/Join", "", joinInfo.to_json, "", "")
end
listContact(corpNum, userID = "") click to toggle source

Get list Corp Contact

# File lib/popbill.rb, line 409
def listContact(corpNum, userID = "")
  if corpNum.length != 10
    raise PopbillException.new('-99999999', '사업자등록번호가 올바르지 않습니다.')
  end
  httpget("/IDs", corpNum, userID)
end
registContact(corpNum, contactInfo, userID = "") click to toggle source

Regist Contact

# File lib/popbill.rb, line 426
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) click to toggle source
# File lib/popbill.rb, line 47
def setIpRestrictOnOff(value)
  @ipRestrictOnOff = value
end
setIsTest(testValue) click to toggle source
# File lib/popbill.rb, line 43
def setIsTest(testValue)
  @isTest = testValue
end
setUseStaticIP(value) click to toggle source
# File lib/popbill.rb, line 51
def setUseStaticIP(value)
  @useStaticIP = value
end
updateContact(corpNum, contactInfo, userID = "") click to toggle source

Update Contact Info

# File lib/popbill.rb, line 417
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 = "") click to toggle source

Update Corp Info

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