환불 처리하기
결제 요청 후 자동으로 매입 처리가 됐거나 따로 매입을 실행했다면 승인된 결제를 취소할 수 없고 환불만 처리할 수 있습니다. 다음과 같이 결제 요청 시 입력한 주문 번호를 이용해 결제 금액 전액을 환불 처리할 수 있습니다.
try {
let refundResponse = await requestOfflineAPI({
method: "POST",
baseUrl: targetAPIServer,
apiPath: `/v2/payments/orders/${orderId}/refund`,
});
console.log("Refund response: ", refundResponse);
} catch (error) {
console.log(error);
}
만약, 일부 금액만 환불해야 한다면 다음과 같이 본문 파라미터에 부분 환불할 금액(refundAmount
)을 입력하세요.
const refund = async function (orderId, amount = 0) {
if (amount < 0) throw new Error("Amount cannot be negative number");
const refundBody = amount > 0 ? { refundAmount: amount } : null;
let refundResponse = await requestOfflineAPI({
method: "POST",
baseUrl: targetAPIServer,
apiPath: `/v2/payments/orders/${orderId}/refund`,
data: refundBody,
});
return refundResponse;
};
// ...
try {
let response = refund("test_order_1", 500);
console.log(response);
} catch (error) {
console.log(error);
}
환불 요청이 문제 없이 처리되면, 아래와 같이 환불 트랜잭션 정보가 응답으로 반환됩니다.
{
"returnCode": "0000",
"returnMessage": "success",
"info": {
"refundTransactionId": 2018082512345678911,
"refundTransactionDate": "2018-08-25T09:15:01Z"
}
}