Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 16 additions & 25 deletions lib/src/chargebee.dart
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,12 @@ class Chargebee {
/// [queryParams] The map value to be passed as queryParams.
/// Example: {"customer_id": "abc"}.
///
/// The list of [Subscripton] object be returned if api success.
/// The list of [Subscription] object be returned if api success.
/// Throws an [PlatformException] in case of failure.
static Future<List<Subscripton?>> retrieveSubscriptions(
static Future<List<Subscription?>> retrieveSubscriptions(
Map<String, String> queryParams,
) async {
final subscriptions = <Subscripton>[];
final subscriptions = <Subscription>[];
if (_isIOS) {
final String result = await platform.invokeMethod(
Constants.mSubscriptionMethod,
Expand All @@ -189,8 +189,8 @@ class Chargebee {
final List jsonData = jsonDecode(result.toString());
if (jsonData.isNotEmpty) {
for (final value in jsonData) {
final wrapper = SubscriptonList.fromJson(value);
subscriptions.add(wrapper.subscripton!);
final wrapper = SubscriptionList.fromJson(value);
subscriptions.add(wrapper.subscription!);
}
}
} else {
Expand All @@ -201,8 +201,8 @@ class Chargebee {
final List jsonData = jsonDecode(result);
if (jsonData.isNotEmpty) {
for (final value in jsonData) {
final wrapper = SubscriptonList.fromJsonAndroid(value);
subscriptions.add(wrapper.subscripton!);
final wrapper = SubscriptionList.fromJsonAndroid(value);
subscriptions.add(wrapper.subscription!);
}
}
}
Expand Down Expand Up @@ -234,10 +234,8 @@ class Chargebee {
static Future<List<String>> retrieveProductIdentifiers([
Map<String, String>? queryParams,
]) async {
final String result =
await platform.invokeMethod(Constants.mProductIdentifiers, queryParams);
return CBProductIdentifierWrapper.fromJson(jsonDecode(result))
.productIdentifiersList;
final String result = await platform.invokeMethod(Constants.mProductIdentifiers, queryParams);
return CBProductIdentifierWrapper.fromJson(jsonDecode(result)).productIdentifiersList;
}

/// Retrieves entitlements for the subscription.
Expand All @@ -250,8 +248,7 @@ class Chargebee {
static Future<List<CBEntitlementWrapper>> retrieveEntitlements(
Map<String, String> queryParams,
) async {
final String result =
await platform.invokeMethod(Constants.mGetEntitlements, queryParams);
final String result = await platform.invokeMethod(Constants.mGetEntitlements, queryParams);
return CBEntitlementList.fromJson(jsonDecode(result)).entitlementsList;
}

Expand All @@ -268,16 +265,14 @@ class Chargebee {
var itemsFromServer = [];
final listItems = <CBItem>[];
if (_isIOS) {
final String result =
await platform.invokeMethod(Constants.mRetrieveAllItems, queryParams);
final String result = await platform.invokeMethod(Constants.mRetrieveAllItems, queryParams);
itemsFromServer = jsonDecode(result);
for (final value in itemsFromServer) {
final wrapper = CBItemsList.fromJson(value);
listItems.add(wrapper.cbItem!);
}
} else {
final String result =
await platform.invokeMethod(Constants.mRetrieveAllItems, queryParams);
final String result = await platform.invokeMethod(Constants.mRetrieveAllItems, queryParams);
itemsFromServer = jsonDecode(result);
for (final value in itemsFromServer) {
final wrapper = CBItemsList.fromJsonAndroid(value);
Expand All @@ -300,16 +295,14 @@ class Chargebee {
var plansFromServer = [];
final listPlans = <CBPlan>[];
if (_isIOS) {
final String result =
await platform.invokeMethod(Constants.mRetrieveAllPlans, queryParams);
final String result = await platform.invokeMethod(Constants.mRetrieveAllPlans, queryParams);
plansFromServer = jsonDecode(result);
for (final value in plansFromServer) {
final wrapper = CBPlansList.fromJson(value);
listPlans.add(wrapper.cbPlan!);
}
} else {
final String result =
await platform.invokeMethod(Constants.mRetrieveAllPlans, queryParams);
final String result = await platform.invokeMethod(Constants.mRetrieveAllPlans, queryParams);
plansFromServer = jsonDecode(result);
for (final value in plansFromServer) {
final wrapper = CBPlansList.fromJsonAndroid(value);
Expand Down Expand Up @@ -344,8 +337,7 @@ class Chargebee {
debugPrint('result $result');
if (result.isNotEmpty) {
for (var i = 0; i < result.length; i++) {
final subscription =
CBRestoreSubscription.fromJson(jsonDecode(result[i].toString()));
final subscription = CBRestoreSubscription.fromJson(jsonDecode(result[i].toString()));
restorePurchases.add(subscription);
}
}
Expand Down Expand Up @@ -397,8 +389,7 @@ class Chargebee {
///
/// If purchase success [NonSubscriptionPurchaseResult] object be returned.
/// Throws an [PlatformException] in case of failure.
static Future<NonSubscriptionPurchaseResult>
validateReceiptForNonSubscriptions(
static Future<NonSubscriptionPurchaseResult> validateReceiptForNonSubscriptions(
String productId,
ProductType productType, [
CBCustomer? customer,
Expand Down
59 changes: 24 additions & 35 deletions lib/src/models/product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,12 @@ class Product {
/// Subscription period, which consists of unit and number of units
late SubscriptionPeriod subscriptionPeriod;

Product(this.id, this.price, this.priceString, this.title, this.currencyCode,
this.subscriptionPeriod);
Product(this.id, 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>);
final subscriptionPeriod = SubscriptionPeriod.fromMap(json['subscriptionPeriod'] as Map<String, dynamic>);
return Product(
json['productId'] as String,
json['productPrice'] as num,
Expand Down Expand Up @@ -78,8 +76,7 @@ class PurchaseResult {
json['status'] as String,
);
@override
String toString() =>
'PurchaseResult(subscriptionId: $subscriptionId, planId: $planId, status: $status)';
String toString() => 'PurchaseResult(subscriptionId: $subscriptionId, planId: $planId, status: $status)';
}

/// Store the information related to non subscription product purchase
Expand All @@ -97,16 +94,14 @@ class NonSubscriptionPurchaseResult {

/// convert json data and returned NonSubscriptionPurchaseResult object
factory NonSubscriptionPurchaseResult.fromJson(Map<String, dynamic> json) =>
NonSubscriptionPurchaseResult(json['invoiceId'] as String,
json['chargeId'] as String, json['customerId'] as String);
NonSubscriptionPurchaseResult(json['invoiceId'] as String, json['chargeId'] as String, json['customerId'] as String);

@override
String toString() =>
'NonSubscriptionPurchaseResult(invoiceId: $invoiceId, chargeId: $chargeId,customerId: $customerId)';
String toString() => 'NonSubscriptionPurchaseResult(invoiceId: $invoiceId, chargeId: $chargeId,customerId: $customerId)';
}

/// Store information about the list of subscriptions
class Subscripton {
class Subscription {
/// subscription id
String? subscriptionId;

Expand Down Expand Up @@ -135,7 +130,7 @@ class Subscripton {
/// subscription term start
String? currentTermStartString;

Subscripton({
Subscription({
this.subscriptionId,
this.customerId,
this.status,
Expand All @@ -145,8 +140,8 @@ class Subscripton {
this.planAmount,
});

/// convert json data into Subscripton model for iOS
Subscripton.fromJson(Map<String, dynamic> json) {
/// convert json data into Subscription model for iOS
Subscription.fromJson(Map<String, dynamic> json) {
subscriptionId = json['subscription_id'] as String;
customerId = json['customer_id'] as String;
status = json['status'] as String;
Expand All @@ -158,8 +153,8 @@ class Subscripton {
/// Plan amount sometime we are getting double value sometime Int
}

/// convert json data into Subscripton model for Android
Subscripton.fromJsonAndroid(Map<String, dynamic> json) {
/// convert json data into Subscription model for Android
Subscription.fromJsonAndroid(Map<String, dynamic> json) {
activatedAtString = json['activated_at'].toString();
currentTermEndString = json['current_term_end'].toString();
currentTermStartString = json['current_term_start'].toString();
Expand All @@ -171,36 +166,32 @@ class Subscripton {
}

/// Gets list of subscriptions
class SubscriptonList {
class SubscriptionList {
/// subscription object
Subscripton? subscripton;
Subscription? subscription;

SubscriptonList({this.subscripton});
SubscriptionList({this.subscription});

/// json data converts into SubscriptonList for iOS
SubscriptonList.fromJson(Map<String, dynamic> json) {
subscripton = json['cb_subscription'] != null
? Subscripton.fromJson(json['cb_subscription'])
: null;
/// json data converts into SubscriptionList for iOS
SubscriptionList.fromJson(Map<String, dynamic> json) {
subscription = json['cb_subscription'] != null ? Subscription.fromJson(json['cb_subscription']) : null;
}

/// json data converts into SubscriptonList for Android
SubscriptonList.fromJsonAndroid(Map<String, dynamic> json) {
subscripton = json['cb_subscription'] != null
? Subscripton.fromJsonAndroid(json['cb_subscription'])
: null;
/// json data converts into SubscriptionList for Android
SubscriptionList.fromJsonAndroid(Map<String, dynamic> json) {
subscription = json['cb_subscription'] != null ? Subscription.fromJsonAndroid(json['cb_subscription']) : null;
}
}

class CBSubscriptionWrapper {
List<Subscripton>? list;
List<Subscription>? list;

CBSubscriptionWrapper({this.list});

CBSubscriptionWrapper.fromJson(List<Map<String, dynamic>> json) {
final subsArray = <Subscripton>[];
final subsArray = <Subscription>[];
for (final value in json) {
subsArray.add(Subscripton.fromJson(value));
subsArray.add(Subscription.fromJson(value));
}
}
}
Expand Down Expand Up @@ -261,9 +252,7 @@ class CBEntitlementWrapper {

/// Convert entitlement object into CBEntitlement
CBEntitlementWrapper.fromJson(dynamic json) {
cbEntitlement = json['subscription_entitlement'] != null
? CBEntitlement.fromJson(json['subscription_entitlement'])
: null;
cbEntitlement = json['subscription_entitlement'] != null ? CBEntitlement.fromJson(json['subscription_entitlement']) : null;
}
}

Expand Down