Skip to content

Instantly share code, notes, and snippets.

@enigmaticape
Created May 21, 2014 22:16
Show Gist options
  • Save enigmaticape/36ba91e7be9d48a80035 to your computer and use it in GitHub Desktop.
Save enigmaticape/36ba91e7be9d48a80035 to your computer and use it in GitHub Desktop.
- (id) parseReceiptData:(NSData *)receipt_data OnAppStoreUsingURL:(NSURL *) store_URL {
NSDictionary * request_contents = @{
@"receipt-data" : [receipt_data base64EncodedStringWithOptions:0]
};
NSError * error = nil;
NSData * request_data = (
[NSJSONSerialization
dataWithJSONObject: request_contents
options : 0
error : &error
]
);
NSAssert(error == nil, @"JSON Serialization Error : %@", error);
NSMutableURLRequest * store_request = [NSMutableURLRequest requestWithURL:store_URL];
[store_request setHTTPMethod:@"POST"];
[store_request setHTTPBody:request_data];
NSURLResponse * response;
NSData * result = (
[NSURLConnection
sendSynchronousRequest: store_request
returningResponse : &response
error : &error
]
);
NSAssert(error == nil, @"NSURLConnection Error : %@", error);
NSDictionary * json_response;
if( result ) {
json_response = (
[NSJSONSerialization
JSONObjectWithData: result
options : 0
error : &error
]
);
NSAssert(error == nil, @"JSON Serialization Error : %@", error);
}
return json_response;
}
- (NSDictionary *) parseReceiptOnAppStore {
NSURL * receipt_url = [[NSBundle mainBundle] appStoreReceiptURL];
NSData * receipt_data = [NSData dataWithContentsOfURL:receipt_url];
NSURL * sandbox_url = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];
NSURL * production_url = [NSURL URLWithString:@"https://buy.itunes.apple.com/verifyReceipt"];
NSDictionary * response;
response = (
[self
parseReceiptData : receipt_data
OnAppStoreUsingURL: production_url
]
);
// https://developer.apple.com/library/ios/technotes/tn2259/_index.html#//apple_ref/doc/uid/DTS40009578-CH1-FREQUENTLY_ASKED_QUESTIONS
// The magic number 21007 means we hit up the production app store servers
// from the sandbox, viz we are in testing and should switch to the sandbox
// server thusly ...
if( [response[@"status"] intValue] == 21007 ) {
response = (
[self
parseReceiptData : receipt_data
OnAppStoreUsingURL: sandbox_url
]
);
}
return response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment