ios 8,Swift,IAP的收据验证

时间:2014-11-11 19:30:06

标签: ios validation swift receipt alamofire

我正在拼命尝试在使用Swift,IOS8.1编写的未来IOS应用程序中验证应用内的收据

我正在使用Alamofire框架来补偿NSURLConnection中的一个错误,因为swift但仍然没有从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)
            }
    }
}

来自服务器的反馈是:

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;
} })

取代申请收据......

任何帮助表示赞赏

2 个答案:

答案 0 :(得分:1)

因此。你的问题很简单。你看反应标题而不是身体。观看数据。另外,response方法更好地使用.responseJSON(options: [], completionHandler:)方法,请参阅我的代码

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)
                })
        }
    }
}

答案 1 :(得分:0)

感谢@ R00We

Swift 5 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
            }

        }
 }

使用request<Parameters: Encodable>

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


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