Jump to content

Payment Gateways disallow based on invoice total


Recommended Posts

Has anyone ever implemented any hook or template change to create some conditional logic based on payment gatway?

We have one Payment Gateway that only has to appear when Invoice total is greater than X. If that's not the case, the payment gateway shouldn't even appear at the dropdown.

Is that possible?

Thanks!

Link to comment
Share on other sites

you could modify the hook I posted in the thread below...

in that instance, if total is zero, it removes all gateways apart from those specified in the hook... you could change that zero value and use multiple conditional statements to determine what gateways are shown at specific total values.

Link to comment
Share on other sites

@brian!, thank you, that will probably work, will test it today.

But I guess that still leaves one problem: the same restriction has to work when an invoice is being viewed.

Technically that won't forbid the client of opening an invoice and change the payment method to the one that it shouldn't be possible.

Consider that allowing clients to change payment gateway is essential, unfortunately I can't remove that possibility.

Thanks!

Link to comment
Share on other sites

you could use a viewinvoice hooks similar to one I posted in the previous thread...

<?php

# ViewInvoice Gateway Hook
# Written by brian!

function hook_view_invoice_gateway($vars) {

	if ($vars['status'] == 'Unpaid' && $vars['total']->toNumeric() <= 50) {
		$gatewaydropdown = $vars['gatewaydropdown'];
		$asiapay = '<option value="asiapay">AsiaPay</option>';
		$gd2 = str_replace($asiapay, '', $gatewaydropdown) ;
		return array("gatewaydropdown" => $gd2);
	}
}
add_hook('ClientAreaPageViewInvoice', 1, 'hook_view_invoice_gateway');
?>

now that should work fine if your site is only using one currency, e.g any unpaid invoices less than $50.01 will not have AsiaPay as an option, but it's no good if you have a multi-currency site... e.g the above would trigger at $50.01,  £50.01, €50.01 or even ฿50.01 - all of which are varying and unequal amounts.

<?php

# ViewInvoice Gateway Hook
# Written by brian!

function hook_view_invoice_gateway($vars) {
	
	$client = $vars['clientsdetails'];
	$currency = $client['currency'];
	if ($currency == '1') {
		$gatewaylimit = 50;
	}
	elseif ($currency == '2') {
		$gatewaylimit = 75;
	}
	else {
		$gatewaylimit = 0;
	}
	if ($vars['status'] == 'Unpaid' && $vars['total']->toNumeric() <= $gatewaylimit) {
		$gatewaydropdown = $vars['gatewaydropdown'];
		$asiapay = '<option value="asiapay">AsiaPay</option>';
		$gd2 = str_replace($asiapay, '', $gatewaydropdown) ;
		return array("gatewaydropdown" => $gd2);
	}
}
add_hook('ClientAreaPageViewInvoice', 1, 'hook_view_invoice_gateway');
?>

so in the above hook, if we imagine currency #1 is USD and #2 is GBP, the limit for clients using USD is $50.01; the limit for those using GBP is £75.01 and so on.

I suppose if you have the cron updating the exchange rates, you could just set a limit in your default currency and then get the hook to calculate that limit in the other currencies, but that would require querying the db etc.

Link to comment
Share on other sites

Thank you! you are the best! I´ll test this tonight

For the other currencies, I wouldn't bother, because this payment method that I have to limit is available only to my country (Brazil), so if I can detect what currency it is (not hard), I can just hide that gateway anyway.

Link to comment
Share on other sites

16 hours ago, anderbytes said:

For the other currencies, I wouldn't bother, because this payment method that I have to limit is available only to my country (Brazil), so if I can detect what currency it is (not hard), I can just hide that gateway anyway.

you could detect the clients registered country from the $clientsdetails array (the same one that tells you the currency), so you could check that they are in Brazil and/or using that Currency.

Link to comment
Share on other sites

That would be effective for invoice page, but not for cart, because when it's a new client, at the checkout page I don't know yet the client details (he/she is filling in exactly that screen).

So I had to check $['language'] and when the client uses our language to browse, I have to assume that he is local and he can pay in our local gateways that can't appear to people outside our country.

Anyway, everything worked perfectly! I can't thank you enough!  :-)

Link to comment
Share on other sites

  • 2 years later...

How would you modify this to include 2 gateway options? @brian! I'm more than happy to pay for your time. I know you contribute a LOT around here and it all takes time.

It's not as simple as this, is it?

<?php

# ViewInvoice Gateway Hook
# Written by brian!

