iOS7 - 未在沙箱验证的收据 - 错误21002(java.lang.IllegalArgumentException)

Yim*_*ong 12 php sandbox objective-c in-app-purchase ios7

我正在将应用程序从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];
Run Code Online (Sandbox Code Playgroud)

以上是代码中唯一的变化.以下是我验证它的方法,没有变化:

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
    }
Run Code Online (Sandbox Code Playgroud)

在服务器端,这是我用来验证收据的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));
Run Code Online (Sandbox Code Playgroud)

这是错误日志输出:

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
Run Code Online (Sandbox Code Playgroud)

看起来我在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;
}
Run Code Online (Sandbox Code Playgroud)

经过一些实验后的新细节已经确定将现有数据作为基数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
Run Code Online (Sandbox Code Playgroud)

请注意,成功发送和不发送之间的差异小于100个字节.

小智 8

我遇到了这个问题并且无处不在,包括在Apple的开发论坛上.Apple会给出一些股票回复,就是这样.我认为这是苹果方面的一个漏洞.设备本地验证将起作用,因此请尝试转换为该设备.如果您绝对必须使用服务器端验证,那么transactionReceipt现在似乎只能工作.

该功能刚刚被弃用,未被禁止,所以我只是使用它并希望Apple批准该应用程序.事实上,这就是我刚刚做的,手指交叉,等待批准.

你可以通过包围你的代码来关闭Xcode中的警告,如下所示:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
// code using transactionReceipt
#pragma clang diagnostic pop
Run Code Online (Sandbox Code Playgroud)

  • 我目前在使用iOS7样式收据进行服务器端收据验证方面取得了成功.见下文. (3认同)

Chr*_*nce 7

我在iOS7收据上取得了成功,从应用程序包中获取,使用:

NSURL *receiptURL = [[NSBundle mainBundle] performSelector:@selector(appStoreReceiptURL)];
receipt = [NSData dataWithContentsOfURL:receiptURL];
Run Code Online (Sandbox Code Playgroud)

从我的服务器,从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"}
                  ]
    }
}
Run Code Online (Sandbox Code Playgroud)

漂亮的印记缩进对我有利,严格来说不是苹果给出的.

这是我的客户端的胆量:

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];
Run Code Online (Sandbox Code Playgroud)

以下是我在服务器端使用的内容,其中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);
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用iOS6和iOS7样式收据的代码取得了成功.