Jump to content

Invoice Overpayment Notification


SolaDrive
Go to solution Solved by leemahoney3,

Recommended Posts

WHMCS suggested I post here for better help. Does anyone know how to enable a notification if a clients invoice gets a double payment or over payment. Clients often are not even aware of this if they have multiple subscriptions setup. Having a notification for this would help avoid this.

Link to comment
Share on other sites

This is possible by using a WHMCS hook, but only a WHMCS expert or developer can help you provide the code.

If the paid amount is greater than the invoice amount, then use the WHMCS local API to send an email to the customer using either a simple email format or a WHMCS email template.

Link to comment
Share on other sites

I use a module (BX2, by Katamaze) that does this and, for this feature, works fine.

But even with this module, if you use Paypal there are a number of different situations that you can get payments that WHMCS does not process.

So, on a monthly basis I download the WHMCS transaction report, the PayPal transactions report, put them in a spreadsheet and cross-check all of them.

A mess? Of course, but sadly we don't live in a perfect world... 

Link to comment
Share on other sites

  • Solution

Quick hook that'll send an admin report on the daily cron run. Can be improved greatly but I don't have time.

<?php

use WHMCS\Billing\Invoice;

add_hook('DailyCronJob', 1, static function () {
    $invoices = [];
    Invoice::paid()->each(function (Invoice $invoice) use (&$invoices) {
        if ($invoice->balance >= 0) {
            return;
        }

        $invoices[$invoice->id] = $invoice->balance;
    });

    if (!$invoices) {
        return;
    }

    $body = '<p>The following invoices are currently overpaid:</p>';
    foreach ($invoices as $invoiceId => $invoiceBalance) {
        $body .= "Invoice #$invoiceId (Balance: $invoiceBalance)<br />";
    }

    $postData = [
        'customsubject' => 'Overpaid Invoice Report',
        'custommessage' => $body,
    ];
    localAPI('SendAdminEmail', $postData);
});

Results in an email such as

image.png.f7e5b0cb4afd4b702119707f785657d6.png

Link to comment
Share on other sites

8 hours ago, SolaDrive said:

This is great, thank you so much! Its exactly what I was looking for.

Great! If I've some time later I'll see if I can optimize the hook further as its currently looping through all paid invoices (WHMCS sets the balance attribute in the model rather than having it as a field in the database which makes the lookup a bit trickier)

Thanks for the coffees!

Link to comment
Share on other sites

Another day late, my bad!

Here you go, it checks for transactions in the last 24 hours and then checks their invoice for overpayments...

<?php

use Carbon\Carbon;
use WHMCS\Billing\Invoice;
use WHMCS\Billing\Payment\Transaction;

add_hook('DailyCronJob', 1, static function () {
    $invoices = [];
    Transaction::where('date', '>=', Carbon::now()->subDay())->each(function (Transaction $transaction) use (&$invoices) {
        $invoice = $transaction->invoice;

        if ($invoice->balance >= 0 || $invoice->status !== Invoice::STATUS_PAID) {
            return;
        }

        $invoices[$invoice->id] = $invoice->balance;
    });

    if (!$invoices) {
        return;
    }

    $body = '<p>The following invoices are currently overpaid:</p>';
    foreach ($invoices as $invoiceId => $invoiceBalance) {
        $body .= "Invoice #$invoiceId (Balance: $invoiceBalance)<br />";
    }

    $postData = [
        'customsubject' => 'Overpaid Invoice Report',
        'custommessage' => $body,
    ];
    localAPI('SendAdminEmail', $postData);
});

 

Edited by leemahoney3
Link to comment
Share on other sites

1 hour ago, SolaDrive said:

Just tested the new code for the first time as part of the daily cron. Its working as expected, thank you! This should really help us out a lot with duplicate payment issues. How many coffees should I buy ya?

Delighted to hear that! You've already bought me coffees so thank you, just happy that I can help 🙂 

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.

  • 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