๐Ÿ“Œ SpringBoot์—์„œ ์นด์นด์˜คํŽ˜์ด ๊ฒฐ์ œ API ์—ฐ๋™ํ•˜๊ธฐ

My Pale Blue Dotยท2025๋…„ 5์›” 14์ผ

SPRING BOOT

๋ชฉ๋ก ๋ณด๊ธฐ
25/40
post-thumbnail

๐Ÿ“… 2025-05-14

๐Ÿ“ ํ•™์Šต ๋‚ด์šฉ


1๏ธโƒฃ ์นด์นด์˜คํŽ˜์ด API ์—ฐ๋™ ์ค€๋น„

โœ… ์นด์นด์˜ค ๋””๋ฒจ๋กœํผ ์„ค์ •

  1. ์นด์นด์˜ค ๋””๋ฒจ๋กœํผ์Šค ๋กœ๊ทธ์ธ
  2. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ƒ์„ฑ
  3. ์•ฑ ํ‚ค ํ™•์ธ ๋ฐ ๋ฐœ๊ธ‰
    • JavaScript ํ‚ค
    • REST API ํ‚ค
    • Admin ํ‚ค
    • โœ… Secret Key (dev) โ† ๊ฒฐ์ œ ์—ฐ๋™ ์‹œ ์‚ฌ์šฉ
  4. ํ”Œ๋žซํผ ๋“ฑ๋ก
    • ๋„๋ฉ”์ธ ๋“ฑ๋ก: http://localhost:8090

2๏ธโƒฃ ๊ฒฐ์ œ ์ค€๋น„ API ํ˜ธ์ถœ (/kakao/pay/req)

โœ… ์š”์ฒญ ํ๋ฆ„

  • ์„œ๋ฒ„์—์„œ ์นด์นด์˜คํŽ˜์ด ๊ฒฐ์ œ ์ค€๋น„ API ํ˜ธ์ถœ
  • ์‘๋‹ต์œผ๋กœ tid์™€ ๊ฒฐ์ œ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ URL ํš๋“

โœ… ์ฃผ์š” URL

  • API ์—”๋“œํฌ์ธํŠธ: https://open-api.kakaopay.com/online/v1/payment/ready
  • ์‘๋‹ต์— ํฌํ•จ๋˜๋Š” ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ URL:
    • next_redirect_pc_url
    • next_redirect_mobile_url
    • android_app_scheme, ios_app_scheme

โœ… ์ปจํŠธ๋กค๋Ÿฌ ์ฝ”๋“œ

package com.example.demo.C04Kakao;

import lombok.extern.slf4j.Slf4j;
import org.json.simple.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;

@Controller
@Slf4j
@RequestMapping("/kakao/pay")
public class C04KakaoPayController {
    private String SECRET_KEY = "dev secret key ์‚ฌ์šฉ"; 

    @GetMapping("/req")
    @ResponseBody
    public void req() {
        log.info("GET /kakao/pay/req...");

        // 1. ์š”์ฒญ URL
        String url = "https://open-api.kakaopay.com/online/v1/payment/ready";

        // 2. ํ—ค๋” ์„ค์ •
        HttpHeaders header = new HttpHeaders();
        header.add("Authorization", "SECRET_KEY " + SECRET_KEY);
        header.add("Content-Type", "application/json");

        // 3. ๋ฐ”๋”” ์„ค์ • (JSON ํ˜•์‹)
        JSONObject params = new JSONObject();
        params.put("cid", "TC0ONETIME");
        params.put("partner_order_id", "partner_order_id");
        params.put("partner_user_id", "partner_user_id");
        params.put("item_name", "์ดˆ์ฝ”ํŒŒ์ด");
        params.put("quantity", "1");
        params.put("total_amount", "2200");
        params.put("vat_amount", "200");
        params.put("tax_free_amount", "0");
        params.put("approval_url", "http://localhost:8090/kakao/pay/success");
        params.put("fail_url", "http://localhost:8090/kakao/pay/fail");
        params.put("cancel_url", "http://localhost:8090/kakao/pay/cancel");

        // 4. ์š”์ฒญ ๋ณด๋‚ด๊ธฐ
        HttpEntity<JSONObject> entity = new HttpEntity<>(params, header);
        RestTemplate rt = new RestTemplate();
        ResponseEntity<String> response = rt.exchange(url, HttpMethod.POST, entity, String.class);

        // 5. ์‘๋‹ต ์ถœ๋ ฅ
        System.out.println(response);
    }

    // ๊ฒฐ์ œ ์„ฑ๊ณต ์‹œ
    @GetMapping("/success")
    @ResponseBody
    public void success() {
        log.info("GET /kakao/pay/success...");
    }

    // ๊ฒฐ์ œ ์‹คํŒจ ์‹œ
    @GetMapping("/fail")
    @ResponseBody
    public void fail() {
        log.info("GET /kakao/pay/fail...");
    }

    // ๊ฒฐ์ œ ์ทจ์†Œ ์‹œ
    @GetMapping("/cancel")
    @ResponseBody
    public void cancel() {
        log.info("GET /kakao/pay/cancel...");
    }
}

๐Ÿ”น ์ฝ˜์†” ์‘๋‹ต ์˜ˆ์‹œ

{
  "tid": "T823e8ef555b0f374b0d",
  "next_redirect_pc_url": "https://online-payment.kakaopay.com/mockup/bridge/pc/pg/one-time/payment/...",
  "next_redirect_mobile_url": "...",
  "android_app_scheme": "...",
  "ios_app_scheme": "..."
}
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ http://localhost:8090/kakao/pay/req๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ์‘๋‹ต์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Œ
  • ์‹ค์ œ ์„œ๋น„์Šค์—์„œ๋Š” response.getBody()์—์„œ URL์„ ์ถ”์ถœํ•ด ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฆฌ๋‹ค์ด๋ ‰ํŠธ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์•ผ ํ•จ

๐Ÿ”ฅ ์ •๋ฆฌ

  • ์นด์นด์˜คํŽ˜์ด ๊ฒฐ์ œ๋Š” 2๋‹จ๊ณ„๋กœ ์ง„ํ–‰๋จ
    1. ๊ฒฐ์ œ ์ค€๋น„ API ์š”์ฒญ โ†’ tid, redirect_url ์ˆ˜์‹ 
    2. ๊ฒฐ์ œ ์Šน์ธ API์—์„œ pg_token ์‚ฌ์šฉํ•ด ์ตœ์ข… ๊ฒฐ์ œ ์ฒ˜๋ฆฌ
  • ํ˜„์žฌ๋Š” ๊ฒฐ์ œ ์ค€๋น„ ๋‹จ๊ณ„๊นŒ์ง€ ๊ตฌํ˜„๋จ (์Šน์ธ ์š”์ฒญ์€ ์ถ”ํ›„ ๊ตฌํ˜„)

๐Ÿ”— ์ฐธ๊ณ  ์ž๋ฃŒ


profile
Here, My Pale Blue.๐ŸŒ

0๊ฐœ์˜ ๋Œ“๊ธ€