Skip to content

Commit e949ba7

Browse files
authored
[in_app_purchase_storekit] expose jsonRepresentation for Transactions (flutter#8430)
Exposes [jsonRepresentation](https://developer.apple.com/documentation/storekit/transaction/jsonrepresentation) for Transactions. Helpful for developers to who want to access the properties of Transaction directly if they arent already exposed. Fixes flutter#158882
1 parent 6d98122 commit e949ba7

File tree

8 files changed

+52
-8
lines changed

8 files changed

+52
-8
lines changed

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.3.20+4
2+
3+
* Exposes `jsonRepresentation` field for transactions.
4+
15
## 0.3.20+3
26

37
* Fixes `finishTransaction` not completing.

packages/in_app_purchase/in_app_purchase_storekit/darwin/Classes/StoreKit2/InAppPurchasePlugin+StoreKit2.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,10 @@ extension InAppPurchasePlugin: InAppPurchase2API {
179179
}
180180
}
181181

182-
// MARK: - Convenience Functions
182+
// MARK: - Internal Convenience Functions
183183

184184
/// Helper function that fetches and unwraps all verified transactions
185-
private func rawTransactions() async -> [Transaction] {
185+
func rawTransactions() async -> [Transaction] {
186186
var transactions: [Transaction] = []
187187
for await verificationResult in Transaction.all {
188188
switch verificationResult {
@@ -196,7 +196,7 @@ extension InAppPurchasePlugin: InAppPurchase2API {
196196
}
197197

198198
/// Helper function to fetch specific transaction
199-
private func fetchTransaction(by id: UInt64) async throws -> Transaction? {
199+
func fetchTransaction(by id: UInt64) async throws -> Transaction? {
200200
for await result in Transaction.all {
201201
switch result {
202202
case .verified(let transaction):

packages/in_app_purchase/in_app_purchase_storekit/darwin/Classes/StoreKit2/StoreKit2Translators.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ extension Transaction {
200200
purchasedQuantity: Int64(purchasedQuantity),
201201
appAccountToken: appAccountToken?.uuidString,
202202
restoring: receipt != nil,
203-
receiptData: receipt
203+
receiptData: receipt,
204+
jsonRepresentation: String(decoding: jsonRepresentation, as: UTF8.self)
204205
)
205206
}
206207
}

packages/in_app_purchase/in_app_purchase_storekit/darwin/Classes/StoreKit2/sk2_pigeon.g.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ struct SK2TransactionMessage {
318318
var restoring: Bool
319319
var receiptData: String? = nil
320320
var error: SK2ErrorMessage? = nil
321+
var jsonRepresentation: String? = nil
321322

322323
// swift-format-ignore: AlwaysUseLowerCamelCase
323324
static func fromList(_ pigeonVar_list: [Any?]) -> SK2TransactionMessage? {
@@ -331,6 +332,7 @@ struct SK2TransactionMessage {
331332
let restoring = pigeonVar_list[7] as! Bool
332333
let receiptData: String? = nilOrValue(pigeonVar_list[8])
333334
let error: SK2ErrorMessage? = nilOrValue(pigeonVar_list[9])
335+
let jsonRepresentation: String? = nilOrValue(pigeonVar_list[10])
334336

335337
return SK2TransactionMessage(
336338
id: id,
@@ -342,7 +344,8 @@ struct SK2TransactionMessage {
342344
appAccountToken: appAccountToken,
343345
restoring: restoring,
344346
receiptData: receiptData,
345-
error: error
347+
error: error,
348+
jsonRepresentation: jsonRepresentation
346349
)
347350
}
348351
func toList() -> [Any?] {
@@ -357,6 +360,7 @@ struct SK2TransactionMessage {
357360
restoring,
358361
receiptData,
359362
error,
363+
jsonRepresentation,
360364
]
361365
}
362366
}
@@ -524,8 +528,8 @@ class InAppPurchase2APISetup {
524528
static var codec: FlutterStandardMessageCodec { sk2_pigeonPigeonCodec.shared }
525529
/// Sets up an instance of `InAppPurchase2API` to handle messages through the `binaryMessenger`.
526530
static func setUp(
527-
binaryMessenger: FlutterBinaryMessenger, api: InAppPurchase2API?,
528-
messageChannelSuffix: String = ""
531+
binaryMessenger: FlutterBinaryMessenger,
532+
api: InAppPurchase2API?, messageChannelSuffix: String = ""
529533
) {
530534
let channelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""
531535
let canMakePaymentsChannel = FlutterBasicMessageChannel(

packages/in_app_purchase/in_app_purchase_storekit/example/shared/RunnerTests/InAppPurchaseStoreKit2PluginTests.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,34 @@ final class InAppPurchase2PluginTests: XCTestCase {
129129
XCTAssert(fetchedProductMsg?.count == 0)
130130
}
131131

132+
func testGetTransactionJsonRepresentation() async throws {
133+
let expectation = self.expectation(description: "Purchase request should succeed")
134+
135+
plugin.purchase(id: "consumable", options: nil) { result in
136+
switch result {
137+
case .success(_):
138+
expectation.fulfill()
139+
case .failure(let error):
140+
XCTFail("Purchase should NOT fail. Failed with \(error)")
141+
}
142+
}
143+
144+
await fulfillment(of: [expectation], timeout: 5)
145+
146+
let transaction = try await plugin.fetchTransaction(
147+
by: UInt64(session.allTransactions()[0].originalTransactionIdentifier))
148+
149+
guard let transaction = transaction else {
150+
XCTFail("Transaction does not exist.")
151+
return
152+
}
153+
154+
let jsonRepresentationString = String(decoding: transaction.jsonRepresentation, as: UTF8.self)
155+
156+
XCTAssert(jsonRepresentationString.localizedStandardContains("Type\":\"Consumable"))
157+
XCTAssert(jsonRepresentationString.localizedStandardContains("storefront\":\"USA"))
158+
}
159+
132160
//TODO(louisehsu): Add testing for lower versions.
133161
@available(iOS 17.0, macOS 14.0, *)
134162
func testGetProductsWithStoreKitError() async throws {

packages/in_app_purchase/in_app_purchase_storekit/lib/src/sk2_pigeon.g.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class SK2TransactionMessage {
306306
this.restoring = false,
307307
this.receiptData,
308308
this.error,
309+
this.jsonRepresentation,
309310
});
310311

311312
int id;
@@ -328,6 +329,8 @@ class SK2TransactionMessage {
328329

329330
SK2ErrorMessage? error;
330331

332+
String? jsonRepresentation;
333+
331334
Object encode() {
332335
return <Object?>[
333336
id,
@@ -340,6 +343,7 @@ class SK2TransactionMessage {
340343
restoring,
341344
receiptData,
342345
error,
346+
jsonRepresentation,
343347
];
344348
}
345349

@@ -356,6 +360,7 @@ class SK2TransactionMessage {
356360
restoring: result[7]! as bool,
357361
receiptData: result[8] as String?,
358362
error: result[9] as SK2ErrorMessage?,
363+
jsonRepresentation: result[10] as String?,
359364
);
360365
}
361366
}

packages/in_app_purchase/in_app_purchase_storekit/pigeons/sk2_pigeon.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ class SK2TransactionMessage {
146146
this.appAccountToken,
147147
this.error,
148148
this.receiptData,
149+
this.jsonRepresentation,
149150
this.restoring = false});
150151
final int id;
151152
final int originalId;
@@ -157,6 +158,7 @@ class SK2TransactionMessage {
157158
final bool restoring;
158159
final String? receiptData;
159160
final SK2ErrorMessage? error;
161+
final String? jsonRepresentation;
160162
}
161163

162164
class SK2ErrorMessage {

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.3.20+3
5+
version: 0.3.20+4
66

77
environment:
88
sdk: ^3.4.0

0 commit comments

Comments
 (0)