In-app-purchase, backend on php and iTunes response problem
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) . "\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;
}
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?
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
精彩评论