🔦5. 결제정보 검증하기
결제결과를 검증하여 안정적인 결제서비스 구축에 관한 가이드 입니다.
STEP 01 결제결과 서버 수신
app.use(bodyParser.json());
// "{서버의 결제 정보를 받는 가맹점 endpoint}" POST 요청 수신부
app.post("/payments/complete", async (req, res) => {
try {
// req의 body에서 imp_uid, merchant_uid 추출
const { imp_uid, merchant_uid } = req.body;
} catch (e) {
res.status(400).send(e);
}
});STEP 02 결제내역 단건 조회
app.use(bodyParser.json());
...
app.post("/payments/complete", async (req, res) => {
try {
// req의 body에서 imp_uid, merchant_uid 추출
const { imp_uid, merchant_uid } = req.body;
...
// 액세스 토큰(access token) 발급 받기
const getToken = await axios({
url: "https://api.iamport.kr/users/getToken",
method: "post", // POST method
headers: { "Content-Type": "application/json" },
data: {
imp_key: "imp_apikey", // REST API 키
imp_secret: "ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f" // REST API Secret
}
});
const { access_token } = getToken.data.response; // 인증 토큰
...
// imp_uid로 아임포트 서버에서 결제 정보 조회
const getPaymentData = await axios({
// imp_uid 전달
url: \`https://api.iamport.kr/payments/\${imp_uid}\`,
// GET method
method: "get",
// 인증 토큰 Authorization header에 추가
headers: { "Authorization": access_token }
});
const paymentData = getPaymentData.data.response; // 조회한 결제 정보
...
} catch (e) {
res.status(400).send(e);
}
});STEP 03 결제정보 검증
Last updated