dtihe8614 2011-02-28 17:34
浏览 47
已采纳

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

Here is a sample PHP code for verifying the receipt:

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) . "
" . $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;
}

So simple.

But I always get exception in response from iTunes: I tried a lot of combinations of $postData string, but it doesn't help.

There are exceptions from the iTunes: java.lang.ClassCastException: java.lang.String cannot be cast to com.webobjects.foundation.NSDictionary for {"receipt-data":"MTAwMDAwMDAwMTU1MjM1Ng=="}

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

And so strange error '<html><head><title>Error</title></head><body>Your request produced an error. <BR>[newNullResponse]</body></html>' for string "{\"receipt-data\":\"MTAwMDAwMDAwMTU1MjM1Ng==\"}"

Could you provide the working json string for the recipt validation?

  • 写回答

1条回答 默认 最新

  • duanshang9426 2011-03-06 11:00
    关注

    Your back-end looks like correct. Please, check iPhone code to send raw transaction receipt hex as string:

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

    BTW, your script should also work without base64_encode for receipt string

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看