服务器验证在应用程序购买OSX应用程序的收据

时间:2013-08-28 08:58:56

标签: macos in-app-purchase receipt

我正在开发应用程序购买验证OSX应用程序并获得一些问题〜

我的请求代码如下:

    NSData* data = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
    NSString* base64Str = [data base64EncodedString];

    NSDictionary* dict = [NSDictionary dictionaryWithObject:base64Str forKey:@"receipt-data"];
    SBJSON *jsonParser = [[[SBJSON alloc] init] autorelease];
    NSString* jsonStr = [jsonParser stringWithObject:dict error:nil];
    postUrl = [NSURL URLWithString:@"https://sandbox.itunes.apple.com/verifyReceipt"];

    _verifyRequest = [[NSMutableURLRequest alloc] init];
    [_verifyRequest setTimeoutInterval:_sTimeoutInterval];
    [_verifyRequest setURL:postUrl];
    [_verifyRequest setHTTPBody:[jsonStr dataUsingEncoding:NSUTF8StringEncoding]];
    [_verifyRequest setHTTPMethod:ASCommon_HttpMethodPost];
    _verifyConnection = [[NSURLConnection alloc] initWithRequest:_verifyRequest delegate:self];
    [_verifyConnection start];

我得到了这样的结果

{
    exception = "java.lang.IllegalArgumentException";
    status = 21002;
}

我记录jsonStr变量似乎没有问题:

{"receipt-data":"..........................................................."}

Apple doc here说“检索收据数据.......在OS X上,这是应用程序包内的收据文件的全部内容。使用base64编码对收据数据进行编码。” p>

我是否误解了收据数据是文件的全部内容?或者我的代码出了什么问题?

1 个答案:

答案 0 :(得分:1)

我想我找到了解决方案。默认情况下,base64编码的字符串类不会删除换行符。您需要更改它以删除换行符。改变这个功能:

- (NSString *)base64EncodedString
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], true, &outputLength);

    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

- (NSString *)base64EncodedString : (BOOL)includeLineBreaks
{
    size_t outputLength;
    char *outputBuffer =
        NewBase64Encode([self bytes], [self length], includeLineBreaks, &outputLength);

    NSString *result =
        [[[NSString alloc]
            initWithBytes:outputBuffer
            length:outputLength
            encoding:NSASCIIStringEncoding]
        autorelease];
    free(outputBuffer);
    return result;
}

并在参数中传递NO。