POPBill Developers
가이드

튜토리얼

.NET Core 개발환경에서 팝빌 SDK를 추가하여 단문 문자 메시지 전송 (SendSMS) 함수를 구현하는 예시입니다.

1. POPBiLL SDK 추가

[프로젝트 > NuGet 패키지 관리] 메뉴에서 popbill을 검색하여 최신 버전의 패키지를 설치합니다.

2. POPBiLL SDK 설정

① 프로젝트의 Startup.cs 파일에 koreanName 서비스 인스턴스 클래스를 생성하고, Startup클래스의 ConfigureServices() 함수에 의존성 주입 패턴으로 Singleton 서비스 인스턴스를 추가합니다.

② koreanName 서비스명으로 컨트롤러를 생성하고 생성한 컨트롤러의 생성자 함수에서 koreanName 인스턴스 객체를 할당합니다.

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Popbill.moduleName;

public class serviceNameInstance
{
  // 링크아이디
  private string linkID = "TESTER";
  // 비밀키
  private string secretKey = "SwWxqU+0TErBXy/9TVjIPEnI0VTUMMSQZtJf3Ed8q3I=";

  // koreanName  서비스 객체 선언
  public serviceNameService classNameService;

  public serviceNameInstance()
  {
    // koreanName  서비스 객체 초기화
    messageService = new Message(linkID, seretKey);

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

    // 인증토큰 IP 검증 설정, ture-사용, false-미사용, (기본값: true)
    messageService.IPRestrictOnOff = true;

    // 통신 고정 IP, true-사용, false-미사용, (기본값: false)
    messageService.UseStaticIP = false;

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

namespace serviceNameExample
{
  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<serviceNameInstance>();
    }

    // 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=serviceName}/{action=Index}");
      });
    }
  }
}

3. SendSMS 기능 구현

문자 서비스명으로 생성한 컨트롤러의 생성자 함수에 인스턴스 객체를 할당하고, 단문 문자 메시지 전송 함수(SendSMS) 호출 코드를 추가합니다.

// Controllers/MessageController.cs

using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Popbill;
using Popbill.Message;

namespace MessageExample.Controllers
{
    public class MessageController : Controller
    {
        private readonly MessageService _messageService;

        public MessageController(MessageInstance MSGinstance)
        {
            // 문자 서비스 객체 주입
            _messageService = MSGinstance.messageService;
        }

        public IActionResult SendSMS()
        {
            //팝빌 연동회원 사업자번호 (하이픈 '-' 제외 10자리)
            string corpNum = "1234567890";

            //팝빌 연동회원 아이디
            string userID = "testkorea";

            // 발신번호
            string senderNum = "07043042992";

            // 발신자명
            string senderName = "발신자명";

            // 수신번호
            string receiverNum = "010111222";

            // 수신자명
            string receiverName = "수신자명";

            // 메시지내용, 90byte초과된 내용은 삭제되어 전송됨.
            string contents = "단문 문자 메시지 내용. 90byte 초과시 삭제되어 전송";

            // 예약전송일시(yyyyMMddHHmmss), null인 경우 즉시전송
            // ex) DateTime sndDT = new DateTime(20220130120000);
            DateTime? sndDT = null;

            // 광고문자여부 (기본값 false)
            // [참고] "광고메시지 전송방법 안내" [ http://blog.linkhubcorp.com/2642/ ]
            bool adsYN = false;

            // 요청번호, 파트너가 전송요청에 대한 관리번호를 직접 할당하여 관리하는 경우 기재
            // 최대 36자리, 영문, 숫자, 언더바('_'), 하이픈('-')을 조합하여 사업자별로 중복되지 않도록 구성
            string requestNum = "";

            try
            {
                var receiptNum = _messageService.SendSMS(corpNum, senderNum, senderName, receiverNum,
                          receiverName, contents, sndDT, adsYN, requestNum);
                return View("Response", receiptNum);
            }
            catch (PopbillException pe)
            {
                return View("Exception", pe);
            }
        }
    }
}

4. 결과 확인

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