환불 처리하기
결제 승인 후 자동으로 매입이 처리됐거나 매입을 분리해 실행했다면 승인된 결제를 취소할 수 없고 환불만 처리할 수 있습니다. 다음과 같이 완료된 결제의 트랜잭션 ID를 이용해 결제 금액 전액을 환불 처리할 수 있습니다.
온라인 API를 호출할 때 필요한 자격 증명 정보를 얻는 방법은 API 자격 증명 준비를 참고하세요. 이 페이지에 설명된 코드 예제는 API 자격 증명 준비 코드 예제에 정의된
requestOnlineAPI()
함수를 이용합니다.
try {
let refundResponse = await requestOnlineAPI({
method: "POST",
baseUrl: targetAPIServer,
apiPath: `/v3/payments/${transactionId}/refund`,
});
console.log("Refund response: ", refundResponse);
} catch (error) {
console.log(error);
}
트랜잭션 ID 정보를 다룰 때 서비스 구현 언어에 따라 트랜잭션 ID를 문자열로 처리해야 할 수도 있습니다. 자세한 설명은 트랜잭션 ID를 참고하세요.
일부 금액만 환불한다면 다음과 같이 본문 파라미터에 부분 환불할 금액(refundAmount
)을 입력하세요.
const refund = async function (transactionId, amount = 0) {
if (!transactionId) throw new Error("Transaction ID is required!");
if (amount < 0) throw new Error("Amount cannot be negative number");
const refundBody = amount > 0 ? { refundAmount: amount } : null;
let refundResponse = await requestOnlineAPI({
method: "POST",
baseUrl: targetAPIServer,
apiPath: `/v3/payments/${transactionId}/refund`,
data: refundBody,
});
return refundResponse;
};
// ...
try {
let response = refund("2018082512345678910", 500);
console.log(response);
} catch (error) {
console.log(error);
}
환불 요청이 문제 없이 처리되면, 아래와 같이 환불 트랜잭션 정보가 응답으로 반환됩니다.
{
"returnCode": "0000",
"returnMessage": "success",
"info": {
"refundTransactionId": 2018082512345678911,
"refundTransactionDate": "2018-08-25T09:15:01Z"
}
}