Nie jestem w stanie wysłać to systemu testowego zakodowanego tokena.
program napisany w typescript, nestjs cały czas mam zwrot error 415
może źle koduje już sam nie wiem, proszę o pomoc
kod kodowania
encrypt(publicKeyStr: string, token: string, timestamp: string): string {
const publicKey = pki.publicKeyFromPem(publicKeyStr);
const text = `${token}|${timestamp}`;
const encrypted = publicKey.encrypt(text, 'RSAES-PKCS1-V1_5', { md: md.sha256.create() });
return Buffer.from(encrypted).toString('base64');
}
kod wysyłki
async initSessionTokenRequest(dto:{nip: string }): Promise<any> {
let nip ='1111111111';
const response_data = await this.authorisationChallenge(nip);
console.log(response_data);
const challengeTimeIso = response_data['timestamp'];
const wynikChallenge = response_data['challenge'];
// const dz = await this.ini({nip: nip});
// const challengeTimeIso = dz.timestamp;
// const wynikChallenge = dz.challenge;
//
console.log('wynik challenge: ', wynikChallenge);
await new Promise((resolve) => setTimeout(resolve, 1000));
const challengeTime = this.isoToMilliseconds(challengeTimeIso);
const roundedChallengeTime = this.roundToNearestThousand(challengeTime);
console.log('challenge_time: ', roundedChallengeTime);
const combinedData = `${this.token_prawdziwy}|${roundedChallengeTime}`;
console.log(`Polaczony token i czas (Token|ChallengeTime): ${combinedData}`);
// const czytK = await this.czytajklucz();
//let key = czytK.klucz;
const result = this.encrypt(this.PUBLIC_KEY, this.token_prawdziwy, roundedChallengeTime.toString());
// const result = this.encryptWithRSA(combinedData,key);
// const result =this.rsaEncryptWithPublic(combinedData,this.PUBLIC_KEY);
console.log(`Token i czas z szyfrowania: ${result}`);
await new Promise((resolve) => setTimeout(resolve, 1000));
const xmlTemplate =
'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' +
'<ns3:InitSessionTokenRequest xmlns="http://ksef.mf.gov.pl/schema/gtw/svc/online/types/2021/10/01/0001" xmlns:ns2="http://ksef.mf.gov.pl/schema/gtw/svc/types/2021/10/01/0001" xmlns:ns3="http://ksef.mf.gov.pl/schema/gtw/svc/online/auth/request/2021/10/01/0001">'+
'<ns3:Context>'+
'<Challenge>'+wynikChallenge+'</Challenge>'+
'<Identifier xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:SubjectIdentifierByCompanyType">'+
'<ns2:Identifier>'+nip+'</ns2:Identifier>'+
'</Identifier>'+
'<DocumentType>'+
'<ns2:Service>KSeF</ns2:Service>'+
'<ns2:FormCode>'+
'<ns2:SystemCode>FA (2)</ns2:SystemCode>'+
'<ns2:SchemaVersion>1-0E</ns2:SchemaVersion>'+
'<ns2:TargetNamespace>http://crd.gov.pl/wzor/2023/06/29/12648/</ns2:TargetNamespace>'+
'<ns2:Value>FA</ns2:Value>'+
'</ns2:FormCode>'+
'</DocumentType>'+
'<Token>'+result+'</Token>'+
'</ns3:Context>'+
'</ns3:InitSessionTokenRequest>';
const formattedXml = xmlTemplate;
const initTokenEndpoint = `${this.BASE_URL}/online/Session/InitToken`;
const response = await this.httpService.post(initTokenEndpoint, formattedXml, {
}).toPromise();
console.log(response.status);
console.log(response.data);
}