얼음꽃의 일지

[JS] toss 빌링키 연결 및 사용 api 본문

잡다한 지식

[JS] toss 빌링키 연결 및 사용 api

얼음꽃 2023. 11. 6. 21:41
728x90

toss에 요청해서 연결해서 받는 키값은 있다는 전제하에 진행됩니다.

 

첫번째로 프론트에서 toss 를 연결해서 카드 입력 후 오는 authKey값을 받아서 카드 등록을 진행합니다.

const superagent = require('superagent');

const billingKey = await superagent
        .post('https://api.tosspayments.com/v1/billing/authorizations/issue')
        .set(
          {
            Authorization: `Basic ${Buffer.from(
              `${토스키}:`,
              'utf-8',
            ).toString('base64')}`,
          },
          {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        )
        .send({
          customerKey : "고객값",
          authKey : "프론트에서 토스 진행해서 얻은 값",
        });

 

이렇게 진행하게되면 response 값으로 text 값에 고유 빌링키가 생성됩니다.

 

그 이후 나중에 빌링키로 다음과 같이 api를 작성하게되면 자동결제를 할 수 있습니다.

const superagent = require('superagent');

const data = {
      customerKey: "보내고자 하는 고객값",
      amount: "금액",
      orderId: "unique한 주문 번호",
      orderName : "이름",
      customerEmail: "이메일",
      customerName: "이름",
      cardInstallmentPlan: 0, // 부가세
    };

    try {
      const tossPayment = await superagent
        .post(
          `https://api.tosspayments.com/v1/billing/빌링키`,
        )
        .set(
          {
            Authorization: `Basic ${Buffer.from(
              `${토스키값}:`,
              'utf-8',
            ).toString('base64')}`,
          },
          {
            'Content-Type': 'application/json; charset=UTF-8',
          },
        )
        .send(data);

필요한 데이터를 넣고 api를 타면 요청한 amount 만큼 결제가 됩니다.

 

추가적인 내용은 다음을 참조하시면 좋습니다.

 

https://docs.tosspayments.com/guides/billing/integration

 

자동결제(빌링) 결제창 연동하기 | 토스페이먼츠 개발자센터

자동결제는 다른 이름으로 빌링, 또는 정기결제로 불리는 결제 방식입니다. 카드 등록창에서 고객의 카드를 한 번만 등록하고 나면, 별도의 고객 인증 없이 간편하게 결제를 요청할 수 있습니다

docs.tosspayments.com

 

728x90