๐
2025-05-14
๐ ํ์ต ๋ด์ฉ
1๏ธโฃ ์นด์นด์คํ์ด API ์ฐ๋ ์ค๋น
โ
์นด์นด์ค ๋๋ฒจ๋กํผ ์ค์
- ์นด์นด์ค ๋๋ฒจ๋กํผ์ค ๋ก๊ทธ์ธ
- ์ ํ๋ฆฌ์ผ์ด์
์์ฑ
- ์ฑ ํค ํ์ธ ๋ฐ ๋ฐ๊ธ
- JavaScript ํค
- REST API ํค
- Admin ํค
- โ
Secret Key (dev) โ ๊ฒฐ์ ์ฐ๋ ์ ์ฌ์ฉ
- ํ๋ซํผ ๋ฑ๋ก
- ๋๋ฉ์ธ ๋ฑ๋ก:
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...");
String url = "https://open-api.kakaopay.com/online/v1/payment/ready";
HttpHeaders header = new HttpHeaders();
header.add("Authorization", "SECRET_KEY " + SECRET_KEY);
header.add("Content-Type", "application/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");
HttpEntity<JSONObject> entity = new HttpEntity<>(params, header);
RestTemplate rt = new RestTemplate();
ResponseEntity<String> response = rt.exchange(url, HttpMethod.POST, entity, String.class);
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๋จ๊ณ๋ก ์งํ๋จ
- ๊ฒฐ์ ์ค๋น API ์์ฒญ โ
tid, redirect_url ์์
- ๊ฒฐ์ ์น์ธ API์์
pg_token ์ฌ์ฉํด ์ต์ข
๊ฒฐ์ ์ฒ๋ฆฌ
- ํ์ฌ๋ ๊ฒฐ์ ์ค๋น ๋จ๊ณ๊น์ง ๊ตฌํ๋จ (์น์ธ ์์ฒญ์ ์ถํ ๊ตฌํ)
๐ ์ฐธ๊ณ ์๋ฃ