1



Retrieve the receipt data from the transaction’s transactionReceipt property and encode it using base64 encoding.

How can I encode NSData using base64 encoding? Please give the code for that.

EDIT
I did it. but now the response is

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

my recipt verification method is this

-(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;
}

Where

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

returns me base64 encoded string.
I also tried

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

but response is same.
can any one of you let me know where I could be wrong.
Thanks-

Developer
  • 6,375
  • 12
  • 58
  • 92

2 Answers2

15
+ (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];
}
Saurabh
  • 22,743
  • 12
  • 84
  • 133
  • Thank you for this! – beretis Sep 25 '16 at 15:34
  • where did you get the base64 encoding code that works with Apple receipt verifier server? it was working but it is no longer working anymore (current iOS 10.0, base sdk: 8.0) – Zennichimaro Jan 13 '17 at 03:53
  • sorry.. I POST receipt-data="xxx" whereas I should POST in JSON "{ \"receipt-data\" : \"xxx\"}" – Zennichimaro Jan 13 '17 at 07:30
  • now it works, but I am still curious where did you get the code.. even Apple's [NSData base64EncodedStringWithOptions] doesn't work, whatever the options is... stupid Apple.. – Zennichimaro Jan 13 '17 at 07:31
  • This above is the core algorithm to convert anything to base64 if you search google you could find this in almost every other language – Saurabh Jan 16 '17 at 14:28
1

You need to post your receipt to "https://sandbox.itunes.apple.com/verifyReceipt" while testing in the sandbox environment.

deko
  • 2,534
  • 3
  • 34
  • 48