Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 결제 내역 조회 #75

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ordertogether.team14_be.payment.domain;

import java.util.Arrays;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

Expand All @@ -21,4 +22,12 @@ public boolean isSuccess() {
public boolean isFail() {
return this == FAIL;
}

public static PaymentStatus fromString(String statusName) {
return Arrays.stream(PaymentStatus.values())
.filter(paymentStatus -> paymentStatus.name().equalsIgnoreCase(statusName))
.findFirst()
.orElseThrow(
() -> new IllegalArgumentException("%s 는 올바른 결제 상태가 아닙니다.".formatted(statusName)));
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.ordertogether.team14_be.payment.persistence.jpa.repository;

import com.ordertogether.team14_be.payment.domain.PaymentOrder;
import com.ordertogether.team14_be.payment.domain.PaymentStatus;
import com.ordertogether.team14_be.payment.persistence.jpa.entity.PaymentOrderEntity;
import com.ordertogether.team14_be.payment.persistence.jpa.entity.ProductEntity;
import com.ordertogether.team14_be.payment.persistence.jpa.mapper.PaymentOrderMapper;
import com.ordertogether.team14_be.payment.persistence.jpa.mapper.ProductMapper;
import com.ordertogether.team14_be.payment.persistence.repository.PaymentOrderRepository;
import com.ordertogether.team14_be.payment.web.dto.PaymentHistory;
import java.math.BigDecimal;
import java.util.List;
import java.util.NoSuchElementException;
Expand Down Expand Up @@ -74,6 +76,11 @@ public BigDecimal getPaymentTotalAmount(String orderId) {
() -> new NoSuchElementException("주문 번호: %s 에 해당하는 주문이 존재하지 않습니다.".formatted(orderId)));
}

@Override
public List<PaymentHistory> getChargeHistory(Long memberId, PaymentStatus paymentStatus) {
return simpleJpaPaymentOrderRepository.getChargeHistory(memberId, paymentStatus);
}

private ProductEntity getProductEntity(PaymentOrder paymentOrder) {
return simpleJpaProductRepository
.findById(paymentOrder.getProductId())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ordertogether.team14_be.payment.persistence.jpa.repository;

import com.ordertogether.team14_be.payment.domain.PaymentStatus;
import com.ordertogether.team14_be.payment.persistence.jpa.entity.PaymentOrderEntity;
import com.ordertogether.team14_be.payment.web.dto.PaymentHistory;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
Expand All @@ -15,4 +17,12 @@ public interface SimpleJpaPaymentOrderRepository extends JpaRepository<PaymentOr
Optional<BigDecimal> getPaymentTotalAmount(String orderId);

List<PaymentOrderEntity> findByOrderId(String orderId);

@Query(
"SELECT new com.ordertogether.team14_be.payment.web.dto.PaymentHistory(poe.amount, poe.createdAt) FROM PaymentOrderEntity poe"
+ " WHERE poe.orderId IN "
+ " (SELECT bpee.orderId "
+ " FROM PaymentEventEntity bpee "
+ " WHERE bpee.buyerId = :memberId AND bpee.paymentStatus = :paymentStatus) ")
List<PaymentHistory> getChargeHistory(Long memberId, PaymentStatus paymentStatus);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.ordertogether.team14_be.payment.persistence.repository;

import com.ordertogether.team14_be.payment.domain.PaymentOrder;
import com.ordertogether.team14_be.payment.domain.PaymentStatus;
import com.ordertogether.team14_be.payment.web.dto.PaymentHistory;
import java.math.BigDecimal;
import java.util.List;
import java.util.Optional;
Expand All @@ -22,4 +24,6 @@ public interface PaymentOrderRepository {
* @return 총 결제 금액
*/
BigDecimal getPaymentTotalAmount(String orderId);

List<PaymentHistory> getChargeHistory(Long memberId, PaymentStatus paymentStatus);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.ordertogether.team14_be.payment.service;

import com.ordertogether.team14_be.payment.domain.PaymentStatus;
import com.ordertogether.team14_be.payment.persistence.repository.PaymentOrderRepository;
import com.ordertogether.team14_be.payment.web.dto.PaymentHistory;
import com.ordertogether.team14_be.payment.web.response.PaymentHistoryResponse;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class PaymentHistoryService {

private final PaymentOrderRepository paymentOrderRepository;

public PaymentHistoryResponse getChargeHistory(Long memberId, PaymentStatus paymentStatus) {
List<PaymentHistory> histories =
paymentOrderRepository.getChargeHistory(memberId, paymentStatus);
return PaymentHistoryResponse.builder().histories(histories).build();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.ordertogether.team14_be.payment.web.controller;

import com.ordertogether.team14_be.common.web.response.ApiResponse;
import com.ordertogether.team14_be.member.persistence.entity.Member;
import com.ordertogether.team14_be.member.presentation.LoginMember;
import com.ordertogether.team14_be.payment.domain.PaymentStatus;
import com.ordertogether.team14_be.payment.service.PaymentConfirmService;
import com.ordertogether.team14_be.payment.service.PaymentHistoryService;
import com.ordertogether.team14_be.payment.service.PaymentPreparationService;
import com.ordertogether.team14_be.payment.web.request.PaymentConfirmRequest;
import com.ordertogether.team14_be.payment.web.request.PaymentHistoryRequest;
import com.ordertogether.team14_be.payment.web.request.PaymentPrepareRequest;
import com.ordertogether.team14_be.payment.web.response.PaymentConfirmationResponse;
import com.ordertogether.team14_be.payment.web.response.PaymentHistoryResponse;
import com.ordertogether.team14_be.payment.web.response.PaymentPrepareResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -22,12 +29,12 @@ public class PaymentController {

private final PaymentPreparationService paymentPreparationService;
private final PaymentConfirmService paymentConfirmService;
private final PaymentHistoryService paymentHistoryService;

@PostMapping
public ResponseEntity<ApiResponse<PaymentPrepareResponse>> preparePayment(
@RequestBody PaymentPrepareRequest request) {
// todo: 1L -> UserDetail.getUserId()
request.addBuyerId(1L);
@RequestBody PaymentPrepareRequest request, @LoginMember Member member) {
request.addBuyerId(member.getId());
PaymentPrepareResponse data = paymentPreparationService.prepare(request);

return ResponseEntity.ok(ApiResponse.with(HttpStatus.OK, "결제 정보를 저장하였습니다.", data));
Expand All @@ -43,4 +50,15 @@ public ResponseEntity<ApiResponse<PaymentConfirmationResponse>> confirmPayment(
}
return ResponseEntity.ok(ApiResponse.with(HttpStatus.OK, "결제가 완료되었습니다.", data));
}

@GetMapping("/history")
public ResponseEntity<ApiResponse<PaymentHistoryResponse>> getHistory(
@RequestBody PaymentHistoryRequest request, @LoginMember Member member) {
return ResponseEntity.ok(
ApiResponse.with(
HttpStatus.OK,
"포인트 사용 내역을 조회하였습니다.",
paymentHistoryService.getChargeHistory(
member.getId(), PaymentStatus.fromString(request.paymentStatus()))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ordertogether.team14_be.payment.web.controller;

import com.ordertogether.team14_be.common.web.response.ApiResponse;
import com.ordertogether.team14_be.payment.web.request.UsePointRequest;
import com.ordertogether.team14_be.payment.web.response.PointResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/points")
public class PointController {

@PutMapping
public ResponseEntity<ApiResponse<PointResponse>> usePoint(@RequestBody UsePointRequest request) {
return ResponseEntity.ok(
ApiResponse.with(HttpStatus.OK, "포인트 사용이 완료되었습니다.", createUsePointResponse()));
}

private PointResponse createUsePointResponse() {
return PointResponse.builder().remainingPoint(100000).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ordertogether.team14_be.payment.web.dto;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public record PaymentHistory(BigDecimal amount, LocalDateTime date) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.ordertogether.team14_be.payment.web.request;

public record PaymentHistoryRequest(String paymentStatus) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.ordertogether.team14_be.payment.web.request;

public record UsePointRequest(Integer paymentPoint // 사용할 포인트 총액
) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.ordertogether.team14_be.payment.web.response;

import com.ordertogether.team14_be.payment.web.dto.PaymentHistory;
import java.util.List;
import lombok.Builder;

@Builder
public record PaymentHistoryResponse(List<PaymentHistory> histories) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ordertogether.team14_be.payment.web.response;

import lombok.Builder;

@Builder
public record PointResponse(Integer remainingPoint) {}
23 changes: 22 additions & 1 deletion src/main/resources/data.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
INSERT INTO member (id, email, point, phone_number, delivery_name, platform) VALUES (1, '[email protected]', 100000, '010-1234-5678', 'John Doe', 'Kakao');
INSERT INTO member (id, email, point, phone_number, delivery_name, platform) VALUES (1, '[email protected]', 1000000, '010-0000-0001', 'Member01', 'Kakao');
INSERT INTO member (id, email, point, phone_number, delivery_name, platform) VALUES (2, '[email protected]', 1000000, '010-0000-0002', 'Member02', 'Kakao');

INSERT INTO product (id, name, price, created_at, modified_at, created_by, modified_by) VALUES (1, 'Product 1', 10000, now(), now(), 1, 1);
INSERT INTO product (id, name, price, created_at, modified_at, created_by, modified_by) VALUES (2, 'Product 2', 20000, now(), now(), 1, 1);
INSERT INTO product (id, name, price, created_at, modified_at, created_by, modified_by) VALUES (3, 'Product 3', 30000, now(), now(), 1, 1);

INSERT INTO payment_event (id, buyer_id, created_at, modified_at, order_id, order_name, payment_key, payment_status) VALUES (1, 1, now(), now(), 'test-order-id-1', 'Product 1, Product 2, Product 3', 'test-payment-key-1', 'SUCCESS');
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (1, 1, 'test-order-id-1', 'Product 1', 10000, now(), now());
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (2, 2, 'test-order-id-1', 'Product 2', 20000, now(), now());
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (3, 3, 'test-order-id-1', 'Product 3', 30000, now(), now());

INSERT INTO payment_event (id, buyer_id, created_at, modified_at, order_id, order_name, payment_key, payment_status) VALUES (2, 1, now(), now(), 'test-order-id-2', 'Product 1, Product 2', 'test-payment-key-2', 'SUCCESS');
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (4, 1, 'test-order-id-2', 'Product 1', 10000, now(), now());
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (5, 2, 'test-order-id-2', 'Product 2', 20000, now(), now());

INSERT INTO payment_event (id, buyer_id, created_at, modified_at, order_id, order_name, payment_key, payment_status) VALUES (3, 1, now(), now(), 'test-order-id-3', 'Product 1, Product 3', 'test-payment-key-3', 'FAIL');
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (6, 1, 'test-order-id-3', 'Product 1', 10000, now(), now());
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (7, 3, 'test-order-id-3', 'Product 3', 30000, now(), now());

INSERT INTO payment_event (id, buyer_id, created_at, modified_at, order_id, order_name, payment_key, payment_status) VALUES (4, 2, now(), now(), 'test-order-id-4', 'Product 1', 'test-payment-key-4', 'SUCCESS');
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (8, 1, 'test-order-id-4', 'Product 1', 10000, now(), now());

INSERT INTO payment_event (id, buyer_id, created_at, modified_at, order_id, order_name, payment_key, payment_status) VALUES (5, 2, now(), now(), 'test-order-id-5', 'Product 1, Product 2', 'test-payment-key-5', 'SUCCESS');
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (9, 1, 'test-order-id-5', 'Product 1', 10000, now(), now());
INSERT INTO payment_order (id, product_id, order_id, order_name, amount, created_at, modified_at) VALUES (10, 2, 'test-order-id-5', 'Product 2', 20000, now(), now());
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ public interface PaymentDatabaseHelper {
void clean();

void saveTestData();

void setOrderId(String orderId);
}
Loading