false,'message'=>'POST required.']);exit;} try{ include_once './Home_include.php'; require_once __DIR__.'/PA_SHOP_RECOVERY_REQUEST.php'; require_once __DIR__.'/PA_SHOP_BILLING_REQUEST.php'; require_once __DIR__.'/PA_SHOP_ADMIN_GATE.php'; $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.'); $id=$_POST['subscription_id']??'';$operation=$_POST['operation']??'';$action=$_POST['action']??'';$mode=$_POST['service_mode']??''; if(!is_string($id)||!preg_match('/^[1-9][0-9]{0,17}$/D',$id)||!in_array($operation,['pause','resume'],true)||!in_array($action,['prepare','apply'],true))throw new RuntimeException('Invalid billing request.'); $gate=PAShopAdminGate::subscription($PAAPP_CONFIG_DB_LINK,$PASYSCONF,'BILLING_CONTROL',$admin,(int)$id); if(($gate['PausePolicy']??'')!=='billing_only_keep_paid_dates')throw new RuntimeException('Billing controls disabled.'); if($action==='prepare'&&($operation==='pause'?!in_array($mode,['keep','suspend'],true):$mode!=='auto'))throw new RuntimeException('Invalid tracker service choice.'); $command=null; if($action==='apply'){$command=PAShopBillingRequest::consume($_SESSION,$admin,$_POST);if(!session_write_close())throw new RuntimeException('Confirmation could not be saved.');} $db=$PAAPP_CONFIG_DB_LINK;$timezone=new DateTimeZone(date_default_timezone_get()); 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($timezone){ $post=$_POST;$get=$_GET;$method=$_SERVER['REQUEST_METHOD'];$uri=$_SERVER['REQUEST_URI']??'/'; $_POST=[];$_GET=[];$_SERVER['REQUEST_METHOD']='GET';$_SERVER['REQUEST_URI']='/';ob_start(); try{require_once '/opt/SA/DEV/WEB/SHOP/wp-load.php';}finally{ob_end_clean();$_POST=$post;$_GET=$get;$_SERVER['REQUEST_METHOD']=$method;$_SERVER['REQUEST_URI']=$uri;date_default_timezone_set($timezone->getName());} })($timezone); if(!function_exists('wcs_get_subscription'))throw new RuntimeException('Subscriptions unavailable.'); require_once '/opt/SA/DEV/WEB/SHOP/wp-content/plugins/pa-device-transaction/PA_SHOP_BILLING_CONTROL.php'; $carrier=null; if(($gate['AllowServiceSuspension']??false)===true||$operation==='resume'||($command['service_mode']??'')==='suspend'){ try{ require_once '/opt/SA/DEV/WEB/SHOP/wp-content/plugins/pa-device-transaction/PA_SHOP_CARRIER.php'; $carrierConfig=$PASYSCONF['CARRIER']['ONCE']??[]; $carrier=new PAShopCarrier($carrierConfig['URLAUTH']??'',$carrierConfig['APIKEY']??''); }catch(Throwable $ignored){} } $load=function($id){$s=wcs_get_subscription($id);if($s)$s->get_data_store()->read($s);return $s;}; $worker=new PAShopBillingControl($db,$load,wp_timezone(),$gate,$carrier); if($action==='prepare')$result=PAShopBillingRequest::issue($_SESSION,$admin,$operation,$worker->inspect($admin,(int)$id,$operation,$mode)); else $result=$worker->run($command['admin'],$command['id'],$command['action'],$command['fingerprint'],$command['request'],$command['reason'],$command['service_mode'],$command['alert_acknowledged']); if($action==='apply' && empty($result['replayed']) && in_array($result['state']??'',['succeeded','review_required'],true)){ try{ $statement=$db->prepare('SELECT ClientDeviceTransactionID,State,ServiceMode FROM PA_ShopBillingControl WHERE RequestID=?'); $statement->execute([$command['request']]);$row=$statement->fetch(PDO::FETCH_ASSOC); if(!$row||$row['State']!==$result['state'])throw new RuntimeException('Billing notice source unavailable.'); require_once '/opt/SA/DEV/WEB/SHOP/wp-content/plugins/pa-device-transaction/PA_SHOP_NOTIFICATIONS.php'; $source='billing-'.$command['request'];$txID=(int)$row['ClientDeviceTransactionID']; if($result['state']==='review_required'){ PAShopNotifications::admin($db,$source,$txID,'Billing '.$command['action']); }else{ $type=$command['action']==='resume'?'PA_SHOP_BILLING_RESUMED': ($row['ServiceMode']==='suspend'?'PA_SHOP_BILLING_PAUSED_SUSPEND':'PA_SHOP_BILLING_PAUSED_KEEP'); PAShopNotifications::customer($db,$type,$source,$txID); } }catch(Throwable $noticeError){error_log('PA shop billing notice needs review | SubscriptionID='.(int)$id);} } echo json_encode(['status'=>true,'data'=>$result]); }catch(Throwable $e){echo json_encode(['status'=>false,'message'=>'Billing change was not confirmed. Refresh and review payment, service dates and permissions. Do not submit another payment.']);}