2025-05-15
๊ฒฐ์ ์ฐ๋ > ์ฐ๋์ ๋ณด
์์ ๋ค์ ํญ๋ชฉ ํ์ธ ๊ฐ๋ฅ:<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PortOne ํ
์คํธ</title>
</head>
<body>
<!-- UI -->
<button onclick="pay()">๊ฒฐ์ ํ๊ธฐ</button> |
<button onclick="auth()">๋ณธ์ธ์ธ์ฆ</button>
<!-- PortOne SDK ๋ก๋ -->
<script src="https://cdn.iamport.kr/v1/iamport.js"></script>
<script>
IMP.init("imp68801222"); // ๊ณ ๊ฐ์ฌ ์๋ณ ์ฝ๋
function pay() {
IMP.request_pay({
channelKey: "channel-key-xxxxx",
pay_method: "phone",
merchant_uid: "merchant_" + crypto.randomUUID(), // ๊ณ ์ ์ฃผ๋ฌธ๋ฒํธ
name: "ํ
์คํธ ๊ฒฐ์ ",
amount: 100,
buyer_tel: "010-0000-0000"
});
}
function auth() {
IMP.certification({
channelKey: "channel-key-xxxxx",
merchant_uid: "test_" + new Date().toString()
}, function(resp) {
console.log(resp); // ๋ณธ์ธ์ธ์ฆ ๊ฒฐ๊ณผ ์ฝ๋ฐฑ
});
}
</script>
</body>
</html>
@GetMapping("/getToken")
@ResponseBody
public void getToken() {
log.info("GET/portOne/getToken");
String url = "https://api.iamport.kr/users/getToken";
HttpHeaders header = new HttpHeaders();
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("imp_key", RESTAPI_KEY); // ๋ฐ๊ธ๋ฐ์ ํค ์
๋ ฅ
params.add("imp_secret", RESTAPI_SECRET); // ๋ฐ๊ธ๋ฐ์ ์ํฌ๋ฆฟ ์
๋ ฅ
HttpEntity<MultiValueMap<String, String>> entity = new HttpEntity<>(params, header);
RestTemplate rt = new RestTemplate();
ResponseEntity<PortOneTokenResponse> response = rt.exchange(url, HttpMethod.POST, entity, PortOneTokenResponse.class);
this.portOneTokenResponse = response.getBody(); // ์ถํ ์กฐํ์ ์ฌ์ฉ
System.out.println(this.portOneTokenResponse);
}
@Data
private static class TokenData {
public String access_token;
public int now;
public int expired_at;
}
@Data
private static class PortOneTokenResponse {
public int code;
public Object message;
public TokenData response;
}
@GetMapping("/getPayments")
@ResponseBody
public void payments() {
log.info("GET/portOne/getPayments...");
// imp_uid ๊ธฐ๋ฐ์ผ๋ก ๋ค๊ฑด ์กฐํ
String url = "https://api.iamport.kr/payments?imp_uid[]=imp_xxxxx";
HttpHeaders header = new HttpHeaders();
header.add("Authorization", "Bearer " + this.portOneTokenResponse.getResponse().getAccess_token());
header.add("Content-Type", "application/json");
HttpEntity<?> entity = new HttpEntity<>(header);
RestTemplate rt = new RestTemplate();
ResponseEntity<String> response = rt.exchange(url, HttpMethod.GET, entity, String.class);
System.out.println(response);
}
{
"code": 0,
"message": null,
"response": [
{
"amount": 100,
"buyer_tel": "010-0000-0000",
"imp_uid": "imp_",
"merchant_uid": "merchant_",
"pay_method": "phone",
"pg_provider": "danal",
"status": "paid"
}
]
}
207 Multi-Status
์๋ต์ ์ ์)