diff --git a/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt b/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt index 648fa9b..3bc397a 100644 --- a/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt +++ b/android/src/main/kotlin/com/chargebee/flutter/sdk/ChargebeeFlutterSdkPlugin.kt @@ -1,7 +1,6 @@ package com.chargebee.flutter.sdk import android.app.Activity -import android.content.Context import android.util.Log import androidx.annotation.NonNull import com.chargebee.android.Chargebee @@ -557,26 +556,38 @@ fun defaultSubscriptionPeriod(): Map { } fun SubscriptionOffer.toMap(product: CBProduct): Map { - val pricingPhase = pricingPhases.first() - return mapOf( + val pricingPhase = pricingPhases.last() + val subscription = mutableMapOf( "productId" to product.id, + "productType" to product.type.value, "baseProductId" to basePlanId, - "offerId" to offerId.orEmpty(), "offerToken" to offerToken, "productTitle" to product.title, "productPrice" to pricingPhase.convertPriceAmountInMicros(), "productPriceString" to pricingPhase.formattedPrice, "currencyCode" to pricingPhase.currencyCode, - "subscriptionPeriod" to subscriptionPeriod() + "subscriptionPeriod" to pricingPhase.subscriptionPeriod() ) + offerId?.let { + subscription["offer"] = offer() + } + return subscription } -private fun SubscriptionOffer.subscriptionPeriod(): Any { - val subscriptionPricing = this.pricingPhases.last() - val subscriptionPeriod = subscriptionPricing.billingPeriod - val numberOfUnits = subscriptionPeriod?.substring(1, subscriptionPeriod.length - 1)?.toInt() +private fun SubscriptionOffer.offer(): Map { + val pricingPhase = pricingPhases.first() + return mapOf( + "id" to offerId.orEmpty(), + "price" to pricingPhase.convertPriceAmountInMicros(), + "priceString" to pricingPhase.formattedPrice, + "period" to pricingPhase.subscriptionPeriod() + ) +} +private fun PricingPhase.subscriptionPeriod(): Map { + val subscriptionPeriod = billingPeriod + val numberOfUnits = subscriptionPeriod?.substring(1, subscriptionPeriod.length - 1)?.toInt() ?: 0 return mapOf( - "periodUnit" to subscriptionPricing.periodUnit(), + "periodUnit" to periodUnit(), "numberOfUnits" to numberOfUnits ) } diff --git a/example/lib/product_listview.dart b/example/lib/product_listview.dart index 9e7125e..9c837f2 100644 --- a/example/lib/product_listview.dart +++ b/example/lib/product_listview.dart @@ -23,6 +23,7 @@ class ProductListViewState extends State { late var productId = ''; late String? baseProductId = ''; late var currencyCode = ''; + late Offer? offer = null; late ProgressBarUtil mProgressBarUtil; final TextEditingController productIdTextFieldController = TextEditingController(); @@ -45,6 +46,11 @@ class ProductListViewState extends State { productId = listProducts[pos].id; baseProductId = listProducts[pos].baseProductId; currencyCode = listProducts[pos].currencyCode; + offer = listProducts[pos].offer; + var priceToDisplay = productPrice + ' (currencyCode: ' + currencyCode + ')'; + if(offer != null) { + priceToDisplay = offer!.priceString + ' Offer:' + offer!.id ; + } return Card( child: ListTile( title: Text( @@ -56,7 +62,7 @@ class ProductListViewState extends State { ), ), subtitle: Text( - productPrice + ' (currencyCode: ' + currencyCode + ')', + priceToDisplay, style: const TextStyle( fontWeight: FontWeight.normal, fontSize: 15, diff --git a/lib/src/models/product.dart b/lib/src/models/product.dart index 7bb8ae2..3148993 100644 --- a/lib/src/models/product.dart +++ b/lib/src/models/product.dart @@ -5,13 +5,13 @@ class Product { /// Id of the product late String id; - /// For Android, the basePlanId will be returned. + /// For Android, returns the basePlanId late String? baseProductId; - /// For Android, the Offer ID will be returned. - late String? offerId; + /// For Android, returns the Offer details if present + late Offer? offer; - /// For Android, the offerToken will be returned. + /// For Android, returns the offerToken late String? offerToken; /// Title of the product @@ -29,7 +29,7 @@ class Product { /// Subscription period, which consists of unit and number of units late SubscriptionPeriod subscriptionPeriod; - Product(this.id, this.baseProductId, this.offerId, this.offerToken, this.price, this.priceString, this.title, this.currencyCode, + Product(this.id, this.baseProductId, this.offer, this.offerToken, this.price, this.priceString, this.title, this.currencyCode, this.subscriptionPeriod); /// convert json data into Product model @@ -37,10 +37,15 @@ class Product { debugPrint('json: $json'); final subscriptionPeriod = SubscriptionPeriod.fromMap( json['subscriptionPeriod'] as Map); + var offer = null; + if(json['offer'] != null) { + offer = Offer.fromMap( + json['offer'] as Map); + } return Product( json['productId'] as String, json['baseProductId'] as String?, - json['offerId'] as String?, + offer, json['offerToken'] as String?, json['productPrice'] as num, json['productPriceString'] as String, @@ -52,7 +57,7 @@ class Product { @override String toString() => - 'Product(id: $id, baseProductId: $baseProductId, offerId: $offerId, offerToken: $offerToken, price: $price, priceString: $priceString title: $title, currencyCode: $currencyCode, subscriptionPeriod: $subscriptionPeriod)'; + 'Product(id: $id, baseProductId: $baseProductId, offer: $offer, offerToken: $offerToken, price: $price, priceString: $priceString title: $title, currencyCode: $currencyCode, subscriptionPeriod: $subscriptionPeriod)'; } class SubscriptionPeriod { @@ -71,6 +76,37 @@ class SubscriptionPeriod { } } +class Offer { + /// Offer Id + late String id; + + /// Local currency offer price for the product offer in double + late num price; + + /// Local currency offer price for the product offer in string + late String priceString; + + /// Subscription Offer period, which consists of unit and number of units + late SubscriptionPeriod offerPeriod; + + Offer(this.id, this.price, this.priceString, this.offerPeriod); + + /// convert map object into SubscriptionPeriod + factory Offer.fromMap(Map map) { + final offerPeriod = SubscriptionPeriod.fromMap( + map['period'] as Map); + return Offer(map['id'] as String, + map['price'] as num, + map['priceString'] as String, + offerPeriod + ); + } + + @override + String toString() => + 'Offer(id: $id, price: $price, priceString: $priceString, offerPeriod: $offerPeriod)'; +} + /// Store the information related to product subscriptions class PurchaseResult { /// product subscriptions id