我正在将应用从iOS6转换为iOS7。在我使用已弃用的transactionReceipt
方法之前,我现在尝试使用推荐的方法来检索收据,然后在base 64中进行编码:
NSData *working = [NSData dataWithContentsOfURL:[[NSBundle mainBundle] appStoreReceiptURL]];
// Tried 64 or 76 chars/line and LF or CR line endings
NSString *receipt = [working base64EncodedStringWithOptions:kNilOptions];
以上是代码中唯一的变化。以下是我验证它的方法,没有变化:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue,
^{
NSMutableString *url = [NSMutableString string];
[url appendFormat:@"%@", WEB_SERVICE];
[url appendFormat:@"receipt=%@", receipt];
NSStringEncoding encoding;
NSError *error = [NSError new];
NSURL *URL = [NSURL URLWithString:url];
NSString *json = [NSString stringWithContentsOfURL:URL usedEncoding:&encoding error:&error];
// check json and error
// ... code omitted
}
在服务器端,这是我用来验证收据的PHP代码,除了针对任何错误尝试沙箱之外没有其他任何变化:
// Encode as JSON
$json = json_encode(array('receipt-data' => $receipt));
// Try production first, if it doesn't work, then try the sandbox
$working = postJSONToURL('https://buy.itunes.apple.com/verifyReceipt', $json, false);
error_log('production - '.print_r($working, true));
if (@$working['status'] !== 0) // === 21007)
$working = postJSONToURL('https://sandbox.itunes.apple.com/verifyReceipt', $json, true);
error_log('sandbox - '.print_r($working, true));
这是错误日志输出:
production - Array\n(\n [status] => 21002\n [exception] => java.lang.IllegalArgumentException\n)\n
sandbox - Array\n(\n [status] => 21002\n [exception] => java.lang.IllegalArgumentException\n)\n
看起来我在Apple上抛出了各种各样的例外情况!
唯一的区别是如何检索和编码收据。有没有人遇到过这个问题并修好了?
感谢阅读。
/ YR
根据要求,PostJSONToURL的代码:
function postJSONToURL($url, $json, $disableSSLVerify = false)
{
$resource = curl_init($url);
curl_setopt($resource, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($resource, CURLOPT_POSTFIELDS, $json);
curl_setopt($resource, CURLOPT_RETURNTRANSFER, true);
curl_setopt($resource, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: '.strlen($json)));
curl_setopt($resource, CURLOPT_HEADER, 0);
if ($disableSSLVerify)
{
curl_setopt($resource, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($resource, CURLOPT_SSL_VERIFYPEER, 0);
}
//curl_setopt($resource, CURLOPT_VERBOSE, true);
//curl_setopt($resource, CURLOPT_STDERR, $fp = fopen('/tmp/curl_output'.rand(1000, 9999).'.txt', 'w'));
$contents = json_decode(curl_exec($resource), true);
if (!$contents)
$contents = array();
curl_close($resource);
//fclose($fp);
return $contents;
}
经过一些实验后的新细节已确定将现有数据作为基数64编码发送可能会侵犯某些内部限制。如果它超过某个内部限制,则数据甚至不被发送,它在设备上本地失败,低于它,它被发送。列是:数据格式,编码数据的大小,是否到达服务器:
raw receipt data 5K N/A
base64 no options 6.66K yes
base64 76 chars/line 6.75K no
base64 64 chars/line 6.77K no
hex coded 10K no
请注意,成功发送和不发送之间的差异小于100个字节。
答案 0 :(得分:9)
我遇到了这个问题并且无处不在,包括在Apple的开发论坛上。 Apple会给出一些股票回复,就是这样。我认为这是苹果方面的一个漏洞。设备本地验证将起作用,因此请尝试转换为该设备。如果您绝对必须使用服务器端验证,那么现在只有transactionReceipt
似乎可以正常工作。
该功能刚刚被弃用,未被禁止,所以我只是使用它并希望Apple批准该应用程序。事实上,这就是我刚刚做的,手指交叉,等待批准。
您可以通过将代码包括在内来关闭Xcode中的警告:
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// code using transactionReceipt
#pragma clang diagnostic pop
答案 1 :(得分:7)
我在iOS7收据上取得了成功,使用:
从应用包中获取NSURL *receiptURL = [[NSBundle mainBundle] performSelector:@selector(appStoreReceiptURL)];
receipt = [NSData dataWithContentsOfURL:receiptURL];
从我的服务器,从Apple的服务器到iOS7样式收据的响应与之前的收据样式的响应大不相同。以下是验证收据的示例:
{"status":0,
"environment":"Sandbox",
"receipt":
{"receipt_type":"ProductionSandbox",
"adam_id":0,
"bundle_id":"<snip>",
"application_version":"1.0",
"download_id":0,
"request_date":"2013-11-12 01:43:06 Etc\/GMT",
"request_date_ms":"1384220586352",
"request_date_pst":"2013-11-11 17:43:06 America\/Los_Angeles",
"in_app":[
{"quantity":"1",
"product_id":"<snip>",
"transaction_id":"1000000092978110",
"original_transaction_id":"1000000092978110",
"purchase_date":"2013-11-12 01:36:49 Etc\/GMT",
"purchase_date_ms":"1384220209000",
"purchase_date_pst":"2013-11-11 17:36:49 America\/Los_Angeles",
"original_purchase_date":"2013-11-12 01:36:49 Etc\/GMT",
"original_purchase_date_ms":"1384220209000",
"original_purchase_date_pst":"2013-11-11 17:36:49 America\/Los_Angeles",
"is_trial_period":"false"}
]
}
}
漂亮印刷的缩进对我有利,严格来说不是苹果给出的。
以下是我的客户方的胆量:
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters addEntriesFromDictionary:[credentials dictionary]];
// receipt is an object of my own making, but base64String just returns an
// NSString representation of the receipt data.
parameters[PURCHASE_RECEIPT] = [receipt base64String];
NSURLRequest *request =
[[AFHTTPRequestSerializer serializer]
requestWithMethod:@"POST"
URLString:urlString
parameters:parameters];
AFHTTPRequestOperation *operation =
[[AFHTTPRequestOperation alloc]
initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];
<snip>
[operation start];
以下是我在服务器端使用的内容,其中URL是生产或沙箱验证服务器:
// Some code from http://stackoverflow.com/questions/5647461/how-do-i-send-a-post-request-with-php
private static function validateReceipt($receiptData, $URL) {
// Connect to Apple server and validate.
$data = json_encode(array("receipt-data" => $receiptData));
// use key 'http' even if you send the request to https://...
// This: 'content' => http_build_query($data),
// seems to generate an error (21002)
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded",
'method' => 'POST',
'content' => $data
),
);
$context = stream_context_create($options);
$result = file_get_contents($URL, false, $context);
//Utils::writeToLog("done validateReceipt: " . $result);
// See http://php.net/manual/en/function.file-get-contents.php
// for the use of === comparison.
if ($result === FALSE) {
return NULL;
} else {
// Decode the result as an associative array.
return json_decode($result, true);
}
}
我在使用iOS6和iOS7样式收据的代码方面取得了成功。
答案 2 :(得分:3)
我有同样的症状:从我自己的服务器验证io7样式收据时出现错误21002(java.lang.IllegalArgumentException)。
原来有两个问题:
我的收据数据很糟糕。不知何故,在将数据传递到我的服务器时,它最终在base64编码的收据数据中有一堆“\ r \ n”字符。 (我用一些搜索和替换代码删除了它们。)
如果您正在使用自动续订订阅,那么您必须在JSON有效负载中传递两个参数来验证Receipt:“receipt-data”,还有“password”,它应该是来自itunes connect的共享密钥。
一旦我解决了这两件事,我的verifyReceipt http请求按预期工作。
答案 3 :(得分:0)