팝빌 현금영수증 SDK 튜토리얼
Java - SpringMVC/JSP 개발환경에서 팝빌 Java SDK를 추가하여 현금영수증 즉시발행(RegistIssue) SDK 함수를 구현하는 예시입니다.
- SpringMVC
- JSP
1. Maven을 이용한 Popbill SDK 추가 
① 팝빌 Java SDK를 추가하기 위해 Spring 프로젝트 "pom.xml" 파일에 팝빌 Java SDK dependency 정보를 추가하고 Maven 업데이트합니다.
<dependency>
<groupId>kr.co.linkhub</groupId>
<artifactId>popbill-sdk</artifactId>
<version>1.33.0</version>
</dependency>
② 현금영수증 클래스를 Spring 빈으로 추가합니다. 아래의 코드를 참조하여 "servlet-context.xml" 파일을 수정합니다.
연동신청시 발급받은 인증정보로 링크아이디(LinkID)와 비밀키(SecretKey) property 값을 변경하시기 바랍니다.
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC @Controller programming model -->
<annotation-driven/>
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/"/>
<!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/"/>
<beans:property name="suffix" value=".jsp"/>
</beans:bean>
<context:component-scan base-package="com.popbill.example"/>
<util:properties id="EXAMPLE_CONFIG">
<!-- 링크아이디 -->
<beans:prop key="LinkID">TESTER</beans:prop>
<!-- 비밀키 -->
<beans:prop key="SecretKey">SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I=</beans:prop>
<!-- 연동환경 설정값 true(개발용), false(상업용) -->
<beans:prop key="IsTest">true</beans:prop>
<!-- 인증토큰 아이피 제한 기능 사용여부 권장(true) -->
<beans:prop key="IsIPRestrictOnOff">true</beans:prop>
<!-- 팝빌 API 서비스 고정 IP 사용여부(GA), true-사용, false-미사용, 기본값(false) -->
<beans:prop key="UseStaticIP">false</beans:prop>
<!-- 로컬서버 시간 사용여부 true-사용(기본값-권장), false-미사용 -->
<beans:prop key="UseLocalTimeYN">true</beans:prop>
</util:properties>
<beans:beans>
<!-- 현금영수증 Service Implementation Bean registration. -->
<beans:bean id="cashbillService" class="com.popbill.api.cashbill.CashbillServiceImp">
<beans:property name="linkID" value="#{EXAMPLE_CONFIG.LinkID}"/>
<beans:property name="secretKey" value="#{EXAMPLE_CONFIG.SecretKey}"/>
<beans:property name="test" value="#{EXAMPLE_CONFIG.IsTest}"/>
<beans:property name="IPRestrictOnOff" value="#{EXAMPLE_CONFIG.IsIPRestrictOnOff}"/>
<beans:property name="useStaticIP" value="#{EXAMPLE_CONFIG.UseStaticIP}"/>
<beans:property name="useLocalTimeYN" value="#{EXAMPLE_CONFIG.UseLocalTimeYN}"/>
</beans:bean>
</beans:beans>
</beans:beans>
2. 현금영수증 즉시발행(RegistIssue) 기능 구현
① 서비스 클래스 빈 객체 추가를 위해 @Autowired 어노테이션과 registIssue 함수 코드를 추가합니다.
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.popbill.api.CashbillService;
import com.popbill.api.PopbillException;
import com.popbill.api.Response;
import com.popbill.api.cashbill.Cashbill;
@Controller
public class CashbillServiceExample {
@Autowired
private CashbillService cashbillService;
@RequestMapping(value = "registIssue", method = RequestMethod.GET)
public String registIssue(Model m) {
// 가맹점 사업자번호
String corpNum = "1234567890";
// 메모
String Memo = "현금영수증 즉시발행 메모";
// 현금영수증 정보 객체
Cashbill cashbill = new Cashbill();
// 문서번호, 최대 24자리, 영문, 숫자 '-', '_'로 구성
cashbill.setMgtKey("20190827-001");
// 문서형태, {승인거래, 취소거래} 중 기재
cashbill.setTradeType("승인거래");
// 취소거래시 기재, 원본 현금영수증 국세청 승인번호 - getInfo API를 통해 confirmNum 값 기재
cashbill.setOrgConfirmNum("");
// 취소거래시 기재, 원본 현금영수증 거래일자 - getInfo API를 통해 tradeDate 값 기재
cashbill.setOrgTradeDate("");
// 과세형태, {과세, 비과세} 중 기재
cashbill.setTaxationType("과세");
// 거래처 식별번호, 거래유형에 따라 작성
// 소득공제용 - 주민등록/휴대폰/카드번호 기재가능
// 지출증빙용 - 사업자번호/주민등록/휴대폰/카드번호 기재가능
cashbill.setIdentityNum("0101112222");
// 거래구분, {소득공제용, 지출증빙용} 중 기재
cashbill.setTradeUsage("소득공제용");
// 거래유형, {읿반, 도서공연, 대중교통} 중 기재
cashbill.setTradeOpt("대중교통");
// 공급가액, 숫자만 가능
cashbill.setSupplyCost("10000");
// 부가세, 숫자만 가능
cashbill.setTax("1000");
// 봉사료, 숫자만 가능
cashbill.setServiceFee("0");
// 합계금액, 숫자만 가능, 봉사료 + 공급가액 + 부가세
cashbill.setTotalAmount("11000");
// 발행자 사업자번호, '-'제외 10자리
cashbill.setFranchiseCorpNum(corpNum);
// 발행자 상호
cashbill.setFranchiseCorpName("발행자 상호");
// 발행자 대표자명
cashbill.setFranchiseCEOName("발행자 대표자");
// 발행자 주소
cashbill.setFranchiseAddr("발행자 주소");
// 발행자 연락처
cashbill.setFranchiseTEL("07043042991");
// 발행안내 문자 전송여부
cashbill.setSmssendYN(false);
// 거래처 고객명
cashbill.setCustomerName("고객명");
// 거래처 주문상품명
cashbill.setItemName("상품명");
// 거래처 주문번호
cashbill.setOrderNumber("주문번호");
// 거래처 이메일
// 팝빌 개발환경에서 테스트하는 경우에도 안내 메일이 전송되므로,
// 실제 거래처의 메일주소가 기재되지 않도록 주의
cashbill.setEmail("test@test.com");
// 거래처 휴대폰
cashbill.setHp("010111222");
try {
Response response = cashbillService.registIssue(corpNum, cashbill, Memo);
m.addAttribute("Response", response);
} catch (PopbillException e) {
// 예외 발생 시, e.getCode() 로 오류 코드를 확인하고, e.getMessage()로 오류 메시지를 확인합니다.
System.out.println("오류 코드" + e.getCode());
System.out.println("오류 메시지" + e.getMessage());
}
return "response";
}
}
② 함수 호출결과 코드와 메시지를 매핑하는 "src/main/webapp/WEB-INF/views/response.jsp" 페이지를 추가합니다.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8"%>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Popbill SDK Response</title>
</head>
<body>
<p>응답코드 (Response.code) : ${Response.code}</p>
<p>응답메시지 (Response.message) : ${Response.message}</p>
</body>
</html>
③ 웹브라우저 페이지를 호출하여 함수호출 결과를 확인합니다.

