Skip to content

Commit

Permalink
Improve effective price calculation (#48)
Browse files Browse the repository at this point in the history
* Improve effective price calculation
  • Loading branch information
koziolk authored Jul 30, 2020
1 parent 8ad568d commit 6e26a9e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ 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 {
return "SubscriptionPurchaseRequest(receipt=${receipt.substring(IntRange(0,100))} price=$price, currencyCode=$currencyCode, countryCode=$countryCode)"
}
}


data class SubscriptionRenewRequest(
@get:JsonProperty("receipt") @param:NotBlank val receipt: String,
@get:JsonProperty("discountCode") val discountCode: String?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 6e26a9e

Please sign in to comment.