ios 8, Swift, receipt validation for IAP

2.8k views Asked by At

i'm desperately trying to validate a receipt for in-app in a futur IOS application written with Swift, IOS8.1

I'm using the Alamofire framework to compensate a bug in NSURLConnection for swift but still not having any return from the Apple Server.

func checkReceipt(data:NSData) {

    let ITMS_PROD_VERIFY_RECEIPT_URL        = "https://buy.itunes.apple.com/verifyReceipt"
    let ITMS_SANDBOX_VERIFY_RECEIPT_URL     = "https://sandbox.itunes.apple.com/verifyReceipt"

    let base64String = self.base64forData(data)
    if base64String != nil {

        let decodedData = NSData(base64EncodedString: base64String!, options: NSDataBase64DecodingOptions(0))
        let receiptInformation = self.dictionaryFromPlistData(decodedData!)

        let payload:NSString = "{\"receipt-data\" : \"\(base64String)\"}"
        println("-----------------------------------------")
        println("payload \(payload)")
        println("-----------------------------------------")
        let payloadData:NSData = payload.dataUsingEncoding(NSUTF8StringEncoding)!

        let serverURL = NSURL(string: ITMS_SANDBOX_VERIFY_RECEIPT_URL) //ITMS_PROD_VERIFY_RECEIPT_URL;

        var request = NSMutableURLRequest(URL: serverURL!)    // (URL:
        request.HTTPMethod = "POST"
        request.HTTPBody = payloadData

        let (param, _) = Alamofire.ParameterEncoding.URL.encode(request, parameters: nil)

        Alamofire.request(param)
            .response { (request, response, data, error) in
                    println(request)
                    println(response)
                    println(error)
            }
    }
}

The feedback from the server is :

Optional(<NSHTTPURLResponse: 0x17403b760> { URL: https://sandbox.itunes.apple.com/verifyReceipt } { status code: 200, headers {
    "Cache-Control" = "private, no-cache, no-store, no-transform, must-revalidate, max-age=0";
    Connection = "keep-alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 36;
    Date = "Tue, 11 Nov 2014 19:20:38 GMT";
    Expires = "Tue, 11 Nov 2014 19:20:38 GMT";
    "Set-Cookie" = "Pod=100; version=\"1\"; expires=Thu, 11-Dec-2014 19:20:38 GMT; path=/; domain=.apple.com, itspod=100; version=\"1\"; expires=Thu, 11-Dec-2014 19:20:38 GMT; path=/; domain=.apple.com, mzf_in=990212; version=\"1\"; path=/WebObjects; domain=.apple.com; secure; HttpOnly, mzf_dr=0; version=\"1\"; expires=Thu, 01-Jan-1970 00:00:00 GMT; path=/WebObjects; domain=.apple.com";
    "apple-timing-app" = "193 ms";
    "edge-control" = "no-store, cache-maxage=0";
    itspod = 100;
    pod = 100;
    "x-apple-application-instance" = nnnnnnnn;
    "x-apple-application-site" = SB;
    "x-apple-jingle-correlation-key" = xxxxxxxxx;
    "x-apple-lokamai-no-cache" = true;
    "x-apple-orig-url" = "http://sandbox.itunes.apple.com/WebObjects/MZFinance.woa/wa/verifyReceipt";
    "x-apple-translated-wo-url" = "/WebObjects/MZFinance.woa/wa/verifyReceipt";
    "x-frame-options" = SAMEORIGIN;
    "x-webobjects-loadaverage" = 0;
} })

Instead of the Application receipt...

Any help appreciated

2

There are 2 answers

0
dengApro On

Thanks @R00We

Swift 5 , and Alamofire (5.0.0-beta.7)

  func validateReceipt() {
        guard let receiptURL = Bundle.main.appStoreReceiptURL, let data = try? Data(contentsOf: receipt ) else {
              return
        }

        let model = ApplePayModel(receipt: data.base64EncodedString(options: []), "password": "YOU_SHARED_SECRET")

        AF.request( "https://sandbox.itunes.apple.com/verifyReceipt" , method: HTTPMethod.post, parameters: model, encoder: JSONParameterEncoder.prettyPrinted, headers: <Your Header, maybe be nil > ).response { (response: DataResponse<Data?>) in

            if let data = response.data{
                 // JSONSerialization it
            }

        }
 }

Use request<Parameters: Encodable>,

struct ApplePayModel: Encodable{
    let receipt: String
    let order_id: String


    private enum CodingKeys: String, CodingKey {
        case receipt = "receipt-data", order_id
    }
}
0
R00We On

So. You problem is simple. You watch on response header instead body. Watch data. Also instead response method better use .responseJSON(options: [], completionHandler:) method, see my code

func validateReceipt() {
    if let receipt =  NSBundle.mainBundle().appStoreReceiptURL {
        if let data = NSData(contentsOfURL: receipt) {
            let requestContents:[String:String] = ["receipt-data":data.base64EncodedStringWithOptions([]), "password": "YOU_SHARED_SECRET"]
            let requestData = try! NSJSONSerialization.dataWithJSONObject(requestContents,options: [])
            let request = NSMutableURLRequest(URL: NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt")!)
            request.HTTPMethod = "POST"
            request.HTTPBody = requestData
            let (param, _) = Alamofire.ParameterEncoding.URL.encode(request, parameters: nil)

            Alamofire.request(param)
                .responseJSON(options: [], completionHandler: { (result) -> Void in
                    print(result)
                })
        }
    }
}