-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
107 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
billinghelper/src/main/kotlin/com/vojtkovszky/billinghelper/SubscriptionPurchaseParams.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.vojtkovszky.billinghelper | ||
|
||
import com.android.billingclient.api.BillingFlowParams | ||
import com.android.billingclient.api.ProductDetails | ||
|
||
/** | ||
* Additional parameters often required for subscription purchases. | ||
* | ||
* @param basePlanId define base plan id to initiate a purchase with. If no value is provided, first offer will be used. Can be combined with [offerId] | ||
* @param offerId define offer id to initiate a purchase with. If no value is provided, first offer will be used. Can be combined with [basePlanId] | ||
* @param updateOldToken Google Play Billing purchase token that the user is upgrading or downgrading from. | ||
* See [https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.SubscriptionUpdateParams]. | ||
* Note that [productName] must also be a subscription for this to take effect. | ||
* @param updateExternalTransactionId If the originating transaction for the subscription | ||
* that the user is upgrading or downgrading from was processed via alternative billing. | ||
* See [https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.SubscriptionUpdateParams.Builder#setOriginalExternalTransactionId(java.lang.String)]. | ||
* @param updateReplacementMode Supported replacement modes to replace an existing subscription with a new one. | ||
* See [https://developer.android.com/reference/com/android/billingclient/api/BillingFlowParams.SubscriptionUpdateParams.ReplacementMode]. | ||
*/ | ||
data class SubscriptionPurchaseParams( | ||
val basePlanId: String? = null, | ||
val offerId: String? = null, | ||
val updateOldToken: String? = null, | ||
val updateExternalTransactionId: String? = null, | ||
val updateReplacementMode: Int? = null | ||
) { | ||
// return offer token for given product details | ||
internal fun getOfferToken(productDetails: ProductDetails): String? { | ||
// set if found, or apply first found by default. | ||
return productDetails.subscriptionOfferDetails?.firstOrNull { offer -> | ||
(basePlanId == null || offer.basePlanId == basePlanId) && | ||
(offerId == null || offer.offerId == offerId) | ||
}?.offerToken | ||
} | ||
|
||
// return SubscriptionUpdateParams if we can build it | ||
internal fun getSubscriptionUpdateParams(): BillingFlowParams.SubscriptionUpdateParams? { | ||
if (updateOldToken != null || updateExternalTransactionId != null) { | ||
return BillingFlowParams.SubscriptionUpdateParams | ||
.newBuilder() | ||
.apply { | ||
updateOldToken?.let { token -> | ||
setOldPurchaseToken(token) | ||
} | ||
updateExternalTransactionId?.let { id -> | ||
setOriginalExternalTransactionId(id) | ||
} | ||
updateReplacementMode?.let { mode -> | ||
setSubscriptionReplacementMode(mode) | ||
} | ||
} | ||
.build() | ||
} | ||
return null | ||
} | ||
} |