Search code examples
iosin-app-purchasereceiptreceipt-validation

How to get app receipt after purchase using test account in iOS


I am working with IAP, I want to get the receipt so that I can validate it.

I have tried this

-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions {
    for (SKPaymentTransaction *transaction in transactions) {


        switch (transaction.transactionState)//Each transaction should be processed by examining transactionState property.
        {
             case SKPaymentTransactionStatePurchased:
            {

                if([transaction.payment.productIdentifier isEqualToString:@"TC0001"])
                {

                }

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

                NSError *error;

                NSDictionary *response = [NSJSONSerialization JSONObjectWithData: data options: 0 error: &error];  //I am using sbjson to parse

                NSLog(@"%@",response);**               


                //Finish transaction deletes the transaction from the default queue
                [[SKPaymentQueue defaultQueue] finishTransaction:transaction];                

            }
                break;                

            default:
                break;
        }
    }
}

I hope I am doing right, because this is what mentioned in docs. But I get "null for response. So is that I am missing something.


Solution

  • The receipt is not stored in plain-text JSON.

    It's actually in the format of a PKCS7 encoded container which is a binary format based on an old-time telecom encoding format called ASN.1.

    I can't point you to any examples because no one has shared their code for decoding the container. Developers are reluctant to share their code because Apple pointed out that if many apps share similar code for handling IAP, a single security exploit would threaten not just one, but many apps.

    There's a WWDC presentation: "WWDC 2013: Using Receipts to Protect Your Digital Sales" where they provide some suggestions on how to get started, and there's also a document: "Receipt Validation Programming Guide" in the Xcode docs.

    Read those and then Google for pertinent terms, you should find enough bits and pieces to assemble a solution.