false,'message'=>'POST required.']);exit;} try{ include_once './Home_include.php'; require_once __DIR__.'/PA_SHOP_RECOVERY_REQUEST.php';require_once __DIR__.'/PA_SHOP_SUBSCRIPTION_REQUEST.php'; $post=$_POST; $admin=PAShopRecoveryRequest::admin($PAAPP_CONFIG_DB_LINK,$_COOKIE['PHPSESSID']??null,$post['DUMMYSESSID']??null,$_SERVER['REMOTE_ADDR']??null); if(session_status()!==PHP_SESSION_ACTIVE||session_id()!==($_COOKIE['PHPSESSID']??null))throw new RuntimeException('Session confirmation unavailable.'); $action=$post['action']??null; if(!is_string($action)||!in_array($action,['customers','list','details','details_transaction','prepare_cancel','apply_cancel','check_sim','prepare_method','apply_method','export_history'],true))throw new RuntimeException('Invalid request.'); $proof=null;if($action==='apply_cancel'){$proof=PAShopSubscriptionRequest::consume($_SESSION,$admin,$post);if(!session_write_close())throw new RuntimeException('Confirmation could not be saved.');} foreach(['WP_USE_THEMES'=>false,'WP_ADMIN'=>true,'DOING_AJAX'=>true,'DISABLE_WP_CRON'=>true,'WP_HTTP_BLOCK_EXTERNAL'=>true,'WP_MEMORY_LIMIT'=>'512M'] as $k=>$v)if(!defined($k))define($k,$v); if(!function_exists('wcs_get_subscription'))(function(){ $post=$_POST; $get=$_GET;$method=$_SERVER['REQUEST_METHOD'];$uri=$_SERVER['REQUEST_URI']??'/';$timezone=date_default_timezone_get(); $_GET=[];$_POST=[];$_SERVER['REQUEST_METHOD']='GET';$_SERVER['REQUEST_URI']='/';ob_start(); try{require_once '/opt/SA/DEV/WEB/SHOP/wp-load.php';}finally{ob_end_clean();$_GET=$get;$_POST=$post;$_SERVER['REQUEST_METHOD']=$method;$_SERVER['REQUEST_URI']=$uri;date_default_timezone_set($timezone);} })(); require_once __DIR__.'/PA_SHOP_DEVICE_SUBSCRIPTIONS.php'; $load=static function($id){$s=wcs_get_subscription($id);if($s)$s->get_data_store()->read($s);return $s;}; $worker=new PAShopDeviceSubscriptions($PAAPP_CONFIG_DB_LINK,$load,wp_timezone(),$PASYSCONF); if(in_array($action,['prepare_method','apply_method'],true)){ require_once '/opt/SA/DEV/WEB/SHOP/wp-content/plugins/pa-device-transaction/PA_SHOP_ACCOUNT_RENEWAL.php'; $id=$post['transaction_id']??null;$mode=$post['renewal_mode']??null; if(!is_scalar($id)||!preg_match('/^[1-9][0-9]{0,17}$/D',(string)$id)||!is_string($mode))throw new RuntimeException('Select a current device transaction and renewal method.'); $control=PAShopAccountRenewal::worker($PAAPP_CONFIG_DB_LINK,$PASYSCONF);$actor=['kind'=>'admin','id'=>$admin]; if($action==='prepare_method'){ $s=$control->inspect($actor,(int)$id,$mode);$token=bin2hex(random_bytes(32));$request=bin2hex(random_bytes(16)); $_SESSION['pa_renewal_method']=['admin'=>$admin,'id'=>(int)$id,'mode'=>$mode,'fingerprint'=>$s['fingerprint'],'token'=>hash('sha256',$token),'request'=>$request,'expires'=>time()+300]; $result=['token'=>$token,'request_id'=>$request,'transaction_id'=>(int)$id,'renewal_mode'=>$mode,'device_code'=>$s['device']['SimHash'],'device_name'=>$s['device']['NameTag'],'service_end_utc'=>$s['end_utc'],'message'=>$s['message']]; if($s['sub'])$result['recurring_charge']=(string)$s['sub']->get_total().' '.$s['sub']->get_currency().' / '.$s['sub']->get_billing_period(); }else{ $p=$_SESSION['pa_renewal_method']??[];unset($_SESSION['pa_renewal_method']); if(($p['admin']??null)!==$admin||($p['id']??null)!==(int)$id||($p['mode']??null)!==$mode||($p['expires']??0)run($actor,(int)$id,$mode,$p['fingerprint'],$p['request'],$post['reason'],true); PAShopAccountRenewal::notice($PAAPP_CONFIG_DB_LINK,(int)$id,$p['request'],$mode,$result); } }elseif($action==='export_history'){ $id=$post['transaction_id']??null;if(!is_scalar($id)||!preg_match('/^[1-9][0-9]{0,17}$/D',(string)$id))throw new RuntimeException('Select a transaction.'); $result=$worker->export($admin,(int)$id); }elseif(in_array($action,['customers','list'],true)){ $search=$post['search']??'';$offset=$post['offset']??'0'; if(!is_string($search)||!is_scalar($offset)||!preg_match('/^[0-9]{1,7}$/D',(string)$offset))throw new RuntimeException('Invalid search or page.'); if($action==='customers'){ $filter=$post['filter']??'all';if(!is_string($filter))throw new RuntimeException('Invalid filter.'); $limit=$post['limit']??'20';$sort=$post['sort']??'customer';$direction=$post['direction']??'asc'; if(!is_scalar($limit)||!preg_match('/^[0-9]{1,3}$/D',(string)$limit)||!is_string($sort)||!is_string($direction))throw new RuntimeException('Invalid table options.'); $result=$worker->customers($admin,trim($search),$filter,(int)$offset,(int)$limit,$sort,$direction); }else{ $client=$post['client_id']??null;if(!is_scalar($client)||!preg_match('/^[1-9][0-9]{0,17}$/D',(string)$client))throw new RuntimeException('Select a customer.'); $result=$worker->list($admin,'',(int)$offset,(int)$client); } }elseif($action==='apply_cancel')$result=$worker->cancel($admin,$proof); elseif($action==='details_transaction'){ $id=$post['transaction_id']??null;if(!is_scalar($id)||!preg_match('/^[1-9][0-9]{0,17}$/D',(string)$id))throw new RuntimeException('Invalid transaction.'); $result=$worker->transactionDetails($admin,(int)$id); } else{ $id=$post['device_id']??null;if(!is_scalar($id)||!preg_match('/^[1-9][0-9]{0,17}$/D',(string)$id))throw new RuntimeException('Invalid device.'); if($action==='details')$result=$worker->details($admin,(int)$id); elseif($action==='check_sim')$result=$worker->carrierStatus($admin,(int)$id); else{$sub=$post['subscription_id']??null;if(!is_scalar($sub)||!preg_match('/^[1-9][0-9]{0,17}$/D',(string)$sub))throw new RuntimeException('Invalid subscription.');$result=PAShopSubscriptionRequest::issue($_SESSION,$admin,$worker->cancellation($admin,(int)$id,(int)$sub));} } echo json_encode(['status'=>true,'data'=>$result],JSON_THROW_ON_ERROR); }catch(Throwable $e){ $message=$e instanceof RuntimeException&&!($e instanceof PDOException)?$e->getMessage():'Subscription request could not be verified. Refresh and review before another action.'; echo json_encode(['status'=>false,'message'=>$message]); }