Jump to content

How to Update Credit Balance after changing the Invoice Items Data

Recommended Posts


How to update the Credit Balance for a client after changing the Invoice and InvoiceItems?

I'm using Paddle for processing my payments.

When sending the invoice data to Paddle I send it without taxes. 

Then Paddle handles the taxes and at the Subscription Payment Success webhook I get if there are any taxes.

Then I change the Invoice Items and the Invoice.

((((Don't know if UpdateClientProduct autorecalc is not working in v8.2 or it is used for something else))))

At the end I also update the Invoice with tax amount and the new total with taxes.

I'm new with WHMCS (2 months now) and PHP (3 months).

Now, I can't find the way how to update or trigger something to update the Credit Balance on the Summary for Client. It is still the old value that was before adding taxes to Invoices.

Is there an API to update the Clients amounts, including Credit Balance and do I need to update something else after my changes to the Invoices?


Here the part of the code that is doing this:


$withoutTax = $fields['balance_fee'] + $fields['balance_earnings'];
$taxAmount = $fields['balance_tax'];
$withTax = $fields['balance_gross'];

if ( $withoutTax <> 0 ) {
    $taxPercent = round(($taxAmount / $withoutTax) * 100, 2);
   } else {
    $taxPercent = 0;

if ( $taxPercent <> 0 ) {
    $itemTaxed = 1;
} else {
    $itemTaxed = 0;

$command = 'GetInvoice';
$values = array(
    'invoiceid' => $invoiceId,
//$adminUsername = 'ADMIN_USERNAME'; // Optional for WHMCS 7.2 and later

// Call the localAPI function
$invoice = localAPI($command, $values, $adminUsername);

logModuleCall('paddle_checkout_gateway_callback', 'UpdateInvoice_localAPI_invoice', $invoice, "");

if ($invoice['result'] == 'success') {
    foreach ($invoice['items']['item'] as $item)
        //error_log($vars['invoiceid'].' item '.$item['id'].' has description of "'.$item['description'].'"');
        $text = $item['description'];

            $updatedInfo = array(
                'invoiceid' => $invoiceId,
                'taxrate' => $taxPercent,
                'itemdescription' => array($item['id'] => $text),
                'itemamount' => array($item['id'] => $item['amount']),
                'itemtaxed' => array($item['id'] => $itemTaxed)
            $updatedInvoice = localAPI('UpdateInvoice', $updatedInfo, $adminUsername);
            logModuleCall('paddle_checkout_gateway_callback', 'UpdateInvoice', $updatedInfo, $updatedInvoice);

} else {
    //echo "An Error Occurred: " . $results['result'];

            // let's now trigger the recalculate of each hostingId from this updated invoiceid
            //    I think that this is not working in v8.2 or the UpdateClientProduct autorecalc is used for something else
            $invoiceitems = Capsule::table('tblinvoiceitems')
            ->where('invoiceid', $invoiceId)
            ->where('type', 'Hosting')->get();
            foreach( $invoiceitems as $invoiceitem ){
                //$invoiceitem->relid this is the id from the tblHosting table
                $resultOfAPI = localAPI('UpdateClientProduct', array('serviceid' => $invoiceitem->relid, 'autorecalc' => true), $adminUsername);

                logModuleCall('paddle_checkout_gateway_callback', 'recalculateProductItems', $invoiceitem->relid, $resultOfAPI);


            // 20210927 maybe a bug in v8.2
            //    not updating the tax and the total amount of the invoice with UpdateClientProduct 'autorecalc'
            //    manualy updating the table tblinvoices

            try {
                ->where('id', $invoiceId)
                    "tax" => $taxAmount,
                    "total" => $withTax
            } catch (\Exception $e) {
                logModuleCall('paddle_checkout_gateway_callback', 'recalculateProductItems_ERROR', "tax " .$taxAmount . " total " .$withtax ,$e);
                echo "Error in updating tblInvoices. {$e->getMessage()}";


Edited by Davor

Share this post

Link to post
Share on other sites

autorecalc does not know about or care about taxes, that is the invoice's job.  It only takes the product's billing cycle and configurable options in to account when recalculating the cycle's total.

Credit would be giving money back to the client and that does not seem to correct unless they were overcharged. 

Just to be clear, you want to add the taxes to the invoice, correct?  So say the invoice was for 10 and the tax was 10%.  Now the invoice total should be 11 .  Adding the tax rate as you are doing should be correct way .  However, you would then need to charge Paddle again for the difference.

Share this post

Link to post
Share on other sites

Thanks @steven99 for your reply.

It is correct that I want to add the taxes to the invoice, and this is after Paddle gets the payment from the customer and I'm updating the invoice with the correct numbers, if Paddle collected taxes I add taxes to the invoice.

I have managed to find the solution. All of this is happening in the Gateway Callback after I get the webhook from Paddle that the transaction has been sucesful.

The ting was that I was adding the payment (addInvoicePayment) before I was updating the invoices. Now that I have added the addInvoicePayment after this, everything is ok and the system updates the invoice items and also triggers the summary client data update.

Thanks for taking the time and for responding @steven99



Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Similar Content

    • By Balram
      i am trying to add bank transfer details but having some problem.
      I am unable to add space after text below attaching screenshot for understanding.
      My issue-  https://prnt.sc/1x5pccy
      i found one website using same theme without this problem - https://prnt.sc/1x5p7zo
      here is how i have added details to whmcs admin area- https://prnt.sc/1x5pjhz
    • By Fany
      Hi friends,
      I tried to find an invoice template that would meet EU regulations. I didn't find it, maybe I'm looking wrong.
      According to EU rules, the type and amount of VAT must be specified for each item.
      I give an example of what an invoice should look like in the attachment.
      Can anyone solve this problem? Or do you know of a teplat that would meet these rules?
      Black texts: are firmly in the template
      Green texts: data generated from WHMCS
      Because I use the Czech language, the Czech names of the items are listed in the template
      I gave an example of generating products on an invoice.
      I am willing to evaluate the help financially. I need to change both the HTML and PDF invoice.

    • By mohd
      the problem is when the cust buys 2 item and only of them has promo but the invoice somehow calculate both it together in the same promo.
      this is not promotion setting problem. Can anyone help out? 
    • By Nelson Neoh
      Hi there.
      After a long search in Google and this community, I didn't really able to find a clue on how to handle this, if there is a product which upgrade will incurs a full charge, rather than default prorated charge in WHMCS.  Do you guys has any hints or workaround?
      Here's the full list of issues I am experiencing now, hope will get any help from you here.  Thanks in advance.
      1. What's the proper procedures to take, if there is a product/service renewal or upgrade will incurs a full charge rather than prorated according to remaining days?
      2. When a product/service renewed (recurring auto CC charged or invoice paid), which hook or function in Addons/Provisions can use to trigger follow up actions?  Like post an API request to principle for such renewal action.
      3. When a Renewal button being click, either on Admin/Client Panel, it will triggers invoice and charge directly on client deposited funds if sufficient fund available.  Which localAPI should I call?
      4. I noticed if the Product/Service being created with recurring terms, for example monthly or annually, it will automatically triggered an invoice by WHMCS cron.  Wish to know if there is sufficient fund in client account, will the cron direct credited its balance and send a paid invoice?  Which hook should I use if the invoice send is a paid invoice?
      Best Regards,
    • By Cartitarul
      Hi, i live in Romania and in our country is mandatory to have the total of the invoice in the local currency also for example if the total is 5 Euro, to display under the currency in RON also.
      Plz help me configure this invoice so i can start my buissnes.
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated