Skip to content

Commit

Permalink
Merge pull request #208 from Genti2024/feat/pgreq-two
Browse files Browse the repository at this point in the history
Fix: #207 영수증 검증 API(인앱결제) 로직에 예외 잡는 코드 추가(for Debugging)
  • Loading branch information
LeeJae-H authored Dec 2, 2024
2 parents 2d0af4f + 75b6a20 commit 24a3ea5
Showing 1 changed file with 58 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.google.api.services.androidpublisher.model.ProductPurchase;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import com.gt.genti.error.ExpectedException;
import com.gt.genti.error.ResponseCode;
import com.gt.genti.purchase.dto.request.PurchaseRequestDto;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
Expand All @@ -29,50 +31,66 @@ public class InAppPurchaseService {

public Boolean validateReceipt(Long userId, PurchaseRequestDto purchaseRequestDto) {

try {
// try {
// ================= Google Credential 생성 =================
JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

InputStream inputStream = new ClassPathResource(googleAccountFilePath).getInputStream();
GoogleCredentials credentials = GoogleCredentials
.fromStream(inputStream)
.createScoped(AndroidPublisherScopes.ANDROIDPUBLISHER);

// ======================== API 호출 ========================
AndroidPublisher.Builder builder = new AndroidPublisher.Builder(httpTransport, JSON_FACTORY, new HttpCredentialsAdapter(credentials));
AndroidPublisher publisher = builder.setApplicationName(googleApplicationPackageName).build();
AndroidPublisher.Purchases.Products.Get gas = publisher.purchases()
.products()
.get(
"packageName",
"productId",
"purchaseToken");
ProductPurchase purchase = gas.execute();

AndroidPublisher.Purchases.Products.Get get = publisher.purchases().products()
.get(purchaseRequestDto.getPackageName(), purchaseRequestDto.getProductId(), purchaseRequestDto.getPurchaseToken()); //inapp 아이템의 구매 및 소모 상태 확인
ProductPurchase productPurchase = get.execute(); //검증 결과
System.out.println(productPurchase.toPrettyString());

// 인앱 상품의 소비 상태. 0 아직 소비 안됨(Yet to be consumed) / 1 소비됨(Consumed)
Integer consumptionState = productPurchase.getConsumptionState();

// 개발자가 지정한 임의 문자열 정보
String developerPayload = productPurchase.getDeveloperPayload();

// 구매 상태. 0 구매완료 / 1 취소됨
Integer purchaseState = productPurchase.getPurchaseState();
if(purchaseState == 1){
return false;

AndroidPublisher.Builder builder;
try {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();

InputStream inputStream = new ClassPathResource(googleAccountFilePath).getInputStream();
GoogleCredentials credentials = GoogleCredentials
.fromStream(inputStream)
.createScoped(AndroidPublisherScopes.ANDROIDPUBLISHER);
builder = new AndroidPublisher.Builder(httpTransport, JSON_FACTORY, new HttpCredentialsAdapter(credentials));
} catch (IOException | GeneralSecurityException e){
throw ExpectedException.withLogging(ResponseCode.CashoutNotFound,
"---------------------------------------구매 에러 IOException, GeneralSecurityException 에러 1111111111: " + e + "-------------------------------------------------");
}

// 상품이 구매된 시각. 타임스탬프 형태
Long purchaseTimeMillis = productPurchase.getPurchaseTimeMillis();
AndroidPublisher publisher;
try {
// ======================== API 호출 ========================
publisher = builder.setApplicationName(googleApplicationPackageName).build();
AndroidPublisher.Purchases.Products.Get gas = publisher.purchases()
.products()
.get(
"packageName",
"productId",
"purchaseToken");
ProductPurchase purchase = gas.execute();
} catch (IOException e){
throw ExpectedException.withLogging(ResponseCode.HttpMessageNotReadable,
"---------------------------------------구매 에러 IOException 에러 2222222222: " + e + "-------------------------------------------------");
}


try{
AndroidPublisher.Purchases.Products.Get get = publisher.purchases().products()
.get(purchaseRequestDto.getPackageName(), purchaseRequestDto.getProductId(), purchaseRequestDto.getPurchaseToken()); //inapp 아이템의 구매 및 소모 상태 확인
ProductPurchase productPurchase = get.execute(); //검증 결과
System.out.println(productPurchase.toPrettyString());

return true;
} catch (IOException | GeneralSecurityException e) {
return false;
}
// 인앱 상품의 소비 상태. 0 아직 소비 안됨(Yet to be consumed) / 1 소비됨(Consumed)
Integer consumptionState = productPurchase.getConsumptionState();

// 개발자가 지정한 임의 문자열 정보
String developerPayload = productPurchase.getDeveloperPayload();

// 구매 상태. 0 구매완료 / 1 취소됨
Integer purchaseState = productPurchase.getPurchaseState();
if(purchaseState == 1){
return false;
}

// 상품이 구매된 시각. 타임스탬프 형태
Long purchaseTimeMillis = productPurchase.getPurchaseTimeMillis();

return true;
} catch (IOException e) {
throw ExpectedException.withLogging(ResponseCode.FileTypeNotProvided,
"---------------------------------------구매 에러 IOException 에러 333333333333: " + e + "-------------------------------------------------");
}
}
}

0 comments on commit 24a3ea5

Please sign in to comment.