Skip to content

Commit 7e1d1c6

Browse files
authored
[in_app_purchase_storekit] Expose sk2Transaction on SK2PurchaseDetails (#13341)
Fixes flutter/flutter#186488 Exposes the underlying `SK2Transaction` on `SK2PurchaseDetails` so callers can access fields like `originalId` and `quantity` from `purchaseStream` updates. Returns `null` when a purchase has not produced a transaction . ## Pre-Review Checklist
1 parent 5da18a3 commit 7e1d1c6

5 files changed

Lines changed: 33 additions & 6 deletions

File tree

‎packages/in_app_purchase/in_app_purchase_storekit/CHANGELOG.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.4.15
2+
3+
* Exposes `sk2Transaction` on `SK2PurchaseDetails`.
4+
15
## 0.4.14+1
26

37
* Suppresses deprecation warnings for StoreKit 1 APIs, which are used as

‎packages/in_app_purchase/in_app_purchase_storekit/lib/src/store_kit_2_wrappers/sk2_transaction_wrapper.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ extension on SK2TransactionMessage {
163163
purchaseID: id > 0 ? id.toString() : null,
164164
appAccountToken: appAccountToken,
165165
expirationDate: _secondsToMillisecondsSinceEpochString(expirationDate),
166+
sk2Transaction: id > 0 ? convertFromPigeon() : null,
166167
);
167168
}
168169

‎packages/in_app_purchase/in_app_purchase_storekit/lib/src/types/app_store_purchase_details.dart‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'package:in_app_purchase_platform_interface/in_app_purchase_platform_inte
66

77
import '../../in_app_purchase_storekit.dart';
88
import '../../store_kit_wrappers.dart';
9+
import '../store_kit_2_wrappers/sk2_transaction_wrapper.dart';
910
import '../store_kit_wrappers/enum_converters.dart';
1011

1112
/// The class represents the information of a purchase made with the Apple
@@ -93,6 +94,7 @@ class SK2PurchaseDetails extends PurchaseDetails {
9394
required super.status,
9495
this.appAccountToken,
9596
this.expirationDate,
97+
this.sk2Transaction,
9698
});
9799

98100
/// A UUID that associates the transaction with a user on your own service.
@@ -104,6 +106,13 @@ class SK2PurchaseDetails extends PurchaseDetails {
104106
/// Null if the purchased product is not a subscription.
105107
final String? expirationDate;
106108

109+
/// Points back to the [SK2Transaction] which was used to generate this
110+
/// [SK2PurchaseDetails] object.
111+
///
112+
/// Null if the purchase has not produced a transaction (for example, when
113+
/// [status] is [PurchaseStatus.pending] or [PurchaseStatus.canceled]).
114+
final SK2Transaction? sk2Transaction;
115+
107116
@override
108117
bool get pendingCompletePurchase => status == PurchaseStatus.purchased;
109118
}

‎packages/in_app_purchase/in_app_purchase_storekit/pubspec.yaml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: in_app_purchase_storekit
22
description: An implementation for the iOS and macOS platforms of the Flutter `in_app_purchase` plugin. This uses the StoreKit Framework.
33
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_storekit
44
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
5-
version: 0.4.14+1
5+
version: 0.4.15
66

77
environment:
88
sdk: ^3.11.0

‎packages/in_app_purchase/in_app_purchase_storekit/test/in_app_purchase_storekit_2_platform_test.dart‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,18 @@ void main() {
183183
expect(result.first.productID, dummyProductWrapper.id);
184184
expect(result.first.verificationData.serverVerificationData, 'receiptData');
185185
expect(result.first.verificationData.localVerificationData, 'jsonRepresentation');
186-
expect((result.first as SK2PurchaseDetails).appAccountToken, 'appAccountToken');
187-
expect(
188-
(result.first as SK2PurchaseDetails).expirationDate,
189-
(321321.32 * 1000).round().toString(),
190-
);
186+
final sk2Details = result.first as SK2PurchaseDetails;
187+
expect(sk2Details.appAccountToken, 'appAccountToken');
188+
expect(sk2Details.expirationDate, (321321.32 * 1000).round().toString());
189+
expect(sk2Details.sk2Transaction, isNotNull);
190+
expect(sk2Details.sk2Transaction!.id, '1');
191+
expect(sk2Details.sk2Transaction!.originalId, '2');
192+
expect(sk2Details.sk2Transaction!.productId, dummyProductWrapper.id);
193+
expect(sk2Details.sk2Transaction!.purchaseDate, (123123.121 * 1000).round().toString());
194+
expect(sk2Details.sk2Transaction!.expirationDate, (321321.32 * 1000).round().toString());
195+
expect(sk2Details.sk2Transaction!.appAccountToken, 'appAccountToken');
196+
expect(sk2Details.sk2Transaction!.receiptData, 'receiptData');
197+
expect(sk2Details.sk2Transaction!.jsonRepresentation, 'jsonRepresentation');
191198
},
192199
);
193200

@@ -381,6 +388,7 @@ void main() {
381388
expect(result.first.productID, dummyProductWrapper.id);
382389
expect(result.first.status, PurchaseStatus.canceled);
383390
expect(result.first.pendingCompletePurchase, false);
391+
expect((result.first as SK2PurchaseDetails).sk2Transaction, isNull);
384392
});
385393

386394
test('pending purchase should emit pending status to purchaseStream', () async {
@@ -406,6 +414,7 @@ void main() {
406414
expect(result.first.productID, dummyProductWrapper.id);
407415
expect(result.first.status, PurchaseStatus.pending);
408416
expect(result.first.pendingCompletePurchase, false);
417+
expect((result.first as SK2PurchaseDetails).sk2Transaction, isNull);
409418
});
410419

411420
test('unverified purchase should receive transaction with purchased status', () async {
@@ -468,6 +477,10 @@ void main() {
468477
expect(actual.status, PurchaseStatus.restored);
469478
// In storekit 2, restored purchases don't have to finished.
470479
expect(actual.pendingCompletePurchase, false);
480+
final sk2Details = actual as SK2PurchaseDetails;
481+
expect(sk2Details.sk2Transaction, isNotNull);
482+
expect(sk2Details.sk2Transaction!.id, expected.id.toString());
483+
expect(sk2Details.sk2Transaction!.originalId, expected.originalId.toString());
471484
}
472485
});
473486
});

0 commit comments

Comments
 (0)