Skip to content

Commit

Permalink
YEL-176 [feat] google noti 로직 구현
Browse files Browse the repository at this point in the history
YEL-176 [feat] google noti 로직 구현
  • Loading branch information
euije authored Oct 15, 2023
2 parents ea4ae53 + b949508 commit b63a81a
Show file tree
Hide file tree
Showing 23 changed files with 478 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static com.yello.server.global.common.SuccessCode.POST_GOOGLE_NOTIFICATION_SUCCESS;

import com.yello.server.domain.purchase.dto.request.AppleNotificationRequest;
import com.yello.server.domain.purchase.dto.request.GooglePubSubNotificationRequest;
import com.yello.server.domain.purchase.service.PurchaseService;
import com.yello.server.global.common.dto.BaseResponse;
import com.yello.server.global.common.dto.EmptyObject;
Expand Down Expand Up @@ -32,9 +33,9 @@ public BaseResponse appleNotification(

@PostMapping("/v2/google/notifications")
public BaseResponse<EmptyObject> googleNotification(
@RequestBody Object request
@RequestBody GooglePubSubNotificationRequest request
) {
System.out.println("request = " + request);
return BaseResponse.success(POST_GOOGLE_NOTIFICATION_SUCCESS);
purchaseService.googleNotification(request);
return BaseResponse.success(POST_GOOGLE_NOTIFICATION_SUCCESS, EmptyObject.builder().build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yello.server.domain.purchase.dto.request;

import com.yello.server.domain.purchase.dto.request.google.GooglePubSubMessage;

public record GooglePubSubNotificationRequest(
GooglePubSubMessage message,
String subscription
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yello.server.domain.purchase.dto.request.google;

import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_FIVE_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_ONE_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_TWO_TICKET_ID;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.yello.server.domain.purchase.entity.ProductType;
import org.springframework.util.ObjectUtils;

@JsonInclude(Include.NON_NULL)
public record DeveloperNotification(
String version,
String packageName,
Long eventTimeMillis,
OneTimeProductNotification oneTimeProductNotification,
SubscriptionNotification subscriptionNotification,
TestNotification testNotification
) {

public ProductType getProductType() {
if (ObjectUtils.isEmpty(testNotification)) {
return ProductType.TEST;
} else if (!ObjectUtils.isEmpty(subscriptionNotification)) {
return ProductType.YELLO_PLUS;
} else if (!ObjectUtils.isEmpty(oneTimeProductNotification)) {
switch (oneTimeProductNotification.sku()) {
case GOOGLE_ONE_TICKET_ID -> {
return ProductType.ONE_TICKET;
}
case GOOGLE_TWO_TICKET_ID -> {
return ProductType.TWO_TICKET;
}
case GOOGLE_FIVE_TICKET_ID -> {
return ProductType.FIVE_TICKET;
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yello.server.domain.purchase.dto.request.google;

import java.util.Map;
import java.util.Optional;

public record GooglePubSubMessage(
Optional<Map<String, String>> attributes,
String data,
Long messageId,
Optional<Long> message_id,
Optional<String> publishTime,
Optional<String> publish_time
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yello.server.domain.purchase.dto.request.google;

public record OneTimeProductNotification(
String version,
OneTimeProductNotificationType notificationType,
String purchaseToken,
String sku
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.yello.server.domain.purchase.dto.request.google;

import lombok.Getter;

@Getter
public enum OneTimeProductNotificationType {
ONE_TIME_PRODUCT_PURCHASED(1),
ONE_TIME_PRODUCT_CANCELED(2);

private final Long value;

OneTimeProductNotificationType(long value) {
this.value = value;
}

@Override
public String toString() {
return this.name();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yello.server.domain.purchase.dto.request.google;

public record SubscriptionNotification(
String version,
SubscriptionNotificationType subscriptionNotificationType,
String purchaseToken,
String subscriptionId
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yello.server.domain.purchase.dto.request.google;

import lombok.Getter;

@Getter
public enum SubscriptionNotificationType {
SUBSCRIPTION_RECOVERED(1),
SUBSCRIPTION_RENEWED(2),
SUBSCRIPTION_CANCELED(3),
SUBSCRIPTION_PURCHASED(4),
SUBSCRIPTION_ON_HOLD(5),
SUBSCRIPTION_IN_GRACE_PERIOD(6),
SUBSCRIPTION_RESTARTED(7),
SUBSCRIPTION_PRICE_CHANGE_CONFIRMED(8),
SUBSCRIPTION_DEFERRED(9),
SUBSCRIPTION_PAUSED(10),
SUBSCRIPTION_PAUSE_SCHEDULE_CHANGED(11),
SUBSCRIPTION_REVOKED(12),
SUBSCRIPTION_EXPIRED(13);

private final Long value;

SubscriptionNotificationType(long value) {
this.value = value;
}

@Override
public String toString() {
return this.name();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.yello.server.domain.purchase.dto.request.google;

public record TestNotification(
String version
) {

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
package com.yello.server.domain.purchase.entity;

import static com.yello.server.global.common.util.ConstantUtil.FIVE_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_FIVE_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_ONE_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_TWO_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.GOOGLE_YELLO_PLUS_ID;
import static com.yello.server.global.common.util.ConstantUtil.ONE_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.TWO_TICKET_ID;
import static com.yello.server.global.common.util.ConstantUtil.YELLO_PLUS_ID;

import java.text.MessageFormat;
import java.util.Arrays;
import lombok.Getter;
Expand All @@ -11,7 +20,8 @@ public enum ProductType {
YELLO_PLUS("yello_plus"),
ONE_TICKET("one_ticket"),
TWO_TICKET("two_ticket"),
FIVE_TICKET("five_ticket");
FIVE_TICKET("five_ticket"),
TEST("test");

private final String intial;

Expand All @@ -23,8 +33,26 @@ public static ProductType fromCode(String dbData) {
MessageFormat.format("존재하지 않는 상품타입입니다. {0}", dbData)));
}

public static ProductType getProductType(String productId) {
return switch (productId) {
case GOOGLE_ONE_TICKET_ID, ONE_TICKET_ID -> ProductType.ONE_TICKET;
case GOOGLE_TWO_TICKET_ID, TWO_TICKET_ID -> ProductType.TWO_TICKET;
case GOOGLE_FIVE_TICKET_ID, FIVE_TICKET_ID -> ProductType.FIVE_TICKET;
case GOOGLE_YELLO_PLUS_ID, YELLO_PLUS_ID -> ProductType.YELLO_PLUS;
default -> null;
};
}

public static Integer getTicketAmount(ProductType productType) {
return switch (productType) {
case ONE_TICKET -> 1;
case TWO_TICKET -> 2;
case FIVE_TICKET -> 5;
default -> null;
};
}

public String intial() {
return intial;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
name = "transactionId_unique",
columnNames = {"transactionId"}
),
@UniqueConstraint(
name = "puchaseToken_unique",
columnNames = {"purchaseToken"}
),
}
)
public class Purchase extends AuditingTimeEntity {
Expand All @@ -55,16 +59,29 @@ public class Purchase extends AuditingTimeEntity {
@Convert(converter = GatewayConverter.class)
private Gateway gateway;

@Column
private String purchaseToken;

@Column
@Convert(converter = PurchaseStateConverter.class)
private PurchaseState state;

@Column
private String rawData;

@Column(nullable = false)
@Convert(converter = ProductTypeConverter.class)
private ProductType productType;

public static Purchase of(User user, ProductType productType, Gateway gateway,
String transactionId) {
String transactionId, String purchaseToken, PurchaseState purchaseState, String rawData) {
return Purchase.builder()
.price(setPrice(productType.toString()))
.user(user)
.gateway(gateway)
.purchaseToken(purchaseToken)
.state(purchaseState)
.rawData(rawData)
.productType(productType)
.transactionId(transactionId)
.build();
Expand All @@ -86,8 +103,16 @@ public static int setPrice(String productType) {
}

public static Purchase createPurchase(User user, ProductType productType, Gateway gateway,
String transactionId) {
return Purchase.of(user, productType, gateway, transactionId);
String transactionId, String purchaseToken, PurchaseState purchaseState, String rawData) {
return Purchase.of(user, productType, gateway, transactionId, purchaseToken, purchaseState, rawData);
}

public void setPurchaseState(PurchaseState state) {
this.state = state;
}

public void setRawData(String rawData) {
this.rawData = rawData;
}

public void setTransactionId(String transactionId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.yello.server.domain.purchase.entity;

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

@Getter
@RequiredArgsConstructor
public enum PurchaseState {
ACTIVE("ACTIVE"),
CANCELED("CANCELED"),
PAUSED("PAUSED"),
INACTIVE("INACTIVE");

private final String intial;

public static PurchaseState fromCode(String dbData) {
return Arrays.stream(PurchaseState.values())
.filter(v -> v.getIntial().equals(dbData))
.findAny()
.orElseThrow(() -> new IllegalArgumentException(
MessageFormat.format("존재하지 않는 결제 상태입니다. {0}", dbData)));
}

public String intial() {
return intial;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.yello.server.domain.purchase.entity;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import lombok.extern.log4j.Log4j2;

@Converter
@Log4j2
public class PurchaseStateConverter implements AttributeConverter<PurchaseState, String> {

@Override
public String convertToDatabaseColumn(PurchaseState purchaseState) {
if (purchaseState == null) {
return null;
}
return purchaseState.getIntial();
}

@Override
public PurchaseState convertToEntityAttribute(String dbData) {
if (dbData == null) {
return null;
}
try {
return PurchaseState.fromCode(dbData);
} catch (IllegalArgumentException exception) {
log.error("failure to convert cause unexpected code" + dbData + exception);
throw exception;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public interface PurchaseJpaRepository extends JpaRepository<Purchase, Long> {

Optional<Purchase> findByTransactionId(String transactionId);

Optional<Purchase> findByPurchaseToken(String purchaseToken);

List<Purchase> findAllByUser(User user);

Optional<Purchase> findTopByUserAndProductTypeOrderByCreatedAtDesc(User user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public interface PurchaseRepository {

Optional<Purchase> findByTransactionId(String transactionId);

Optional<Purchase> findByPurchaseToken(String purchaseToken);

List<Purchase> findAllByUser(User user);

Optional<Purchase> findTopByUserAndProductTypeOrderByCreatedAtDesc(User user,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public Optional<Purchase> findByTransactionId(String transactionId) {
return purchaseJpaRepository.findByTransactionId(transactionId);
}

@Override
public Optional<Purchase> findByPurchaseToken(String purchaseToken) {
return purchaseJpaRepository.findByPurchaseToken(purchaseToken);
}

@Override
public List<Purchase> findAllByUser(User user) {
return purchaseJpaRepository.findAllByUser(user);
Expand All @@ -40,7 +45,7 @@ public Optional<Purchase> findTopByUserAndProductTypeOrderByCreatedAtDesc(User u
return purchaseJpaRepository.findTopByUserAndProductTypeOrderByCreatedAtDesc(user,
productType);
}

@Override
public void delete(Purchase purchase) {
purchaseJpaRepository.delete(purchase);
Expand Down
Loading

0 comments on commit b63a81a

Please sign in to comment.