次のメソッドを使用して領収書データを取得しています。
// 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