function hook_view_invoice_gateway($vars) {
	
	$client = $vars['clientsdetails'];
	$currency = $client['currency'];
	if ($currency == '1') {
		$gatewaylimit = 200;
	}
	elseif ($currency == '2') {
		$gatewaylimit = 75;
	}
	else {
		$gatewaylimit = 0;
	}
	if ($vars['status'] == 'Unpaid' && $vars['total']->toNumeric() <= $gatewaylimit) {
		$gatewaydropdown = $vars['gatewaydropdown'];
		$authorize = '<option value="authorize">Credit / Debit Card</option>';
                $authorizeecheck = '<option value="authorizeecheck">ACH / Direct Debit</option>';
		$gd2 = str_replace($authorize, $authorizeecheck, '', $gatewaydropdown) ;
		return array("gatewaydropdown" => $gd2);
	}
}
add_hook('ClientAreaPageViewInvoice', 1, 'hook_view_invoice_gateway');
?>

 

Link to comment
Share on other sites

11 minutes ago, evolve hosting said:

It's not as simple as this, is it?

nearly - I don't like the look of that str_replace line - it would either have to be two separate replace lines, or you use an array, for the string variables...

$gd2 = str_replace(array($authorize, $authorizeecheck), '', $gatewaydropdown);
Link to comment
Share on other sites

10 minutes ago, evolve hosting said:

Would the customers default payment method matter?

yes - this hook wouldn't change it... it only affects the values in the dropdown menu.

19 minutes ago, evolve hosting said:

Right now, their default payment method is one that I do not want to offer once this hook executes.

then i'd be tempted to run a database update to change the gateway assigned to the invoice....

I wrote a hook for the thread below which might be more suitable for your needs - I don't think I posted in publicly, so i'll send it via PM...

there's also a link in there to Kian's version - i'm sure at least one of them will do the job you're looking for. 🙂

Link to comment
Share on other sites

39 minutes ago, brian! said:

yes - this hook wouldn't change it... it only affects the values in the dropdown menu.

When I used the hook, I was hoping to see 1 of the 3 available gateways not offered. All 3 were options in the dropdown. Maybe it was a browser cache issue so I'll check that.

I'm trying to force the customer to only pay by credit card or ACH (not paypal) when the invoice is over $200.

Link to comment
Share on other sites

13 minutes ago, evolve hosting said:

When I used the hook, I was hoping to see 1 of the 3 available gateways not offered. All 3 were options in the dropdown. Maybe it was a browser cache issue so I'll check that.

one other thing to look for would be if one of those gateways were the default or first, because their <option> code potentially wouldn't match your current strings, e.g it might include select="selected" ... using the array in the str_replace does work if the strings match.

Edited by brian!
Link to comment
Share on other sites

  • 2 months later...

mhhh, it looks like this won't work anymore in 8.1.1 with twenty-one.

 

function viewinvoice_gateway_removal_based_on_loggedin_status_hook($vars)
{
        $client = Menu::context('client');
        $paidinvoices = $vars['clientsstats']['numpaidinvoices'];


                if ($paidinvoices == 0) {

                $gatewaydropdown = $vars['gatewaydropdown'];
                $stripe = '<option value="stripe">VISA / Mastercard / American Express</option>';
                $stripe_sepa = '<option value="stripe_sepa">SEPA Lastschrift</option>';
                $gd2 = str_replace($stripe,'', $gatewaydropdown) ;
                $gd3 = str_replace(array ($stripe_sepa,$stripe),
                                   array ('','',),
                                  $gatewaydropdown);
                return array("gatewaydropdown" => $gd3);

                }



}
add_hook("ClientAreaPageViewInvoice", 1, "viewinvoice_gateway_removal_based_on_loggedin_status_hook");

Have you a idea @brian! why it no longer works?

Link to comment
Share on other sites

Ok, the problem was the template.

In six it was:

 <form method="post" action="{$smarty.server.PHP_SELF}?id={$invoiceid}" class="form-inline">
                                {$gatewaydropdown}
                            </form>

and in twenty-one:

   <form method="post" action="{$smarty.server.PHP_SELF}?id={$invoiceid}" class="form-inline">
                                {$tokenInput}
                                <select name="gateway" class="custom-select" onchange="submit()">
                                    {foreach $availableGateways as $gatewayModule => $gatewayName}
                                        <option value="{$gatewayModule}"{if $gatewayModule == $selectedGateway} selected="selected"{/if}>{$gatewayName}</option>
                                    {/foreach}
                                </select>
                            </form>

Is it a secuity issue to need the $tokenInput? Or I can change back to {$gatewaydropdown}?

Link to comment
Share on other sites

Ok I have now found a solution.

 

Hook:

                $availableGateways = $vars['availableGateways'];
                $stripe = 'VISA / Mastercard / American Express';
                $stripe_sepa = 'SEPA Lastschrift';
                $gd3 = str_replace(array ($stripe_sepa,$stripe),
                                   array ('','',),
                                  $availableGateways);
                return array("availableGateways" => $gd3);

