function recoveryReference(receipt,operation,profile){ const saved=nativeObject(receipt,'receipt'); const id=typeof operation==='string'&&operation.startsWith('operation:')?operation.slice(10):operation; if(!['phone','messaging','complete'].includes(profile)||saved.profile!==profile||saved.operationId!==id||!/^\+[1-9][0-9]{6,14}$/.test(saved.e164||'')||!/^[a-f0-9]{8}-[a-f0-9]{4}-[1-8][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i.test(id||''))throw Error('Use the saved original operation and matching profile receipt; never buy a replacement.'); return {operationId:id,url:'https://quietpass.com/v1/automation/highlevel/'+id,receipt:JSON.stringify(saved)}; } function nativeObject(value,name){ let parsed=value; if(typeof value==='string'){ try{parsed=JSON.parse(value);}catch(error){ // Native workflow interpolation HTML-encodes the JSON string. Decode // exactly one layer only when the opening JSON key is visibly encoded. if(!/^\s*\{(?:"|"|")/i.test(value))throw error; const entities={'quot':'"','amp':'&','apos':"'",'lt':'<','gt':'>'}; const decoded=value.replace(/&(#x[0-9a-f]+|#[0-9]+|quot|amp|apos|lt|gt);/gi,(entity,key)=>{ if(key[0]!=='#')return entities[key.toLowerCase()]??entity; const point=key[1].toLowerCase()==='x'?parseInt(key.slice(2),16):parseInt(key.slice(1),10); return point>0&&point<=0x10ffff?String.fromCodePoint(point):entity; }); parsed=JSON.parse(decoded); } } if(!parsed||typeof parsed!=='object'||Array.isArray(parsed))throw Error(name+' must be a JSON object.'); return parsed; } var receipt=nativeObject(inputData.receipt,'receipt'); return {...recoveryReference(receipt,inputData.originalOperationId,receipt.profile),profile:receipt.profile};