Skip to content

Commit

Permalink
[#199, #198] 환불 API 작성, Payment의 Id 타입을 UUID로 변경 (#200)
Browse files Browse the repository at this point in the history
* refactor: PaymentSaveResponse -> PaymentPendingResponse 네이밍 변경, PaymentPendingRequest dto 생성

* feat: Payment 환불 도메인 로직 작성

* feat: Payment 환불 컨트롤러,서비스 로직 작성

* feat: 환불API js코드예시 작성

* refactor: PaymentService#cancelPayment의 불필요한 로직 정리

* refactor: PaymentCancelRequest의 환불금액 필드 제거

* test: Payment 도메인 테스트 작성

* feat: Payment의 id 타입을 UUID로 변경

* feat: Payment 관련 DTO의 merchantUid 타입을 UUID로 변경, 더이상 사용되지 않는 DonationRequest 삭제

* fix: request dto에서 UUID를 받지 못하는 이슈 해결

* fix: PaymentNotFoundException 메시지/에러코드 수정, 사용되지 않는 PaymentRequestException,  PaymentCancelException 삭제

* refactor: IllegalPaymentInfoException 예외메시지 수정

* fix: 환불요청dto 유효성검사 실패시 PaymentCancelRequestException 발생하도록 변경

* test: Payment 컨트롤러 테스트케이스 추가

* refactor: convertToPaymentInfo 수정

* feat: Payment id타입 Long으로 변경, merchantUid UUID타입 컬럼 추가

* style: 리포매팅

* refactor: PaymentRequest -> PaymentCompleteRequest 리네임

* refactor: 사용되지 않는 클래스 제거

* refactor: 환불요청 dto의 merchantUid 타입 UUID로 변경

* feat: Payment의 merchantUid 컬럼 nullable=false 추가

* feat: UUID 커스텀 Validator 생성, DTO의 merchantUid를 String타입으로 변경

* refactor: 스네이크케이스 -> 카멜케이스로 변경

* feat: UUIDValidator null 검증 추가, 테스트작성

Co-authored-by: dwl5 <ssop6403@gmail.com>
  • Loading branch information
Joyykim and DWL5 committed Jul 31, 2021
1 parent 1226a7d commit bab4312
Show file tree
Hide file tree
Showing 41 changed files with 729 additions and 394 deletions.
2 changes: 1 addition & 1 deletion server/securityKey
@@ -1,9 +1,6 @@
package com.example.tyfserver.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand Down
Expand Up @@ -2,7 +2,6 @@

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.util.Base64;
import com.example.tyfserver.common.exception.S3FileNotFoundException;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Expand Up @@ -6,7 +6,7 @@
import com.example.tyfserver.donation.exception.DonationMessageRequestException;
import com.example.tyfserver.donation.exception.DonationRequestException;
import com.example.tyfserver.donation.service.DonationService;
import com.example.tyfserver.payment.dto.PaymentRequest;
import com.example.tyfserver.payment.dto.PaymentCompleteRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
Expand All @@ -25,11 +25,11 @@ public class DonationController {
private final DonationService donationService;

@PostMapping
public ResponseEntity<DonationResponse> createDonation(@Valid @RequestBody PaymentRequest paymentRequest, BindingResult result) {
public ResponseEntity<DonationResponse> createDonation(@Valid @RequestBody PaymentCompleteRequest paymentCompleteRequest, BindingResult result) {
if (result.hasErrors()) {
throw new DonationRequestException();
}
return ResponseEntity.status(HttpStatus.CREATED).body(donationService.createDonation(paymentRequest));
return ResponseEntity.status(HttpStatus.CREATED).body(donationService.createDonation(paymentCompleteRequest));
}

@PostMapping("/{donationId}/messages")
Expand Down

This file was deleted.

Expand Up @@ -9,7 +9,7 @@
import com.example.tyfserver.member.exception.MemberNotFoundException;
import com.example.tyfserver.member.repository.MemberRepository;
import com.example.tyfserver.payment.domain.Payment;
import com.example.tyfserver.payment.dto.PaymentRequest;
import com.example.tyfserver.payment.dto.PaymentCompleteRequest;
import com.example.tyfserver.payment.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
Expand All @@ -28,8 +28,8 @@ public class DonationService {
private final MemberRepository memberRepository;
private final PaymentService paymentService;

public DonationResponse createDonation(PaymentRequest paymentRequest) {
Payment payment = paymentService.completePayment(paymentRequest);
public DonationResponse createDonation(PaymentCompleteRequest paymentCompleteRequest) {
Payment payment = paymentService.completePayment(paymentCompleteRequest);
Donation donation = new Donation(payment);
Member member = memberRepository.findByPageName(payment.getPageName())
.orElseThrow(MemberNotFoundException::new);
Expand Down
Expand Up @@ -8,7 +8,6 @@
import com.example.tyfserver.member.exception.PageNameValidationRequestException;
import com.example.tyfserver.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
Expand Down
Expand Up @@ -80,7 +80,7 @@ public void updateNickName(LoginMember loginMember, String nickName) {

private Member findMember(Long id) {
return memberRepository.findById(id)
.orElseThrow(MemberNotFoundException::new);
.orElseThrow(MemberNotFoundException::new);
}

private void deleteProfile(Member member) {
Expand Down
@@ -1,9 +1,11 @@
package com.example.tyfserver.payment.controller;

import com.example.tyfserver.payment.dto.PaymentSaveRequest;
import com.example.tyfserver.payment.dto.PaymentSaveResponse;
import com.example.tyfserver.payment.exception.PaymentRequestException;
import com.example.tyfserver.payment.exception.PaymentSaveRequestException;
import com.example.tyfserver.payment.dto.PaymentCancelRequest;
import com.example.tyfserver.payment.dto.PaymentCancelResponse;
import com.example.tyfserver.payment.dto.PaymentPendingRequest;
import com.example.tyfserver.payment.dto.PaymentPendingResponse;
import com.example.tyfserver.payment.exception.PaymentCancelRequestException;
import com.example.tyfserver.payment.exception.PaymentPendingRequestException;
import com.example.tyfserver.payment.service.PaymentService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand All @@ -23,13 +25,23 @@ public class PaymentController {
private final PaymentService paymentService;

@PostMapping
public ResponseEntity<PaymentSaveResponse> payment(@Valid @RequestBody PaymentSaveRequest paymentSaveRequest, BindingResult result) {
public ResponseEntity<PaymentPendingResponse> payment(@Valid @RequestBody PaymentPendingRequest paymentPendingRequest, BindingResult result) {
if (result.hasErrors()) {
throw new PaymentPendingRequestException();
}

PaymentPendingResponse response = paymentService.createPayment(paymentPendingRequest);
return ResponseEntity.ok(response);
}

// todo 환불시 이메일 인증 필요
@PostMapping("/cancel")
public ResponseEntity<PaymentCancelResponse> cancelPayment(@Valid @RequestBody PaymentCancelRequest paymentCancelRequest, BindingResult result) {
if (result.hasErrors()) {
throw new PaymentSaveRequestException();
throw new PaymentCancelRequestException();
}

PaymentSaveResponse response = paymentService.createPayment(paymentSaveRequest);
PaymentCancelResponse response = paymentService.cancelPayment(paymentCancelRequest);
return ResponseEntity.ok(response);
}
}
Expand Up @@ -2,14 +2,14 @@

import com.example.tyfserver.common.domain.BaseTimeEntity;
import com.example.tyfserver.payment.exception.IllegalPaymentInfoException;
import com.example.tyfserver.payment.exception.PaymentRequestException;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.util.UUID;

import static com.example.tyfserver.payment.exception.PaymentRequestException.*;
import static com.example.tyfserver.payment.exception.IllegalPaymentInfoException.*;

@Entity
@Getter
Expand All @@ -33,15 +33,36 @@ public class Payment extends BaseTimeEntity {

private String impUid;

public Payment(Long id, Long amount, String email, String pageName) {
@Column(nullable = false)
private UUID merchantUid;

@PrePersist
protected void onCreate() {
merchantUid = UUID.randomUUID();
}

public Payment(Long id, Long amount, String email, String pageName, UUID merchantUid) {
this.id = id;
this.amount = amount;
this.email = email;
this.pageName = pageName;
this.merchantUid = merchantUid;
}

public Payment(Long amount, String email, String pageName, UUID merchantUid) {
this(null, amount, email, pageName, merchantUid);
}

public Payment(Long id, Long amount, String email, String pageName) {
this(id, amount, email, pageName, null);
}

public Payment(Long amount, String email, String pageName) {
this(null, amount, email, pageName);
this(null, amount, email, pageName, null);
}

public void updateStatus(PaymentStatus paymentStatus) {
this.status = paymentStatus;
}

public void complete(PaymentInfo paymentInfo) {
Expand All @@ -50,21 +71,32 @@ public void complete(PaymentInfo paymentInfo) {
this.status = PaymentStatus.PAID;
}

public void updateStatus(PaymentStatus paymentStatus) {
this.status = paymentStatus;
}

private void validatePaymentComplete(PaymentInfo paymentInfo) {
if (!PaymentStatus.isPaid(paymentInfo.getStatus())) {
updateStatus(paymentInfo.getStatus());
throw IllegalPaymentInfoException.from(IllegalPaymentInfoException.ERROR_CODE_NOT_PAID, paymentInfo.getModule());
throw IllegalPaymentInfoException.from(ERROR_CODE_NOT_PAID, paymentInfo.getModule());
}

validatePaymentInfo(paymentInfo);
}

public void cancel(PaymentInfo paymentInfo) {
validatePaymentCancel(paymentInfo);
this.impUid = paymentInfo.getImpUid();
this.status = PaymentStatus.CANCELLED;
}

private void validatePaymentCancel(PaymentInfo paymentInfo) {
if (!PaymentStatus.isCancelled(paymentInfo.getStatus())) {
updateStatus(paymentInfo.getStatus());
throw IllegalPaymentInfoException.from(ERROR_CODE_NOT_CANCELLED, paymentInfo.getModule());
}

validatePaymentInfo(paymentInfo);
}

private void validatePaymentInfo(PaymentInfo paymentInfo) {
if (!id.equals(paymentInfo.getMerchantId())) {
if (!merchantUid.equals(paymentInfo.getMerchantUid())) {
updateStatus(PaymentStatus.INVALID);
throw IllegalPaymentInfoException.from(ERROR_CODE_INVALID_MERCHANT_ID, paymentInfo.getModule());
}
Expand All @@ -76,7 +108,7 @@ private void validatePaymentInfo(PaymentInfo paymentInfo) {

if (!pageName.equals(paymentInfo.getPageName())) {
updateStatus(PaymentStatus.INVALID);
throw IllegalPaymentInfoException.from(ERROR_INVALID_CREATOR, paymentInfo.getModule());
throw IllegalPaymentInfoException.from(ERROR_CODE_INVALID_CREATOR, paymentInfo.getModule());
}
}
}
@@ -1,19 +1,20 @@
package com.example.tyfserver.payment.domain;

import com.example.tyfserver.payment.util.IamPortPaymentServiceConnector;
import lombok.Getter;

import java.util.UUID;

@Getter
public class PaymentInfo {
private Long merchantId;
private UUID merchantUid;
private PaymentStatus status;
private Long amount;
private String pageName;
private String impUid;
private String module;

public PaymentInfo(Long merchantId, PaymentStatus status, Long amount, String pageName, String impUid, String module) {
this.merchantId = merchantId;
public PaymentInfo(UUID merchantUid, PaymentStatus status, Long amount, String pageName, String impUid, String module) {
this.merchantUid = merchantUid;
this.status = status;
this.amount = amount;
this.pageName = pageName;
Expand Down
@@ -1,7 +1,10 @@
package com.example.tyfserver.payment.domain;

import com.example.tyfserver.payment.dto.PaymentRequest;
import java.util.UUID;

public interface PaymentServiceConnector {
PaymentInfo requestPaymentInfo(PaymentRequest paymentRequest);

PaymentInfo requestPaymentInfo(UUID merchantUid);

PaymentInfo requestPaymentCancel(UUID merchantUid);
}
Expand Up @@ -11,9 +11,10 @@ public enum PaymentStatus {
}

public static boolean isPaid(PaymentStatus paymentStatus) {
if (paymentStatus.equals(PAID)) {
return true;
}
return false;
return paymentStatus.equals(PAID);
}

public static boolean isCancelled(PaymentStatus paymentStatus) {
return paymentStatus.equals(CANCELLED);
}
}
Expand Up @@ -12,22 +12,22 @@ public class IamPortPaymentInfo {
public IamPortPaymentInfo(Response response) {
this.response = response;
}

@NoArgsConstructor
@Getter
public class Response {
public static class Response {
String status;
String merchant_uid;
String merchantUid;
String amount;
String name;
String imp_uid;
String impUid;

public Response(String status, String merchant_uid, String amount, String name, String imp_uid) {
public Response(String status, String merchantUid, String amount, String name, String impUid) {
this.status = status;
this.merchant_uid = merchant_uid;
this.merchantUid = merchantUid;
this.amount = amount;
this.name = name;
this.imp_uid = imp_uid;
this.impUid = impUid;
}
}
}
@@ -0,0 +1,18 @@
package com.example.tyfserver.payment.dto;

import com.example.tyfserver.payment.util.UUID;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PaymentCancelRequest {

@UUID
private String merchantUid;

public PaymentCancelRequest(String merchantUid) {
this.merchantUid = merchantUid;
}
}
@@ -0,0 +1,18 @@
package com.example.tyfserver.payment.dto;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.UUID;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PaymentCancelResponse {

private UUID merchantUid;

public PaymentCancelResponse(UUID merchantUid) {
this.merchantUid = merchantUid;
}
}

0 comments on commit bab4312

Please sign in to comment.