Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions Yosemite/Yosemite/Stores/InAppPurchaseStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import Networking

public class InAppPurchaseStore: Store {
private var listenTask: Task<Void, Error>?
private let remote: InAppPurchasesRemote
private var useBackend = true
Copy link
Contributor

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 :)

Copy link
Member Author

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.


public override init(dispatcher: Dispatcher, storageManager: StorageManagerType, network: Network) {
remote = InAppPurchasesRemote(network: network)
Copy link
Contributor

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at all the other stores in AuthenticatedState, I don't think we typically inject remotes, but instead allow injecting a mock Network to simulate network responses

super.init(dispatcher: dispatcher, storageManager: storageManager, network: network)
listenForTransactions()
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand All @@ -101,4 +132,10 @@ public extension InAppPurchaseStore {
case transactionProductUnknown
case storefrontUnknown
}

enum Constants {
static let identifiers = [
"debug.woocommerce.ecommerce.monthly"
]
}
}