@@ -7,6 +7,7 @@ import io.branch.referral.util.*
77import java.math.BigDecimal
88
99class BillingGooglePlayV6V7 : BillingGooglePlayInterface {
10+
1011 lateinit var billingClient: BillingClient
1112
1213 companion object {
@@ -29,8 +30,7 @@ class BillingGooglePlayV6V7 : BillingGooglePlayInterface {
2930 }
3031 }
3132
32-
33- override fun startBillingClient (callback : (Boolean ) -> Unit ) {
33+ fun startBillingClient (callback : (Boolean ) -> Unit ) {
3434 if (billingClient.isReady) {
3535 BranchLogger .v(" Billing Client has already been started.." )
3636 callback(true )
@@ -56,10 +56,15 @@ class BillingGooglePlayV6V7 : BillingGooglePlayInterface {
5656 }
5757 }
5858
59- override fun logEventWithPurchase (
60- context : Context ,
61- purchase : Purchase
62- ) {
59+ private val purchasesUpdatedListener = PurchasesUpdatedListener { _, _ -> }
60+
61+ /* *
62+ * Logs a Branch Commerce Event based on an in-app purchase
63+ *
64+ * @param context Current context
65+ * @param purchase Respective purchase
66+ */
67+ fun logEventWithPurchase (context : Context , purchase : Purchase ) {
6368 val productIds = purchase.products
6469 val productList: MutableList <QueryProductDetailsParams .Product > = ArrayList ()
6570 val subsList: MutableList <QueryProductDetailsParams .Product > = ArrayList ()
@@ -158,6 +163,96 @@ class BillingGooglePlayV6V7 : BillingGooglePlayInterface {
158163 }
159164 }
160165
166+ private fun createBUOWithSubsProductDetails (product : ProductDetails ? ): BranchUniversalObject {
167+ if (product != null ) {
168+
169+ val pricingPhaseList =
170+ product.subscriptionOfferDetails?.get(0 )?.pricingPhases?.pricingPhaseList?.get(0 )
171+
172+ val currency = pricingPhaseList?.let {
173+ CurrencyType .valueOf(
174+ it.priceCurrencyCode
175+ )
176+ }
177+
178+ val price = pricingPhaseList?.priceAmountMicros?.div(1000000.0 )
161179
180+ val buo = BranchUniversalObject ()
181+ .setCanonicalIdentifier(product.productId)
182+ .setTitle(product.title)
183+
184+ val contentMetadata = ContentMetadata ()
185+ .addCustomMetadata(" product_type" , product.productType)
186+ .setProductName(product.name)
187+ .setQuantity(1.0 )
188+ .setContentSchema(BranchContentSchema .COMMERCE_PRODUCT )
189+
190+ if (price != null && currency != null ) {
191+ contentMetadata.setPrice(price, currency);
192+ }
193+
194+ buo.contentMetadata = contentMetadata;
195+
196+ return buo;
197+ } else {
198+ return BranchUniversalObject ()
199+ }
200+ }
162201
202+ private fun createBUOWithInAppProductDetails (
203+ product : ProductDetails ? ,
204+ quantity : Int
205+ ): BranchUniversalObject {
206+ if (product != null ) {
207+
208+ val currency = product.oneTimePurchaseOfferDetails?.priceCurrencyCode?.let {
209+ CurrencyType .valueOf(it)
210+ }
211+ val price = product.oneTimePurchaseOfferDetails?.priceAmountMicros?.div(1000000.0 )
212+
213+ val buo = BranchUniversalObject ()
214+ .setCanonicalIdentifier(product.productId)
215+ .setTitle(product.title)
216+
217+ val contentMetadata = ContentMetadata ()
218+ .addCustomMetadata(" product_type" , product.productType)
219+ .setProductName(product.name)
220+ .setQuantity(quantity.toDouble())
221+ .setContentSchema(BranchContentSchema .COMMERCE_PRODUCT )
222+
223+ if (price != null && currency != null ) {
224+ contentMetadata.setPrice(price, currency);
225+ }
226+
227+ buo.contentMetadata = contentMetadata;
228+
229+ return buo;
230+ } else {
231+ return BranchUniversalObject ()
232+ }
233+ }
234+
235+ fun createAndLogEventForPurchase (
236+ context : Context ,
237+ purchase : Purchase ,
238+ contentItems : List <BranchUniversalObject >,
239+ currency : CurrencyType ,
240+ revenue : Double ,
241+ productType : String
242+ ) {
243+ BranchEvent (BRANCH_STANDARD_EVENT .PURCHASE )
244+ .setCurrency(currency)
245+ .setDescription(purchase.orderId)
246+ .setCustomerEventAlias(productType)
247+ .setRevenue(revenue)
248+ .addCustomDataProperty(" package_name" , purchase.packageName)
249+ .addCustomDataProperty(" order_id" , purchase.orderId)
250+ .addCustomDataProperty(" logged_from_IAP" , " true" )
251+ .addCustomDataProperty(" is_auto_renewing" , purchase.isAutoRenewing.toString())
252+ .addCustomDataProperty(" purchase_token" , purchase.purchaseToken)
253+ .addContentItems(contentItems)
254+ .logEvent(context)
255+
256+ BranchLogger .i(" Successfully logged in-app purchase as Branch Event" )
257+ }
163258}
0 commit comments