2017-11-20 5 views
0

SKPaymentTransactionObserver.paymentQueue:updatedTransactionsは、トランザクションの配列を返します。支払いをする際に、どの取引が私が行った支払いであるかをどのように知ることができますか?支払いをすると常に1つの取引が返されますか?iOS IAP paymentQueue:updatedTransactions

一方、このオブザーバー関数は、トランザクションを復元するときにも呼び出されます。ですから、updatedTransactionsを処理するベストプラクティスは何ですか?

私のサブスクリプション製品は、自動更新サブスクリプションです。

+0

はどれだけ私は1つがある知っている、彼らのトランザクション状態 – Paulw11

答えて

0

トランザクションのループを繰り返し、各トランザクションをチェックします。ここ

public func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) { 
     for transaction in transactions { 
      switch (transaction.transactionState) { 
      case .purchased: 
       completeTransaction(transaction: transaction) 
       break 
      case .failed: 
       failedTransaction(transaction: transaction) 
       break 
      case .restored: 
       restoreTransaction(transaction: transaction) 
       break 
      case .deferred: 
       // TODO show user that is waiting for approval 

       break 
      case .purchasing: 
       break 
      } 
     } 
    } 

    private func completeTransaction(transaction: SKPaymentTransaction) { 

     print("completeTransaction...") 

     deliverPurchaseForIdentifier(identifier: transaction.payment.productIdentifier) 
     SKPaymentQueue.default().finishTransaction(transaction) 

    } 

    private func restoreTransaction(transaction: SKPaymentTransaction) { 


     guard let productIdentifier = transaction.original?.payment.productIdentifier else { return } 

     print("restoreTransaction... \(productIdentifier)") 


     deliverPurchaseForIdentifier(identifier: productIdentifier) 
     SKPaymentQueue.default().finishTransaction(transaction) 
    } 

    private func failedTransaction(transaction: SKPaymentTransaction) { 

     if let error = transaction.error as NSError? { 
      if error.domain == SKErrorDomain { 
       // handle all possible errors 
       switch (error.code) { 
       case SKError.unknown.rawValue: 
        print("Unknown error") 

        BaseViewController.currentViewController?.Alert(title: MFErrors.purchaseFaild.messgae.title, msg: MFErrors.purchaseFaild.messgae.body) 

       case SKError.clientInvalid.rawValue: 
        print("client is not allowed to issue the request") 

        BaseViewController.currentViewController?.Alert(title: MFErrors.accountNotAllowedToMakePurchase.messgae.title, msg: MFErrors.accountNotAllowedToMakePurchase.messgae.body) 

       case SKError.paymentCancelled.rawValue: 
        print("user cancelled the request") 

       case SKError.paymentInvalid.rawValue: 
        print("purchase identifier was invalid") 

       case SKError.paymentNotAllowed.rawValue: 
        print("this device is not allowed to make the payment") 
        BaseViewController.currentViewController?.Alert(title: MFErrors.purchaseFaild.messgae.title, msg: MFErrors.purchaseFaild.messgae.body) 
       default: 
        break; 
       } 
      } 

      ProgressViewManager.shared.hide() 
     } 

     SKPaymentQueue.default().finishTransaction(transaction) 
    } 

    private func deliverPurchaseForIdentifier(identifier: String?) { 

     guard let identifier = identifier else { return } 

     //Check if this transactions is a subscription 
     //SubscriptionsProductIdentifiers is an array of subscriptions product ids you sent to the app store to get SKProducts 

     //If subscription 
     if SubscriptionsProductIdentifiers.contains(identifier) { 


     }else{ 
      //If non-consumable, consumables etc... 

     } 


    } 

は私の前の回答では、完全な店長です:すべてのこれらの取引は、同じ製品である場合に How to handle shouldAddStorePayment for In-App Purchases in iOS 11?

+0

上のトランザクションベースのすべてを処理します最新?そして、私は今作ったお支払いのためにどちらを使いますか? – jarly

+0

最初に、ユーザーが購入を決めた商品IDを保存してから、上記の例のIDと保存済みの商品IDを比較してどの商品を知るかを確認しますが、これでは検証に領収書を送る必要はありませんAppleサーバーでJSONにデコードされたレシートを取得し、購入日や有効期限などの各サブスクリプションのすべての情報を取得します。 – Jad

関連する問題