这个,在SKPaymentTransactionStatePurchased完成之后
- 获取购买完成后的本地收据数据,以BASE64编码的字符串进行传输
- 将本地收据数据组装成JSON object,并返回 JSON data
- 用本地数据返回的收据数据创建一个POST 请求,根据请求返回的JSON data解析,是否需要重新验证
异步方式--apple
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
NSError *error;
NSDictionary *requestContents = @{@"receipt-data": [receipt base64EncodedStringWithOptions:0]};
NSData *requestData = [NSJSONSerialization dataWithJSONObject:requestContents
options:0
error:&error];
if (!requestData)
{
NSLog(@"%@", error);
/* ... Handle error ... */
}
else
{
NSLog(@"%@", error);
}
NSURL *storeURL = [NSURL URLWithString:AppStore];
NSMutableURLRequest *storeRequest = [NSMutableURLRequest requestWithURL:storeURL];
[storeRequest setHTTPMethod:@"POST"];
[storeRequest setHTTPBody:requestData];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:storeRequest queue:queue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
{
if (connectionError)
{
NSLog(@"%@", error);
/* ... Handle error ... */
}
else
{
NSError *error;
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (jsonResponse != nil)
{
NSString *status = [NSString stringWithFormat:@"%@", jsonResponse[@"status"]];
if ([status isEqualToString:@"21007"])
{
//ddddddd
}
}
}
}
同步的的方式
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL]; //交易完成后,会将凭据存放在该地址
//
NSData *receiptDataOnLocal = [NSData dataWithContentsOfURL:receiptURL]; // 从沙盒中获取到购买凭据
NSURL *url = [NSURL URLWithString:AppStore]; //对购买凭据进行验证
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0f];
request.HTTPMethod = @"POST";
NSString *encodeStr = [receiptDataOnLocal base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
NSString *payload = [NSString stringWithFormat:@"{\"receipt-data\" : \"%@\"}", encodeStr];
NSData *payloadData = [payload dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = payloadData;
NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
if (result == nil)
{
NSLog(@"验证失败");
}
else
{
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:result options:NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", dict);
if (dict != nil)
{
// bundle_id、application_version、product_id、transaction_id
NSLog(@"验证成功");
NSLog(@"bundle_id:%@ , application_version:%@", dict[@"receipt"][@"bundle_id"], dict[@"receipt"][@"application_version"]);
NSDictionary *dict1 = [dict[@"receipt"][@"in_app"] firstObject];
NSLog(@"product_id:%@ , transaction_id:%@", dict1[@"product_id"], dict1[@"transaction_id"]);
}
}