0

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

Bruno
  • 181
  • 16

1 Answers1

1

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.

wesamly
  • 1,484
  • 1
  • 16
  • 23
  • It seems working well. But I would like to put some protections in this code, because it can be accessible from anyone. It is possible to authenticate my admin user and with authentication success get the pdfdata? So the url will be something like http://whmcs-example.com/gen_invoice_pdf.php?us=[username];pw=[md5password];id=[idinvoice] – Bruno May 24 '16 at 07:54
  • You mean WHMCS admin user? or another system user? – wesamly May 24 '16 at 09:43
  • Exacly, WHMCS Admin user. Similar to API of WHMCS when you create a external call. – Bruno May 24 '16 at 09:53
  • @Bruno updated to match your request, hope it helps. – wesamly May 26 '16 at 14:50