Class: Cetustek::Services::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/cetustek/services/response_handler.rb

Overview

Parses the CreateInvoiceV3 return value. Three shapes are possible: the Table 8 JSON object (RtnMsg=Json), the 15-character "發票號碼;隨機碼" string, or a bare Table 7 result code.

Constant Summary collapse

InvalidResponseError =
Deprecated.

rescue Cetustek::ResultError instead. Kept as an alias of the class actually raised so pre-0.7 rescues keep matching.

ResultError
SUCCESS_LENGTH =

發票號碼 10 碼 + ';' + 隨機碼 4 碼

15
RESULT_MESSAGES =

Spec AVM-26-03 Table 7.

ResultCode::COMMON.merge(ResultCode::DETAILS).merge(
  'S1' => '資料庫發生錯誤',
  'S2' => '訂單日期超過開立日期',
  'S3' => '未在申報期內',
  'S4' => '未取得發票號碼',
  'S5' => '發票號碼已使用完畢',
  'S6' => '超過租賃張數限制',
  'S7' => '訂單號碼已存在,若需重開請先作廢原發票號碼',
  'S8' => '開立的總金額為負值'
).freeze

Instance Method Summary collapse

Constructor Details

#initialize(response, invoice_data, xml = nil) ⇒ ResponseHandler

Returns a new instance of ResponseHandler.



29
30
31
32
33
# File 'lib/cetustek/services/response_handler.rb', line 29

def initialize(response, invoice_data, xml = nil)
  @response = response
  @invoice_data = invoice_data
  @xml = xml
end

Instance Method Details

#processObject



35
36
37
38
39
40
41
42
43
# File 'lib/cetustek/services/response_handler.rb', line 35

def process
  body = @response.body[:create_invoice_v3_response][:return].to_s.strip
  json = parse_json(body)

  return success(json_result(json)) if json && json['msg'] == 'Success'
  return success(string_result(body)) if json.nil? && success_string?(body)

  fail_with(json ? json['msg'].to_s : body)
end