其他分享
首页 > 其他分享> > 苹果自动续费

苹果自动续费

作者:互联网

 

 

 

 

下面详细讲一下,作为服务端具体要做些什么,并贴上对应的代码:
第一步:通过客户端传过来的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]; // 数据返回
    }

 

————————————————
原文链接:https://blog.csdn.net/weixin_45306360/article/details/114892813

 

标签:info,uid,data,receipt,自动,苹果,str,type,续费
来源: https://www.cnblogs.com/ccdr/p/16288369.html