Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EducoreAPK opened this issue Oct 15, 2024 · 1 comment

Comments

@EducoreAPK
Copy link

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

@thaifanisalla
Copy link
Contributor

thaifanisalla commented Nov 23, 2024

Hi @EducoreAPK

How you collect the certificate content.

the certificate must be created by passing the CSID , and sign it by private key
like this

use phpseclib3\File\X509;

 $certificate  = (new X509());
 
 $certificate->loadX509( 'certificate text');
 
        

Than use it in signing invoice
here a full example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants