1. 빌링키 결제 API 구현하기 (서버)

포트원 빌링키 결제 API를 사용해 가맹점의 빌링키 결제 요청 API를 구현합니다.

Deprecated

이 문서는 더 이상 관리되지 않습니다.

PortOne 개발자센터를 이용해주세요.

예시를 위해 경로가 /billings인 빌링키 결제 요청 API를 구현합니다.

이 API는 내부적으로 포트원의 빌링킼 결제 API를 호출하며, 결제창 혹은 API를 통해 발급된 빌링키를 이용해 실제 결제를 진행합니다.

// bodyParser 등을 통해 body의 JSON 데이터를 파싱할 수 있는지 확인해주세요.
app.use(bodyParser.json());
// POST 요청을 받는 /billings
app.post("/billings", async (req, res) => {
  try {
    // 요청의 body로 빌링키가 오기를 기대합니다.
    const { billingKey } = req.body;
    
    // 1. 포트원 API를 사용하기 위해 액세스 토큰을 발급받습니다.
    const signinResponse = await axios({
      url: "https://api.portone.io/v2/signin/api-key",
      method: "post",
      headers: { "Content-Type": "application/json" },
      data: {
        api_key: PORTONE_API_KEY, // 포트원 API Key
      },
    });
    const { access_token } = signinResponse.data;

    // 가맹점에서 채번하는 새로운 결제 ID를 만들어주세요.
    const paymentId = OrderService.getNewPaymentId();

    // 2. 포트원 빌링키 결제 API 호출
    const paymentResponse = await axios({
      url: `https://api.portone.io/v2/payments/${paymentId}/billing-key/pay`,
      method: "post",
      // 1번에서 발급받은 액세스 토큰을 Bearer 형식에 맞게 넣어주세요.
      headers: { "Authorization": "Bearer " + access_token },
      data: {
        billing_key: billingKey,
        order_name: "월간 이용권 정기결제"
        total_amount: 8900,
        // 빌링키 결제 API를 참고해 고객 정보를 채워주세요.
        customer: YOUR_CUSTOMER_INFO,
        currency: "KRW"
      }
    });

    const tx_id = paymentResponse.data;
    // 3. 포트원 결제내역 단건조회 API를 호출하여 빌링키 결제 결과를 확인하세요.
    const paymentResponse = await axios({
      url: `https://api.portone.io/v2/payments/${paymentId}`,
      method: "get",
      // 1번에서 발급받은 액세스 토큰을 Bearer 형식에 맞게 넣어주세요.
      headers: { "Authorization": "Bearer " + access_token },
    });
    
    // 인증결제 연동하기 - 3. 결제검증 API 구현하기를 참고하여 검증 로직을 작성해주세요.
  } catch (e) {
    // 빌링키 결제에 실패했습니다.
    res.status(400).send(e);
  }
});

Last updated