【问题标题】:Verifying a Receipt with the App Store使用 App Store 验证收据
【发布时间】:2011-04-06 13:40:27
【问题描述】:



从交易的 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]];

但是反应是一样的。
你们中的任何人都可以让我知道我可能错在哪里。
谢谢-

【问题讨论】:

  • 了解更多上下文会很有帮助。

标签: iphone


【解决方案1】:
+ (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];
}

【讨论】:

  • 谢谢!
  • 您从哪里获得适用于 Apple 收据验证服务器的 base64 编码代码?它工作正常,但不再工作(当前 iOS 10.0,基本 sdk:8.0)
  • 对不起.. 我发布收据数据 =“xxx” 而我应该在 JSON 中发布 "{ \"receipt-data\" : \"xxx\"}"
  • 现在它可以工作了,但我仍然很好奇你从哪里得到代码..即使苹果的 [NSData base64EncodedStringWithOptions] 也不起作用,无论选项是什么......愚蠢的苹果..
  • 以上是将任何内容转换为 base64 的核心算法,如果您搜索谷歌,您几乎可以在所有其他语言中找到它
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2011-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-22
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    相关资源
    最近更新 更多