iosswiftin-app-purchasereceipt-validation

Receipt Data Validation Info Not Coming


I have some issues with receipt validation. I want to purchased subscription expires date on show some places but do not get receipt data info.

 do {

        guard let receipt = Bundle.main.getReceiptData()?.base64EncodedString() else { return }

        let requestDictionary = ["receipt-data": receipt]

        guard JSONSerialization.isValidJSONObject(requestDictionary) else {
            print("requestDictionary is not valid JSON")
            return
        }
        let requestData = try JSONSerialization.data(withJSONObject: requestDictionary)

        let validationURLString = "https://sandbox.itunes.apple.com/verifyReceipt"
        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, options: .allowFragments)
                    if let dict = appReceiptJSON as? NSDictionary {
                        print(dict["last_receipt_info"]!) // nil value ????
                    }
                }catch {
                    print("JSON serialization failed with error: \(error.localizedDescription)")
                }
            } else {
                print("Upload receipt data but something went wrong. Error: \(error?.localizedDescription)")
            }
        }
        task.resume()
    } catch let error as NSError {
        print("JSON serialization failed. Error: \(error.localizedDescription)")
    }

This function result only status code.. Why ??

The resources I have reviewed:

Thank you for help to me..


Solution

  • Solved problem. Problem is not added shared secret key.

    let requestDictionary = ["receipt-data": receipt, "password": "xxxxxxxxxxxxxxx"] 
    

    True way 👆🏻