보라코딩

스프링부트 iamport 이니시스 환불하기!! 본문

코딩/Spring

스프링부트 iamport 이니시스 환불하기!!

new 보라 2023. 6. 8. 15:41




실제 환불된다!!!
근데 왜 아임포트에서는 안보일까...?
 
 
 

html
<button onClick="cancelPay()" class="iamport">iamport 환불</button>
function cancelPay() {
$.ajax({

"url": "/pay/inicisCancel",
"type": "POST",
"contentType": "application/json",
"data": JSON.stringify({
"merchant_uid": "merchant_1686295852827"
"cancel_request_amount": 123, // 환불금액
"reason": "테스트 결제 환불" // 환불사유
}),
"dataType": "json"
});
}

 
 
 
 

Controller
@PostMapping("/inicisCancel")
@ResponseBody
public void inicisCancel(@RequestBody InicisRefundVO inicisRefundVO) throws IOException {

log.info("inicisCancel!!!!!!!!!!!!!!! : " + inicisRefundVO);

//토큰 DB에서 가져오기
String token = "토큰값 취소할거 넣기!!!!!";

paymentService.inicisRefund(inicisRefundVO, token);

log.info("controller 환불 완료!!!");

}

 
 
 

Service

 

public void inicisRefund(InicisRefundVO inicisRefundVO, String token) throws IOException {

String cancelUrl = "https://api.iamport.kr/payments/cancel";

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", token);

RestTemplate restTemplate = new RestTemplate();

JSONObject requestData = new JSONObject();
requestData.put("reason", inicisRefundVO.getReason());
requestData.put("merchant_uid", inicisRefundVO.getMerchant_uid());
requestData.put("imp_uid", "imp_688853734311");   // DB값 사용하기
requestData.put("amount", inicisRefundVO.getCancel_request_amount());


HttpEntity<String> requestEntity = new HttpEntity<>(requestData.toString(), headers);

ResponseEntity<String> responseEntity = restTemplate.exchange(cancelUrl, HttpMethod.POST, requestEntity, String.class);
if (responseEntity.getStatusCode().is2xxSuccessful()) {

log.info("으어어어?");
String responseData = responseEntity.getBody();
log.info("responseData?"+responseData);
//JSONObject responseJson = new JSONObject();
// JSONObject response = responseJson.getJSONObject("response");
// 처리 결과에 대한 작업 수행
} else {
// 요청 실패에 대한 작업 수행
}

}

 
 
 

InicisRefundVO

 

package com.tastemate.domain;

import lombok.Data;
import org.apache.ibatis.type.Alias;

@Data
@Alias("InicisRefundVO")
public class InicisRefundVO {


private String merchant_uid;
private String cancel_request_amount;
private String reason;



}