使用App Store验证收据

时间:2011-04-06 13:40:28

标签: iphone



从事务的transactionReceipt属性中检索收据数据,并使用base64编码对其进行编码。

如何使用base64编码对NSData进行编码?请提供相应的代码。

修改 的 我做到了。但现在响应是

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

我的收件人验证方法是这个

-(BOOL)verifyReceipt:(SKPaymentTransaction *)transaction
{
    NSString *recieptString = [transaction.transactionReceipt base64EncodingWithLineLength:0];
    NSLog(@"%@",recieptString);

    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://buy.itunes.apple.com/verifyReceipt"]]];

    [request setPostValue:recieptString forKey:@"receipt-data"];
    [request setPostValue:@"95140bdac98d47a2b15e8e5555f55d41" forKey:@"password"];
    [request start];

    NSDictionary* subsInfo = [[request responseString] JSONValue];
    NSLog(@"%@",subsInfo);

    return subscriptionEnabled;
}

哪里

NSString *recieptString = [transaction.transactionReceipt base64EncodingWithLineLength:0];

返回base64编码的字符串。
我也试过了

NSString *recieptString = [transaction.transactionReceipt base64EncodingWithLineLength:[transaction.transactionReceipt length]];

但反应相同。
你们中的任何一个人都可以让我知道我哪里错了。
谢谢 -

2 个答案:

答案 0 :(得分:14)

+ (NSString*)base64forData:(NSData*)theData {
    const uint8_t* input = (const uint8_t*)[theData bytes];
    NSInteger length = [theData length];

    static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

    NSMutableData* data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
    uint8_t* output = (uint8_t*)data.mutableBytes;

    NSInteger i;
    for (i=0; i < length; i += 3) {
        NSInteger value = 0;
        NSInteger j;
        for (j = i; j < (i + 3); j++) {
            value <<= 8;

            if (j < length) {
                value |= (0xFF & input[j]);
            }
        }

        NSInteger theIndex = (i / 3) * 4;
        output[theIndex + 0] =                    table[(value >> 18) & 0x3F];
        output[theIndex + 1] =                    table[(value >> 12) & 0x3F];
        output[theIndex + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
        output[theIndex + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
    }

    return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];
}

答案 1 :(得分:1)

在沙盒环境中进行测试时,您需要将收据发布到“https://sandbox.itunes.apple.com/verifyReceipt”。