[ASP.NET Core] 팝빌 전자명세서 SDK 튜토리얼
ASP.NET Core 개발환경에서 NuGet을 이용하여 팝빌 SDK를 추가한 후 전자명세서 즉시발행(RegistIssue) 함수를 구현하는 예시입니다.
1. Popbill SDK 패키지 추가 및 연동환경 설정
① [프로젝트 > NuGet 패키지 관리] 메뉴에서 popbill을 검색하여 최신 버전의 패키지를 설치합니다.

② 프로젝트의 Startup.cs 파일에 전자명세서 서비스 인스턴스 클래스를 생성하고, Startup클래스의 ConfigureServices() 함수에 의존성 주입 패턴으로 Singleton 서비스 인스턴스를 추가합니다.
연동신청시 발급받은 인증정보로 링크아이디(LinkID)와 비밀키(SecretKey) 값을 변경하시기 바랍니다.
③ 전자명세서 서비스명으로 컨트롤러를 생성하고 생성한 컨트롤러의 생성자 함수에서 전자명세서 인스턴스 객체를 할당합니다.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Popbill.Statement;
public class StatementInstance
{
// 연동신청 후 메일로 발급받은 링크아이디(LinkID)와 비밀키(SecretKey)값 으로 변경하시기 바랍니다.
private string linkID = "TESTER";
private string secretKey = "SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I=";
public StatementService statementService;
public StatementInstance()
{
//전자명세서 서비스 객체 초기화
statementService = new StatementService(linkID, secretKey);
//연동환경 설정값, 개발용(true), 상업용(false)
statementService.IsTest = true;
// 인증토큰 아이피 제한 기능 사용여부 권장(true)
statementService.IPRestrictOnOff = true;
// 팝빌 API 서비스 고정 IP 사용여부(GA), true-사용, false-미사용, 기본값(false)
statementService.UseStaticIP = false;
}
}
namespace StatementExample
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//전자명세서 서비스 객체 의존성 주입
services.AddSingleton<StatementInstance>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Statement}/{action=Index}");
});
}
}
}
2. 전자명세서 즉시발행(RegistIssue) 기능 구현
① 전자명세서 서비스명으로 생성한 컨트롤러의 생성자 함수에 인스턴스 객체를 할당하고, 전자명세서 즉시발행 (RegistIssue) 함수 호출 코드를 추가합니다.
// Controllers/StatementController.cs
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Popbill;
using Popbill.Statement;
namespace StatementExample.Controllers
{
public class StatementController : Controller
{
private readonly StatementService _statementService;
public StatementController(StatementInstance STMinstance)
{
//전자명세서 서비스 객체 주입
_statementService = STMinstance.statementService;
}
public IActionResult RegistIssue()
{
//팝빌 연동회원 사업자번호 (하이픈 '-' 제외 10자리)
string corpNum = "1234567890";
//팝빌 연동회원 아이디
string userID = "testkorea";
// 전자명세서 정보 객체
Statement statement = new Statement();
// [필수], 기재상 작성일자 날짜형식(yyyyMMdd)
statement.writeDate = "20190829";
// [필수], {영수, 청구} 중 기재
statement.purposeType = "영수";
// [필수], 과세형태, {과세, 영세, 면세} 중 기재
statement.taxType = "과세";
// 맞춤양식코드, 기본값을 공백('')으로 처리하면 기본양식으로 처리.
statement.formCode = "";
// [필수] 명세서 코드 - 121(거래명세서), 122(청구서), 123(견적서), 124(발주서), 125(입금표), 126(영수증)
statement.itemCode = 121;
// [필수] 문서번호, 1~24자리 숫자, 영문, '-', '_' 조합으로 사업자별로 중복되지 않도록 구성
statement.mgtKey = "20190829-021";
/**************************************************************************
* 발신자 정보 *
**************************************************************************/
// [필수] 발신자 사업자번호
statement.senderCorpNum = corpNum;
// 종사업자 식별번호. 필요시 기재. 형식은 숫자 4자리.
statement.senderTaxRegID = "";
// 발신자 상호
statement.senderCorpName = "발신자 상호";
// 발신자 대표자성명
statement.senderCEOName = "발신자 대표자 성명";
// 발신자 주소
statement.senderAddr = "발신자 주소";
// 발신자 종목
statement.senderBizClass = "발신자 종목";
// 발신자 업태
statement.senderBizType = "발신자 업태";
// 발신자 종목
statement.senderBizClass = "발신자 종목";
// 발신자 성명
statement.senderContactName = "발신자 담당자명";
// 발신자 부서명
statement.senderDeptName = "발신자 부서명";
// 발신자 연락처
statement.senderTEL = "070-7070-0707";
// 발신자 휴대전화
statement.senderHP = "010-000-2222";
// 발신자 이메일주소
statement.senderEmail = "test@test.com";
// 발신자 팩스번호
statement.senderFAX = "02-111-2222";
/**************************************************************************
* 수신자 정보 *
**************************************************************************/
// 수신자 사업자번호
statement.receiverCorpNum = "8888888888";
// [필수] 수신자 상호
statement.receiverCorpName = "수신자 상호";
// 수신자 대표자성명
statement.receiverCEOName = "수신자 대표자 성명";
// 수신자 주소
statement.receiverAddr = "수신자 주소";
// 수신자 종목
statement.receiverBizClass = "수신자 종목";
// 수신자 업태
statement.receiverBizType = "수신자 업태";
// 수신자 종목
statement.receiverBizClass = "수신자 종목";
// [필수] 수신자 성명
statement.receiverContactName = "수신자 담당자명";
// 수신자 부서명
statement.receiverDeptName = "수신자 부서명";
// 수신자 연락처
statement.receiverTEL = "070-7070-0707";
// 수신자 휴대전화
statement.receiverHP = "010-000-2222";
// 수신자 이메일주소
// 팝빌 개발환경에서 테스트하는 경우에도 안내 메일이 전송되므로,
// 실제 거래처의 메일주소가 기재되지 않도록 주의
statement.receiverEmail = "test@test.com";
// 수신자 팩스번호
statement.receiverFAX = "02-111-2222";
/**************************************************************************
* 전자명세서 기재항목 *
**************************************************************************/
// [필수] 공급가액 합계
statement.supplyCostTotal = "200000";
// [필수] 세액 합계
statement.taxTotal = "20000";
// 합계금액
statement.totalAmount = "220000";
// 기재상 일련번호 항목
statement.serialNum = "123";
// 기재상 비고 항목
statement.remark1 = "비고1";
statement.remark2 = "비고2";
statement.remark3 = "비고3";
// 사업자등록증 첨부 여부
statement.businessLicenseYN = false;
// 통장사본 첨부 여부
statement.bankBookYN = false;
// 문자 자동전송 여부
statement.smssendYN = false;
// 발행시 자동승인 여부
statement.autoAcceptYN = false;
// 상세항목(품목) 정보 객체
statement.detailList = new List<StatementDetail>();
StatementDetail detail = new StatementDetail();
detail.serialNum = 1; // 일련번호 1부터 순차기재
detail.purchaseDT = "20190829"; // 거래일자 작성형식 yyyyMMdd
detail.itemName = "품목명"; // 품목명
detail.spec = "규격"; // 규격
detail.qty = "1"; // 수량
detail.unitCost = "100000"; // 단가
detail.supplyCost = "100000"; // 공급가액
detail.tax = "10000"; // 세액
detail.remark = "품목비고"; // 비고
detail.spare1 = "spare1"; // 여분
detail.spare2 = "spare2";
detail.spare3 = "spare3";
detail.spare4 = "spare4";
detail.spare5 = "spare5";
statement.detailList.Add(detail);
detail = new StatementDetail();
detail.serialNum = 2; // 일련번호 1부터 순차기재
detail.purchaseDT = "20190829"; // 거래일자 작성형식 yyyyMMdd
detail.itemName = "품목명"; // 품목명
detail.spec = "규격"; // 규격
detail.qty = "1"; // 수량
detail.unitCost = "100000"; // 단가
detail.supplyCost = "100000"; // 공급가액
detail.tax = "10000"; // 세액
detail.remark = "품목비고"; // 비고
detail.spare1 = "spare1"; // 여분
detail.spare2 = "spare2";
detail.spare3 = "spare3";
detail.spare4 = "spare4";
detail.spare5 = "spare5";
statement.detailList.Add(detail);
// 추가속성항목, 자세한사항은 "전자명세서 API 연동매뉴얼> 5.2 기본양식 추가속성 테이블" 참조.
statement.propertyBag = new propertyBag();
statement.propertyBag.Add("Balance", "15000"); // 전잔액
statement.propertyBag.Add("Deposit", "5000"); // 입금액
statement.propertyBag.Add("CBalance", "20000"); // 현잔액
// 즉시발행
string memo = "즉시발행 메모";
try
{
var response = _statementService.RegistIssue(corpNum, statement, memo, userID);
return View("Response", response);
}
catch (PopbillException pe)
{
return View("Exception", pe);
}
}
}
}
② 함수호출 응답코드/ 메시지 확인을 위해 /Views/Shared/Response.cshtml 파일을 추가하고 호출 결과를 확인합니다.
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Response</title>
</head>
<body>
<div id="content">
<fieldset class="fieldset1">
<legend>Response</legend>
<ul>
<li>code (응답코드) : @Model.code</li>
<li>message (응답메시지) : @Mode.message</li>
</ul>
</fieldset>
</div>
</body>
</html>
