以下是实际测试通过的苹果支付服务端代码,给大家提供些许思路帮助。再加强的安全处理根据自己的业务增加即可,这儿只列出了可用的骨架。
public function applepayAction(){
$receipt = isset($this->param['receipt'])?$this->param['receipt']:'';
$orderid = isset($this->param['transactionId'])?$this->param['transactionId']:'';
$username = isset($this->param['username'])?$this->param['username']:'';
$password = isset($this->param['password'])?$this->param['password']:''
if(!$receipt||!$orderid){
$this->ajaxerror('参数缺失');
}
$rechargeModel = new \Model\RechargeModel;
$membersModel = M('Members');
$member = $membersModel->where(array('uid'=>$uid))->find();
//创建订单,使用苹果给的订单号
$record = $rechargeModel->where(array('waterno'=>$orderid))->find();
$orderno = date('YmdHis',time()).rand(1000,9999);
if(empty($record)){
$data = array(
'uid' => $uid,
'orderno' => $orderno,
'waterno' => $orderid,
'ordertime' => time(),
'fromaddr' => $_SERVER['HTTP_HOST'],
'paytype' => 10,
);
$rechargeModel->add($data);
}
$isSandbox = false;//沙箱是测试环境,正式环境改为false
$info = $this->getReceiptData($receipt, $isSandbox);//去苹果进行二次验证,防止收到的是伪造的数据
if(is_array($info) && $info['status'] == 0){//没有错误就进行业务逻辑的处理,订单设置成已支付,给用户加钱
$answer['status'] = 1;
$answer['msg'] = '支付成功';
if($record['status'] != '1'){
$data['amount'] = $this->product[$info['product_id']];//这个价格列表是你提交给苹果的,苹果不会直接给你具体多少钱
$rechargeModel->where(array('waterno'=>$orderid))->save($data);
//实际充值
}else{
$answer['msg'] = '该订单已支付';
}
}elseif(is_array($info) && $info['status'] == 21007){
$infonew = $this->getReceiptData($receipt, true);//接着去苹果官网进行二次验证(沙盒)
if(is_array($infonew) && $infonew['status'] == 0){
$answer['status'] = 1;
$answer['msg'] = '支付成功';
}else{
$answer['status'] = -100;
$answer['msg'] = '正式不通过改测沙盒依旧不通过,错误码:'.$infonew['status'];
}
}else{
$answer['status'] = -100;
$answer['msg'] = '正式不通过且非21007不测沙盒,错误码:'.$info['status'];
}
$this->ajaxReturn($answer);
}
private $product = [
'cb_18' => 18,
'cb_68' => 68,
'cb_118' => 118,
'cb_198' => 198,
'cb_208' => 208,
'cb_298' => 298,
'cb_308' => 308,
'cb_518' => 518,
'cb_698' => 698,
'cb_1048' => 1048,
'cb_1998' => 1998,
'cb_2298' => 2298,
'cb_5898' => 5898,
'cb_6498' => 6498
];
private function getReceiptData($receipt, $isSandbox = false){
if ($isSandbox) {
$endpoint = 'https://sandbox.itunes.apple.com/verifyReceipt';//沙箱地址
} else {
$endpoint = 'https://buy.itunes.apple.com/verifyReceipt';//真实运营地址
}
$postData = json_encode(
array('receipt-data' => $receipt)
);
$ch = curl_init($endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($ch);
$errno = curl_errno($ch);
curl_close($ch);
if ($errno != 0) {//curl请求有错误
$this->ajaxerror('请求超时,请稍后重试');
}else{
$data = json_decode($response, true);
if(isset($data['status'])){
//返回产品的信息
$order = $data['receipt']['in_app'][0];
$order['status']=$data['status'];
}else{
$order['status']=30000;
}
return $order;
}
}
再给个返回示例
/*返回数据参照样例
array (
'status' => 0,
'environment' => 'Sandbox',
'receipt' =>
array (
'receipt_type' => 'ProductionSandbox',
'adam_id' => 0,
'app_item_id' => 0,
'bundle_id' => 'com.abcde.www',
'application_version' => '0.0.9',
'download_id' => 0,
'version_external_identifier' => 0,
'receipt_creation_date' => '2016-07-13 18:22:19 Etc/GMT',
'receipt_creation_date_ms' => '1468434139000',
'receipt_creation_date_pst' => '2016-07-13 11:22:19 America/Los_Angeles',
'request_date' => '2016-07-13 18:22:22 Etc/GMT',
'request_date_ms' => '1468434142143',
'request_date_pst' => '2016-07-13 11:22:22 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' =>
array (
0 =>
array (
'quantity' => '1',
'product_id' => 'price_1',//去看$this->product里对应的价格,就是你的充值额
'transaction_id' => '1000000223463280',
'original_transaction_id' => '1000000223463280',
'purchase_date' => '2016-07-13 18:22:19 Etc/GMT',
'purchase_date_ms' => '1468434139000',
'purchase_date_pst' => '2016-07-13 11:22:19 America/Los_Angeles',
'original_purchase_date' => '2016-07-13 18:22:19 Etc/GMT',
'original_purchase_date_ms' => '1468434139000',
'original_purchase_date_pst' => '2016-07-13 11:22:19 America/Los_Angeles',
'is_trial_period' => 'false',
),
),
),
)
*/