benjaminmayo/merchantkit

ProductState is still unknown

yildirimatcioglu opened this issue · 2 comments

Hello, I created auto-renewable and non-renewable products for a new app. I filled all information on IAP screen and also in banking-agreements. I had capabilities for IAP and I wrote the code same as the example but ProductState is still unknown. Do you have any idea for about it? Btw I can make purchase with this tutorial but with merchantkit it does not work.
Here is my repository which is doing all the stuff:
` protocol PurchaseRepositoryProtocol {

}

class PurchaseRepository: NSObject, PurchaseRepositoryProtocol {
static let shared = PurchaseRepository()

var isPurchased: Bool = false
var dailyPurchaseEndDate: Date?

override init() {
    super.init()
    SKPaymentQueue.default().add(self)
}
var merchant: Merchant?

func startMerchant() {
    self.merchant = Merchant(configuration: .default, delegate: self)
    let dailyProduct = Product(identifier: MerchantProductsIdentifiers.dailyMembership, kind: .subscription(automaticallyRenews: false))
    let weeklyProduct = Product(identifier: MerchantProductsIdentifiers.weeklyMembership, kind: .subscription(automaticallyRenews: false))
    let monthlyProduct = Product(identifier: MerchantProductsIdentifiers.monthlyMembership, kind: .subscription(automaticallyRenews: true))
    self.merchant?.register([dailyProduct, weeklyProduct, monthlyProduct])
    self.merchant?.setup()
    getStatesOfProducts()
}

func getStatesOfProducts() {
    isPurchased = false
    if let dailyDate = dailyPurchaseEndDate {
        if dailyDate < Date() {
            isPurchased = true
        }
    }
    if self.merchant?.state(for:  Product(identifier: MerchantProductsIdentifiers.dailyMembership, kind:  .subscription(automaticallyRenews: false))).isPurchased ?? false {
        isPurchased = true
    }
    if self.merchant?.state(for: Product(identifier: MerchantProductsIdentifiers.weeklyMembership, kind:     .subscription(automaticallyRenews: false))).isPurchased ?? false {
        isPurchased = true
    }
    if self.merchant?.state(for: Product(identifier: MerchantProductsIdentifiers.monthlyMembership, kind:   .subscription(automaticallyRenews: true))).isPurchased ?? false {
        isPurchased = true
    }
}

func buyMembership(successCompletion:@escaping () -> Void,errorCompletionn:@escaping () -> Void) -> Void {
    let productW = self.merchant?.product(withIdentifier: MerchantProductsIdentifiers.dailyMembership)
    let productSet = Set(arrayLiteral: productW!)
    let interface = ProductInterfaceController(products: productSet, with: self.merchant!)
    let state = interface.state(for: productW!)
    print(state)
    switch state {
    case .purchasable(let purchase):
        let task = merchant?.commitPurchaseTask(for: purchase)
        task?.onCompletion = { result in
            switch result {
            case .success:
                print("success")
                self.getStatesOfProducts()
                successCompletion()
            case .failure(_):
                errorCompletionn()
            }
        }
        
        task?.start()
    default:
        errorCompletionn()
        break
    }
}

}

extension PurchaseRepository: MerchantDelegate,SKPaymentTransactionObserver {
func merchant(_ merchant: Merchant, didChangeStatesFor products: Set) {
print(products)
}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        if transaction.transactionState == .purchased {
            print(transaction)
        }else if transaction.transactionState == .failed{
            print(transaction.error.debugDescription)
            print(transaction.error?.localizedDescription)
        }
    }
}

}
`

Did you get anywhere with this?

no it did not change anything