В моем списке товаров есть подписка как на непотребляемые, так и на автообновляемую. Поэтому, когда я пытаюсь восстановить покупки, я получаю все сделанные покупки. Теперь какой должен быть подход при восстановлении покупки.
В моем случае у меня есть два продукта, которые заряжаются ежемесячно и раз в год, и один, который не потребляет и дает пожизненный доступ. Поэтому во время тестирования я сделал несколько транзакций. Но при восстановлении я заметил, что получаю весь список купленных транзакций, из которого мне нужно восстановить действительный продукт для пользователя. Так как же узнать, какой предмет восстановить?
Из документов я нашел способ подтвердить подписку и проверить продукт. Но из моего ответа я вижу, что у меня есть как месячный, так и пожизненный продукт, который можно восстановить. Что мне здесь делать?
Это мой код
@IBAction func restoreButtonPressed(_ sender: Any) {
// SubscriptionService.shared.restorePurchases()
if self.premium != nil {
// contact alert
self.showActionSheet(title: "Contact us", message: "Tell us your query at \(Settings.requestEmail)", email: Settings.requestEmail)
return
}
notificationShown = false
hud.show(in: self.view)
// let appleValidator = AppleReceiptValidator(service: .sandbox, sharedSecret: Payment.sharedSecret)
SwiftyStoreKit.restorePurchases(atomically: true) { results in
for purchase in results.restoredPurchases {
print("//////////////////////")
print(purchase.productId)
print("Original Transaction Date: \((purchase.originalTransaction?.transactionDate)!)")
print("Original Transaction State: \((purchase.originalTransaction?.transactionState)!)")
print("Transaction Date: \((purchase.transaction.transactionDate)!)")
print("Transaction State: \((purchase.transaction.transactionState))")
print("//////////////////////")
if purchase.productId == Payment.lifetime {
SubscriptionService.shared.verifyPurchase(productId: purchase.productId, transactionDate: purchase.originalTransaction?.transactionDate, completion: { (success) in
self.hud.dismiss(afterDelay: 3.0)
if success {
Premium().check().done { premium in
self.premium = premium
self.initiatePremiumView()
self.initPurchaseButton()
}
} else {
if self.notificationShown == false {
self.showNotification(title: "No item to restore", buttonTitle: "", type: .failure)
self.notificationShown = true
}
}
if purchase.needsFinishTransaction {
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
})
} else {
SubscriptionService.shared.verifySubscription(productId: purchase.productId, transactionDate: purchase.originalTransaction?.transactionDate, completion: { (success) in
self.hud.dismiss(afterDelay: 3.0)
if success {
Premium().check().done { premium in
self.premium = premium
self.initiatePremiumView()
self.initPurchaseButton()
}.catch { _ in }
} else {
if self.notificationShown == false {
self.showNotification(title: "No item to restore", buttonTitle: "", type: .failure)
self.notificationShown = true
}
}
})
}
if purchase.needsFinishTransaction {
// Deliver content from server, then:
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
}
}
// self.showAlert(self.alertForRestorePurchases(results))
}
func verifySubscription(productId: String, transactionDate: Date?, completion: @escaping (Bool) -> ()) {
let appleValidator = AppleReceiptValidator(service: .sandbox, sharedSecret: Payment.sharedSecret)
SwiftyStoreKit.verifyReceipt(using: appleValidator, forceRefresh: false) { result in
switch result {
case .success(let receipt):
// print(receipt.count)
if receipt.count > 0 {
let purchaseResult = SwiftyStoreKit.verifySubscription(
ofType: .autoRenewable, // or .nonRenewing (see below)
productId: productId,
inReceipt: receipt)
switch purchaseResult {
case .purchased(let expiryDate, let items):
// give premium access
let premium = Premium(hasFullAccess: true, subscribedProduct: productId, expires: expiryDate, purchaseDate: transactionDate)
premium.giveFullAccess().done { result in
// show full access Dialog
completion(true)
}.catch { error in
// give error msg
completion(false)
// print("hell no")
}
// print("\(productId) is valid until \(expiryDate)\n\n")
case .expired(let expiryDate, let items):
// remove premium access
completion(false)
Premium().removePremiumAccess().done {_ in }.catch {_ in }
// print("\(productId) is expired since \(expiryDate)\n\n")
case .notPurchased:
// remove premium access
completion(false)
Premium().removePremiumAccess().done {_ in }.catch {_ in }
// print("The user has never purchased \(productId)")
}
}
// print("Verify receipt success: \(receipt)")
case .error(let error):
print("Verify receipt failed: \(error)")
}
}
}
func verifyPurchase(productId: String, transactionDate: Date?, completion: @escaping (Bool) -> ()) {
let appleValidator = AppleReceiptValidator(service: .sandbox, sharedSecret: Payment.sharedSecret)
SwiftyStoreKit.verifyReceipt(using: appleValidator, forceRefresh: false) { result in
switch result {
case .success(let receipt):
if receipt.count > 0 {
let purchaseResult = SwiftyStoreKit.verifyPurchase(
productId: productId,
inReceipt: receipt)
switch purchaseResult {
case .purchased(let receiptItem):
let premium = Premium(hasFullAccess: true, subscribedProduct: productId, expires: nil, purchaseDate: transactionDate)
premium.giveFullAccess().done { result in
// show full access Dialog
completion(true)
}.catch { error in
// give error msg
completion(false)
// print("hell no")
}
// print("\(productId) is purchased: \(receiptItem)")
case .notPurchased:
completion(false)
Premium().removePremiumAccess().done {_ in }.catch {_ in }
}
}
// print("Verify receipt success: \(receipt)")
case .error(let error):
print("Verify receipt failed: \(error)")
}
}
}
** Полный список купленных товаров при восстановлении: **
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-23 13:32:35 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 06:39:38 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-23 13:17:35 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 09:35:03 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 06:29:08 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 07:10:37 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 09:09:36 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 07:20:37 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 10:52:29 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: yearly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-23 12:14:35 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 09:30:01 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: yearly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-23 12:17:35 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 09:14:36 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-23 13:22:35 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-23 13:27:35 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 06:44:38 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 06:34:38 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 07:25:37 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 06:17:10 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 06:22:55 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 09:25:01 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 07:30:37 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 10:31:38 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: lifetime
Original Transaction Date: 2019-04-24 11:06:13 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 09:23:14 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-24 09:20:01 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 07:05:37 +0000
Transaction State: restored
//////////////////////
//////////////////////
Product Id: monthly
Original Transaction Date: 2019-04-23 12:14:36 +0000
Original Transaction State: purchased
Transaction Date: 2019-04-25 07:15:37 +0000
Transaction State: restored
//////////////////////