Skip to content

Commit

Permalink
[in_app_purchase_storekit] expose jsonRepresentation for Transactio…
Browse files Browse the repository at this point in the history
…ns (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/flutter#158882
  • Loading branch information
LouiseHsu authored Jan 15, 2025
1 parent 6d98122 commit e949ba7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.3.20+4

* Exposes `jsonRepresentation` field for transactions.

## 0.3.20+3

* Fixes `finishTransaction` not completing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ extension InAppPurchasePlugin: InAppPurchase2API {
}
}

// MARK: - Convenience Functions
// MARK: - Internal Convenience Functions

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

/// Helper function to fetch specific transaction
private func fetchTransaction(by id: UInt64) async throws -> Transaction? {
func fetchTransaction(by id: UInt64) async throws -> Transaction? {
for await result in Transaction.all {
switch result {
case .verified(let transaction):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ extension Transaction {
purchasedQuantity: Int64(purchasedQuantity),
appAccountToken: appAccountToken?.uuidString,
restoring: receipt != nil,
receiptData: receipt
receiptData: receipt,
jsonRepresentation: String(decoding: jsonRepresentation, as: UTF8.self)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ struct SK2TransactionMessage {
var restoring: Bool
var receiptData: String? = nil
var error: SK2ErrorMessage? = nil
var jsonRepresentation: String? = nil

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

return SK2TransactionMessage(
id: id,
Expand All @@ -342,7 +344,8 @@ struct SK2TransactionMessage {
appAccountToken: appAccountToken,
restoring: restoring,
receiptData: receiptData,
error: error
error: error,
jsonRepresentation: jsonRepresentation
)
}
func toList() -> [Any?] {
Expand All @@ -357,6 +360,7 @@ struct SK2TransactionMessage {
restoring,
receiptData,
error,
jsonRepresentation,
]
}
}
Expand Down Expand Up @@ -524,8 +528,8 @@ class InAppPurchase2APISetup {
static var codec: FlutterStandardMessageCodec { sk2_pigeonPigeonCodec.shared }
/// Sets up an instance of `InAppPurchase2API` to handle messages through the `binaryMessenger`.
static func setUp(
binaryMessenger: FlutterBinaryMessenger, api: InAppPurchase2API?,
messageChannelSuffix: String = ""
binaryMessenger: FlutterBinaryMessenger,
api: InAppPurchase2API?, messageChannelSuffix: String = ""
) {
let channelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""
let canMakePaymentsChannel = FlutterBasicMessageChannel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,34 @@ final class InAppPurchase2PluginTests: XCTestCase {
XCTAssert(fetchedProductMsg?.count == 0)
}

func testGetTransactionJsonRepresentation() async throws {
let expectation = self.expectation(description: "Purchase request should succeed")

plugin.purchase(id: "consumable", options: nil) { result in
switch result {
case .success(_):
expectation.fulfill()
case .failure(let error):
XCTFail("Purchase should NOT fail. Failed with \(error)")
}
}

await fulfillment(of: [expectation], timeout: 5)

let transaction = try await plugin.fetchTransaction(
by: UInt64(session.allTransactions()[0].originalTransactionIdentifier))

guard let transaction = transaction else {
XCTFail("Transaction does not exist.")
return
}

let jsonRepresentationString = String(decoding: transaction.jsonRepresentation, as: UTF8.self)

XCTAssert(jsonRepresentationString.localizedStandardContains("Type\":\"Consumable"))
XCTAssert(jsonRepresentationString.localizedStandardContains("storefront\":\"USA"))
}

//TODO(louisehsu): Add testing for lower versions.
@available(iOS 17.0, macOS 14.0, *)
func testGetProductsWithStoreKitError() async throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ class SK2TransactionMessage {
this.restoring = false,
this.receiptData,
this.error,
this.jsonRepresentation,
});

int id;
Expand All @@ -328,6 +329,8 @@ class SK2TransactionMessage {

SK2ErrorMessage? error;

String? jsonRepresentation;

Object encode() {
return <Object?>[
id,
Expand All @@ -340,6 +343,7 @@ class SK2TransactionMessage {
restoring,
receiptData,
error,
jsonRepresentation,
];
}

Expand All @@ -356,6 +360,7 @@ class SK2TransactionMessage {
restoring: result[7]! as bool,
receiptData: result[8] as String?,
error: result[9] as SK2ErrorMessage?,
jsonRepresentation: result[10] as String?,
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class SK2TransactionMessage {
this.appAccountToken,
this.error,
this.receiptData,
this.jsonRepresentation,
this.restoring = false});
final int id;
final int originalId;
Expand All @@ -157,6 +158,7 @@ class SK2TransactionMessage {
final bool restoring;
final String? receiptData;
final SK2ErrorMessage? error;
final String? jsonRepresentation;
}

class SK2ErrorMessage {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: in_app_purchase_storekit
description: An implementation for the iOS and macOS platforms of the Flutter `in_app_purchase` plugin. This uses the StoreKit Framework.
repository: https://github.com/flutter/packages/tree/main/packages/in_app_purchase/in_app_purchase_storekit
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+in_app_purchase%22
version: 0.3.20+3
version: 0.3.20+4

environment:
sdk: ^3.4.0
Expand Down

0 comments on commit e949ba7

Please sign in to comment.