Jump to content

How to Update Credit Balance after changing the Invoice Items Data


Davor

Recommended Posts

Hello,

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();
            //->first();
            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 {
                Capsule::table('tblinvoices')
                ->where('id', $invoiceId)
                ->update([
                    "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
Link to comment
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.

Link to comment
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

Davor

 

Link to comment
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.

Guest
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.

×
×
  • 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