diff --git a/src/main/kotlin/com/dietmap/yaak/api/appstore/receipt/ReceiptResponse.kt b/src/main/kotlin/com/dietmap/yaak/api/appstore/receipt/ReceiptResponse.kt index 4acd936..622ce8e 100644 --- a/src/main/kotlin/com/dietmap/yaak/api/appstore/receipt/ReceiptResponse.kt +++ b/src/main/kotlin/com/dietmap/yaak/api/appstore/receipt/ReceiptResponse.kt @@ -87,7 +87,7 @@ data class LatestReceiptInfo ( @get:JsonProperty("expires_date") val expiresDate: String, @get:JsonProperty("expires_date_ms") val expiresDateMs: Long, @get:JsonProperty("expires_date_pst") val expiresDatePst: String?, - @get:JsonProperty("is_in_intro_offer_period") val isInIntroOfferPeriod: String, + @get:JsonProperty("is_in_intro_offer_period") val isInIntroOfferPeriod: Boolean, @get:JsonProperty("is_trial_period") val isTrialPeriod: Boolean, @get:JsonProperty("is_upgraded") val isUpgraded: Boolean?, @get:JsonProperty("original_purchase_date") val originalPurchaseDate: String, diff --git a/src/main/kotlin/com/dietmap/yaak/api/appstore/subscription/SubscriptionRequest.kt b/src/main/kotlin/com/dietmap/yaak/api/appstore/subscription/SubscriptionRequest.kt index a377b91..8fbfb8f 100644 --- a/src/main/kotlin/com/dietmap/yaak/api/appstore/subscription/SubscriptionRequest.kt +++ b/src/main/kotlin/com/dietmap/yaak/api/appstore/subscription/SubscriptionRequest.kt @@ -10,7 +10,8 @@ data class SubscriptionPurchaseRequest( @get:JsonProperty("price") @param:NotBlank val price: BigDecimal, @get:JsonProperty("currencyCode") @param:NotBlank val currencyCode: String, @get:JsonProperty("countryCode") @param:NotBlank val countryCode: String, - @get:JsonProperty("discountCode") val discountCode: String? + @get:JsonProperty("discountCode") val discountCode: String?, + @get:JsonProperty("effectivePrice") val effectivePrice: BigDecimal? ): Serializable { override fun toString(): String { @@ -18,7 +19,6 @@ data class SubscriptionPurchaseRequest( } } - data class SubscriptionRenewRequest( @get:JsonProperty("receipt") @param:NotBlank val receipt: String, @get:JsonProperty("discountCode") val discountCode: String? diff --git a/src/main/kotlin/com/dietmap/yaak/domain/appstore/AppStoreSubscriptionService.kt b/src/main/kotlin/com/dietmap/yaak/domain/appstore/AppStoreSubscriptionService.kt index afd0d11..52ca60f 100644 --- a/src/main/kotlin/com/dietmap/yaak/domain/appstore/AppStoreSubscriptionService.kt +++ b/src/main/kotlin/com/dietmap/yaak/domain/appstore/AppStoreSubscriptionService.kt @@ -26,12 +26,19 @@ class AppStoreSubscriptionService(val userAppClient: UserAppClient, val appStore val latestReceiptInfo = receiptResponse.latestReceiptInfo!!.stream().findFirst().get() + var effectivePrice = subscriptionPurchaseRequest.price + + // intro offer period purchase + if (latestReceiptInfo.isInIntroOfferPeriod && subscriptionPurchaseRequest.effectivePrice != null) { + effectivePrice = subscriptionPurchaseRequest.effectivePrice + } + val notification = UserAppSubscriptionNotification( notificationType = NotificationType.SUBSCRIPTION_PURCHASED, description = "Subscription purchase from AppStore", productId = latestReceiptInfo.productId, countryCode = subscriptionPurchaseRequest.countryCode, - price = subscriptionPurchaseRequest.price, + price = effectivePrice, currencyCode = subscriptionPurchaseRequest.currencyCode, transactionId = latestReceiptInfo.transactionId, originalTransactionId = latestReceiptInfo.originalTransactionId, diff --git a/src/main/kotlin/com/dietmap/yaak/domain/googleplay/GooglePlaySubscriptionService.kt b/src/main/kotlin/com/dietmap/yaak/domain/googleplay/GooglePlaySubscriptionService.kt index 84e2cb6..0d98a3c 100644 --- a/src/main/kotlin/com/dietmap/yaak/domain/googleplay/GooglePlaySubscriptionService.kt +++ b/src/main/kotlin/com/dietmap/yaak/domain/googleplay/GooglePlaySubscriptionService.kt @@ -27,11 +27,26 @@ class GooglePlaySubscriptionService(val androidPublisherApiClient: AndroidPublis fun handlePurchase(purchaseRequest: PurchaseRequest, initialPurchase: Boolean = true): SubscriptionPurchase? { val subscription = androidPublisherApiClient.Purchases().Subscriptions().get(purchaseRequest.packageName, purchaseRequest.subscriptionId, purchaseRequest.purchaseToken).execute() checkArgument(subscription.paymentState in listOf(PAYMENT_RECEIVED_CODE, PAYMENT_FREE_TRIAL_CODE)) { "Subscription has not been paid yet, paymentState=${subscription.paymentState}" } + + var effectivePrice = BigDecimal(subscription.priceAmountMicros).divide(BigDecimal(1000 * 1000)) + + // test purchase + if (subscription.purchaseType == 0) { + effectivePrice = BigDecimal(0.0) + } + + // introductory price purchase + if (!subscription.orderId.contains("..")) { + if (subscription.introductoryPriceInfo?.introductoryPriceAmountMicros != null) { + effectivePrice = BigDecimal(subscription.introductoryPriceInfo.introductoryPriceAmountMicros).divide(BigDecimal(1000 * 1000)) + } + } + val notificationResponse = userAppClient.sendSubscriptionNotification(UserAppSubscriptionNotification( notificationType = if (initialPurchase) NotificationType.SUBSCRIPTION_PURCHASED else NotificationType.SUBSCRIPTION_RENEWED, appMarketplace = AppMarketplace.GOOGLE_PLAY, countryCode = subscription.countryCode, - price = BigDecimal(subscription.priceAmountMicros).divide(BigDecimal(1000 * 1000)), + price = effectivePrice, currencyCode = subscription.priceCurrencyCode, transactionId = subscription.orderId, originalTransactionId = toInitialOrderId(subscription.orderId),