From 17de05c2b97dc6d603b191ec6767105563b9cabb Mon Sep 17 00:00:00 2001 From: hyeonjeongs Date: Tue, 12 Sep 2023 16:33:27 +0900 Subject: [PATCH] =?UTF-8?q?YEL-155=20[feat]=20apple=20refund=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../purchase/service/PurchaseManager.java | 1 + .../purchase/service/PurchaseManagerImpl.java | 35 ++++++++++++++++++- .../purchase/service/PurchaseService.java | 14 ++++---- .../yello/server/domain/user/entity/User.java | 7 ++-- .../domain/vote/service/VoteService.java | 2 +- .../global/common/util/ConstantUtil.java | 3 ++ .../domain/purchase/FakePurchaseManager.java | 5 +++ .../server/domain/user/small/UserTest.java | 2 +- 8 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/yello/server/domain/purchase/service/PurchaseManager.java b/src/main/java/com/yello/server/domain/purchase/service/PurchaseManager.java index be2ca77f..534ba543 100644 --- a/src/main/java/com/yello/server/domain/purchase/service/PurchaseManager.java +++ b/src/main/java/com/yello/server/domain/purchase/service/PurchaseManager.java @@ -24,4 +24,5 @@ void handleAppleTransactionError(ResponseEntity respons void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO); + void refundAppleInApp(AppleNotificationPayloadVO payloadVO); } diff --git a/src/main/java/com/yello/server/domain/purchase/service/PurchaseManagerImpl.java b/src/main/java/com/yello/server/domain/purchase/service/PurchaseManagerImpl.java index 307c94f9..7a6e7af7 100644 --- a/src/main/java/com/yello/server/domain/purchase/service/PurchaseManagerImpl.java +++ b/src/main/java/com/yello/server/domain/purchase/service/PurchaseManagerImpl.java @@ -3,6 +3,9 @@ import static com.yello.server.global.common.ErrorCode.APPLE_TOKEN_SERVER_EXCEPTION; import static com.yello.server.global.common.ErrorCode.GOOGLE_SUBSCRIPTIONS_SUBSCRIPTION_EXCEPTION; import static com.yello.server.global.common.ErrorCode.NOT_FOUND_TRANSACTION_EXCEPTION; +import static com.yello.server.global.common.util.ConstantUtil.REFUND_FIVE_TICKET; +import static com.yello.server.global.common.util.ConstantUtil.REFUND_ONE_TICKET; +import static com.yello.server.global.common.util.ConstantUtil.REFUND_TWO_TICKET; import com.fasterxml.jackson.databind.ObjectMapper; import com.yello.server.domain.purchase.dto.apple.AppleNotificationPayloadVO; @@ -68,7 +71,7 @@ public void handleAppleTransactionError(ResponseEntity @Override public AppleNotificationPayloadVO decodeApplePayload(String signedPayload) { - + Map jsonPayload = DecodeTokenFactory.decodeToken(signedPayload); ObjectMapper objectMapper = new ObjectMapper(); @@ -117,5 +120,35 @@ public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) { } } + @Override + public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) { + String transactionId = + decodeAppleNotificationData(payloadVO.data().signedTransactionInfo()); + Purchase purchase = purchaseRepository.findByTransactionId(transactionId) + .orElseThrow(() -> new PurchaseNotFoundException(NOT_FOUND_TRANSACTION_EXCEPTION)); + User user = purchase.getUser(); + + switch (purchase.getProductType()) { + case YELLO_PLUS -> { + user.setSubscribe(Subscribe.NORMAL); + } + case ONE_TICKET -> { + validateTicketCount(REFUND_ONE_TICKET, user); + } + case TWO_TICKET -> { + validateTicketCount(REFUND_TWO_TICKET, user); + } + case FIVE_TICKET -> { + validateTicketCount(REFUND_FIVE_TICKET, user); + } + } + } + + public void validateTicketCount(int ticketCount, User user) { + if (user.getTicketCount() >= ticketCount) { + user.setTicketCount(-Math.abs(ticketCount)); + } + } + } diff --git a/src/main/java/com/yello/server/domain/purchase/service/PurchaseService.java b/src/main/java/com/yello/server/domain/purchase/service/PurchaseService.java index a7cccfe2..3e49e7eb 100644 --- a/src/main/java/com/yello/server/domain/purchase/service/PurchaseService.java +++ b/src/main/java/com/yello/server/domain/purchase/service/PurchaseService.java @@ -110,7 +110,7 @@ public void verifyAppleSubscriptionTransaction(Long userId, } purchaseManager.createSubscribe(user, Gateway.APPLE, request.transactionId()); - user.addTicketCount(3); + user.setTicketCount(3); } @Transactional @@ -126,17 +126,17 @@ public void verifyAppleTicketTransaction(Long userId, AppleTransaction request) case ONE_TICKET_ID: purchaseManager.createTicket(user, ProductType.ONE_TICKET, Gateway.APPLE, request.transactionId()); - user.addTicketCount(1); + user.setTicketCount(1); break; case TWO_TICKET_ID: purchaseManager.createTicket(user, ProductType.TWO_TICKET, Gateway.APPLE, request.transactionId()); - user.addTicketCount(2); + user.setTicketCount(2); break; case FIVE_TICKET_ID: purchaseManager.createTicket(user, ProductType.FIVE_TICKET, Gateway.APPLE, request.transactionId()); - user.addTicketCount(5); + user.setTicketCount(5); break; default: throw new PurchaseException(NOT_FOUND_TRANSACTION_EXCEPTION); @@ -206,7 +206,7 @@ public GoogleSubscriptionGetResponse verifyGoogleSubscriptionTransaction(Long us case ConstantUtil.GOOGLE_PURCHASE_SUBSCRIPTION_ACTIVE -> { final Purchase subscribe = purchaseManager.createSubscribe(user, Gateway.GOOGLE, request.orderId()); - user.addTicketCount(3); + user.setTicketCount(3); subscribe.setTransactionId(request.orderId()); } } @@ -259,7 +259,7 @@ public GoogleTicketGetResponse verifyGoogleTicketTransaction(Long userId, Purchase ticket = purchaseManager.createTicket(user, getProductType(request.productId()), Gateway.GOOGLE, request.orderId()); - user.addTicketCount(getTicketAmount(request.productId()) * request.quantity()); + user.setTicketCount(getTicketAmount(request.productId()) * request.quantity()); ticket.setTransactionId(inAppResponse.getBody().orderId()); } else { throw new GoogleBadRequestException(GOOGLE_INAPP_BAD_REQUEST_EXCEPTION); @@ -304,7 +304,7 @@ public void appleNotification(AppleNotificationRequest request) { purchaseManager.changeSubscriptionStatus(payloadVO); break; case APPLE_NOTIFICATION_REFUND: - System.out.println("dd"); + purchaseManager.refundAppleInApp(payloadVO); break; case APPLE_NOTIFICATION_TEST: return; diff --git a/src/main/java/com/yello/server/domain/user/entity/User.java b/src/main/java/com/yello/server/domain/user/entity/User.java index 4bcdf736..d433e453 100644 --- a/src/main/java/com/yello/server/domain/user/entity/User.java +++ b/src/main/java/com/yello/server/domain/user/entity/User.java @@ -122,7 +122,8 @@ public static User of(SignUpRequest signUpRequest, School group) { .group(group) .groupAdmissionYear(signUpRequest.groupAdmissionYear()) .email(signUpRequest.email()) - .deviceToken(Objects.equals(signUpRequest.deviceToken(), "") ? null : signUpRequest.deviceToken()) + .deviceToken(Objects.equals(signUpRequest.deviceToken(), "") ? null + : signUpRequest.deviceToken()) .subscribe(Subscribe.NORMAL) .ticketCount(0) .build(); @@ -167,7 +168,7 @@ public void increaseRecommendPoint() { } public void plusPoint(Integer point) { - if (this.getSubscribe() == Subscribe.NORMAL) { + if (this.getSubscribe()==Subscribe.NORMAL) { this.point += point; return; } @@ -194,7 +195,7 @@ public void setSubscribe(Subscribe subscribe) { this.subscribe = subscribe; } - public void addTicketCount(int ticketCount) { + public void setTicketCount(int ticketCount) { this.ticketCount += ticketCount; } diff --git a/src/main/java/com/yello/server/domain/vote/service/VoteService.java b/src/main/java/com/yello/server/domain/vote/service/VoteService.java index 4e893782..09f79756 100644 --- a/src/main/java/com/yello/server/domain/vote/service/VoteService.java +++ b/src/main/java/com/yello/server/domain/vote/service/VoteService.java @@ -195,7 +195,7 @@ public RevealFullNameResponse revealFullName(Long userId, Long voteId) { } vote.checkNameIndexOf(CHECK_FULL_NAME); - sender.addTicketCount(MINUS_TICKET_COUNT); + sender.setTicketCount(MINUS_TICKET_COUNT); return RevealFullNameResponse.of(vote.getSender()); } diff --git a/src/main/java/com/yello/server/global/common/util/ConstantUtil.java b/src/main/java/com/yello/server/global/common/util/ConstantUtil.java index 0940bdb7..6c330fbd 100644 --- a/src/main/java/com/yello/server/global/common/util/ConstantUtil.java +++ b/src/main/java/com/yello/server/global/common/util/ConstantUtil.java @@ -54,6 +54,9 @@ public class ConstantUtil { public static final String APPLE_NOTIFICATION_TEST = "TEST"; public static final String APPLE_SUBTYPE_AUTO_RENEW_DISABLED = "AUTO_RENEW_DISABLED"; public static final String APPLE_SUBTYPE_VOLUNTARY = "VOLUNTARY"; + public static final int REFUND_ONE_TICKET = 1; + public static final int REFUND_TWO_TICKET = 2; + public static final int REFUND_FIVE_TICKET = 5; private ConstantUtil() { diff --git a/src/test/java/com/yello/server/domain/purchase/FakePurchaseManager.java b/src/test/java/com/yello/server/domain/purchase/FakePurchaseManager.java index cd1e7136..382dca89 100644 --- a/src/test/java/com/yello/server/domain/purchase/FakePurchaseManager.java +++ b/src/test/java/com/yello/server/domain/purchase/FakePurchaseManager.java @@ -67,4 +67,9 @@ public String decodeAppleNotificationData(String signedTransactionInfo) { public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) { } + + @Override + public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) { + + } } diff --git a/src/test/java/com/yello/server/domain/user/small/UserTest.java b/src/test/java/com/yello/server/domain/user/small/UserTest.java index e58eeff1..acc3d969 100644 --- a/src/test/java/com/yello/server/domain/user/small/UserTest.java +++ b/src/test/java/com/yello/server/domain/user/small/UserTest.java @@ -137,7 +137,7 @@ void init() { assertThat(user.getTicketCount()).isZero(); // when - user.addTicketCount(10); + user.setTicketCount(10); // then assertThat(user.getTicketCount()).isEqualTo(10);