hyeonga_code

[JAVA/SPRING] 결제취소(전체)canclePayment()_포트원/아임포트 결제 적용 IamportClient<> 이해하기 본문

Spring

[JAVA/SPRING] 결제취소(전체)canclePayment()_포트원/아임포트 결제 적용 IamportClient<> 이해하기

hyeonga 2024. 2. 15. 08:29
반응형

2024.02.14

portone에서 제공하는 github의 IamportRestTest.java

https://github.com/iamport/iamport-rest-client-java/blob/master/src/test/java/com/siot/IamportRestClient/IamportRestTest.java

 

직접 실행해보면서 어떻게 돌아가는지 확인해봄

 

-- canclePaymentAlreadyCancelledImpUid()

---- 주어진 imp_uid에 해당하는 결제를 취소하는 메소드

---- 이미 취소된 결제건을 조회하는 코드 포함(이미 취소된 결제를 다시 취소하려고 하는 경우 에러메세지 출력하는지 확인)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
   public void testCancelPaymentAlreadyCancelledImpUid() {
        System.err.println(">>> testCancelPaymentAlreadyCancelledImpUid");
        client = new IamportClient(" [ REST API Key ] "" [ Rest API Secret ] ");
        String test_already_cancelled_imp_uid = "imp_497911164010";
        CancelData cancel_data = new CancelData(test_already_cancelled_imp_uid, true); //imp_uid를 통한 전액취소
 
        try {
            IamportResponse<Payment> payment_response = client.cancelPaymentByImpUid(cancel_data);
            System.err.println("getMessage : " + payment_response.getMessage());
            System.err.println("getResponse : " + payment_response.getResponse());
            setPayment(payment_response.getResponse());
 
        } catch (IamportResponseException e) {
            System.err.println(e.getMessage());
 
            switch (e.getHttpStatusCode()) {
                case 401:
                    //TODO
                    break;
                case 500:
                    //TODO
                    break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

 

@@실행결과

1) 
>>> testCancelPaymentAlreadyCancelledImpUid
getMessage : 취소할 결제건이 존재하지 않습니다.
getResponse : null

2) 
>>> testCancelPaymentAlreadyCancelledImpUid
getMessage : null
getResponse : com.siot.IamportRestClient.response.Payment@263a4d24
===== Payment =====
____getApplyNum : 
____getBankCode : null
____getBankName : null
____getBuyerAddr : 주소
____getBuyerEmail : email@email.com
____getBuyerName : 이름
____getBuyerPostcode : 우편번호
____getBuyerTel : 전화번호
____getCancelReason : 취소요청api
____getCardCode : null
____getCardName : null
____getCardNumber : null
____getCardQuota : 0
____getCardType : 0
____getChannel : pc
____getCurrency : KRW
____getCustomData : null
____getCustomerUid : null
____getCustomerUidUsage : null
____getEmbPgProvider : null
____getFailReason : null
____getImpUid : imp_497911164010
____getMerchantUid : nobody_1707893910566
____getName : 주문상품이름
____getPayMethod : point
____getPgProvider : kakaopay
____getPgTid : T5cc64971c025b0feab6
____getReceiptUrl : https://mockup-pg-web.kakao.com/v1/confirmation/p/T5cc64971c025b0feab6/c78f2dc54c15183fbbfdc805b40bdfe1a05dbde062a8a9196ad6b9a715b6333b
____getStartedAt : 1707893911
____getStatus : cancelled
____getVbankCode : null
____getVbankHolder : null
____getVbankIssuedAt : 0
____getAmount : 100
____getCancelAmount : 100
____getPaidAt : Wed Feb 14 15:58:45 KST 2024
=================

 

 

-- canclePaymentByImpUid() 

---- 결제를 취소하는 메소드

---- 전액 취소(검증 추가) 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
    public void testCancelPaymentChecksumByImpUid() {
        System.err.println(">>> testCancelPaymentChecksumByImpUid");
        client = new IamportClient(" [ REST API Key ] "" [ Rest API Secret ] ");
        String test_already_cancelled_imp_uid = "imp_089778900116";
        CancelData cancel_data = new CancelData(test_already_cancelled_imp_uid, true); //imp_uid를 통한 전액취소
        cancel_data.setChecksum(BigDecimal.valueOf(100)); // checksum 으로 검증 추가
 
        try {
            IamportResponse<Payment> payment_response = client.cancelPaymentByImpUid(cancel_data);
            System.err.println("getMessage : " + payment_response.getMessage());
            System.err.println("getResponse : " + payment_response.getResponse());
            setPayment(payment_response.getResponse());
 
        } catch (IamportResponseException e) {
            System.err.println(e.getMessage());
 
            switch (e.getHttpStatusCode()) {
                case 401:
                    //TODO
                    break;
                case 500:
                    //TODO
                    break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

 

@@실행결과

1) 
>>> testCancelPaymentChecksumByImpUid
getMessage : 취소할 결제건이 존재하지 않습니다.
getResponse : null
===== Payment =====

2)
>>> testCancelPaymentChecksumByImpUid
getMessage : null
getResponse : com.siot.IamportRestClient.response.Payment@7664415
===== Payment =====
____getApplyNum : 
____getBankCode : null
____getBankName : null
____getBuyerAddr : 주소
____getBuyerEmail : email@email.com
____getBuyerName : 이름
____getBuyerPostcode : 우편번호
____getBuyerTel : 전화번호
____getCancelReason : 취소요청api
____getCardCode : null
____getCardName : null
____getCardNumber : null
____getCardQuota : 0
____getCardType : 0
____getChannel : pc
____getCurrency : KRW
____getCustomData : null
____getCustomerUid : null
____getCustomerUidUsage : null
____getEmbPgProvider : null
____getFailReason : null
____getImpUid : imp_688863851303
____getMerchantUid : nobody_1707901863097
____getName : 주문상품이름
____getPayMethod : point
____getPgProvider : kakaopay
____getPgTid : T5cc83a71c025b0fec7f
____getReceiptUrl : https://mockup-pg-web.kakao.com/v1/confirmation/p/T5cc83a71c025b0fec7f/fb33ec520cf52a3d0b07a007fd4dc4e486231c6f3579047b54b638b533e173a7
____getStartedAt : 1707901863
____getStatus : cancelled
____getVbankCode : null
____getVbankHolder : null
____getVbankIssuedAt : 0
____getAmount : 100
____getCancelAmount : 100
____getPaidAt : Wed Feb 14 18:11:12 KST 2024
=================

 

 

-- cancelPaymentAlreadyCancelledMerchantUid()

---- merchant_uid를 사용하여 결제를 취소하는 메소드

---- 결제정보가 null이 아닌경우 취소가 된 것

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    public void testCancelPaymentAlreadyCancelledMerchantUid() {
        System.err.println(">>> testCancelPaymentAlreadyCancelledMerchantUid");
        client = new IamportClient(" [ REST API Key ] "" [ Rest API Secret ] ");
        String test_already_cancelled_merchant_uid = "nobody_1707893778305";
        CancelData cancel_data = new CancelData(test_already_cancelled_merchant_uid, false); //merchant_uid를 통한 전액취소
        cancel_data.setEscrowConfirmed(true); //에스크로 구매확정 후 취소인 경우 true설정
 
        try {
            IamportResponse<Payment> payment_response = client.cancelPaymentByImpUid(cancel_data);
 
            System.err.println(payment_response.getMessage());
            System.err.println("getMessage : " + payment_response.getMessage());
            System.err.println("getResponse : " + payment_response.getResponse());
            setPayment(payment_response.getResponse());
        } catch (IamportResponseException e) {
            System.err.println(e.getMessage());
 
            switch (e.getHttpStatusCode()) {
                case 401:
                    //TODO
                    break;
                case 500:
                    //TODO
                    break;
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

 

@@실행결과

1)
>>> testCancelPaymentAlreadyCancelledMerchantUid
취소할 결제건이 존재하지 않습니다.
getMessage : 취소할 결제건이 존재하지 않습니다.
getResponse : null
===== Payment =====

2)
>>> testCancelPaymentAlreadyCancelledMerchantUid
null
getMessage : null
getResponse : com.siot.IamportRestClient.response.Payment@6bd7154a
===== Payment =====
____getApplyNum : 
____getBankCode : null
____getBankName : null
____getBuyerAddr : 주소
____getBuyerEmail : email@email.com
____getBuyerName : 이름
____getBuyerPostcode : 우편번호
____getBuyerTel : 전화번호
____getCancelReason : 취소요청api
____getCardCode : null
____getCardName : null
____getCardNumber : null
____getCardQuota : 0
____getCardType : 0
____getChannel : pc
____getCurrency : KRW
____getCustomData : null
____getCustomerUid : null
____getCustomerUidUsage : null
____getEmbPgProvider : null
____getFailReason : null
____getImpUid : imp_089778900116
____getMerchantUid : nobody_1707893778305
____getName : 주문상품이름
____getPayMethod : point
____getPgProvider : kakaopay
____getPgTid : T5cc6412196d5d20582d
____getReceiptUrl : https://mockup-pg-web.kakao.com/v1/confirmation/p/T5cc6412196d5d20582d/2cdc5bb902c98c0b4b3979e33accf0b6f3c66d88df80700d6d1a8cbd92527729
____getStartedAt : 1707893778
____getStatus : cancelled
____getVbankCode : null
____getVbankHolder : null
____getVbankIssuedAt : 0
____getAmount : 100
____getCancelAmount : 100
____getPaidAt : Wed Feb 14 15:56:36 KST 2024
=================

 

 

반응형