-
Notifications
You must be signed in to change notification settings - Fork 121
Integrate InAppPurchaseStore with networking #7883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,8 +5,11 @@ import Networking | |
|
|
||
| public class InAppPurchaseStore: Store { | ||
| private var listenTask: Task<Void, Error>? | ||
| private let remote: InAppPurchasesRemote | ||
| private var useBackend = true | ||
|
|
||
| public override init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) { | ||
| remote = InAppPurchasesRemote(network: network) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we could inject the remote so we can mock if required when testing?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at all the other stores in |
||
| super.init(dispatcher: dispatcher, storageManager: storageManager, network: network) | ||
| listenForTransactions() | ||
| } | ||
|
|
@@ -37,10 +40,7 @@ private extension InAppPurchaseStore { | |
| func loadProducts(completion: @escaping (Result<[StoreKit.Product], Error>) -> Void) { | ||
| Task { | ||
| do { | ||
| // TODO: use identifiers from remote | ||
| let identifiers = [ | ||
| "debug.woocommerce.ecommerce.monthly" | ||
| ] | ||
| let identifiers = try await getProductIdentifiers() | ||
| let products = try await StoreKit.Product.products(for: identifiers) | ||
| completion(.success(products)) | ||
| } catch { | ||
|
|
@@ -80,7 +80,38 @@ private extension InAppPurchaseStore { | |
| } | ||
|
|
||
| func submitTransaction(_ transaction: StoreKit.Transaction) async throws { | ||
| // TODO: actually send this to the backend | ||
| guard useBackend else { | ||
| return | ||
| } | ||
| guard let appAccountToken = transaction.appAccountToken else { | ||
| throw Errors.transactionMissingAppAccountToken | ||
| } | ||
| guard let siteID = AppAccountToken.siteIDFromToken(appAccountToken) else { | ||
| throw Errors.appAccountTokenMissingSiteIdentifier | ||
| } | ||
|
|
||
| let products = try await StoreKit.Product.products(for: [transaction.productID]) | ||
| guard let product = products.first else { | ||
| throw Errors.transactionProductUnknown | ||
| } | ||
| let priceInCents = Int(truncating: NSDecimalNumber(decimal: product.price * 100)) | ||
| guard let countryCode = await Storefront.current?.countryCode else { | ||
| throw Errors.storefrontUnknown | ||
| } | ||
| _ = try await remote.createOrder( | ||
| for: siteID, | ||
| price: priceInCents, | ||
| productIdentifier: product.id, | ||
| appStoreCountryCode: countryCode, | ||
| receiptData: transaction.jsonRepresentation | ||
| ) | ||
| } | ||
|
|
||
| func getProductIdentifiers() async throws -> [String] { | ||
| guard useBackend else { | ||
| return Constants.identifiers | ||
| } | ||
| return try await remote.loadProducts() | ||
| } | ||
|
|
||
| func listenForTransactions() { | ||
|
|
@@ -101,4 +132,10 @@ public extension InAppPurchaseStore { | |
| case transactionProductUnknown | ||
| case storefrontUnknown | ||
| } | ||
|
|
||
| enum Constants { | ||
| static let identifiers = [ | ||
| "debug.woocommerce.ecommerce.monthly" | ||
| ] | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering, what will be the usage of this property? How are we going to set it? I see that it is a private var, but I don't see any place where we change its value :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initially I wanted to make it configurable through the debug screen, but it got a bit complicated. I left that there so it was easier to change behavior during development/testing.