$admin,'order'=>$order,'fingerprint'=>$review['fingerprint'], 'request'=>bin2hex(random_bytes(16)),'token'=>bin2hex(random_bytes(32)), 'expires'=>$now+300]; $session['shop_paid_replacement']=$saved; return ['token'=>$saved['token'],'request_id'=>$saved['request'], 'replacement_order_id'=>$order,'client_id'=>$snapshot['client_id']??null, 'device_id'=>$snapshot['device_id']??null, 'subscription_id'=>$snapshot['subscription_id']??null, 'paid_through_utc'=>$snapshot['paid_through_utc']??null]; } public static function consume(array &$session, int $admin, array $post, ?int $now=null): array { $now=$now??time();$saved=$session['shop_paid_replacement']??null; if (!is_array($saved)||$saved['admin']!==$admin||$saved['expires']<=$now || !is_string($post['token']??null)||!hash_equals($saved['token'],$post['token']) || ($post['request_id']??null)!==$saved['request'] || (string)($post['replacement_order_id']??'')!==(string)$saved['order'] || ($post['confirmed']??null)!=='yes') { throw new RuntimeException('Paid replacement confirmation expired or invalid.'); } $reason=$post['reason']??null; if (!is_string($reason)||strlen(trim($reason))<8||strlen($reason)>500 || preg_match('/[\x00-\x1f\x7f]/',$reason)) { throw new RuntimeException('A reason of 8 to 500 characters is required.'); } unset($session['shop_paid_replacement']); return ['admin'=>$admin,'order'=>$saved['order'], 'fingerprint'=>$saved['fingerprint'],'request'=>$saved['request'], 'reason'=>trim($reason)]; } }