我的产品列表中有非消耗性订阅和自动续订订阅。因此,当我尝试恢复购买时,我会收到所有已购买的商品。那么恢复购买时应该采取什么方法呢?
就我而言,我有两种按月和按年收费的产品,另一种是非消耗品并提供终身使用权。因此,在测试时,我进行了几笔交易。但是在恢复时,我注意到我获得了所有购买的交易列表,我需要从中为用户恢复有效的产品。那么我如何知道要恢复哪个项目?
从文档中,我找到了一种验证订阅和验证产品的方法。但从我的回复中我可以看到,我有每月和终生产品,可用于恢复有效。我应该在这里做什么?
这是我的代码
@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
//////////////////////
您应该检查从最新排序的有效订阅,如果有效则解锁它们。 这取决于你的订阅逻辑。
就我而言,我检查以下顺序。
a) 如果购买了终身套餐,它将被解锁 寿命。
b) 如果没有购买终身,则将检查订阅情况 排序购买列表并找出是否有任何活跃的订阅 如果存在则解锁。
此外,我还会检查是否有任何未完成的购买,然后解锁内容并在解锁后将其标记为已完成。
同样的问题,谁能帮帮我们?请!