ios移动端购买虚拟产品时 需要后端进行二次验证,下面是整个验证的思路及处理过程:
1:ios会申请一个product_id,平台的不同产品对应不同的product_id
数据库商品表新增 product_id,ios_price(由于苹果要扣除部分收益,所以一般ios端的价格与安卓不同)
uid apple_product_id,receipt_data,transaction_id,bundle_id
1:通过product_id验证商品是否存在
2:向苹果服务器请求 获取返回参数
3:验证返回的bundle_id与请求的是否一致 或 in_array
4:验证是否为越狱手机 即 in_app为空
5:判断transaction_id是否已存在order表中 表示已验证过
6:校验订单号transaction_id
7:校验商品ID apple_product_id
8:检验是否过期 订单时间过长
```
**//在进行验证的控制器里复制如下代码**
protected $response = ['result' => true, 'code' => 10000, 'msg' => ''];
/**
* 来源数组
* @var array
*/
private $targetArray = ['a_sysj', 'i_sysj', 'a_lpds', 'i_lpds','a_jjds','i_jjds'];
private $testMember =[];//定好你们内部测试,人员,若人员比较多的化就做成后台管理的从数据库中取出
/**
* @name 检验凭证并分发后续业务的逻辑
* 请求参数 uid apple_product_id,receipt_data,transaction_id,bundle_id
*/
public function credentialsCheckAction()
{
set_time_limit(0);
IS_POST ||$this->jsonError();//判断是否是POST $parameters =I('post.');//这里最好的相应的参数进行验证,并且使用验签校验,这里我就省略了这部分,还有下面部分最好也放在你封装或者放在你相应的模型内
$receive_array = json_decode($_POST['json'], true);
$parameters =$receive_array;//这里最好的相应的参数进行验证,并且使用验签校验,这里我就省略了这部分,还有下面部分最好也放在你封装或者放在你相应的模型内
//通过product_id验证商品是否存在
$orderInfo = M('goods') -> where(array('apple_product_id' => $parameters['apple_product_id'])) -> find();
if(empty($orderInfo['id'])){
$this->response['result'] = false;
$this->response['status'] = 6;
$this->response['code'] = 20012;
$this->response['msg'] = '商品不存在';
$this->jsonError($this->response);//返回数据给前端
}
$orderInfo['uid'] = $parameters['uid'];
$AppleAipController = new AppleAipController();
$sandboxStatus = 1;//0正式,1开发
//发送请求
$checkData = $AppleAipController->send($parameters['receipt_data'],$sandboxStatus);
//测试环境和正式环境均请求一遍 都不好使再返回错误
if($checkData['status']==0){
$this -> check($checkData,$parameters,$orderInfo);
}elseif($checkData['status']==21007){//21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
$sandboxStatus = 1;//0正式,1开发
$checkData = $AppleAipController->send($parameters['receipt_data'],$sandboxStatus);
if($checkData['status']==0){
$this -> check($checkData,$parameters,$orderInfo);
}else{
$status = $checkData['status'];
$this->response['status'] = $status;
$this->response['result'] = false;
$this->response['msg'] = '验证失败';
$this->jsonError($this->response);//返回数据给前端
}
}else{
$status = $checkData['status'];
$this->response['status'] = $status;
$this->response['result'] = false;
$this->response['msg'] = '验证失败';
$this->jsonError($this->response);//返回数据给前端
}
}
/**
* 整合异步发放
* @param $order
* @param $time
* @return int
*/
public function notifyAdd($order,$arr)
{
//这里写你相应的数据库订单操作
}
**//该部分为封装好的请求api**
formalityCurl;
}else{
$url =$this ->sandboxCurl;
}
$POSTFIELDS = array("receipt-data" => $receipt_data);
$POSTFIELDS = json_encode($POSTFIELDS);
//简单的curl
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);//一定要post形式传参
curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
$result = curl_exec($ch);
curl_close($ch);
$data = json_decode($result,true);
$order = $data['receipt'];
$order['status'] = $data['status'];//status为苹果服务器返回的状态 0为成功
return $order;
}
}
/**
* 服务器二次验证代码
* 21000 App Store不能读取你提供的JSON对象
* 21002 receipt-data域的数据有问题
* 21003 receipt无法通过验证
* 21004 提供的shared secret不匹配你账号中的shared secret
* 21005 receipt服务器当前不可用
* 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
* 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
* 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
*/
/*返回数据参照样例
"check": {
"receipt_type": "ProductionSandbox",
"adam_id": 0,
"app_item_id": 0,
"bundle_id": "保密",
"application_version": "6",
"download_id": 0,
"version_external_identifier": 0,
"receipt_creation_date": "2019-01-18 07:45:44 Etc/GMT",
"receipt_creation_date_ms": "1547797544000",
"receipt_creation_date_pst": "2019-01-17 23:45:44 America/Los_Angeles",
"request_date": "2019-01-21 03:07:28 Etc/GMT",
"request_date_ms": "1548040048993",
"request_date_pst": "2019-01-20 19:07:28 America/Los_Angeles",
"original_purchase_date": "2013-08-01 07:00:00 Etc/GMT",
"original_purchase_date_ms": "1375340400000",
"original_purchase_date_pst": "2013-08-01 00:00:00 America/Los_Angeles",
"original_application_version": "1.0",
"in_app": [
{
"quantity": "1",
"product_id": "保密",
"transaction_id": "1000000495556247",
"original_transaction_id": "1000000495556247",
"purchase_date": "2019-01-18 06:55:58 Etc/GMT",
"purchase_date_ms": "1547794558000",
"purchase_date_pst": "2019-01-17 22:55:58 America/Los_Angeles",
"original_purchase_date": "2019-01-18 06:55:58 Etc/GMT",
"original_purchase_date_ms": "1547794558000",
"original_purchase_date_pst": "2019-01-17 22:55:58 America/Los_Angeles",
"is_trial_period": "false"
},
{
"quantity": "1",
"product_id": "保密",
"transaction_id": "1000000495559445",
"original_transaction_id": "1000000495559445",
"purchase_date": "2019-01-18 07:06:12 Etc/GMT",
"purchase_date_ms": "1547795172000",
"purchase_date_pst": "2019-01-17 23:06:12 America/Los_Angeles",
"original_purchase_date": "2019-01-18 07:06:12 Etc/GMT",
"original_purchase_date_ms": "1547795172000",
"original_purchase_date_pst": "2019-01-17 23:06:12 America/Los_Angeles",
"is_trial_period": "false"
},
],
"status": 0
}
*/
```
到这里,苹果内购二次验证就写完啦。当初刚得知要写验证的时候一脸懵,苹果内购是啥?验证是啥?验证啥?我咋验证?一搜文档,全英文!!没办法,只能百度各猿友分享的步骤,最后缕清大概步骤,历经1天成功购买。当然 过程中也遇到了不少坑,写验证之前一定要确认好移动端是怎么传参给你的,哪些参数经过转码,哪些需要你再转回来,还有请求参数的结构和苹果返回的数据结构,都要确认好,不然都是坑。
验证的时候最容易遇到的问题就是返回码status=21002,此时应该检验服务端获取的receipt_data与移动端传给你的是否一致
加油,同学们,希望这篇文章对你能有帮助。
下面是相关链接
https://www.jianshu.com/p/bd44860a8f85
https://www.jianshu.com/p/5cf686e92924