Skip to content

Commit 0d5abd3

Browse files
authored
Merge pull request #41 from 9oormthon-univ/develop
결제 API 도입
2 parents 5bd8168 + 7a56848 commit 0d5abd3

File tree

12 files changed

+142
-5
lines changed

12 files changed

+142
-5
lines changed

build.gradle

+9
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ configurations {
2121

2222
repositories {
2323
mavenCentral()
24+
maven {url 'https://jitpack.io'}
2425
}
2526

2627
dependencies {
@@ -43,6 +44,14 @@ dependencies {
4344
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
4445
runtimeOnly 'io.jsonwebtoken:jjwt-impl:0.12.6'
4546
runtimeOnly 'io.jsonwebtoken:jjwt-jackson:0.12.6'
47+
48+
// iamPort
49+
implementation 'com.github.iamport:iamport-rest-client-java:0.2.22'
50+
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.9.0'
51+
implementation 'com.google.code.gson:gson:2.8.5'
52+
implementation 'com.squareup.okhttp3:okhttp:4.9.3'
53+
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
54+
4655
}
4756

4857
//tasks.named('test') {

src/main/java/com/univ/sohwakhaeng/global/common/advice/ControllerExceptionAdvice.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.univ.sohwakhaeng.global.common.advice;
22

33

4+
import com.univ.sohwakhaeng.enterprise.exception.EnterpriseNotFoundException;
45
import com.univ.sohwakhaeng.global.common.dto.BaseResponse;
56
import com.univ.sohwakhaeng.global.common.exception.ErrorCode;
67
import com.univ.sohwakhaeng.global.common.exception.model.CustomException;
78
import com.univ.sohwakhaeng.global.common.exception.model.RefreshTokenInvalidException;
9+
import com.univ.sohwakhaeng.payment.exception.PaymentFailException;
810
import jakarta.servlet.http.HttpServletRequest;
911
import lombok.extern.slf4j.Slf4j;
1012
import org.hibernate.service.spi.ServiceException;
1113
import org.springframework.http.HttpStatus;
14+
import org.springframework.http.HttpStatusCode;
1215
import org.springframework.http.ResponseEntity;
1316
import org.springframework.security.authentication.BadCredentialsException;
1417
import org.springframework.security.core.AuthenticationException;
@@ -32,6 +35,13 @@ public class ControllerExceptionAdvice {
3235
/**
3336
* 400 BAD_REQUEST
3437
*/
38+
@ExceptionHandler(PaymentFailException.class)
39+
public ResponseEntity<BaseResponse> handleEnterpriseNotFoundException() {
40+
ErrorCode errorCode = ErrorCode.PAYMENT_FAIL;
41+
BaseResponse response = BaseResponse.error(errorCode);
42+
return new ResponseEntity<>(response, HttpStatusCode.valueOf(errorCode.getHttpStatusCode()));
43+
}
44+
3545
@ResponseStatus(HttpStatus.BAD_REQUEST)
3646
@ExceptionHandler(MethodArgumentNotValidException.class)
3747
protected BaseResponse handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
@@ -41,7 +51,7 @@ protected BaseResponse handleMethodArgumentNotValidException(final MethodArgumen
4151
}
4252

4353
@ResponseStatus(HttpStatus.BAD_REQUEST)
44-
@ExceptionHandler(MissingRequestHeaderException.class)
54+
@ExceptionHandler({MissingRequestHeaderException.class})
4555
protected BaseResponse handleMissingRequestHeaderException(final MissingRequestHeaderException e) {
4656
log.error("Missing Request Header: {}", e.getMessage());
4757
return BaseResponse.error(ErrorCode.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION, String.format("%s (%s)", ErrorCode.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION.getMessage(), e.getHeaderName()));

src/main/java/com/univ/sohwakhaeng/global/common/exception/ErrorCode.java

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public enum ErrorCode {
1919
REQUEST_METHOD_VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "요청 메소드가 잘못됐습니다."),
2020
REQUEST_PARAMETER_VALIDATION_EXCEPTION(HttpStatus.BAD_REQUEST, "요청 파라미터가 잘못됐습니다."),
2121
BAD_CREDENTIALS(HttpStatus.BAD_REQUEST, "아이디 또는 비밀번호가 잘못됐습니다"),
22+
PAYMENT_FAIL(HttpStatus.BAD_REQUEST, "결제 실패"),
2223
/**
2324
* 401 UNAUTHORIZED
2425
*/

src/main/java/com/univ/sohwakhaeng/global/common/exception/SuccessCode.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum SuccessCode {
2020

2121
// Contract 관련
2222
POST_CONTRACT(HttpStatus.CREATED, "계약 등록 성공"),
23-
23+
GET_CONTRACTS(HttpStatus.OK, "계약 조회 성공"),
2424

2525
// Enterprise 관련
2626
GET_ENTERPRISE_DETAILS(HttpStatus.OK, "상점 상세 정보 조회 성공"),
@@ -30,8 +30,8 @@ public enum SuccessCode {
3030
POST_CART(HttpStatus.OK, "장바구니 등록 성공"),
3131
GET_CART(HttpStatus.OK, "장바구니 전체 조회 성공"),
3232

33-
// Contract 관련
34-
GET_CONTRACTS(HttpStatus.OK, "계약 조회 성공"),
33+
// Payment 관련
34+
POST_PAYMENT(HttpStatus.OK, "결제 성공"),
3535

3636
;
3737
private final HttpStatus httpStatus;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.univ.sohwakhaeng.global.config;
2+
3+
import com.siot.IamportRestClient.IamportClient;
4+
import org.springframework.beans.factory.annotation.Value;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
@Configuration
9+
public class AppConfig {
10+
11+
@Value("${iamport.apikey}")
12+
private String iamPortApiKey;
13+
14+
@Value("${iamport.secretkey}")
15+
private String iamPortApiSecretKey;
16+
17+
@Bean
18+
public IamportClient iamportClient() {
19+
return new IamportClient(iamPortApiKey, iamPortApiSecretKey);
20+
}
21+
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.univ.sohwakhaeng.payment.api;
2+
3+
import com.siot.IamportRestClient.response.IamportResponse;
4+
import com.siot.IamportRestClient.response.Payment;
5+
import com.univ.sohwakhaeng.global.common.dto.BaseResponse;
6+
import com.univ.sohwakhaeng.global.common.exception.SuccessCode;
7+
import com.univ.sohwakhaeng.payment.exception.PaymentFailException;
8+
import com.univ.sohwakhaeng.payment.service.PaymentService;
9+
import lombok.RequiredArgsConstructor;
10+
import lombok.extern.slf4j.Slf4j;
11+
import org.springframework.web.bind.annotation.PathVariable;
12+
import org.springframework.web.bind.annotation.PostMapping;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
@RestController
16+
@Slf4j
17+
@RequiredArgsConstructor
18+
public class PaymentController {
19+
20+
private final PaymentService paymentService;
21+
22+
@PostMapping("/public/payment/{impUid}")
23+
public BaseResponse<IamportResponse<Payment>> validateIamport(@PathVariable String impUid) throws PaymentFailException {
24+
return BaseResponse.success(SuccessCode.POST_PAYMENT, paymentService.validateIamport(impUid));
25+
}
26+
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.univ.sohwakhaeng.payment.domain;
2+
3+
public class Payment {
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.univ.sohwakhaeng.payment.exception;
2+
3+
import lombok.Getter;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.bind.annotation.ResponseStatus;
6+
7+
@Getter
8+
@ResponseStatus(HttpStatus.BAD_REQUEST)
9+
public class PaymentFailException extends Exception {
10+
public PaymentFailException(String msg) {
11+
super(msg);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.univ.sohwakhaeng.payment.repository;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository
6+
public interface PaymentRepository {
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.univ.sohwakhaeng.payment.service;
2+
3+
import com.siot.IamportRestClient.IamportClient;
4+
import com.siot.IamportRestClient.response.IamportResponse;
5+
6+
import com.siot.IamportRestClient.response.Payment;
7+
import com.univ.sohwakhaeng.enterprise.exception.EnterpriseNotFoundException;
8+
import com.univ.sohwakhaeng.payment.exception.PaymentFailException;
9+
import com.univ.sohwakhaeng.payment.repository.PaymentRepository;
10+
import lombok.RequiredArgsConstructor;
11+
import lombok.extern.slf4j.Slf4j;
12+
import org.springframework.http.HttpStatus;
13+
import org.springframework.http.ResponseEntity;
14+
import org.springframework.stereotype.Service;
15+
16+
import static com.univ.sohwakhaeng.global.common.exception.ErrorCode.ENTERPRISE_NOT_FOUND;
17+
import static com.univ.sohwakhaeng.global.common.exception.ErrorCode.PAYMENT_FAIL;
18+
19+
@Service
20+
@RequiredArgsConstructor
21+
@Slf4j
22+
public class PaymentService {
23+
24+
private final IamportClient iamportClient;
25+
26+
public IamportResponse<Payment> validateIamport(String imp_uid) throws PaymentFailException {
27+
try {
28+
IamportResponse<Payment> payment = iamportClient.paymentByImpUid(imp_uid);
29+
log.info("결제 요청 응답. 결제 내역 - 주문 번호: {}", payment.getResponse());
30+
return payment;
31+
} catch (Exception e) {
32+
log.info(e.getMessage());
33+
throw new PaymentFailException(PAYMENT_FAIL.getMessage());
34+
}
35+
}
36+
37+
38+
}

src/main/resources/application-dev.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,7 @@ cloud:
5353
region:
5454
static: ${AWS_REGION}
5555
stack:
56-
auto: false
56+
auto: false
57+
iamport:
58+
apikey: ${IAMPORT_API_KEY}
59+
secretkey: ${IAMPORT_SECRET_KEY}

src/main/resources/application-prod.yml

+3
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ server:
6464
key-alias: tomcat
6565
enabled: true
6666

67+
iamport:
68+
apikey: ${IAMPORT_API_KEY}
69+
secretkey: ${IAMPORT_SECRET_KEY}

0 commit comments

Comments
 (0)