I'm setting appAccountToken for my autorenewing subscriptions and when a purchase completes I get the token back in the transaction.
However, when I test "Ask to Purchase" and create a pending transaction, the approved transaction that is returned does not contain the appAccountToken. I need this, so what am I missing?
I'm using a listener and an update function.
func listenForTransactions() -> Task<Void, Error> {
return Task.detached {
// iterate through any transactions that don't come from a direct call to 'purchase()'.
for await result in Transaction.updates {
do {
let transaction = try await self.checkVerified(result)
//Deliver products to the user.
await self.updateCustomerProductStatus()
await transaction.finish()
} catch {
print("Transaction failed verification.")
}
}
}
}
func updateCustomerProductStatus() async {
var purchasedSubscriptions: [Product] = []
//iterate through all of the user's purchased products.
for await result in Transaction.currentEntitlements {
do {
//Check whether the transaction is verified.
let transaction = try checkVerified(result)
//Check the 'productType' of the transaction and get the corresponding product from the store.
switch transaction.productType {
case .consumable:
//do nothing
break
case .nonConsumable:
//do nothing
break
case .nonRenewable:
//do nothing
break
case .autoRenewable:
if let subscription = subscriptions.first(where: { $0.id == transaction.productID}) {
purchasedSubscriptions.append(subscription)
//Post subscription status back to MYAPPSERVER.
print("transaction: ",transaction)
//API Call to MYAPPSERVER
let params: [String: AnyHashable] = [
"token":transaction.appAccountToken!.uuidString,
"item":transaction.productID,
"transaction":transaction.originalID
]
guard let apiURL = URL(string:"https://MYAPPSERVER/version-test/api/1.1/wf/ios-sub") else {return}
var request = URLRequest(url:apiURL)
request.httpMethod = "POST"
request.setValue("Application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options:[.fragmentsAllowed])
let session = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
print("Error sending subscription to Dryft: \(error.localizedDescription)")
}else{
if let jsonResponse = try? JSONSerialization.jsonObject(with: data!, options: [.fragmentsAllowed]) {
print("The response: \(jsonResponse)")
}
}
}
}.resume()
}
default:
break
}
} catch {
print("Unknown transaction type.")
}
}
//Update the store information with the purchased products.
self.purchasedSubscriptions = purchasedSubscriptions
}