应用内购买,后端处理php和iTunes响应问题

时间:2011-02-28 17:34:25

标签: php iphone in-app-purchase

以下是验证收据的示例PHP代码:

public static function getReceiptData($receipt, $isSandbox = false) {
    if ($isSandbox)
        $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';
    else
        $endpoint = 'https://buy.itunes.apple.com/verifyReceipt';
    $postData = "{\"receipt-data\":\"". base64_encode($receipt) ."\"}";
    $ch = curl_init($endpoint);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    $response = curl_exec($ch);
    $errno = curl_errno($ch);
    $errmsg = curl_error($ch);
    curl_close($ch);
    if ($errno != 0) {
        throw new sfException('connection_to_itunes_error', $errno);
    }
    // FIXME
    mail('my_mail', 'Invalid Itunes Receipt', var_export($data, true) . "\n" . $postData);
    $data = json_decode($response);
    if (!is_object($data)) {
        throw new sfException('invalid_response_data');

    if (!isset($data->status) || $data->status != 0) 
        throw new sfException('invalid_receipt');
    return $data;
}

这么简单。

但我总是得到iTunes的回应: 我尝试了很多$ postData字符串的组合,但它没有帮助。

iTunes有例外: java.lang.ClassCastException: java.lang.String cannot be cast to com.webobjects.foundation.NSDictionary

{"receipt-data":"MTAwMDAwMDAwMTU1MjM1Ng=="} java.lang.NullPointerException

"{"receipt-data":"MTAwMDAwMDAwMTU1MjM1Ng=="}"

这么奇怪的错误 字符'<html><head><title>Error</title></head><body>Your request produced an error. <BR>[newNullResponse]</body></html>'

"{\"receipt-data\":\"MTAwMDAwMDAwMTU1MjM1Ng==\"}"

你能为recipt验证提供工作的json字符串吗?

1 个答案:

答案 0 :(得分:1)

你的后端看起来是正确的。请检查iPhone代码以将原始交易收据十六进制发送为字符串:

NSString *receiptStr = [[NSString alloc] initWithData:transaction.transactionReceipt encoding:NSUTF8StringEncoding];

顺便说一下,您的脚本也应该在没有base64_encode的情况下处理收据字符串