-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from gague-jinsim-in-jadeul/feat/100_add_paym…
…ent_function ✨ Add payment check api
- Loading branch information
Showing
7 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/main/java/org/gagu/gagubackend/payment/controller/PaymentController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package org.gagu.gagubackend.payment.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.gagu.gagubackend.payment.service.PaymentService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "Payment", description = "결제 관련 API 입니다.") | ||
@RestController | ||
@RequestMapping("/payment") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class PaymentController { | ||
private final PaymentService paymentService; | ||
@Operation(summary = "사용자 결제 후 결제 완료 처리", description = "PG 사에 결제 완료 여부를 확인합니다.") | ||
@PostMapping("/complete") | ||
public ResponseEntity<?> checkPayment(@RequestBody String paymentId){ | ||
return paymentService.checkByPaymentId(paymentId); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/gagu/gagubackend/payment/enums/PaymentResultCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.gagu.gagubackend.payment.enums; | ||
|
||
import lombok.Getter; | ||
import org.springframework.http.ResponseEntity; | ||
|
||
@Getter | ||
public enum PaymentResultCode { | ||
OK(200, "거래 내역이 정상 확인됐습니다."), | ||
NOT_FOUND(400,"거래 내역을 확인 할 수 없습니다."); | ||
|
||
private final int code; | ||
private final String msg; | ||
PaymentResultCode(int code, String msg){ | ||
this.code = code; | ||
this.msg = msg; | ||
} | ||
public ResponseEntity<String> toResponseEntity(){ | ||
return ResponseEntity.status(this.code).body(this.msg); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/org/gagu/gagubackend/payment/service/Impl/PaymentServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package org.gagu.gagubackend.payment.service.Impl; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.gagu.gagubackend.global.domain.enums.ResultCode; | ||
import org.gagu.gagubackend.payment.enums.PaymentResultCode; | ||
import org.gagu.gagubackend.payment.service.PaymentService; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
@Slf4j | ||
public class PaymentServiceImpl implements PaymentService { | ||
@Value("${portone.api.key}") | ||
private String PORTONE_API_KEY; | ||
@Value("${portone.api.url}") | ||
private String PORTONE_API_URL; | ||
@Override | ||
public ResponseEntity<?> checkByPaymentId(String paymentId) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
StringBuilder urlBuilder = new StringBuilder(); | ||
|
||
headers.add("Authorization", "PortOne "+PORTONE_API_KEY); | ||
urlBuilder.append(PORTONE_API_URL).append(paymentId); | ||
log.info("[checkByPaymentId] api 호출 : {}", urlBuilder.toString()); | ||
HttpEntity<String> entity = new HttpEntity<>(headers); | ||
try{ | ||
ResponseEntity<String> response = restTemplate.exchange( | ||
urlBuilder.toString(), | ||
HttpMethod.GET, | ||
entity, | ||
String.class); | ||
|
||
if(response.getStatusCode().is4xxClientError()){ | ||
log.error("[checkByPaymentId] 거래 내역 확인 불가"); | ||
return PaymentResultCode.NOT_FOUND.toResponseEntity(); | ||
}else if(response.getStatusCode().is2xxSuccessful()){ | ||
return ResultCode.OK.toResponseEntity(); | ||
} | ||
}catch (Exception e){ | ||
log.error("[checkByPaymentId] error is occured during check payment"); | ||
e.printStackTrace(); | ||
} | ||
return PaymentResultCode.NOT_FOUND.toResponseEntity(); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/main/java/org/gagu/gagubackend/payment/service/PaymentService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.gagu.gagubackend.payment.service; | ||
|
||
import org.springframework.http.ResponseEntity; | ||
|
||
public interface PaymentService { | ||
/** | ||
* @Author HandMK | ||
* @param paymentId | ||
* @return API response | ||
*/ | ||
ResponseEntity<?> checkByPaymentId(String paymentId); | ||
} |