6

我正在使用以下方法获取收据数据:

// Use this method instead of accessing transaction.transactionReceipt directly!
- (NSData *)appStoreReceiptForPaymentTransaction:(SKPaymentTransaction *)transaction {
    NSData *receiptData = nil;

    // This is just a quick/dummy implementation!
    if (kiOS7) {
        NSURL *receiptFileURL = [[NSBundle mainBundle] appStoreReceiptURL];
        receiptData = [NSData dataWithContentsOfURL:receiptFileURL]; // Returns valid NSData object

    } else {
        receiptData = transaction.transactionReceipt; // Returns valid NSData object
    }        

    return receiptData;
}

我正在使用以下代码来读取 JSON 收据:

NSData *receipt = [self appStoreReceiptForPaymentTransaction:transaction];

NSError *error = nil;
NSDictionary *receiptDict = [receipt dictionaryFromPlistData:&error];
NSString *transactionPurchaseInfo = [receiptDict objectForKey:@"purchase-info"];
NSString *decodedPurchaseInfo = [NSString stringWithUTF8String:[[NSData dataFromBase64String:transactionPurchaseInfo] bytes]];
NSDictionary *purchaseInfoDict = [[decodedPurchaseInfo dataUsingEncoding:NSUTF8StringEncoding] dictionaryFromPlistData:&error];

NSString *transactionID = [purchaseInfoDict objectForKey:@"transaction-id"];
NSString *purchaseDateString = [purchaseInfoDict objectForKey:@"purchase-date"];
NSString *signature = [receiptDict objectForKey:@"signature"];
NSString *signatureDecoded = [NSString stringWithUTF8String:[[NSData dataFromBase64String:signature] bytes]];

// Convert the string into a date
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];

NSDate *purchaseDate = [dateFormat dateFromString:[purchaseDateString stringByReplacingOccurrencesOfString:@"Etc/" withString:@""]];

NSLog(@"Raw receipt content: \n%@", [NSString stringWithUTF8String:[receipt bytes]]);
NSLog(@"Purchase Info: %@", purchaseInfoDict);
NSLog(@"Transaction ID: %@", transactionID);
NSLog(@"Purchase Date: %@", purchaseDate);
NSLog(@"Signature: %@", signatureDecoded);

dictionaryFromPlistData:方法为返回的数据返回正确的NSDictionary对象transactionReceipt,但它使用使用返回的nil数据返回appStoreReceiptURL- 因此我没有得到有效的收据!

- (NSDictionary *)dictionaryFromPlistData:(NSError **)outError {
    NSError *error;
    NSDictionary *dictionaryParsed = [NSPropertyListSerialization propertyListWithData:self
                                                                               options:NSPropertyListImmutable
                                                                                format:nil
                                                                                 error:&error];

    if (!dictionaryParsed) {

        if (error) {
            *outError = error;
        }

        return nil;
    }

    return dictionaryParsed;
}

为什么上面的代码在 iOS 7 中不起作用?

重申一下,dictionaryFromPlistData:正确转换NSData返回的transaction.transactionReceipt.

4

1 回答 1

4

iOS 7 改变了收据验证的完成方式。我经历了你失败的确切路径并得到了相同的结果。我发现收据[[NSBundle mainBundle] appStoreReceiptURL]需要与以前不同的解码方法transaction.transactionReceipt

这是Apple 文档中的一个片段:

最外面的部分是 PKCS #7 容器,如 RFC 2315 所定义,其有效负载使用 ASN.1(抽象语法符号一)编码,如 ITU-T X.690 所定义。有效负载由一组收据属性组成。每个收据属性都包含一个类型、一个版本和一个值。

如果您正在寻找快速修复,我在这里看到了成功:https ://github.com/rmaddy/VerifyStoreReceiptiOS

于 2013-12-18T17:43:35.133 回答