Skip to content

Commit

Permalink
Merge pull request #101 from gague-jinsim-in-jadeul/feat/100_add_paym…
Browse files Browse the repository at this point in the history
…ent_function

✨ Add payment check api
  • Loading branch information
MinkeySon authored Dec 17, 2024
2 parents 882e575 + 7053eb2 commit b51962f
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@OpenAPIDefinition(
servers = {
@Server(url = "/", description = "Default Server url")
}
@OpenAPIDefinition(servers = {@Server(url = "https://gagu.me", description = "Default Server url")}
)
@SpringBootApplication
@EnableJpaAuditing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ public ResponseEntity<?> getRequestFurnitures(HttpServletRequest request, @Reque

return ResponseEntity.ok(estimateService.getRequestFurnitures(pageable, nickname, requester));
}
@Operation(summary = "결제 내역 조회", description = "사용자 결제 이후 결재 내역을 PG 사를 통해 확인합니다.")


/**
* RequestSaveFurnitureDto null 확인 메서드
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.gagu.gagubackend.global.config;

import io.swagger.v3.oas.annotations.servers.Server;
import io.swagger.v3.oas.models.Components;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
Expand All @@ -9,15 +8,24 @@
import io.swagger.v3.oas.models.security.SecurityScheme;


import io.swagger.v3.oas.models.servers.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

@Configuration
public class OpenApiConfig {
private static final String BEARER_TOKEN_PREFIX = "Bearer";

@Bean
public OpenAPI openAPI() {
Info info = new Info()
.version("V1.0.0")
.title("GAGU API DOCS")
.description("맞춤형 가구 제작 의뢰 앱 GAGU의 API 문서 입니다.");

String securityJwtName = "JWT";
SecurityRequirement securityRequirement = new SecurityRequirement().addList(securityJwtName);
Components components = new Components()
Expand All @@ -28,6 +36,7 @@ public OpenAPI openAPI() {
.bearerFormat(securityJwtName));

return new OpenAPI()
.info(info)
.addSecurityItem(securityRequirement)
.components(components);
}
Expand Down
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);
}

}
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);
}

}
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();
}
}
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);
}

0 comments on commit b51962f

Please sign in to comment.