简体   繁体   中英

In App Purchase Server verification

I'm currently trying to verify my In App Purchase Receipt with the AppStore using server side verification (in PHP).

Here is my code:

// ViewController.m:
NSString *receiptDataString = [[NSString alloc] initWithData:[transaction transactionReceipt] encoding:NSUTF8StringEncoding];
NSString *verificationDataString = [NSString stringWithFormat:@"receiptData=%@", receiptDataString];
NSData *verificationData = [NSData dataWithBytes:[verificationDataString UTF8String] length:[verificationDataString length]];

NSMutableURLRequest *verificationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://update.csundm.de/inapp/verifyReceipt.php"]];
[verificationRequest setHTTPMethod:@"POST"];
[verificationRequest setHTTPBody:verificationData];

NSData *verificationResultData = [NSURLConnection sendSynchronousRequest:verificationRequest returningResponse:nil error:nil];
NSString *verificationResultString = [[NSString alloc] initWithData:verificationResultData encoding:NSUTF8StringEncoding];
NSLog(@"%@", verificationResultString);

********************************************************

// verifyReceipt.php:
$receiptData = $_POST["receiptData"];
$receiptDataEncoded = base64_encode($receiptData);
$receiptJSONString = json_encode(Array("receipt-data" => $receiptDataEncoded));

$contextData = array ( 
                      "method" => "POST",
                      "header" => "Connection: close\r\n".
                      "Content-Length: ".strlen($receiptJSONString)."\r\n",
                      "content" => $receiptJSONString);
$context = stream_context_create (array ( 'https' => $contextData ));
$result =  file_get_contents ("https://sandbox.itunes.apple.com/verifyReceipt", false, $context);

$jsonResult = json_decode($result);
if ($jsonResult -> {"status"} != 0) {
    echo "Receipt invalid: ".$result;
}
else {
    echo "Receipt valid";
}

Unfortunately I'm getting »Receipt invalid: {"status":21000}« Can anybody imagine what the problem could be? I can't even find information about this error code on the internet. I found one error description but they said that this is only for non-renewable subscriptions.

Thanks a lot, with kind regards, Julian

Try encoding it on the iphone/client side first and then send the encoded data to php/server side. The output from base64'ing on object-c looks like the output from chunk_split(base64_encode($receiptData), 65) on the PHP side, so its slightly different.

I also tried encoding it on the server side using the above code but that doesn't seem to work either, even when normalising all the new lines to use \\r and stripping any trailing white spaces before encoding it. Must be some tiny detail that I left out, anyway, just encode it on the iphone side and save yourself the hassle.

From the StoreKitGuide:

Status codes for auto-renewable subscriptions

Status Code | Description
21000 | The App Store could not read the JSON object you provided.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM