<?php
$lid =1;
$sid =2;
define('IN_WEB', true);
include (dirname(__FILE__)."/../com.php");
$ts = time();
$uniqId = uniqid();
$iosData = file_get_contents('php://input');
oo::logs()->debug5(time()."----".$iosData,'ios-vip.log','logs/'.date('Ymd').'/');
$iosData = json_decode($iosData,true);
oo::logs()->debug5(["time"=>$ts,"info"=>$iosData,"type"=>"join","uniqId"=>$uniqId,"sid"=>2],'vipNew.log','logs/'.date('Ymd').'/');
if(!empty($iosData)) {
    $notification_type = $iosData['notification_type'];
    $transaction_id = $iosData['latest_receipt_info']['original_transaction_id'];
    $expires_date   = $iosData['latest_receipt_info']['expires_date'];
    if(empty($transaction_id)){
        $tempIosData= $iosData['unified_receipt']['latest_receipt_info'];
        foreach ($tempIosData as $row){
            $transaction_id = $row['original_transaction_id'];
            $expires_date   = $row['expires_date_ms'];
            if(!empty($transaction_id) && !empty($expires_date)){
                break;
            }
        }
    }
    $latest_receipt = $iosData['latest_receipt'];
    if(empty($latest_receipt)){
        $latest_receipt = $iosData['unified_receipt']['latest_receipt'];
    }
    if($notification_type == "INITIAL_BUY") {
        $time = time();
        $tb = otable::subscription($sid);
        $ret = oo::commonOprDb('common')->query("INSERT INTO {$tb}(transaction_id, latest_receipt,`time`) VALUES('{$transaction_id}', '{$latest_receipt}',{$time})", false);
    } else if ($notification_type == "CANCEL") {
//        $tb = otable::summarylist();
//        $ret = oo::commonOprDb('common')->getOne("SELECT * FROM {$tb} WHERE sl_platform_serial_num='{$transaction_id}' LIMIT 1", MYSQLI_ASSOC);
//        if(!empty($ret)){
//            oo::commonOprModel('member')->getUserInfo($ret['sl_uid'], NULL, TRUE, ['svip'=>0]);
//            oo::commonOprModel('Workerman')->push($ret['sl_uid'],ocmd::$send['Vip_Rec'],['vipInfo'=>oo::commonOprModel('newvip')->getVipInfo($ret['sl_uid'])]);
//        }
        oo::commonOprRedis('common')->zDelete(okeys::VipMonitor(), $transaction_id);
    } else if ($notification_type == "RENEWAL" || $notification_type == "INTERACTIVE_RENEWAL" || $notification_type == "DID_RENEW" || $notification_type == "DID_CHANGE_RENEWAL_STATUS") {
        $tb = otable::summarylist();
        $ret = oo::commonOprDb('common')->getOne("SELECT * FROM {$tb} WHERE sl_platform_serial_num='{$transaction_id}' LIMIT 1", MYSQLI_ASSOC);
        if(!empty($ret)){
            /**
             * vip 续订记录
             */
            oo::commonOprRedis('User')->incr(okeys::VipRenewTimes($ret['sl_uid'])) && oo::commonOprRedis('User')->expire(okeys::VipRenewTimes($ret['sl_uid']),15*24*60*60);
            oo::commonOprModel('member')->getUserInfo($ret['sl_uid'], NULL, TRUE,['svip'=>1,'svip_expire'=>ceil($expires_date/1000)]);
            oo::commonOprModel('Workerman')->push($ret['sl_uid'],ocmd::$send['Vip_Rec'],['vipInfo'=>oo::commonOprModel('newvip')->getVipInfo($ret['sl_uid'])]);
            oo::async()->add('oo::commonOprModel("notice")->sendNoticeMsg',[$uid,"buyVip"]);//异步跑马灯消息
        }
    }
}

//if(!IS_DEBUF){
//    file_get_contents("http://testapi.superant.cc/api/ios.php", false, stream_context_create(['http'=>['method'=>'POST','header'=>'Content-type:application/x-www-form-urlencoded','content'=>json_encode($iosData),'timeout'=> 60]]));
//}

die(oo::response(1));
?>