在App Purchase Server验证中

时间:2012-06-13 11:24:55

标签: php ios in-app-purchase verification storekit

我目前正在尝试使用服务器端验证(在PHP中)使用AppStore验证我的In App Purchase Receipt。

这是我的代码:

// 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";
}

不幸的是我收到了»收据无效:{“状态”:21000}«有谁可以想象问题可能是什么?我甚至无法在互联网上找到有关此错误代码的信息。我找到了一个错误描述,但他们说这只适用于不可更新的订阅。

非常感谢,亲切的问候,朱利安

2 个答案:

答案 0 :(得分:1)

首先尝试在iphone /客户端编码,然后将编码数据发送到php / server端。来自object-c的base64的输出看起来像PHP侧的chunk_split(base64_encode($receiptData), 65)的输出,所以它略有不同。

我也尝试使用上面的代码在服务器端对其进行编码,但这似乎也不起作用,即使在对所有新行进行规范化以使用\r并在编码之前剥离任何尾随空格时也是如此。必须是我遗漏的一些微小细节,无论如何,只需在iphone侧编码就可以省去麻烦。

答案 1 :(得分:0)

来自StoreKitGuide:

自动续订订阅的状态代码

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