1. Popbill SDK 추가 
① 팝빌 연동자료실에서 Java - JSP SDK 예제코드 다운로드 후 압축을 해제합니다.
② SDK 예제코드의 WEB-INF/lib/ 폴더의 3개 jar파일 구현할 프로젝트의 라이브러리 폴더에 복사합니다.

③ 프로젝트 경로에 현금영수증 서비스 연동환경 설정을 위한 "$CATALINA_HOME/webapps/ROOT/common.jsp" 파일을 생성하고 아래의 코드를 참고하여 인증정보를 설정합니다.
연동신청시 발급받은 인증정보로 링크아이디(LinkID)와 비밀키(SecretKey) 값을 변경하시기 바랍니다.
<%-- 현금영수증 클래스 빈 생성 --%>
<jsp:useBean id="cashbillService" scope="application" class="com.popbill.api.cashbill.CashbillServiceImp" />
<%-- 링크아이디 --%>
<jsp:setProperty name="cashbillService" property="linkID" value="TESTER" />
<%-- 비밀키, 사용자 인증에 사용되는 정보이므로 유출에 주의 --%>
<jsp:setProperty name="cashbillService" property="secretKey" value="SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I=" />
<%-- 연동환경 설정값, 개발용(true), 상업용(false) --%>
<jsp:setProperty name="cashbillService" property="test" value="true" />
<%-- 인증토큰 발급 IP 제한 On/Off, ture -제한기능 사용(기본값-권장), false-제한기능 미사용 --%>
<jsp:setProperty name="cashbillService" property="IPRestrictOnOff" value="true" />
<%-- 팝빌 API 서비스 고정 IP 사용여부(GA), true-사용, false-미사용, 기본값(false) --%>
<jsp:setProperty name="cashbillService" property="useStaticIP" value="false"/>
<%-- 로컬서버 시간 사용여부 true-사용(기본값-권장), false-미사용 --%<
<jsp:setProperty name="cashbillService" property="useLocalTimeYN" value="true"/>
2. 현금영수증 즉시발행(RegistIssue) 기능 구현
① "$CATALINA_HOME/webapps/ROOT/registIssue.jsp" 파일을 생성하고, 아래의 코드를 참조하여 함수 호출 코드를 추가합니다.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Popbill Cashbill Example</title>
</head>
<%@ include file="common.jsp" %>
<%@page import="com.popbill.api.Response"%>
<%@page import="com.popbill.api.PopbillException"%>
<%@page import="com.popbill.api.cashbill.Cashbill"%>
<%
/*
* 1건의 현금영수증을 [즉시발행]합니다.
* - 발행일 기준 오후 5시 이전에 발행된 현금영수증은 다음날 오후 2시에 국세청 전송결과를 확인할 수 있습니다.
*/
// 팝빌회원 사업자번호, '-'제외 10자리
String testCorpNum = "1234567890";
// 팝빌회원 아이디
String testUserID = "testkorea";
// 메모
String memo = "발행 메모";
// 발행안내 메일 제목, 미기재시 기본 양식으로 전송
String emailSubject = "";
// 현금영수증 정보 객체
Cashbill cashbill = new Cashbill();
// 현금영수증 문서번호, 최대 24자리, 영문, 숫자 '-'를 조합하여 사업자별로 중복되지 않도록 구성
cashbill.setMgtKey("ABC0226-TEST001");
// 문서형태, {승인거래, 취소거래} 중 기재
cashbill.setTradeType("승인거래");
// [취소거래 시 필수] 원본 현금영수증 승인번호
cashbill.setOrgConfirmNum("");
// [취소거래 시 필수] 원본 현금영수증 거래일자
cashbill.setOrgTradeDate("");
// 거래구분, {소득공제용, 지출증빙용} 중 기재
cashbill.setTradeUsage("소득공제용");
// 거래유형, {일반, 도서, 대중교통} 중 기재
cashbill.setTradeOpt("일반");
//거래처 식별번호, 거래구분에 따라 작성
//소득공제용 - 주민등록/휴대폰/카드번호(현금영수증 카드)/자진발급용 번호(010-000-1234) 입력
//지출증빙용 - 사업자번호/주민등록/휴대폰/카드번호(현금영수증 카드) 입력
//주민등록번호 13자리, 휴대폰번호 10~11자리, 카드번호 13~19자리, 사업자번호 10자리 입력 가능
cashbill.setIdentityNum("0101112222");
// 과세형태, {과세, 비과세} 중 기재
cashbill.setTaxationType("과세");
// 공급가액, 숫자만 가능
cashbill.setSupplyCost("10000");
// 부가세, 숫자만 가능
cashbill.setTax("1000");
// 봉사료, 숫자만 가능
cashbill.setServiceFee("0");
// 거래금액, 숫자만 가능, 봉사료 + 공급가액 + 부가세
cashbill.setTotalAmount("11000");
// 가맹점 사업자번호, '-'제외 10자리
cashbill.setFranchiseCorpNum(testCorpNum);
// 가맹점 상호
cashbill.setFranchiseCorpName("가맹점 상호");
// 가맹점 대표자성명
cashbill.setFranchiseCEOName("가맹점 대표자");
// 가맹점 주소
cashbill.setFranchiseAddr("가맹점 주소");
// 가맹점 연락처
cashbill.setFranchiseTEL("07043042991");
// 발행시 안내문자 전송여부
cashbill.setSmssendYN(false);
// 거래처 주문자명
cashbill.setCustomerName("주문자명");
// 거래처 주문상품명
cashbill.setItemName("주문상품명");
// 거래처 주문번호
cashbill.setOrderNumber("주문번호");
// 거래처 이메일
// 팝빌 개발환경에서 테스트하는 경우에도 안내 메일이 전송되므로,
// 실제 거래처의 메일주소가 기재되지 않도록 주의
cashbill.setEmail("test@test.com");
// 거래처 휴대폰
cashbill.setHp("010111222");
// 거래처 팩스
cashbill.setFax("070111222");
Response CheckResponse = null;
try {
CheckResponse = cashbillService.registIssue(testCorpNum, cashbill, memo, testUserID, emailSubject);
} catch (PopbillException pe) {
//적절한 오류 처리를 합니다. pe.getCode() 로 오류코드를 확인하고, pe.getMessage()로 관련 오류메시지를 확인합니다.
System.out.println("오류코드 " + pe.getCode());
System.out.println("오류메시지 " + pe.getMessage());
throw pe;
}
%>
<body>
<p>Response</p>
<br/>
<fieldset>
<legend>현금영수증 즉시발행</legend>
<ul>
<li>응답코드 (Response.code) : <%=CheckResponse.getCode()%></li>
<li>응답메시지 (Response.message) : <%=CheckResponse.getMessage()%></li>
</ul>
</fieldset>
</body>
</html>
② 웹브라우저 페이지를 호출하여 함수호출 결과를 확인합니다.
