检查自动续订订阅是否仍然有效

时间:2023-01-12 21:29:51

I would like to check the Auto Renewable Subscription status whenever I open the app.

每当我打开应用程序时,我都想检查自动可续订订阅状态。

This is to make sure that the user is still subscribed to the service. How do I achieve this?

这是为了确保用户仍然订阅了该服务。我该如何实现这一目标?

Any thoughts? Thank you

有什么想法吗?谢谢

P.S.: I am using SwiftyStoreKit

P.S。:我正在使用SwiftyStoreKit

2 个解决方案

#1


3  

Here is several ways to do receipt validation to check is user granted to subscription. Here is two ways of doing it correctly:

以下是几种进行收据验证以检查用户是否已授予订阅的方法。这有两种正确的方法:

  1. Do receipt validation locally as it is written here.
  2. 在此处写入收据验证。
  3. Do receipt validation remotely as it is written here. It is mentioned that receipt should not be sent to App Store white an app. Short summary:

    在此处写入时远程进行收据验证。提到收据不应该发送到App Store白色应用程序。简短的摘要:

    • Your app sends receipt to your backend.
    • 您的应用会将收据发送到您的后端。
    • Your backend sends receipt to Apple backend for validation.
    • 您的后端将收据发送给Apple后端进行验证。
    • Your backend gets response from the apple.
    • 你的后端得到苹果的回应。
    • Your backend sends result back to your app is receipt valid or invalid.
    • 您的后端将结果发回给您的应用,收据有效或无效。

In both ways you will get list of in-app purchases. It will contain expired subscriptions as well. You would need to go throw all subscriptions and and check expiration date. If it is still valid you must to grant user with subscription.

通过这两种方式,您将获得应用内购买列表。它也将包含过期的订阅。您需要抛出所有订阅并检查到期日期。如果它仍然有效,您必须向用户授予订阅。

As I understand you are using SwiftyStoreKit and here is open task for local receipt validation.

据我所知,您正在使用SwiftyStoreKit,这是本地收据验证的开放任务。

#2


3  

You can check with this function. its works with swift4

您可以查看此功能。它适用于swift4

func receiptValidation() {
let SUBSCRIPTION_SECRET = "yourpasswordift"
let receiptPath = Bundle.main.appStoreReceiptURL?.path
if FileManager.default.fileExists(atPath: receiptPath!){
    var receiptData:NSData?
    do{
        receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
    }
    catch{
        print("ERROR: " + error.localizedDescription)
    }
    //let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)

    print(base64encodedReceipt!)


    let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]

    guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
    do {
        let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
        let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"  // this works but as noted above it's best to use your own trusted server
        guard let validationURL = URL(string: validationURLString) else { print("the validation url could not be created, unlikely error"); return }
        let session = URLSession(configuration: URLSessionConfiguration.default)
        var request = URLRequest(url: validationURL)
        request.httpMethod = "POST"
        request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
        let task = session.uploadTask(with: request, from: requestData) { (data, response, error) in
            if let data = data , error == nil {
                do {
                    let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                    print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
                    // if you are using your server this will be a json representation of whatever your server provided
                } catch let error as NSError {
                    print("json serialization failed with error: \(error)")
                }
            } else {
                print("the upload task returned an error: \(error)")
            }
        }
        task.resume()
    } catch let error as NSError {
        print("json serialization failed with error: \(error)")
    }



}
}

#1


3  

Here is several ways to do receipt validation to check is user granted to subscription. Here is two ways of doing it correctly:

以下是几种进行收据验证以检查用户是否已授予订阅的方法。这有两种正确的方法:

  1. Do receipt validation locally as it is written here.
  2. 在此处写入收据验证。
  3. Do receipt validation remotely as it is written here. It is mentioned that receipt should not be sent to App Store white an app. Short summary:

    在此处写入时远程进行收据验证。提到收据不应该发送到App Store白色应用程序。简短的摘要:

    • Your app sends receipt to your backend.
    • 您的应用会将收据发送到您的后端。
    • Your backend sends receipt to Apple backend for validation.
    • 您的后端将收据发送给Apple后端进行验证。
    • Your backend gets response from the apple.
    • 你的后端得到苹果的回应。
    • Your backend sends result back to your app is receipt valid or invalid.
    • 您的后端将结果发回给您的应用,收据有效或无效。

In both ways you will get list of in-app purchases. It will contain expired subscriptions as well. You would need to go throw all subscriptions and and check expiration date. If it is still valid you must to grant user with subscription.

通过这两种方式,您将获得应用内购买列表。它也将包含过期的订阅。您需要抛出所有订阅并检查到期日期。如果它仍然有效,您必须向用户授予订阅。

As I understand you are using SwiftyStoreKit and here is open task for local receipt validation.

据我所知,您正在使用SwiftyStoreKit,这是本地收据验证的开放任务。

#2


3  

You can check with this function. its works with swift4

您可以查看此功能。它适用于swift4

func receiptValidation() {
let SUBSCRIPTION_SECRET = "yourpasswordift"
let receiptPath = Bundle.main.appStoreReceiptURL?.path
if FileManager.default.fileExists(atPath: receiptPath!){
    var receiptData:NSData?
    do{
        receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped)
    }
    catch{
        print("ERROR: " + error.localizedDescription)
    }
    //let receiptString = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions(rawValue: 0))
    let base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)

    print(base64encodedReceipt!)


    let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]

    guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
    do {
        let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)
        let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"  // this works but as noted above it's best to use your own trusted server
        guard let validationURL = URL(string: validationURLString) else { print("the validation url could not be created, unlikely error"); return }
        let session = URLSession(configuration: URLSessionConfiguration.default)
        var request = URLRequest(url: validationURL)
        request.httpMethod = "POST"
        request.cachePolicy = URLRequest.CachePolicy.reloadIgnoringCacheData
        let task = session.uploadTask(with: request, from: requestData) { (data, response, error) in
            if let data = data , error == nil {
                do {
                    let appReceiptJSON = try JSONSerialization.jsonObject(with: data)
                    print("success. here is the json representation of the app receipt: \(appReceiptJSON)")
                    // if you are using your server this will be a json representation of whatever your server provided
                } catch let error as NSError {
                    print("json serialization failed with error: \(error)")
                }
            } else {
                print("the upload task returned an error: \(error)")
            }
        }
        task.resume()
    } catch let error as NSError {
        print("json serialization failed with error: \(error)")
    }



}
}