公司做的app需要做iap订阅支付,自己做完总结一下,希望对小伙伴们有帮助我就很欣慰了。代码写的不好 不要喷我。【推荐:php视频教程】
首先讲一下我的业务逻辑:
先上图
下面详细讲一下,作为服务端具体要做些什么,并贴上对应的代码:
第一步:
通过客户端传过来的recept(票据)进行生成订单的操作【注意这里需要验证订单是否已存在】,订单生成返回客户端相关信息;
public function pay() { $uid = $this->request->header('uid'); $receipt_data = $this->request->post('receipt'); if (!$uid || !$receipt_data) return $this->rep(400); $info = $this->getreceiptdata($receipt_data, $this->issandbox);//去苹果进行验证,防止收到的是伪造的数据 log::info(['uid'=>$uid,'receipt'=>$receipt_data,'iap_info'=>$info]); if (is_array($info) && $info['status'] == 0) {//没有错误就进行生成订单的业务逻辑的处理 } elseif (is_array($info) && $info['status'] == 21007) { $new_info = $this->getreceiptdata($receipt_data, true);//接着去苹果官网进行二次验证(沙盒) // 进行生成订单的业务逻辑的处理 } }
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(['receipt-data' => $receipt, 'password'=>'abde7d535c']); $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请求有错误 $order['status'] = 408; } else { $data = json_decode($response, true); if (isset($data['status'])) { //返回产品的信息 $order = isset($data['receipt']) ? $data['receipt'] : []; $order['status'] = $data['status']; } else { $order['status'] = 30000; } } return $order; }
第二步:
自动订阅续费功能(在支付的基础上,才能进行自动续费的功能,这里的接口不需要客户端调起,这个是苹果端自动回调的,需要在苹果官网上进行填写回调链接,如下图:)
接下来配上自动回调的代码:
/* * 自动续费订阅回调 * doc: https://developer.apple.com/documentation/appstoreservernotifications/notification_type */ public function renew(){ $resp_str = $this->request->post(); log::info(['resp_str'=>$resp_str]); if(!empty($resp_str)) {// $data = json_decode($resp_str,true);// $data = $resp_str['unified_receipt']; //有时候苹果那边会传空数据调用 // notification_type 几种状态描述 // initial_buy 初次购买订阅。latest_receipt通过在app store中验证,可以随时将您的服务器存储在服务器上以验证用户的订阅状态。 // cancel apple客户支持取消了订阅。检查cancellation date以了解订阅取消的日期和时间。 // renewal 已过期订阅的自动续订成功。检查subscription expiration date以确定下一个续订日期和时间。 // interactive_renewal 客户通过使用用app store中的app store以交互方式续订订阅。服务立即可用。 // did_change_renewal_pref 客户更改了在下次续订时生效的计划。当前的有效计划不受影响。 $notification_type = $resp_str['notification_type'];//通知类型 $password = $resp_str['password']; // 共享秘钥 if ($password == "abde7d5353") { $receipt = isset($data['latest_receipt_info']) ? $data['latest_receipt_info'] : $data['latest_expired_receipt_info']; //latest_expired_receipt_info 好像只有更改续订状态才有 //找出来最近的那一组信息 $receipt = self::arraysort($receipt, 'purchase_date', 'desc'); $original_transaction_id = $receipt['original_transaction_id']; // //原始交易id $transaction_id = $receipt['transaction_id']; // //交易的标识 $purchasedate = str_replace(' america/los_angeles','',$receipt['purchase_date_pst']); $orderinfo = order::field('uid,original_transaction_id,money,order_no,pay_time')->where(['original_transaction_id' => $original_transaction_id])->find(); $user_info = user::field('app_uid,device_id,unionid')->get($orderinfo['uid']); if ($notification_type == 'cancel') { //取消订阅,做个记录 ipalog::addlog($orderinfo['uid'], $orderinfo['order_no'], $receipt, $resp_str); } else { if ($notification_type == "interactive_renewal" || $notification_type == "renewal" || $notification_type == 'initial_buy') {// $transtime = $this->totimezone($receipt['purchase_date']); iaprenew::addrenew($orderinfo['uid'], $receipt, $data['latest_receipt'],1,$notification_type,$user_info['app_uid'],$purchasedate); ipalog::addlog($orderinfo['uid'], $orderinfo['order_no'], $receipt, $resp_str); } else { iaprenew::addrenew($orderinfo['uid'], $receipt, $data['latest_receipt'],0,$notification_type,$user_info['app_uid'],$purchasedate); ipalog::addlog($orderinfo['uid'], $orderinfo['order_no'], $receipt, $resp_str); } } } else { log::info('通知传递的密码不正确--password:'.$password); } } } private function totimezone($src, $from_tz = 'etc/gmt', $to_tz = 'asia/shanghai', $fm = 'y-m-d h:i:s') { $datetime = new \datetime($src, new \datetimezone($from_tz)); $datetime->settimezone(new \datetimezone($to_tz)); return $datetime->format($fm); } private static function arraysort($arr, $key, $type='asc') { $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[0]; // 数据返回 }
以上就是总结分享php是如何处理ios自动续费!的详细内容。
