[Spring] ๊ฒฐ์ ์๋น์ค | ํฌํธ์ ์ฐ๊ฒฐํ๊ธฐ
๊ฒฐ์ ์๋น์ค ํ๋ซํผ PortOne
PortOne์ ๊ฒฐ์ ์ฒ๋ฆฌ์ ๊ด๋ จ๋ ๋ค์ํ ์๋น์ค๋ฅผ ์ ๊ณตํ๋ ๊ธฐ์ ๋๋ ํ๋ซํผ์ผ๋ก ์๋ ค์ ธ ์์ต๋๋ค. ์ด๋ค์ ๋ค์ํ ๊ฒฐ์ ์๋จ์ ์ ๊ณตํ๊ณ , ์จ๋ผ์ธ ์๊ฑฐ๋๋ ๋ค๋ฅธ ๋์งํธ ๊ฒฐ์ ํธ๋์ญ์ ์ ์ฒ๋ฆฌํ ์ ์๋ API, ์ํํธ์จ์ด ๋ฐ ๋๊ตฌ๋ฅผ ์ ๊ณตํฉ๋๋ค.
PortOne์ ๋ณด์, ์ ๋ขฐ์ฑ, ๋ค์ํ ๊ฒฐ์ ๋ฐฉ๋ฒ ์ง์ ๋ฑ ๋ค์ํ ๊ธฐ๋ฅ์ ๊ฐ์ถ๊ณ ์์ผ๋ฉฐ, ์์ธ๋ค์ด ์์ ํ๊ณ ํธ๋ฆฌํ๊ฒ ๊ฒฐ์ ์ฒ๋ฆฌ๋ฅผ ๊ด๋ฆฌํ ์ ์๋๋ก ๋์์ค๋ค.
์๋น์ค์ ์ธ๋ถ์ ์ธ ๊ธฐ๋ฅ์ด๋ ์ ๊ณต๋๋ API์ ์ฌ์ฉ ๋ฐฉ๋ฒ ๋ฑ ์๋ ๊ณต์ ์น์ฌ์ดํธ๋ฅผ ์ฐธ๊ณ ํ์ฌ ๊ฐ๋ฐํ๋ฉด ๋๋ค.
ํฌํธ์, ์จ๋ผ์ธ ๋น์ฆ๋์ค๋ฅผ ์ํ ํตํฉ ๊ฒฐ์ ์๋ฃจ์
์ฝ๋ ํ ์ค๋ก ์ธ์ ๋ชจ๋ ๋ฐฉ์์ ๊ฒฐ์ ๋ฅผ ๊ฒฝํํด๋ณด์ธ์
portone.io
๊ฒฐ์ ๋ํ์ฌ ์ค์ ๋ฐ ์ถ๊ฐ
PG ์์ ์์ด๋ (CID)
๊ฒฐ์ ํ๊ธฐ ๋ฒํผ์ ๋ค์๊ณผ ๊ฐ์ด ๊ฐ๋จํ html ์์ ์ฝ๋๋ฅผ ์ด์ฉํ๋ค.
์ด๋ init(๊ฐ๋งน์ ์๋ณ์ฝ๋)์ pg: {๋ฑ๋กํ pg์ฌ}.{pg์์ ์์ด๋} ๋ฅผ ๋ณ๊ฒฝํด์ฃผ์ด์ผ ํ๋ค.
<!DOCTYPE html>
<html lang="en">
<head>
<!-- jQuery -->
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js" ></script>
<!-- iamport.payment.js -->
<script type="text/javascript" src="https://cdn.iamport.kr/js/iamport.payment-1.2.0.js"></script>
<script>
var IMP = window.IMP;
//๊ฐ๋งน์ ์๋ณ์ฝ๋ ์
๋ ฅ
IMP.init("imp62836256");
var today = new Date();
var hours = today.getHours(); // ์
var minutes = today.getMinutes(); // ๋ถ
var seconds = today.getSeconds(); // ์ด
var milliseconds = today.getMilliseconds();
var makeMerchantUid = hours + minutes + seconds + milliseconds;
function requestPay() {
IMP.request_pay({
//๋ฑ๋กํ pg์ฌ.PG์์ ์์ด๋
pg : 'kakaopay.TC0ONETIME',
pay_method : 'card',
merchant_uid: "IMP"+makeMerchantUid,
name : '๋น๊ทผ 10kg',
amount : 50000,
buyer_email : 'Iamport@chai.finance',
buyer_name : '์์ํฌํธ ๊ธฐ์ ์ง์ํ',
buyer_tel : '010-1234-5678',
buyer_addr : '์์ธํน๋ณ์ ๊ฐ๋จ๊ตฌ ์ผ์ฑ๋',
buyer_postcode : '123-456',
display: {
card_quota: [3] // ํ ๋ถ๊ฐ์ 3๊ฐ์๊น์ง ํ์ฑํ
}
}, function (rsp) { // callback
if (rsp.success) {
$.ajax({
type: 'GET',
url: 'http://localhost:8080/validation?impUid='+rsp.imp_uid
}).done(function(data) {
console.log(data);
})
} else {
console.log(rsp);
}
});
}
</script>
<meta charset="UTF-8">
<title>Sample Payment</title>
</head>
<body>
<button onclick="requestPay()">๊ฒฐ์ ํ๊ธฐ</button> <!-- ๊ฒฐ์ ํ๊ธฐ ๋ฒํผ ์์ฑ -->
</body>
</html>
PG์์ ์์ด๋๋ ์๋์ฒ๋ผ ์ฐพ์ ์ ์์๋ค.
CORS Error
ํ๋ก ํธ์ ๋ฐฑ์ url์ด ๋ฌ๋ผ์ ๋๋ error์ด๋ค.
@RestController์ ์ด์ฉํด์ front์ back์ ๋ถ๋ฆฌํด์ ๊ตฌํํ๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ค.
์๋ ๊ทธ๋ฆผ์ฒ๋ผ ๋ค๋ฅธ ์ฌ๋์ด ํ๋ก ํธ๋ง ๋ง๋ค์ด์ ์ฐ๋ฆฌ๊ฐ ๋ง๋ ๋ฐฑ์๋์ ๋ถ์ด์ ์์ฒญํ๋ฉด ํฐ ๋ฌธ์ ๊ฐ ์ด๋๋ ์ ์๋ค.
์ด๊ฑธ ๋ฐฉ์งํ๊ธฐ ์ํด์ ๋ฐฑ์๋์์ ํน์ ํ๋ก ํธ์๋ฒ์ ์์ฒญ๋ง ๋ฐ๋๋ก ์ค์ ํด์ค์ผ ํ๋ค.
๊ฒฐ๋ก ์ ๋ฐฑ์๋์ ํน์ ํ๋ก ํธ์๋๋ง์ ํ์ฉํ๋ ์ค์ ์ ํ๋ฉด ๋๋ค.
์ด ๋ฌธ์ ๋ @CrossOrigin() ์ผ๋ก ํ์ฉํ ํ๋ก ํธ ์๋ฒ๋ฅผ ์ง์ ํจ์ผ๋ก์จ ํด๊ฒฐํ ์ ์๋ค.
//@CrossOrigin ์ผ๋ก ํน์ ํ๋ก ํธ๋ง ์ฐ๊ฒฐ ํ๋๋ก ์ค์ !! CORS ์๋ฌ๋ฅผ ์ก๊ธฐ ์ํจ
// *๋ก ํ์ผ๋ ๋ชจ๋ ํ์ฉํด ์ฃผ๊ฒ ๋ค.
@RestController
@CrossOrigin("*")
public class OrderController {
...
}
๊ฒฐ์ ์ทจ์(ํ๋ถ)
https://developers.portone.io/api/rest-v1/payment?v=v1#post%20%2Fpayments%2Fcancel
์ ๋ฉ๋ด์ผ์ ์ฐธ๊ณ ํ์ฌ ๊ฐ๋ฐํ๋ฉด ๋๋ค.
POST ๋ฐฉ์์ผ๋ก /payments/cancel๋ก Https์์ฒญ์ ๋ณด๋ด๊ฒ ์ฐ๊ฒฐ์์ผ์ฃผ๋ฉด ๋๋ค.
์ฃผ์ํ ์ ์ BODY์ ๋ชจ๋ ๋ถ๋ถ์ด Optional๋ก ์ฃผ์ด์ ธ์์ง๋ง, imp_uid์ merchant_uid ๋ ์ค ํ๋๋ ํ์๋ก ์ ๋ ฅํด์ผ ํ๋ค.
OrderController.java
private void paymentCancel(String token, String impUid, String amount, String reason) throws IOException {
URL url = new URL("https://api.iamport.kr/payments/cancel");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setRequestMethod("POST");
// ์์ฒญ์ Content-Type, Accept, Authorization ํค๋ ์ค์
conn.setRequestProperty("Content-type", "application/json");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Authorization", token);
// ํด๋น ์ฐ๊ฒฐ์ ์ถ๋ ฅ ์คํธ๋ฆผ(์์ฒญ)์ผ๋ก ์ฌ์ฉ
conn.setDoOutput(true);
// JSON ๊ฐ์ฒด์ ํด๋น API๊ฐ ํ์๋กํ๋ ๋ฐ์ดํฐ ์ถ๊ฐ.
JsonObject json = new JsonObject();
json.addProperty("imp_uid", impUid);
json.addProperty("reason", reason);
// ์ถ๋ ฅ ์คํธ๋ฆผ์ผ๋ก ํด๋น conn์ ์์ฒญ
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
bw.write(json.toString());
bw.flush();
bw.close();
// ์
๋ ฅ ์คํธ๋ฆผ์ผ๋ก conn ์์ฒญ์ ๋ํ ์๋ต ๋ฐํ
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Gson gson = new Gson();
String response = gson.fromJson(br.readLine(), Map.class).toString();
System.out.println(response);
br.close();
conn.disconnect();
System.out.println("๊ฒฐ์ ์ทจ์ ์๋ฃ: ์ฃผ๋ฌธ๋ฒํธ " + impUid);
}
์ ์ฒด ์ฝ๋
https://github.com/SongYeonBaek/be02-shopping-mall-practice
GitHub - SongYeonBaek/be02-shopping-mall-practice
Contribute to SongYeonBaek/be02-shopping-mall-practice development by creating an account on GitHub.
github.com