Skip to content

Commit ee5d15e

Browse files
authored
[in_app_purchase] Added currency code and numerical price to product detail model. (#3794)
1 parent 1dbc262 commit ee5d15e

File tree

4 files changed

+92
-1
lines changed

4 files changed

+92
-1
lines changed

packages/in_app_purchase/in_app_purchase/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.5.2
2+
3+
* Added `rawPrice` and `currencyCode` to the ProductDetails model.
4+
15
## 0.5.1+3
26

37
* Configured the iOS example App to make use of StoreKit Testing on iOS 14 and higher.

packages/in_app_purchase/in_app_purchase/lib/src/in_app_purchase/product_details.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ class ProductDetails {
1717
required this.title,
1818
required this.description,
1919
required this.price,
20+
required this.rawPrice,
21+
required this.currencyCode,
2022
this.skProduct,
2123
this.skuDetail});
2224

@@ -33,6 +35,15 @@ class ProductDetails {
3335
/// Formatted with currency symbol ("$0.99").
3436
final String price;
3537

38+
/// The unformatted price of the product, specified in the App Store Connect or Sku in Google Play console based on the platform.
39+
/// The currency unit for this value can be found in the [currencyCode] property.
40+
/// The value always describes full units of the currency. (e.g. 2.45 in the case of $2.45)
41+
final double rawPrice;
42+
43+
/// The currency code for the price of the product.
44+
/// Based on the price specified in the App Store Connect or Sku in Google Play console based on the platform.
45+
final String currencyCode;
46+
3647
/// Points back to the `StoreKits`'s [SKProductWrapper] object that generated this [ProductDetails] object.
3748
///
3849
/// This is `null` on Android.
@@ -49,6 +60,8 @@ class ProductDetails {
4960
this.title = product.localizedTitle,
5061
this.description = product.localizedDescription,
5162
this.price = product.priceLocale.currencySymbol + product.price,
63+
this.rawPrice = double.parse(product.price),
64+
this.currencyCode = product.priceLocale.currencyCode,
5265
this.skProduct = product,
5366
this.skuDetail = null;
5467

@@ -58,6 +71,8 @@ class ProductDetails {
5871
this.title = skuDetails.title,
5972
this.description = skuDetails.description,
6073
this.price = skuDetails.price,
74+
this.rawPrice = ((skuDetails.priceAmountMicros) / 1000000.0).toDouble(),
75+
this.currencyCode = skuDetails.priceCurrencyCode,
6176
this.skProduct = null,
6277
this.skuDetail = skuDetails;
6378
}

packages/in_app_purchase/in_app_purchase/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: in_app_purchase
22
description: A Flutter plugin for in-app purchases. Exposes APIs for making in-app purchases through the App Store and Google Play.
33
homepage: https://github.com/flutter/plugins/tree/master/packages/in_app_purchase
4-
version: 0.5.1+3
4+
version: 0.5.2
55

66
dependencies:
77
flutter:
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:flutter_test/flutter_test.dart';
6+
import 'package:in_app_purchase/in_app_purchase.dart';
7+
import 'package:in_app_purchase/src/store_kit_wrappers/sk_product_wrapper.dart';
8+
9+
void main() {
10+
group('Constructor Tests', () {
11+
test(
12+
'fromSkProduct should correctly parse data from a SKProductWrapper instance.',
13+
() {
14+
final SKProductWrapper dummyProductWrapper = SKProductWrapper(
15+
productIdentifier: 'id',
16+
localizedTitle: 'title',
17+
localizedDescription: 'description',
18+
priceLocale:
19+
SKPriceLocaleWrapper(currencySymbol: '\$', currencyCode: 'USD'),
20+
subscriptionGroupIdentifier: 'com.group',
21+
price: '13.37',
22+
);
23+
24+
ProductDetails productDetails =
25+
ProductDetails.fromSKProduct(dummyProductWrapper);
26+
expect(productDetails.id, equals(dummyProductWrapper.productIdentifier));
27+
expect(productDetails.title, equals(dummyProductWrapper.localizedTitle));
28+
expect(productDetails.description,
29+
equals(dummyProductWrapper.localizedDescription));
30+
expect(productDetails.rawPrice, equals(13.37));
31+
expect(productDetails.currencyCode,
32+
equals(dummyProductWrapper.priceLocale.currencyCode));
33+
expect(productDetails.skProduct, equals(dummyProductWrapper));
34+
expect(productDetails.skuDetail, isNull);
35+
});
36+
37+
test(
38+
'fromSkuDetails should correctly parse data from a SkuDetailsWrapper instance',
39+
() {
40+
final SkuDetailsWrapper dummyDetailWrapper = SkuDetailsWrapper(
41+
description: 'description',
42+
freeTrialPeriod: 'freeTrialPeriod',
43+
introductoryPrice: 'introductoryPrice',
44+
introductoryPriceMicros: 'introductoryPriceMicros',
45+
introductoryPriceCycles: 1,
46+
introductoryPricePeriod: 'introductoryPricePeriod',
47+
price: '13.37',
48+
priceAmountMicros: 13370000,
49+
priceCurrencyCode: 'usd',
50+
sku: 'sku',
51+
subscriptionPeriod: 'subscriptionPeriod',
52+
title: 'title',
53+
type: SkuType.inapp,
54+
originalPrice: 'originalPrice',
55+
originalPriceAmountMicros: 1000,
56+
);
57+
58+
ProductDetails productDetails =
59+
ProductDetails.fromSkuDetails(dummyDetailWrapper);
60+
expect(productDetails.id, equals(dummyDetailWrapper.sku));
61+
expect(productDetails.title, equals(dummyDetailWrapper.title));
62+
expect(
63+
productDetails.description, equals(dummyDetailWrapper.description));
64+
expect(productDetails.price, equals(dummyDetailWrapper.price));
65+
expect(productDetails.rawPrice, equals(13.37));
66+
expect(productDetails.currencyCode,
67+
equals(dummyDetailWrapper.priceCurrencyCode));
68+
expect(productDetails.skProduct, isNull);
69+
expect(productDetails.skuDetail, equals(dummyDetailWrapper));
70+
});
71+
});
72+
}

0 commit comments

Comments
 (0)