使用AFNetworking objective-c进行自动续订的应用内购买收据验证 [英] In-app purchase receipt verification for auto renewal using AFNetworking objective-c

查看:196
本文介绍了使用AFNetworking objective-c进行自动续订的应用内购买收据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的代码中使用AFNetworking进行收据验证,它给我状态= 210002
虽然它在NSMutableURLRequest中给我status = 0

I am writing below code using AFNetworking for receipt verification and it gives me status=210002 While it gives me status=0 in NSMutableURLRequest

请帮助我通过获得解决方案

please help me by getting solution

 NSString *strurl = @"https://sandbox.itunes.apple.com/verifyReceipt";   
 NSData *receipt = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];

 NSDictionary *parameter=@{
                          @"receipt-data" : [receipt base64EncodedStringWithOptions:0],
                          @"password" : @"xxxxxxxxxxxxxxxxxxxx",
                         };


 NSData *jsonParam = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];

 AFHTTPRequestOperationManager *manager =  [AFHTTPRequestOperationManager manager];
 manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/plain"];
 [manager POST:strurl parameters:jsonParam success:^(AFHTTPRequestOperation *oprtation, id responseObject){
    NSLog(@"JSON: %@", responseObject);

 }failure:^(AFHTTPRequestOperation *operation, NSError *error){
    NSLog(@"Error: %@", error);

 }];

谢谢

推荐答案

这是我在我的应用程序中使用的Receipt验证代码,但我在swift中实现了。

Here is the Receipt validation code which i use in my application, but i have implementation in swift.

我也在使用NSMutableURLRequest进行Web服务调用到iTunes服务器。

I am also using NSMutableURLRequest for Web Service call to iTunes Server.

    func verifyPaymentReceipt(){

    let mainBundle = NSBundle.mainBundle() as NSBundle;
    let receiptUrl = mainBundle.appStoreReceiptURL;
    let isPresent = receiptUrl?.checkResourceIsReachableAndReturnError(NSErrorPointer());

    if(isPresent == true){

        let data = NSData(contentsOfURL: receiptUrl! );

        // Create the JSON object that describes the request

        let requestContents  = NSMutableDictionary();
        //            let encodeddata = data!.base64EncodedStringWithOptions(NSDataBase64EncodingOptions());
        let encodeddata = data!.base64EncodedString();

        print("encodeddata = \(encodeddata)");

        requestContents.setObject(encodeddata, forKey: "receipt-data");
        requestContents.setObject("xxxxxxxxxxxxxxxxxxxxxxx", forKey: "password");
        var requestData : NSData?
        do{
            requestData = try NSJSONSerialization.dataWithJSONObject(requestContents, options: NSJSONWritingOptions());
        }catch{
            NSLog("Error in json data creation at verifyPaymentReceipt");
        }

        let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as NSString
        let file = "\(documentsPath)/requestData"

        if(NSFileManager.defaultManager().createFileAtPath(file, contents: data, attributes: nil)){
            NSLog("File %@ ",file);
        }
        else{
            NSLog("error File %@ ",file);
        }



        if(requestData != nil){

            let strRequestData = NSString(data: requestData!, encoding: NSUTF8StringEncoding);
            print(" strRequestData = \(strRequestData)");
            // Create a POST request with the receipt data.

            let storeURL = NSURL(string: "https://sandbox.itunes.apple.com/verifyReceipt");
            let storeRequest = NSMutableURLRequest(URL: storeURL!);
            storeRequest.HTTPMethod = "POST";
            storeRequest.HTTPBody = requestData;

            // Make a connection to the iTunes Store on a background queue.

            let queue = NSOperationQueue();
            NSURLConnection.sendAsynchronousRequest(storeRequest, queue: queue, completionHandler: { (response : NSURLResponse?, data : NSData?, error : NSError?) -> Void in

                if(error != nil){
                    //Handle Error
                }
                else{
                    let d = NSString(data: data!, encoding: NSUTF8StringEncoding);
                    NSLog("DATA:%@", d!);

                    var jsonResponse: NSMutableDictionary?
                    do{
                        jsonResponse = try NSJSONSerialization.JSONObjectWithData(data!,
                            options: NSJSONReadingOptions.AllowFragments) as? NSMutableDictionary;
                        print(jsonResponse);

                    }catch{
                        NSLog("Parsing issue : verifyPaymentReceipt");
                    }

                    if(jsonResponse != nil){

                        let expirationDate: NSDate? = self.expirationDateFromResponse(jsonResponse!);
                        NSLog("Expiration Date: %@", expirationDate!);

                    }
                }
            });

        }

    }

}

As you mention that your code works fine with NSMutableURLRequest but it returns 21002 with AFNetworking.

21002 - receipt-data属性中的数据格式错误或丢失。

21002 - The data in the receipt-data property was malformed or missing.

这意味着您在使用AFNetworking时编码收据数据格式错误。所以我认为这是使用AFNetworking进行编码的问题。

It means that your encoding receipt data is malformed while using AFNetworking. So i think it is issue of encoding with AFNetworking.

在iOS 7中,Apple在NSData上引入了新的base64方法,这使得不必使用第三方base 64解码库,但我仍然建议您尝试使用 Base64 编码进行收据编码。我希望这可以通过AFNetworkig解决您的问题。因为当我从服务器端验证收据时,我也面临21002的相同问题,并且此编码库在这种情况下有效。不知道如何,但它解决了我在服务器端的问题,以进行收据验证调用。希望它也适合你。

In the iOS 7 , Apple introduced new base64 methods on NSData that make it unnecessary to use a 3rd party base 64 decoding library , but i still suggest you to try to use Base64 encoding for receipt encoding. I hope this will solve your problem with AFNetworkig. As i also face same issue of 21002 when I verify receipt from server side and this encoding library works in this case. Don't know How , but it solved my issue at server side for receipt validation call. Hope it will work for you also.

这篇关于使用AFNetworking objective-c进行自动续订的应用内购买收据验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