关于在应用商店处理 iAP 成功的问题

Problem about handle iAP succes on app store

如果用户之前没有支付信息,当 iAP 进程启动时,应用程序将把用户重定向到应用商店,以获取用户填写的支付信息,当用户已经完成并支付完成时(收到警报购买成功)资金被切断,应用商店没有将用户重定向回应用程序,如果用户返回应用程序,则注意到成功。

SKPaymentTransactionObserver 没有调用,用户没有得到任何消耗品(申请中的信用)

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
  switch transaction.transactionState {
  case .deferred:
    print("transactionState = deferred")
  case .purchasing:
    print("transactionState = purchasing")
  case .purchased:
    print("transactionState = purchased")
    SKPaymentQueue.default().finishTransaction(transaction)
    SKPaymentQueue.default().remove(self)
    var receiptBase: String = ""
    if let receiptURL = Bundle.main.appStoreReceiptURL {
      do {
        let receipt = try Data(contentsOf: receiptURL)
        let base64encodedReceipt = receipt.base64EncodedString()
        receiptBase = base64encodedReceipt
      } catch {
        receiptBase = ""
      }
    }
    //call api for add consumable to user
    buyCreditDelegate?.purchasedCredit(transactionId: transaction.transactionIdentifier ?? "", receiptId: receiptBase)
  case .failed:
    SKPaymentQueue.default().finishTransaction(transaction)
    SKPaymentQueue.default().remove(self)
    if let delegate = self.buyCreditDelegate {
      delegate.cancelPurchases()
    }
  case .restored:
    print("transactionState = restored")
}

仅供参考: 我的 iAP 是消耗品。

我该如何解决,抱歉我的英语不好

你可以实施:

func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool {

}

来自 Apple

的更多信息

This delegate method is called when the user starts an in-app purchase in the App Store, and the transaction continues in your app. Specifically, if your app is already installed, the method is called automatically.

If your app is not yet installed when the user starts the in-app purchase in the App Store, the user gets a notification when the app installation is complete. This method is called when the user taps the notification. Otherwise, if the user opens the app manually, this method is called only if the app is opened soon after the purchase was started.

从头到尾删除这一行:

SKPaymentQueue.default().remove(self)

你需要你的观察者是一个像应用程序委托一样的永久持久对象。切勿将其移除,否则商店无法与您交谈。