Jump to content

Payment method according to invoice amount


Recommended Posts

For sure there are more elegant ways but this hook should work.

<?php

use Illuminate\Database\Capsule\Manager as Capsule;

add_hook('ClientAreaPageViewInvoice', 1, function($vars)
{
	$Data = Capsule::select(Capsule::raw('SELECT total, paymentmethod FROM tblinvoices WHERE id = "' . $vars['invoiceid'] . '" LIMIT 1'));

	/**
	 * Use Gateway System Name as key and amount as value
	 *
	 * Invoice total 10 -> banktransfer
	 * Invoice total 15 -> banktransfer
	 * Invoice total 20 -> katamaze_bnlpositivity
	 * Invoice total 30 -> katamaze_gestpay
	 * Invoice total 40 -> katamaze_nexi
	 */
	$Gateways['banktransfer'] = 10;
	$Gateways['katamaze_bnlpositivity'] = 20;
	$Gateways['katamaze_gestpay'] = 30;
	$Gateways['katamaze_nexi'] = 40;

	foreach ($Gateways as $k => $v)
	{
		if ($Data[0]->total >= $v)
		{
			$AssignedGateway = $k;
		}
	}

	if ($AssignedGateway != $Data[0]->paymentmethod)
	{
		localAPI('UpdateInvoice', array('invoiceid' => $vars['invoiceid'], 'paymentmethod' => $AssignedGateway), $adminUsername);
		header('Location: viewinvoice.php?id=' . $vars['invoiceid']);
		die();
	}
});

Every time customer changes payment method or view the invoice, the hook triggers and set the correct payment method depending on invoice total.

Link to comment
Share on other sites

Thank you! The system uses only one currency.
However I could not make it work. I do not understand the hook, how to determine if it is greater than or equal to 20 dollars for example, to use certain payment systems and if it is less than 20 dollars other certain payment systems.
I feel my awkwardness. Could you explain me?

Thank you

Link to comment
Share on other sites

1 hour ago, atlanticadigital said:

I feel my awkwardness

I feel my awkwardness for posting that script. I coded it a lot of time ago for unknown reasons 😄 As I said earlier it's not very elegant and  @brian! is right about multi-currency.

Anyway it loops through all gateway key/value pairs to find the one with the highest limit according to invoice amount. Example:

<?php

$InvoiceTotal = 35;
$Gateways['banktransfer'] = 10;
$Gateways['bnlpositivity'] = 20;
$Gateways['gestpay'] = 30;
$Gateways['nexi'] = 40;

foreach ($Gateways as $k => $v)
{
    echo 'Use ' . strtoupper($k) . ' gateway when amount is greater than or equal to ' . $v . '<br>';
    echo 'Is ' . $InvoiceTotal . ' (invoice amount) greater than or equal to ' . $v . '?<br>';

    if ($InvoiceTotal >= $v)
    {
        echo 'Yes! Let\'s use ' . strtoupper($k);
    }
    else
    {
        echo 'Nope!';
    }

    echo '<br><br>';
}

/* Prints...

Use BANKTRANSFER gateway when amount is greater than or equal to 10
Is 35 (invoice amount) greater than or equal to 10?
Yes! Let's use BANKTRANSFER

Use BNLPOSITIVITY gateway when amount is greater than or equal to 20
Is 35 (invoice amount) greater than or equal to 20?
Yes! Let's use BNLPOSITIVITY

Use GESTPAY gateway when amount is greater than or equal to 30
Is 35 (invoice amount) greater than or equal to 30?
Yes! Let's use GESTPAY

Use NEXI gateway when amount is greater than or equal to 40
Is 35 (invoice amount) greater than or equal to 40?
Nope!

*/

Long story short GESTPAY will be used as gateway. If invoice amount changes to 40 the gateway will be set to NEXI. If it drops to 15 BANKTRANSFER and so on. As I said for sure there are better ways but now I wanna start playing League of Legends 😛

Edited by Kian
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