Monday, 7 April 2014

In App Purchase implementation with receipt validation from iTunes via Server side involvment

Developer just need to encode transactionReceipt into Base64 via below command:



 NSString *receipt= [transaction.transactionReceipt base64EncodedStringWithOptions:kNilOptions];
// call below method and send this string
//and then send this receipt to your server using below code.



-(void)hitLocalServerReceipt:(NSString *)receiptString
{

        NSString *new_url_new =[NSString stringWithFormat:@"http:yourLink"];
    
    //initialize url that is going to be fetched.
    NSString *strURL = [new_url_new stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strURL];
    
    //initialize a request from url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[url standardizedURL]];
    
    //set http method
    [request setHTTPMethod:@"POST"];
    //initialize a post data
    
  
    NSString *postData = [NSString stringWithFormat:@"receipt=%@",receiptString];
    
    [request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
    //set post data of request
    [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
   
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSError *e = nil;
        NSArray* response_arr= [NSJSONSerialization JSONObjectWithData:responseObject options: NSJSONReadingMutableContainers error: &e];
        NSLog(@"response from local server%@",response_arr);
        [self requestingDOwnload:response_arr];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"ERROR : %@",error);
         
     }];
    [operation start];
}


//Here i am using Afnetworking so dont confuse you can use NSURL connection or as per //your requirement