// 确认请求成功 if ($errno != 0) { throw new Exception($errmsg, $errno); } // 解析应答数据 $data = json_decode($response); // 确认应答数据是有效的JSON字符串 if (!is_object($data)) { throw new Exception('Invalid response data'); } // 验证返回成功 if (!isset($data->status) || $data->status != 0) { throw new Exception('Invalid receipt'); } // 建立应答数组,并且返回 return array('quantity' => $data->receipt->quantity, 'product_id' => $data->receipt->product_id, 'transaction_id' => $data->receipt->transaction_id, 'purchase_date' => $data->receipt->purchase_date, 'app_item_id' => $data->receipt->app_item_id, 'bid' => $data->receipt->bid, 'bvrs' => $data->receipt->bvrs); } // 从请求体中取出电子凭证数据 $receipt = $_POST['receipt']; // 从请求体中取出是否使用沙箱标识 $isSandbox = (bool) $_POST['sandbox']; // 验证凭证 try { $info = getReceiptData($receipt, $isSandbox); $res = array_merge($info, array('ResultCode' => 0)); //验证结果以JSON编码方式,返回给客户端 echo json_encode($res); } catch (Exception $ex) { // 验证失败 echo '{"ResultCode":-2}'; }
throw new Exception('Invalid response data'); } // ensure the expected data is present if (!isset($data->status) || $data->status != 0) { throw new Exception('Invalid receipt'); } // build the response array with the returned data return array('quantity' => $data->receipt->quantity, 'product_id' => $data->receipt->product_id, 'transaction_id' => $data->receipt->transaction_id, 'purchase_date' => $data->receipt->purchase_date, 'app_item_id' => $data->receipt->app_item_id, 'bid' => $data->receipt->bid, 'bvrs' => $data->receipt->bvrs); } // fetch the receipt data and sandbox indicator from the post data $httpRequest = new InAppHttpRequest(); $receipt = $httpRequest->body(); // verify the receipt try { // receipt is valid, now do something with $recipt $recipt = getReceiptData($receipt, $isSandbox); // Get all recipt information $quantity = $recipt['quantity']; $product_id = $recipt['product_id']; $transaction_id = $recipt['transaction_id']; $purchase_date = $recipt['purchase_date']; $app_item_id = $recipt['app_item_id']; $bid = $recipt['bid']; $bvrs = $recipt['bvrs']; $fp = fopen('log.txt', 'a'); fwrite($fp, 'Log trans' . date('l jS \\of F Y h:i:s A') . "\r\n"); foreach ($recipt as $key => $value) { fwrite($fp, $key . '=>' . $value . "\r\n"); } fwrite($fp, 'udid=>' . $udid . "\r\n"); fclose($fp);
<?php function getReceiptData($receipt) { $endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt'; $context = ['http' => ['method' => 'POST', 'content' => $receipt]]; $context = stream_context_create($context); $result = file_get_contents($endpoint, false, $context); echo $result; } foreach ($_POST as $key => $value) { $newcontent .= $key . ' ' . $value; } $new = trim($newcontent); $new = trim($newcontent); $new = str_replace('_', '+', $new); $new = str_replace(' =', '==', $new); if (substr_count($new, '=') == 0) { if (strpos('=', $new) === false) { $new .= '='; } } $new = '{"receipt-data":"' . $new . '","password":"******"}'; $info = getReceiptData($new);