JRDesign
JRDesign

Reputation: 21

In App Purchase Receipt Verification

I'm having troubles getting a response from my receipt validation for my in-app purchase. Can you please take a look at the code below and tell me why I'm not getting anything in return? Also, if there are any suggestions on good ways to test receipt verification, please do tell!

Many thanks in advance!

Xcode:

- (BOOL)verifyReceipt:(SKPaymentTransaction *)transaction {

NSString *jsonObjectString = [self encode:(uint8_t *)transaction.transactionReceipt.bytes length:transaction.transactionReceipt.length];      
NSString *completeString = [NSString stringWithFormat:@"http://www.myURL.com/vReceipt.php?receipt=%@", jsonObjectString];                               
NSURL *urlForValidation = [NSURL URLWithString:completeString];               
NSMutableURLRequest *validationRequest = [[NSMutableURLRequest alloc] initWithURL:urlForValidation];                          
[validationRequest setHTTPMethod:@"GET"];             
NSData *responseData = [NSURLConnection sendSynchronousRequest:validationRequest returningResponse:nil error:nil];  
NSString *responseString = [[NSString alloc] initWithData:responseData encoding: NSUTF8StringEncoding];
NSInteger response = [responseString intValue];
 NSLog(@"Response String: %@", jsonObjectString);
 NSLog(@"Response Integer: %@", response);
 return (response = 0);}

- (NSString *)encode:(const uint8_t *)input length:(NSInteger)length {
static char table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

NSMutableData *data = [NSMutableData dataWithLength:((length + 2) / 3) * 4];
uint8_t *output = (uint8_t *)data.mutableBytes;

for (NSInteger i = 0; i < length; i += 3) {
    NSInteger value = 0;
    for (NSInteger j = i; j < (i + 3); j++) {
           value <<= 8;

           if (j < length) {
                value |= (0xFF & input[j]);
           }
    }

    NSInteger index = (i / 3) * 4;
    output[index + 0] =                    table[(value >> 18) & 0x3F];
    output[index + 1] =                    table[(value >> 12) & 0x3F];
    output[index + 2] = (i + 1) < length ? table[(value >> 6)  & 0x3F] : '=';
    output[index + 3] = (i + 2) < length ? table[(value >> 0)  & 0x3F] : '=';
}

return [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];}

PHP Code:

<?php

$myreceipt = json_encode(array('receipt-data' => $_GET['receipt']));
$url = "https://sandbox.itunes.apple.com/verifyReceipt";

//create cURL Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $myreceipt);

//execute cURL Request
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);

$response_json = json_decode($response);

print $response_json->{'status'};

?>

Upvotes: 2

Views: 1540

Answers (2)

FunkyKat
FunkyKat

Reputation: 3223

This string always returns 0:

return (response = 0);}

Corrected one:

return (response == 0);}

Upvotes: 1

over_optimistic
over_optimistic

Reputation: 1419

You should do stripslashes() on the data you get from the iphone perhaps

Upvotes: 0

Related Questions