WHMCS get invoice PDF

1.5k Views Asked by At

I'm trying to import from WHMCS to a c# application all my WHMCS invoices using API methods. There is a problem when I try to download PDF Documents. There is no API method to do that and I can't call dl.php page without login. I can't login as admin with WHMCS API Methods, and neither with client because (obviously) I don't know their password and I have only their MD5 password.

There is any solutions?

Thanks

1

There are 1 best solutions below

4
wesamly On BEST ANSWER

According to this page, this is how you can download the invoice without need to login.

Create a php file, let's say, gen_invoice_pdf.php, and place it inside the root whmcs directory.

Add this code to the file:

<?php
include __DIR__ . '/init.php';
include __DIR__ . '/includes/invoicefunctions.php';

use WHMCS\Auth;

$user = filter_var($_GET['us'], FILTER_SANITIZE_STRING);
$pass = filter_var($_GET['pw'], FILTER_SANITIZE_STRING);
$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);
$authAdmin = new Auth;

if ($authAdmin->getInfobyUsername($user) && $authAdmin->comparePassword($pass)) {
    $isValid = true;
} else {
    $isValid = false;
}

if (!$isValid) {
    die('Access Denied');
}


if ($id > 0) {
    $pdfdata = pdfInvoice($id);

    header("Content-type:application/pdf");
    header("Content-Disposition:attachment;filename=invoice_$id.pdf");

    echo $pdfdata;  

}

To download invoice id: 5, visit the url:

http://whmcs-example.com/gen_invoice_pdf.php?id=5&us=myuser&pw=mypass

Note: this will be accessible to any one knows the url, you can set the page accept connections from specific IP address, for example.

Update:

Added WHMCS admin authentication, works only for WHMCS >= 5.3.9, check: Admin Password Hashing.