diff --git a/packages/in_app_purchase/in_app_purchase/CHANGELOG.md b/packages/in_app_purchase/in_app_purchase/CHANGELOG.md index e3434d4d7118..5e7f54560b8c 100644 --- a/packages/in_app_purchase/in_app_purchase/CHANGELOG.md +++ b/packages/in_app_purchase/in_app_purchase/CHANGELOG.md @@ -1,3 +1,7 @@ +## 0.5.2 + +* Added `rawPrice` and `currencyCode` to the ProductDetails model. + ## 0.5.1+3 * Configured the iOS example App to make use of StoreKit Testing on iOS 14 and higher. diff --git a/packages/in_app_purchase/in_app_purchase/lib/src/in_app_purchase/product_details.dart b/packages/in_app_purchase/in_app_purchase/lib/src/in_app_purchase/product_details.dart index 151086d90f92..ccdec42b7303 100644 --- a/packages/in_app_purchase/in_app_purchase/lib/src/in_app_purchase/product_details.dart +++ b/packages/in_app_purchase/in_app_purchase/lib/src/in_app_purchase/product_details.dart @@ -17,6 +17,8 @@ class ProductDetails { required this.title, required this.description, required this.price, + required this.rawPrice, + required this.currencyCode, this.skProduct, this.skuDetail}); @@ -33,6 +35,15 @@ class ProductDetails { /// Formatted with currency symbol ("$0.99"). final String price; + /// The unformatted price of the product, specified in the App Store Connect or Sku in Google Play console based on the platform. + /// The currency unit for this value can be found in the [currencyCode] property. + /// The value always describes full units of the currency. (e.g. 2.45 in the case of $2.45) + final double rawPrice; + + /// The currency code for the price of the product. + /// Based on the price specified in the App Store Connect or Sku in Google Play console based on the platform. + final String currencyCode; + /// Points back to the `StoreKits`'s [SKProductWrapper] object that generated this [ProductDetails] object. /// /// This is `null` on Android. @@ -49,6 +60,8 @@ class ProductDetails { this.title = product.localizedTitle, this.description = product.localizedDescription, this.price = product.priceLocale.currencySymbol + product.price, + this.rawPrice = double.parse(product.price), + this.currencyCode = product.priceLocale.currencyCode, this.skProduct = product, this.skuDetail = null; @@ -58,6 +71,8 @@ class ProductDetails { this.title = skuDetails.title, this.description = skuDetails.description, this.price = skuDetails.price, + this.rawPrice = ((skuDetails.priceAmountMicros) / 1000000.0).toDouble(), + this.currencyCode = skuDetails.priceCurrencyCode, this.skProduct = null, this.skuDetail = skuDetails; } diff --git a/packages/in_app_purchase/in_app_purchase/pubspec.yaml b/packages/in_app_purchase/in_app_purchase/pubspec.yaml index 6dfb805d6245..c7226078c722 100644 --- a/packages/in_app_purchase/in_app_purchase/pubspec.yaml +++ b/packages/in_app_purchase/in_app_purchase/pubspec.yaml @@ -1,7 +1,7 @@ name: in_app_purchase description: A Flutter plugin for in-app purchases. Exposes APIs for making in-app purchases through the App Store and Google Play. homepage: https://github.com/flutter/plugins/tree/master/packages/in_app_purchase -version: 0.5.1+3 +version: 0.5.2 dependencies: flutter: diff --git a/packages/in_app_purchase/in_app_purchase/test/in_app_purchase_connection/product_details_test.dart b/packages/in_app_purchase/in_app_purchase/test/in_app_purchase_connection/product_details_test.dart new file mode 100644 index 000000000000..a67baf8f5dc6 --- /dev/null +++ b/packages/in_app_purchase/in_app_purchase/test/in_app_purchase_connection/product_details_test.dart @@ -0,0 +1,72 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'package:flutter_test/flutter_test.dart'; +import 'package:in_app_purchase/in_app_purchase.dart'; +import 'package:in_app_purchase/src/store_kit_wrappers/sk_product_wrapper.dart'; + +void main() { + group('Constructor Tests', () { + test( + 'fromSkProduct should correctly parse data from a SKProductWrapper instance.', + () { + final SKProductWrapper dummyProductWrapper = SKProductWrapper( + productIdentifier: 'id', + localizedTitle: 'title', + localizedDescription: 'description', + priceLocale: + SKPriceLocaleWrapper(currencySymbol: '\$', currencyCode: 'USD'), + subscriptionGroupIdentifier: 'com.group', + price: '13.37', + ); + + ProductDetails productDetails = + ProductDetails.fromSKProduct(dummyProductWrapper); + expect(productDetails.id, equals(dummyProductWrapper.productIdentifier)); + expect(productDetails.title, equals(dummyProductWrapper.localizedTitle)); + expect(productDetails.description, + equals(dummyProductWrapper.localizedDescription)); + expect(productDetails.rawPrice, equals(13.37)); + expect(productDetails.currencyCode, + equals(dummyProductWrapper.priceLocale.currencyCode)); + expect(productDetails.skProduct, equals(dummyProductWrapper)); + expect(productDetails.skuDetail, isNull); + }); + + test( + 'fromSkuDetails should correctly parse data from a SkuDetailsWrapper instance', + () { + final SkuDetailsWrapper dummyDetailWrapper = SkuDetailsWrapper( + description: 'description', + freeTrialPeriod: 'freeTrialPeriod', + introductoryPrice: 'introductoryPrice', + introductoryPriceMicros: 'introductoryPriceMicros', + introductoryPriceCycles: 1, + introductoryPricePeriod: 'introductoryPricePeriod', + price: '13.37', + priceAmountMicros: 13370000, + priceCurrencyCode: 'usd', + sku: 'sku', + subscriptionPeriod: 'subscriptionPeriod', + title: 'title', + type: SkuType.inapp, + originalPrice: 'originalPrice', + originalPriceAmountMicros: 1000, + ); + + ProductDetails productDetails = + ProductDetails.fromSkuDetails(dummyDetailWrapper); + expect(productDetails.id, equals(dummyDetailWrapper.sku)); + expect(productDetails.title, equals(dummyDetailWrapper.title)); + expect( + productDetails.description, equals(dummyDetailWrapper.description)); + expect(productDetails.price, equals(dummyDetailWrapper.price)); + expect(productDetails.rawPrice, equals(13.37)); + expect(productDetails.currencyCode, + equals(dummyDetailWrapper.priceCurrencyCode)); + expect(productDetails.skProduct, isNull); + expect(productDetails.skuDetail, equals(dummyDetailWrapper)); + }); + }); +}