Skip to content

Commit

Permalink
YEL-170 [modify] slack webhook request body 값 수정
Browse files Browse the repository at this point in the history
YEL-170 [modify] slack webhook request body 값 수정
  • Loading branch information
hyeonjeongs authored Sep 26, 2023
2 parents 5cc7dd2 + ed62f34 commit e6a9db8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.yello.server.domain.purchase.entity.ProductType;
import com.yello.server.domain.purchase.entity.Purchase;
import com.yello.server.domain.user.entity.User;
import com.yello.server.infrastructure.slack.dto.response.SlackAppleNotificationResponse;
import org.springframework.http.ResponseEntity;

public interface PurchaseManager {
Expand All @@ -20,9 +21,11 @@ void handleAppleTransactionError(ResponseEntity<TransactionInfoResponse> respons

AppleNotificationPayloadVO decodeApplePayload(String signedPayload);

String decodeAppleNotificationData(String signedTransactionInfo);
Purchase decodeAppleNotificationData(String signedTransactionInfo);

void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO);

void refundAppleInApp(AppleNotificationPayloadVO payloadVO);

SlackAppleNotificationResponse checkPurchaseDataByAppleSignedPayload(String payload);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.yello.server.global.common.factory.DecodeTokenFactory;
import com.yello.server.global.common.factory.TokenFactory;
import com.yello.server.global.common.util.ConstantUtil;
import com.yello.server.infrastructure.slack.dto.response.SlackAppleNotificationResponse;
import java.util.Map;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -91,22 +92,23 @@ public AppleNotificationPayloadVO decodeApplePayload(String signedPayload) {
}

@Override
public String decodeAppleNotificationData(String signedTransactionInfo) {
public Purchase decodeAppleNotificationData(String signedTransactionInfo) {

Map<String, Object> decodeToken = DecodeTokenFactory.decodeToken(signedTransactionInfo);
String decodeTransactionId = decodeToken.get("transactionId").toString();

Purchase purchase = purchaseRepository.findByTransactionId(decodeTransactionId)
.orElseThrow(() -> new PurchaseConflictException(NOT_FOUND_TRANSACTION_EXCEPTION));

return purchase.getTransactionId();
return purchase;
}

@Override
public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) {

String transactionId =
decodeAppleNotificationData(payloadVO.data().signedTransactionInfo());
decodeAppleNotificationData(
payloadVO.data().signedTransactionInfo()).getTransactionId();
Purchase purchase = purchaseRepository.findByTransactionId(transactionId)
.orElseThrow(() -> new PurchaseNotFoundException(NOT_FOUND_TRANSACTION_EXCEPTION));

Expand All @@ -121,7 +123,8 @@ public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) {
@Override
public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) {
String transactionId =
decodeAppleNotificationData(payloadVO.data().signedTransactionInfo());
decodeAppleNotificationData(
payloadVO.data().signedTransactionInfo()).getTransactionId();
Purchase purchase = purchaseRepository.findByTransactionId(transactionId)
.orElseThrow(() -> new PurchaseNotFoundException(NOT_FOUND_TRANSACTION_EXCEPTION));
User user = purchase.getUser();
Expand All @@ -142,6 +145,14 @@ public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) {
}
}

@Override
public SlackAppleNotificationResponse checkPurchaseDataByAppleSignedPayload(String payload) {
AppleNotificationPayloadVO payloadVO = decodeApplePayload(payload);
Purchase purchase = decodeAppleNotificationData(payloadVO.data().signedTransactionInfo());

return SlackAppleNotificationResponse.of(payloadVO, purchase);
}

public void validateTicketCount(int ticketCount, User user) {
if (user.getTicketCount() >= ticketCount) {
user.addTicketCount(-Math.abs(ticketCount));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.yello.server.infrastructure.slack.dto.response;

import com.yello.server.domain.purchase.dto.apple.AppleNotificationPayloadVO;
import com.yello.server.domain.purchase.entity.Purchase;
import lombok.Builder;

@Builder
public record SlackAppleNotificationResponse(
String notificationType,
String subtype,
String transactionId,
Long userId,
String userName,
String yelloId

) {

public static SlackAppleNotificationResponse of(AppleNotificationPayloadVO payloadVO,
Purchase purchase) {
return SlackAppleNotificationResponse.builder()
.notificationType(payloadVO.notificationType())
.subtype(payloadVO.subtype())
.transactionId(purchase.getTransactionId())
.userId(purchase.getUser().getId())
.userName(purchase.getUser().getName())
.yelloId(purchase.getUser().getYelloId())
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yello.server.domain.authorization.service.TokenProvider;
import com.yello.server.domain.purchase.dto.apple.AppleNotificationPayloadVO;
import com.yello.server.domain.purchase.service.PurchaseManager;
import com.yello.server.domain.user.entity.User;
import com.yello.server.domain.user.repository.UserRepository;
import com.yello.server.global.common.factory.TimeFactory;
import com.yello.server.infrastructure.slack.dto.response.SlackAppleNotificationResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -241,8 +241,10 @@ private String generateSlackAppleNotificationRequestField(
} catch (Exception e) {
e.printStackTrace();
}
AppleNotificationPayloadVO payloadVO = purchaseManager.decodeApplePayload(payload[0]);

return payloadVO.toString();
SlackAppleNotificationResponse response =
purchaseManager.checkPurchaseDataByAppleSignedPayload(payload[0]);

return response.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.yello.server.domain.purchase.service.PurchaseManager;
import com.yello.server.domain.user.entity.Subscribe;
import com.yello.server.domain.user.entity.User;
import com.yello.server.infrastructure.slack.dto.response.SlackAppleNotificationResponse;
import org.springframework.http.ResponseEntity;

public class FakePurchaseManager implements PurchaseManager {
Expand Down Expand Up @@ -59,7 +60,7 @@ public AppleNotificationPayloadVO decodeApplePayload(String signedPayload) {
}

@Override
public String decodeAppleNotificationData(String signedTransactionInfo) {
public Purchase decodeAppleNotificationData(String signedTransactionInfo) {
return null;
}

Expand All @@ -72,4 +73,9 @@ public void changeSubscriptionStatus(AppleNotificationPayloadVO payloadVO) {
public void refundAppleInApp(AppleNotificationPayloadVO payloadVO) {

}

@Override
public SlackAppleNotificationResponse checkPurchaseDataByAppleSignedPayload(String payload) {
return null;
}
}

0 comments on commit e6a9db8

Please sign in to comment.