Skip to content

Instantly share code, notes, and snippets.

@hyukkwonepic
Created November 10, 2017 08:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hyukkwonepic/a2e5a2bbe07d144387c0bc6cbf129407 to your computer and use it in GitHub Desktop.
Save hyukkwonepic/a2e5a2bbe07d144387c0bc6cbf129407 to your computer and use it in GitHub Desktop.
아임포트 결제 검증 프로세스 node.js 예제
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const app = express();
const IMPORT_API_BASE_URL = "https://api.iamport.kr";
const MY_BASE_URL = "https://www.myawesomeservice.com";
async function validate(imp_uid, response) {
try {
// 인증 토큰 발급 받기
const getToken = await axios({
url: "/users/getToken",
baseURL: IMPOART_API_BASE_URL,
method: "post",
headers: { "Content-Type": "application/json" },
data: {
imp_key: "imp_apikey",
imp_secret: "ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f"
}
});
const { access_token } = getToken.data.response; // 인증 토큰
// imp_uid로 결제 정보 조회
const checkIamportPayment = await axios({
url: `/payments/${imp_uid}`,
baseURL: IMPORT_API_BASE_URL,
method: "get",
headers: { "Authorization": access_token } // 인증 토큰 Authorization header에 추가
});
const iamportAmount = checkIamportPayment.data.response.amount;
const iamportStatus = checkIamportPayment.data.response.status;
// // 내 DB 결제 정보 조회
const checkDBPayment = await axios({
url: `/payment/${imp_uid}`,
baseURL: MY_BASE_URL,
method: "get"
});
// 내 결제 정보의 금액
const dbAmount = checkDBPayment.amount;
if (iamportStatus === "paid" && iamportAmount === dbAmount) {
response.json({ status: "success", type: "payment" });
} else if (iamportStatus === "ready" && iamportAmount === dbAmount){
response.json({ status: "success", type: "vbank" })
} else {
response.status(400).json({ error: "Payment info doesn't match" })
}
} catch(e) {
console.error(e);
}
}
app.use(bodyParser.json());
app.post("/payment/success", (request, response) => {
// 클라이언트에서 전달한 imp_uid 추출
const { imp_uid } = request.body;
validate(imp_uid, response);
})
app.listen(5000, () => console.log("Example app listening on port 5000!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment