Skip to content

Commit

Permalink
Includes offer price and period for android
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-haripriyan committed Dec 1, 2023
1 parent 64de64c commit 7afc243
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -557,26 +556,38 @@ fun defaultSubscriptionPeriod(): Map<String, Any> {
}

fun SubscriptionOffer.toMap(product: CBProduct): Map<String, Any> {
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<String, Any> {
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<String, Any> {
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
)
}
Expand Down
8 changes: 7 additions & 1 deletion example/lib/product_listview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ProductListViewState extends State<ProductListView> {
late var productId = '';
late String? baseProductId = '';
late var currencyCode = '';
late Offer? offer = null;
late ProgressBarUtil mProgressBarUtil;
final TextEditingController productIdTextFieldController =
TextEditingController();
Expand All @@ -45,6 +46,11 @@ class ProductListViewState extends State<ProductListView> {
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(
Expand All @@ -56,7 +62,7 @@ class ProductListViewState extends State<ProductListView> {
),
),
subtitle: Text(
productPrice + ' (currencyCode: ' + currencyCode + ')',
priceToDisplay,
style: const TextStyle(
fontWeight: FontWeight.normal,
fontSize: 15,
Expand Down
50 changes: 43 additions & 7 deletions lib/src/models/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,18 +29,23 @@ 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
factory Product.fromJson(Map<String, dynamic> json) {
debugPrint('json: $json');
final subscriptionPeriod = SubscriptionPeriod.fromMap(
json['subscriptionPeriod'] as Map<String, dynamic>);
var offer = null;
if(json['offer'] != null) {
offer = Offer.fromMap(
json['offer'] as Map<String, dynamic>);
}
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,
Expand All @@ -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 {
Expand All @@ -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<String, dynamic> map) {
final offerPeriod = SubscriptionPeriod.fromMap(
map['period'] as Map<String, dynamic>);
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
Expand Down

0 comments on commit 7afc243

Please sign in to comment.