yagnik suthar
yagnik suthar

Reputation: 150

How to get active product's cancel subscription in In-App Purchase in iOS?

I've done successfully in-app purchase in my application with following demo: https://www.logisticinfotech.com/blog/ios-swift-in-app-subscription-with-receipt-validation

Issue: how to get the current active product which was canceled subscription with receipt data?

Thanks in advance.

Upvotes: 2

Views: 1707

Answers (2)

Okan T.
Okan T.

Reputation: 78

You can Access this information from Product.SubscriptionInfo.RenewalInfo.willAutoRenew at StoreKit2. WillAutoRenew variable tell us the subscription will renew or not. Note: I am assuming that you have one Subscription group.

When start any Subscription which has an group, it willAutoRenew variable set up as true. When user cancel the subscription this variable will change as false.

To open below window please fallow this path: XCODE -> Debug -> StoreKit -> Manage Transactions.

enter image description here

Please Click on Subscription Option button. After Purchased an AutoReNewable subscription transaction will be come here.

enter image description here

Select Cancel Subscription and then click on Done button.

enter image description here

As you can see the picture The subscription will not renew after cancelled by user. So You can learn cancelled information with below function. Please give the product which you want information of cancelled or not.

func IsWillRenew(product: Product) async -> Bool? {
    guard let SubscriptionGroupStatus = try? await product.subscription?.status.first else {
        print("There is no AutoRenewable Group")
        return nil
    }
    
    guard case .verified(let renewalInfo) = SubscriptionGroupStatus.renewalInfo else {
        print("The App Store could not verify your subscription status.")
        return nil
    }
    
    return renewalInfo.willAutoRenew
}

Result

enter image description here

To get Transaction aka Receipt information you can use below function.

@MainActor
func TransactionInformations() async {
    for await result in Transaction.currentEntitlements {
        do {
            //Check whether the transaction is verified. If it isn’t, catch `failedVerification` error.
            let transaction = try checkVerified(result)
            
            switch transaction.productType {
            case .autoRenewable:
                let TransactionInformation = transaction
                print("TransactionInformation", TransactionInformation)
            default: break
            }
        } catch {
            print("Transaction Verify Error: ",error)
        }
    }
}

func checkVerified<T>(_ result: VerificationResult<T>) throws -> T {
    //Check whether the JWS passes StoreKit verification.
    switch result {
    case .unverified:
        //StoreKit parses the JWS, but it fails verification.
        throw StoreError.failedVerification
    case .verified(let safe):
        //The result is verified. Return the unwrapped value.
        return safe
    }
}

Result

enter image description here

Upvotes: 1

Paul Schr&#246;der
Paul Schr&#246;der

Reputation: 1540

You get it the same way as they do with expiration_date in getExpirationDateFromResponse().

Note: The cancellation_date field is added to the receipt only when the subscription was canceled by Apple customer support.

Edit: How to get expiration_intent?

Based on the function getExpirationDateFromResponse(jsonResponse:) (from the link in your question) you could access expiration_intent like this:

guard let unifiedReceipt = jsonResponse["unified_receipt"] as? NSDictionary,
    let pendingRenewalInfos = unifiedReceipt["pending_renewal_info"] as? NSArray,
    let latestInfo = pendingRenewalInfos.firstObject as? NSDictionary,
    let expirationIntent = latestInfo["expiration_intent"] as? String else { fatalError("Couldn‘t determine expiration intent") }
print("expiration_intent \(expirationIntent)")

Upvotes: 0

Related Questions