💸Payment cancellation (refund)
Learn how to cancel a payment using the i'mport payment cancel API.
STEP 01. Request cancellation
Request a refund to the server with the required refund information. In the case of virtual account refund, you need to specify additional parameters for the refund deposit account information. The following example requests a refund with the required refund information.
<button onclick="cancelPay()">Request Refund</button>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script><!-- jQuery CDN --->
<script>
function cancelPay() {
jQuery.ajax({
"url": "{URL to receive refund request}", // Example: http://www.myservice.com/payments/cancel
"type": "POST",
"contentType": "application/json",
"data": JSON.stringify({
"merchant_uid": "{Order ID}", // Example: ORD20180131-0000011
"cancel_request_amount": 2000, // Refund amount
"reason": "Testing payment refund" // Reason for the refund
"refund_holder": "Jone Doe", // [required for virtual account refund] refund account holder's name
"refund_bank": "88" // [required for virtual account refund] refund account bank code (e.g. Shinhan Bank is 88 for KG Inicis)
"refund_account": "56211105948400" // [required for virtual account refund] refund account number
}),
"dataType": "json"
});
}
</script>STEP 02. Get payment information
Assume that there is a Payments table that stores payment information as follows:
Query the payment information of the order from the Payments table using the order ID (merchant_uid) received from the client.
STEP 03. Request refund to i'mport server
To request a refund, you must first get a REST API access token. Use the access token to call the i'mport cancel API to request a refund.
Setting parameters for refund request:
Refund
unique keySet
imp_uidormerchant_uidas theunique keythat identifies the transaction to be refunded. The value ofimp_uidtakes precedence, and if an invalidimp_uidvalue is entered, the refund request will fail regardless of themerchant_uidvalue.
Refund amount (
amount)Enter the refund amount. If amount is not specified, the full amount will be refunded.
Refundable amount (
checksum)Enter the refundable amount. For example, the
checksumof a product that costs 10,000 won is 10,000. If this payment has been partially refunded for 1,000 won in the past, thechecksumis 9000 for the subsequent refund request. Thechecksumis used to check whether the refundable amount is the same between the merchant server and the i'mport server. If they do not match, the refund request will fail. If thechecksumis not specified, the verification is not performed.
Reason for entering checksum
checksum is not required, but it is recommended for comparing the refundable amount between the merchant server and the i'mport server.
For example, consider the case when a partial refund request of 1,000 won for a 10,000 won payment was completed on the i'mport server, but not on the merchant server due to a server or database error. In this case, the checksum of the i'mport server is changed to 9000, but that of the merchant server remains 10,000.
If you attempt to make a refund request with checksum(10000), the request will fail because the value does not match the checksum(9000) of the i'mport server.
The following example requests a refund.
STEP 04. Save refund result
After the refund process is completed, save the result in the database as follows:
Note - When cancelling a payment
Even if a response code of 200 (OK) is returned for the REST API (POST https://api.iamport.kr/payments/cancel) request, a non-zero code in the response body means that the refund has failed. You can check the reason for the failure in the message of the body.
STEP 04. Handle response for refund request
Add the client-side logic to handle the response from the server as follows:
Last updated
