benjaminmayo / merchantkit

A modern In-App Purchases management framework for iOS.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Doesn't form Result handler on restorePurchasesTask method in RestorePurchase button action

ramanocs1145 opened this issue · comments

Hi @benjaminmayo ,

Currently I'm working on RestorePurchase function on calling it's button action.

I have referred restorePurchases() from ProductInterfaceController from your example project like below,

@objc fileprivate func restoreButtonItemAction(_ sender: UIButton) {

        guard self.restorePurchasesTask == nil else { return }
        if let task = self.merchant?.restorePurchasesTask() {
            task.onCompletion = { [weak self] result in
                print("result ==> \(result)")
                guard let self = self else { return }
                self.restorePurchasesTask = nil
                DispatchQueue.main.async {
                    let restoreResult: RestorePurchasesResult = result.map { restoredProducts in
                    }
                    if let updatedProducts = try? restoreResult.get() {
                        print("updatedProducts ==> \(updatedProducts)")
                    }
                }
            }
        }
    }

![Screenshot 2019-04-16 at 6 26 32 PM](https://user-images.githubusercontent.com/25265355/56212077-eff92200-6076-11e9-9c33-a0690e49b1e8.png)

I can't rectify this error. Why it's happening? also I have declared this type alias variable too

public typealias RestorePurchasesResult = Result<Set<Product>, Error>

If you are using ProductInterfaceController, just call restorePurchases() on it. You don't need all that mess.

If you do want to manually create the task, then the code you have looks generally correct. I think your typealias definition is wrong, or conflicting with other types declared in your project. Try removing the typealias entirely.

@benjaminmayo Now the error is gone when declared type alias like,
public typealias RestorePurchasesResult = Swift.Result<Set<Product>, Error>.

But how to fetch this product identifier,
result ==> success(Set([[Product 'com.sample.application.monthly']]))?

    @objc fileprivate func restoreButtonItemAction(_ sender: UIButton) {

        guard self.restorePurchasesTask == nil else { return }

        let products = Set<Product>()
        if let task = self.merchant?.restorePurchasesTask() {
            task.onCompletion = { [weak self] result in
                print("result ==> \(result)")
                guard let self = self else { return }
                self.restorePurchasesTask = nil
                DispatchQueue.main.async {

                    let restoreResult: RestorePurchasesResult = result.map { restoredProducts in
                        products.intersection(restoredProducts)
                    }
                    print("restoredProducts ==> \(String(describing: products))")
                    if let updatedProducts = try? restoreResult.get() {
                        print("updatedProducts ==> \(updatedProducts)")
                    }
                }
            }
            task.start()
            self.restorePurchasesTask = task
        }
    }

I'm getting empty records from `restoredProducts & updatedProducts` on it's print function.

@benjaminmayo It's fixed to fetched that product and it's identifier like below,

let resultedProducts = try? result.get().

Then everything smoth on it's related actions.

Thanks for your support.

Glad you fixed your issue but I'm still confused as to why you are copying and pasting ProductInterfaceController code into your project like this. If you want to use it, instantiate an instance and call to its public methods.