苹果的凭证查询的数据如果是消耗性只能查询一次,如果是订阅型也会有时效性,订阅型如果是同一组会保留一个原始订单号(第一次购买该组订阅的订单号),很多坑需要自己发掘,下面是基本的代码处理
public function arraySort($arr,$key,$type='asc',$subscript=0){
$keyArr = []; // 初始化存放数组将要排序的字段值
foreach ($arr as $k=>$v){
$keyArr[$k] = $v[$key]; // 循环获取到将要排序的字段值
}
if($type == 'asc'){
asort($keyArr); // 排序方式,将一维数组进行相应排序
}else{
arsort($keyArr);
}
foreach ($keyArr as $k=>$v){
$newArray[$k] = $arr[$k]; // 循环将配置的值放入响应的下标下
}
$newArray = array_merge($newArray); // 重置下标
return $newArray[$subscript]; // 数据返回
}
$receipt = '凭证';
$password = '密码';
$jsonItem = json_encode(['receipt-data' => $receipt,'password'=>$password ]);
//$url= 'https://buy.itunes.apple.com/verifyReceipt'; //正式
$url = 'https://sandbox.itunes.apple.com/verifyReceipt'; //测试
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
$result= json_decode($response, true);
if($result['status'] == 21007)
{
$url= 'https://buy.itunes.apple.com/verifyReceipt'; //正式
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //这两行一定要加,不加会报SSL 错误
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);
$errno = curl_errno($ch);
$errmsg = curl_error($ch);
curl_close($ch);
$result= json_decode($response, true);
}
if ($result['status'] !== 0) {
//验证失败 返回app错误状态,可以记录日志
exit;
}
$receiptitem = $result['latest_receipt_info'];
$item = $this->arraySort($receiptitem, 'purchase_date', 'desc');
$orderThird = $item['transaction_id']; //本次订阅的订单号
$orderThirdFirst = $item['original_transaction_id']; //这个是原始订单号
网友评论