Receipt Data is sent as bytes?

2020-05-04 06:11发布

问题:

Hi I am doing InApp purchase. Got the receipt data successfully and encoded with base64EncodedString and sent to our own server then our own server connected to apple server and the response was "Invalid Receipt".Then I added the secret key and encoded it again. Now receipt data is sent as "bytes".How to send the original receipt data to our server.

func receiptValidation(productId:String,requestFrom:String)
    {
        let SUBSCRIPTION_SECRET = "My_Secret_Key"
        applicationDelegate.application=self
        var defaultManager:Alamofire.SessionManager!
        defaultManager = {
              let serverTrustPolicies: [String: ServerTrustPolicy] = [
                  opcodeIpPort : .disableEvaluation
              ]
            let configure = URLSessionConfiguration.background(withIdentifier: Bundle.main.bundleIdentifier!)
            session1=URLSession(configuration: .default, delegate: applicationDelegate.application, delegateQueue: OperationQueue.main)
            configure.httpAdditionalHeaders = Alamofire.SessionManager.defaultHTTPHeaders
            return Alamofire.SessionManager(
                  configuration: configure,
                  serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
              )
          }()
        let defaults = UserDefaults.standard
        var requestData:NSData!
        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 base64encodedReceipt = receiptData?.base64EncodedString(options: NSData.Base64EncodingOptions.endLineWithCarriageReturn)
                let requestDictionary = ["receipt-data":base64encodedReceipt!,"password":SUBSCRIPTION_SECRET]
                 guard JSONSerialization.isValidJSONObject(requestDictionary) else {  print("requestDictionary is not valid JSON");  return }
                        do {
                            requestData = try JSONSerialization.data(withJSONObject: requestDictionary) as NSData
                             requestData.map { String(format: "%02x", $0) }.joined()
                           }
                 catch let error as NSError {
                            print("json serialization failed with error: \(error)")
                        }
                        print("requestData is\(requestData)")
                        var postString =
                            [ "receiptData": requestData,
                               "subscriberId": defaults.array(forKey: "userDetails")?.first as! String, 
                               "password": defaults.array(forKey: "userDetails")?.last as! String,
                               "productId":encodeString(normalString: productId ),
                               "code":opcodeDetails["opCode"]!,
                                "deviceType":"IOS"
                            ] as [String : Any] as [String : Any]
                        let URLForApplication:String = String(format:"%@/api/validate-receipt-data",opcodeDetails["apiProxyBaseUrl"]!)
                        defaultManager.request(URLForApplication, method: .post, parameters: postString, encoding: URLEncoding.default) .responseJSON
                        { response in
                            print(response)
                            if   response.result.isSuccess
                            {
                                if let data = response.data {
                                                              let json = String(data: data, encoding: String.Encoding.utf8)
                                                              print("Response: \(json)")
                                                          }
                            }
                            else
                            {
                                print("there is an error")
                            }

                            defaultManager.session.invalidateAndCancel()

                        }

    }
    }

How to get the receipt data from Data and sent it to out server.Thanks in advance.