POPBill Developers
가이드

튜토리얼

Node.js 개발환경에서 팝빌 SDK를 추가하여 홈택스 전자세금계산서 수집요청 (RequestJob) 함수를 구현하는 예시입니다.

1. POPBiLL SDK 추가

팝빌 Node.js SDK를 추가하기 위해 Express 프로젝트 "package.json" 파일에 팝빌 Node.js SDK 정보를 추가하고 npm install 또는 npm update를 진행합니다.

{
  "name": "Popbill TEST",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "cookie-parser": "~1.4.3",
    "debug": "~2.6.9",
    "ejs": "~2.5.7",
    "express": "~4.16.0",
    "http-errors": "~1.6.2",
    "morgan": "~1.9.0",
    "popbill": "^1.59.3"
  }
}

2. POPBiLL SDK 설정

프로젝트 routes 폴더 하위의 index.js 파일에 연동신청시 발급받은 API Key 를 변수로 선언하고 아래의 코드를 참조하여 홈택스연동(세금) 서비스 객체를 생성 합니다.

// 생략..

var popbill = require('popbill');

popbill.config( {

  // 링크아이디
  LinkID :'TESTER',

  // 비밀키
  SecretKey : 'SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3T=',

  // 연동환경 설정, true-테스트, false-운영(Production), (기본값:false)
  IsTest : true,

  // 통신 IP 고정, true-사용, false-미사용, (기본값:true)
  IPRestrictOnOff: true,

  // 팝빌 API 서비스 고정 IP 사용여부, 기본값(false)
  UseStaticIP: false,

  // 로컬시스템 시간 사용여부, true-사용, false-미사용, (기본값:true)
  UseLocalTimeYN: true,

  defaultErrorHandler: function (Error) {
    console.log('Error Occur : [' + Error.code + '] ' + Error.message);
  }

});

// 홈택스연동(세금) 서비스 객체 초기화
var htTaxinvoiceService = popbill.HTTaxinvoiceService();

// 생략..

3. RequestJob 기능 구현

index.js 파일에 홈택스 전자세금계산서 수집요청 (RequestJob) 함수 호출 코드를 추가합니다.

router.get('/requestJob', function (req, res, next) {

    // 팝빌회원 사업자번호, '-' 제외 10자리
    var testCorpNum = '1234567890';

    // 세금계산서 유형, SELL-매출, BUY-매입, TRUSTEE-수탁
    var type = popbill.MgtKeyType.SELL;

    // 검색일자유형, W-작성일자, I-발행일자, S-전송일자
    var DType = 'S';

    // 시작일자, 날짜형식(yyyyMMdd)
    var SDate = '20220101';

    // 종료일자, 날짜형식(yyyyMMdd)
    var EDate = '20220130';

    htTaxinvoiceService.requestJob(testCorpNum, type, DType, SDate, EDate,
        function (jobID) {
            res.render('result', {path: req.path, result: jobID})
        }, function (Error) {
            res.render('response', {path: req.path, code: Error.code, message: Error.message});
        });
});

함수 호출결과 코드와 메시지를 출력하는 "/views/result.ejs" 파일을 추가합니다.

<!DOCTYPE html>
<html>
<head>
    <title>Response</title>
</head>
<body>
<div>
    <p>Response</p>
    <br/>
    <fieldset>
        <legend><%= path %></legend>
        <ul>
            <li>JobID(작업아이디) : <%= result %></li>
        </ul>
    </fieldset>
</div>
</body>
</html>

4. 결과 확인

함수 호출 반환 결과는 아래와 같습니다.
- 성공 : Response code 로 숫자 1 반환
- 실패 : PopbillException 으로 음의 정수 8자리 숫자값 오류코드와 오류메시지 반환 [오류코드]