(iphone)inAppPurchase verifyReceipt使用MKStoreKit

时间:2011-05-23 07:26:59

标签: iphone in-app-purchase receipt

我正在使用MKStoreKit进行应用购买测试。

我得到回复状态21002,并想知道为什么。
我是否需要设置证书或与苹果服务器通信的内容?

以下是MKStoreKit使用的php代码

<?php

$devmode = TRUE; // change this to FALSE after testing in sandbox                                                                                                                                                                                                             

$receiptdata = $_POST['receiptdata'];
$udid = $_POST['udid'];

if($devmode)
 {
     $appleURL = "https://sandbox.itunes.apple.com/verifyReceipt";
 }   
 else
 {
     $appleURL = "https://buy.itunes.apple.com/verifyReceipt";
 }

$receipt = json_encode(array("receipt-data" => $receiptdata));
$response_json = do_post_request($appleURL, $receipt);
$response = json_decode($response_json);

file_put_contents('php://stderr', print_r($response->{'status'}, true));
file_put_contents('php://stderr', print_r($udid, true));

if($response->{'status'} == 0)
 {
     file_put_contents('php://stderr', print_r("yes", true));
     error_log('udid: %s', $udid);
     error_log('quantity: %d', $response->{'receipt'}->quantity);
     echo ('YES');
 }   
 else
 {
     echo ('NO');
 }

function do_post_request($url, $data, $optional_headers = null)
{
    $params = array('http' => array(
            'method' => 'POST',
            'content' => $data
                                    ));
    if ($optional_headers !== null) {
        $params['http']['header'] = $optional_headers;
    }
    $ctx = stream_context_create($params);
    $fp = @fopen($url, 'rb', false, $ctx);
    if (!$fp) {
        throw new Exception("Problem with $url, $php_errormsg");
    }
    $response = @stream_get_contents($fp);
    if ($response === false) {
        throw new Exception("Problem reading data from $url, $php_errormsg");
    }
    return $response;
}

?>

2 个答案:

答案 0 :(得分:1)

请检查Verify Purchase

答案 1 :(得分:0)

MKStore Kit有一个将receipttdata发送到服务器的错误

你应该base64编码receiptData而不是asciiStringEncoding。

使用以下链接的代码到base64,我获得状态0。 Verify receipt for in App purchase