[Ruby on Rails] 팝빌 현금영수증 SDK 튜토리얼
Rails 프레임워크 개발환경에서 Ruby Gem을 이용해 팝빌 Ruby SDK를 추가한 후 현금영수증 즉시발행(RegistIssue) SDK 함수를 구현하는 예시입니다.
1. Ruby Gem Popbill SDK 추가 
① 팝빌 Ruby SDK를 추가하기 위해 Rails 프로젝트 "Gemfile" 파일에 팝빌 Ruby Gem SDK 정보를 추가하고 bundle install을 진행합니다.
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.3.0'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# 팝빌 API Service
gem 'popbill', '1.17.2'
# 생략
# ...
② 프로젝트에 현금영수증 서비스 기능을 수행할 Controller를 생성합니다.
rails generate controller tutorial
③ Controller에 연동신청시 발급받은 인증정보를 변수로 선언하고 아래의 코드를 참조하여 현금영수증 서비스 객체를 생성 합니다.
연동신청시 발급받은 인증정보로 링크아이디(LinkID)와 비밀키(SecretKey) property 값을 변경하시기 바랍니다.
require 'popbill/cashbill'
class TutorialController < ApplicationController
# 연동신청시 발급받은 링크아이디, 비밀키
LinkID = "LinkID"
SecretKey = "SwWxqU+0TExEXy/9TVjKPExI2VTUMMSLZtJf3Ed8q3I="
# 팝빌 현금영수증 Service 초기화
CBService = CashbillService.instance(
TutorialController::LinkID,
TutorialController::SecretKey
)
# 연동환경 설정값, (true-개발용, false-상업용)
CBService.setIsTest(true)
# 인증토큰 IP제한기능 사용여부, true-권장
CBService.setIpRestrictOnOff(true)
# 팝빌 API 서비스 고정 IP 사용여부(GA), true-사용, false-미사용, 기본값(false)
CBService.setUseStaticIP(false)
end
2. 현금영수증 즉시발행(RegistIssue) 기능 구현
① Controller 코드에 현금영수증 즉시발행(RegistIssue) 함수 호출 코드를 추가합니다.
def registIssue
# 팝빌회원 사업자번호
corpNum = "1234567890"
# 현금영수증 문서번호 (관리번호는 1~24자리로 숫자, 영문 '-', '_' 조합으로 구성할 수 있습니다.)
mgtKey = "20190903-21"
# 현금영수증 정보
cashbill = {
# [필수] 문서번호
"mgtKey" => mgtKey,
# [필수] 문서형태
"tradeType" => "승인거래",
# [필수] 거래구분, {소득공제용, 지출증빙용} 중 기재
"tradeUsage" => "소득공제용",
# [필수] 거래유형, {일반, 도서공연, 대중교통} 중 기재
"tradeOpt" => "일반",
# [필수] 식별번호
# 거래구분(tradeUsage) - '소득공제용' 인 경우 주민등록/휴대폰/카드번호 기재 가능
# 거래구분(tradeUsage) - '지출증빙용' 인 경우 사업자번호/주민등록/휴대폰/카드번호 기재 가능
"identityNum" => "0100001234",
# [필수] 과세형태, {과세, 비과세} 중 기재
"taxationType" => "과세",
# [필수] 공급가액
"supplyCost" => "10000",
# [필수] 부가세
"tax" => "1000",
# [필수] 봉사료
"serviceFee" => "0",
# [필수] 거래금액
"totalAmount" => "11000",
# [필수] 가맹점 사업자번호
"franchiseCorpNum" => corpNum,
# 가맹점 상호
"franchiseCorpName" => "가맹점 상호",
# 가맹점 대표자 성명
"franchiseCEOName" => "가맹점 대표자 성명",
# 가맹점 주소
"franchiseAddr" => "가맹점 주소",
# 가맹점 연락처
"franchiseTEL" => "가맹점 연락처",
# 고객명
"customerName" => "고객명",
# 상품명
"itemName" => "상품명",
# 가맹점 주문번호
"orderNumber" => "가맹점 주문번호",
# 거래처 이메일
# 팝빌 개발환경에서 테스트하는 경우에도 안내 메일이 전송되므로,
# 실제 거래처의 메일주소가 기재되지 않도록 주의
"email" => "test@Testcom",
# 거래처 휴대폰
"hp" => "010-111-222",
# 발행안내문자 전송여부
"smssendYN" => false,
} # end of cashbill hash
begin
@Response = TutorialController::CBService.registIssue(
corpNum,
cashbill,
)
render "home/response"
rescue PopbillException => pe
@Response = pe
render "home/exception"
end
end
② 함수 호출결과 코드와 메시지를 출력하는 "/views/home/response.html.erb" 파일을 추가합니다.
<html xmlns=" http://www.w3.org/1999/xhtml ">
<head>
<title>Popbill Ruby Test</title>
</head>
<body>
<div>
<fieldset>
<ul>
<li>code (응답코드) : <%= @Response["code"] %></li>
<li>message (응답메시지) : <%= @Response["message"] %></li>
</ul>
</fieldset>
</div>
</body>
</html>
③ 웹브라우저에서 페이지를 호출하여 함수호출 결과를 확인합니다.