And then change the template viewinvoice.tpl to:

{foreach $availableGateways as $gatewayModule => $gatewayName}
                                        {if $gatewayName == ''} {else}
                                       <option value="{$gatewayModule}"{if $gatewayModule == $selectedGateway} selected="selected"{/if}>{$gatewayName}</option>{/if}
                                    {/foreach}

 

Link to comment
Share on other sites

8 hours ago, J-B said:

Is it a secuity issue to need the $tokenInput? Or I can change back to {$gatewaydropdown}?

oh if they've dumped gatewaydropdown in 21 and it's now a "normal" array, then stick with using the new way - that should be simpler for removing unwanted gateways.

do I need to rewrite this for 21 or have you been able to do that yourself ?

Link to comment
Share on other sites

10 hours ago, J-B said:

Is it a secuity issue to need the $tokenInput? Or I can change back to {$gatewaydropdown}?

You'll want the token as that is for CSRF and the token is used to ensure the request is coming from a form that was generated by the app and not some bot trying to do XSS attacks. 

Link to comment
Share on other sites

  • 11 months later...

 

On 12/13/2020 at 5:02 PM, evolve hosting said:

How would you modify this to include 2 gateway options? @brian! I'm more than happy to pay for your time. I know you contribute a LOT around here and it all takes time.

It's not as simple as this, is it?


<?php

# ViewInvoice Gateway Hook
# Written by brian!

function hook_view_invoice_gateway($vars) {
	
	$client = $vars['clientsdetails'];
	$currency = $client['currency'];
	if ($currency == '1') {
		$gatewaylimit = 200;
	}
	elseif ($currency == '2') {
		$gatewaylimit = 75;
	}
	else {
		$gatewaylimit = 0;
	}
	if ($vars['status'] == 'Unpaid' && $vars['total']->toNumeric() <= $gatewaylimit) {
		$gatewaydropdown = $vars['gatewaydropdown'];
		$authorize = '<option value="authorize">Credit / Debit Card</option>';
                $authorizeecheck = '<option value="authorizeecheck">ACH / Direct Debit</option>';
		$gd2 = str_replace($authorize, $authorizeecheck, '', $gatewaydropdown) ;
		return array("gatewaydropdown" => $gd2);
	}
}
add_hook('ClientAreaPageViewInvoice', 1, 'hook_view_invoice_gateway');
?>

 

I've tried to follow along this thread, and adapt this hook to my own needs, but struggeling to make it work. Essentially, customers can have one of two currencies on my website. If EUR (currency 1), I'd like to limit the drop down on invoices to PayPal. If ZAR (currency 2), I'd like to limit the dropdown to PayFast.

This is the hook I modified from the above:
 

<?php

# ViewInvoice Gateway Hook
# Written by brian!

function hook_view_invoice_gateway($vars) {
	
	$client = $vars['clientsdetails'];
	$currency = $client['currency'];

	if ($vars['status'] == 'Unpaid' && $currency == '1') {
		$gatewaydropdown = $vars['gatewaydropdown'];
		$paypalcheckout = '<option value="paypalcheckout">PayPal</option>';
		$gd2 = str_replace($paypalcheckout, '', $gatewaydropdown) ;
		return array("gatewaydropdown" => $gd2);
	}

    elseif ($vars['status'] == 'Unpaid' && $currency == '2') {
		$gatewaydropdown = $vars['gatewaydropdown'];
		$payfast = '<option value="payfast">PayFast</option>';
		$gd2 = str_replace($payfast, '', $gatewaydropdown) ;
		return array("gatewaydropdown" => $gd2);
	}
}
add_hook('ClientAreaPageViewInvoice', 1, 'hook_view_invoice_gateway');
?>

Can someone please help me figure out where I'm going wrong?

Link to comment
Share on other sites

  • 1 year later...

I'm trying this but it's not working.  I just want to remove the 'mailin' option from the dropdown on the view invoice page.

 

<?php

# ViewInvoice Gateway Hook


function hook_view_invoice_gateway($vars) {

	if ($vars['status'] == 'Unpaid' && $vars['total']->toNumeric() >= 0) {
		$gateways = $vars['availableGateways'];
				$remove = ['mailin'];
				if (isset($gateways[$remove])) {
    unset($gateways[$remove]);
    
}
				return array("availableGateways" => $gateways);
	}
}
add_hook('ClientAreaPageViewInvoice', 1, 'hook_view_invoice_gateway');
?>

 

Link to comment
Share on other sites

  • 2 weeks later...

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