ZATCA icon indicating copy to clipboard operation
ZATCA copied to clipboard

PublicKey not found in Certificate when trying to Sign the XML Invoice

Open EducoreAPK opened this issue 1 year ago • 0 comments

I started my process with Generating a CSRRequest

$CSRRequest = CSRRequest::make()
    ->setUID($OrganizationIdentifier)
    ->setSerialNumber($solutionName, $version,$serialNumber)
    ->setCommonName($commonName)
    ->setCountryName('SA')
    ->setOrganizationName($organizationName)
    ->setOrganizationalUnitName($organizationalUnitName)
    ->setRegisteredAddress($registeredAddress)
    ->setInvoiceType(true, true) //invoice types , the default is true, true
    ->setCurrentZatcaEnv($currentEnv) //support all modes ['sandbox','simulation','core']
    ->setBusinessCategory($businessCategory);

$CSR = GenerateCSR::fromRequest($CSRRequest)->initialize()->generate();

   openssl_pkey_export($CSR->getPrivateKey(), $privateKey);

    $csr_content = $CSR->getCsrContent();

Then get the Private Key and CSRContent and pass it to API to get the Secret

$curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => 'https://gw-fatoora.zatca.gov.sa/e-invoicing/developer-portal/compliance',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => '',
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 0,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => 'POST',
        CURLOPT_POSTFIELDS =>json_encode(array('csr'=>base64_encode($csr_content))),
        CURLOPT_HTTPHEADER => array(
        'OTP: 123345',
        'Accept-Version: V2',
        'Content-Type: application/json',
        ),
    ));
    $response = curl_exec($curl);
    curl_close($curl);
    // Compliance CSID (Certificate)
    $rData = json_decode($response,true);

Till this point it is working fine

Now i want to generate the certificate and sign the XML E Invoice when i generate the certificate it goes on without any error but when i am trying to sign the XML Invoice it is throwing this error

$invoice = (new InvoiceSign($xmlInvoice, $certificate))->sign(); image

And IF i use This

$tags = UXML::fromString($xmlInvoice)->toTagsArray($certificate, null, null);
$QRCodeAsBase64 = GenerateQrCode::fromArray($tags)->toBase64();

image

If i vardump the $certificate i get following result looks like its all empty image

Can you point out any thing that i am doing wrong here

Thanks

@thaifanisalla and @SallaDev

EducoreAPK avatar Oct 15 '24 12:10 EducoreAPK