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

Cherrypick/feature/subscription #13

Merged
merged 5 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation "org.springframework.cloud:spring-cloud-starter-bus-kafka"
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312260649'
implementation 'io.github.lotteon-maven:blooming-blooms-utils:202312290714'
runtimeOnly 'com.h2database:h2'
implementation 'mysql:mysql-connector-java:8.0.33'
testImplementation 'org.mock-server:mockserver-netty:5.11.2' // 사용 중인 MockServer 버전
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.1'
}

dependencyManagement {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kr.bb.payment.controller.clientcontroller;

import bloomingblooms.domain.payment.KakaopayApproveRequestDto;
import bloomingblooms.domain.payment.KakaopayReadyRequestDto;
import bloomingblooms.domain.payment.KakaopayReadyResponseDto;
import bloomingblooms.response.CommonResponse;
import java.time.LocalDateTime;
import kr.bb.payment.dto.request.KakaopayApproveRequestDto;
import kr.bb.payment.dto.request.KakaopayReadyRequestDto;
import kr.bb.payment.dto.response.KakaopayReadyResponseDto;
import kr.bb.payment.service.KakaopayService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -29,7 +29,6 @@ public CommonResponse<KakaopayReadyResponseDto> payReady(
@PostMapping("/approve")
public CommonResponse<LocalDateTime> payApprove(
@RequestBody KakaopayApproveRequestDto approveRequestDto) {

return CommonResponse.success(kakaopayService.kakaoPayApprove(approveRequestDto));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.bb.payment.dto.request;

import javax.annotation.Nullable;
import javax.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand All @@ -23,4 +24,5 @@ public class KakaopayApproveRequestDto {
@NotNull private String cid;
@NotNull private String tid;
@NotNull private String pgToken;
@Nullable private Long deliveryId;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kr.bb.payment.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -15,7 +16,7 @@
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class KakaoPayApproveResponseDto {
public class KakaopayApproveResponseDto {
private String aid; // 요청 고유번호
private String tid; // 결제 고유번호
private String cid; // 가맹점 코드
Expand All @@ -30,8 +31,8 @@ public class KakaoPayApproveResponseDto {
private String itemName;
private Integer quantity;
@JsonProperty("created_at")
private String createdAt;
private LocalDateTime createdAt;
@JsonProperty("approved_at")
private String approvedAt;
private LocalDateTime approvedAt;
private Amount amount;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kr.bb.payment.dto.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDateTime;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Builder
@AllArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class KakaopayApproveResponseDto {
private String aid; // 요청 고유번호
private String tid; // 결제 고유번호
private String cid; // 가맹점 코드
private String sid; // 정기 결제용 id
@JsonProperty("partner_order_id")
private String partnerOrderId; // 가맹점 주문번호
@JsonProperty("partner_user_id")
private String partnerUserId; // 가맹점 회원
@JsonProperty("payment_method_type")
private String paymentMethodType; // 결제수단
@JsonProperty("item_name")
private String itemName;
private Integer quantity;
@JsonProperty("created_at")
private LocalDateTime createdAt;
@JsonProperty("approved_at")
private LocalDateTime approvedAt;
private Amount amount;
}

This file was deleted.

3 changes: 2 additions & 1 deletion src/main/java/kr/bb/payment/entity/OrderType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
@Getter
public enum OrderType {
ORDER_DELIVERY("주문 배송"),
ORDER_PICKUP("주문 픽업");
ORDER_PICKUP("주문 픽업"),
ORDER_SUBSCRIPTION("주문 구독");

private final String message;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/kr/bb/payment/entity/Payment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.bb.payment.entity;

import bloomingblooms.domain.payment.KakaopayApproveRequestDto;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
Expand All @@ -8,7 +9,6 @@
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import kr.bb.payment.dto.request.KakaopayApproveRequestDto;
import kr.bb.payment.entity.common.BaseEntity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/kr/bb/payment/entity/Subscription.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
package kr.bb.payment.entity;

import java.time.LocalDate;
import java.util.Date;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import kr.bb.payment.entity.common.BaseEntity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@Entity
@Table(name = "subscription")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -24,6 +31,10 @@ public class Subscription extends BaseEntity {
@Column(name = "subscription_id")
private Long subscriptionId;

@Builder.Default
@OneToMany(mappedBy = "subscription", cascade = CascadeType.PERSIST, orphanRemoval = true)
private List<SubscriptionRecords> subscriptionRecordsList = new ArrayList<>();

@Column(name = "order_subscription_id", unique = true, nullable = false)
private String orderSubscriptionId;

Expand All @@ -43,11 +54,11 @@ public class Subscription extends BaseEntity {
private Long subscriptionTotalAmount;

@Column(name = "payment_date", nullable = false)
private LocalDate paymentDate;
private LocalDateTime paymentDate;

@Column(name = "start_date", nullable = false)
private LocalDate startDate;
private LocalDateTime startDate;

@Column(name = "end_date")
private LocalDate endDate;
private LocalDateTime endDate;
}
14 changes: 10 additions & 4 deletions src/main/java/kr/bb/payment/entity/SubscriptionRecords.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@
import kr.bb.payment.entity.common.BaseEntity;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Builder
@Entity
@Table(name = "subscription_records")
@AllArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -26,14 +30,16 @@ public class SubscriptionRecords extends BaseEntity {

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "subcription_id")
private Subscription subscriptionId;
private Subscription subscription;

@Column(name = "delivery_id", unique = true, nullable = false)
private Long deliveryId;

@Column(name = "subscription_total_amount", unique = true, nullable = false)
@Column(name = "subscription_total_amount", nullable = false)
private Long subscriptionTotalAmount;

@Column(name = "subscription_status", unique = true, nullable = false)
private String subscriptionStatus;
public void setSubscription(Subscription subscription){
this.subscription = subscription;
subscription.getSubscriptionRecordsList().add(this);
}
}
29 changes: 29 additions & 0 deletions src/main/java/kr/bb/payment/mapper/SubscriptionMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kr.bb.payment.mapper;

import kr.bb.payment.dto.response.KakaopayApproveResponseDto;
import kr.bb.payment.entity.Subscription;
import kr.bb.payment.entity.SubscriptionRecords;

public class SubscriptionMapper {
public static Subscription toSubscriptionEntity(KakaopayApproveResponseDto responseDto) {
return Subscription.builder()
.orderSubscriptionId(responseDto.getPartnerOrderId())
.subscriptionCid(responseDto.getCid())
.subscriptionTid(responseDto.getTid())
.subscriptionSid(responseDto.getSid())
.subscriptionQuantity(Long.valueOf(responseDto.getQuantity()))
.subscriptionTotalAmount(Long.valueOf(responseDto.getAmount().getTotal()))
.startDate(responseDto.getCreatedAt()) // 시작일
.paymentDate(responseDto.getCreatedAt().plusDays(30)) // 다음 결제일을 저장
.build();
}

public static SubscriptionRecords toSubscriptionRecordsEntity(
KakaopayApproveResponseDto responseDto, Subscription subscription, Long deliveryId) {
return SubscriptionRecords.builder()
.subscription(subscription)
.deliveryId(deliveryId)
.subscriptionTotalAmount(Long.valueOf(responseDto.getAmount().getTotal()))
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.bb.payment.repository;

import kr.bb.payment.entity.SubscriptionRecords;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SubscriptionRecordsRepository extends JpaRepository<SubscriptionRecords, Long> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package kr.bb.payment.repository;

import kr.bb.payment.entity.Subscription;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SubscriptionRepository extends JpaRepository<Subscription, Long> {}
22 changes: 11 additions & 11 deletions src/main/java/kr/bb/payment/service/KakaopayService.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package kr.bb.payment.service;

import bloomingblooms.domain.payment.KakaopayApproveRequestDto;
import bloomingblooms.domain.payment.KakaopayReadyRequestDto;
import bloomingblooms.domain.payment.KakaopayReadyResponseDto;
import java.time.LocalDateTime;
import kr.bb.payment.dto.request.KakaopayApproveRequestDto;
import kr.bb.payment.dto.request.KakaopayReadyRequestDto;
import kr.bb.payment.dto.response.KakaoPayApproveResponseDto;
import kr.bb.payment.dto.response.KakaopayReadyResponseDto;
import kr.bb.payment.dto.response.KakaopayApproveResponseDto;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -24,8 +24,8 @@ public class KakaopayService {
@Value("${kakao.admin}")
private String ADMIN_KEY;

@Value("${endpoint.apigateway-service}")
private String APIGATEWAY_SERVICE_URL;
@Value("${endpoint.order-service}")
private String ORDER_SERVICE_URL;

public KakaopayReadyResponseDto kakaoPayReady(KakaopayReadyRequestDto requestDto) {
String cid = requestDto.isSubscriptionPay() ? "TCSUBSCRIP" : "TC0ONETIME";
Expand All @@ -42,13 +42,13 @@ public KakaopayReadyResponseDto kakaoPayReady(KakaopayReadyRequestDto requestDto

parameters.add(
"approval_url",
APIGATEWAY_SERVICE_URL
ORDER_SERVICE_URL
+ "/api/orders/approve/"
+ requestDto.getOrderId()
+ "/"
+ requestDto.getOrderType());
parameters.add("cancel_url", APIGATEWAY_SERVICE_URL + "/api/orders/cancel");
parameters.add("fail_url", APIGATEWAY_SERVICE_URL + "/api/orders/fail");
parameters.add("cancel_url", ORDER_SERVICE_URL + "/api/orders/cancel");
parameters.add("fail_url", ORDER_SERVICE_URL + "/api/orders/fail");

HttpEntity<MultiValueMap<String, String>> requestEntity =
new HttpEntity<>(parameters, this.getHeaders());
Expand All @@ -74,9 +74,9 @@ public LocalDateTime kakaoPayApprove(KakaopayApproveRequestDto requestDto) {

String url = "https://kapi.kakao.com/v1/payment/approve";

restTemplate.postForObject(url, requestEntity, KakaoPayApproveResponseDto.class);
KakaopayApproveResponseDto responseDto = restTemplate.postForObject(url, requestEntity, KakaopayApproveResponseDto.class);

return paymentService.savePaymentInfo(requestDto);
return paymentService.savePaymentInfo(requestDto, responseDto);
}

@NotNull
Expand Down
Loading
Loading