Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with auto-renewable subscription? #174

Closed
gerchicov-bp opened this issue Mar 29, 2017 · 7 comments
Closed

How to deal with auto-renewable subscription? #174

gerchicov-bp opened this issue Mar 29, 2017 · 7 comments
Labels
answered Questions which have accepted answers. status: code review need to review the code type: question

Comments

@gerchicov-bp
Copy link

gerchicov-bp commented Mar 29, 2017

Need to update a readme file.

Current readme just describes how to send a purchase request.

How to handle result? How to renew it? Does sandbox users' behaviour differs from real one?

@bizz84
Copy link
Owner

bizz84 commented Apr 24, 2017

@gerchicov-bp apologies for the late reply.

This snippet summarises how to:

  • purchase a subscription, then
  • verify that the receipt is valid, then
  • check the status of the subscription
let productId = "your-product-id"
SwiftyStoreKit.purchaseProduct(productId, atomically: true) { result in
    
    if case .success(let product) = result {
        // Deliver content from server, then:
        if product.needsFinishTransaction {
            SwiftyStoreKit.finishTransaction(product.transaction)
        }
        
        let appleValidator = AppleReceiptValidator(service: .production)
        SwiftyStoreKit.verifyReceipt(using: appleValidator, password: "your-shared-secret") { result in
            
            if case .success(let receipt) = result {
                let purchaseResult = SwiftyStoreKit.verifySubscription(
                    type: .autoRenewable,
                    productId: productId,
                    inReceipt: receipt)
                
                switch purchaseResult {
                case .purchased(let expiryDate):
                    print("Product is valid until \(expiryDate)")
                case .expired(let expiryDate):
                    print("Product is expired since \(expiryDate)")
                case .notPurchased:
                    print("This product has never been purchased")
                }

            } else {
                // receipt verification error
            }
        }
        
    } else {
        // purchase error
    }
}

This is a combination of the Purchase and Verify Subscription methods.

As requested, I have updated the documentation in the README: #189

@bizz84 bizz84 added type: question answered Questions which have accepted answers. labels Apr 24, 2017
@bizz84
Copy link
Owner

bizz84 commented Apr 24, 2017

@gerchicov-bp Suggested Read:

Apple - Working with Subscriptions

@bizz84
Copy link
Owner

bizz84 commented May 18, 2017

Documentation further improved on #213.
If you don't have other questions, I will close this issue once the PR is merged.

@bizz84 bizz84 added the status: code review need to review the code label May 18, 2017
@thihaaung6245
Copy link

thihaaung6245 commented Aug 3, 2017

@bizz84 How to handle it for restored? Please correct me if I am wrong. I really need a way to handle restore process on auto-renewable subscription. And I only find documentation for restore provided only for consumable. Any Help?

@bizz84
Copy link
Owner

bizz84 commented Aug 3, 2017

Do you just need to check if an auto-renewable subscription is valid?

If so, this is all you need:

https://github.com/bizz84/SwiftyStoreKit#verify-subscription

@bizz84
Copy link
Owner

bizz84 commented Aug 22, 2017

@htarwara6245 you can information about all previously purchased non-consumable purchases and subscriptions by verifying the receipt, even if the user reinstalled the app. In your case, you can verify an auto-renewable subscription as explained in the README.

Closing this issue.

@bizz84 bizz84 closed this as completed Aug 22, 2017
@WillMays1
Copy link

My code keeps going to the // purchase error else statement. Once I verify the subscription and it shows that it has not been purchased, I call the purchase statement and it goes to the purchase error statement. What do I need to do to get this working for production?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
answered Questions which have accepted answers. status: code review need to review the code type: question
Projects
None yet
Development

No branches or pull requests

4 participants