Jump to content

Hook to disallow CC for new Customer


J-B

Recommended Posts

Hi, 

i am trying to create a Hook to disallow new customer to use Credit card Payment.

So I think ShoppingCartValidateCheckout is the correct Hook for this.

Can anyone help me how to put this in the hook:

<?php
use Illuminate\Database\Capsule\Manager as Capsule;

function checkIfCanUseCC($vars){

 ## No PHP Code

if SELECT * FROM `tblinvoiceitems` WHERE `userid` = ($_SESSION['uid']) AND `invoiceid` = 0 {

       $paymentmethod == 'banktransfer'
       return array("Error: The payment method is not allowed for new customers!", "Please switch to another Payment");
   }

 ##

}
add_hook('ShoppingCartValidateCheckout', 1, 'checkIfCanUseCC');
?>

Anyone a Idea how to set the "No PHP Code" to PHP?

Link to comment
Share on other sites

$number_of_results =  Capsule::table('tblinvoiceitems')->where('userid', $_SESSION['userid'])->where('invoiceid', '>', '1')->count();

 if ($number_of_results == 0 && $paymentmethod == 'banktransfer' ) {

But can anyone explain me why $paymentmethod is undefined?

 

https://developers.whmcs.com/hooks-reference/shopping-cart/#shoppingcartvalidatecheckout the the doc is "paymentmethod    string    The selected payment method".

Why is paymentmethod not the selected payment method?

🧐

Link to comment
Share on other sites

  • 1 month later...
<?php

if (!defined("WHMCS")) die("This file cannot be accessed directly");

use Illuminate\Database\Capsule\Manager as Capsule;

function checkIfCanUseCC($vars) {

    global $_LANG;
    $errormsg = array();

 $number_of_results =  Capsule::table('tblinvoices')->where('userid', $_SESSION['userid'])->where('status', 'Paid')->count();

 if ($number_of_results == 0 && $_SESSION['cart']['paymentmethod'] == "stripe") {

 $errormsg = $_LANG['firstpaymentnocc'];

 }

 return $errormsg;

}
add_hook('ShoppingCartValidateCheckout', 1, 'checkIfCanUseCC');
?>

 

Can anyone tell me why this hook sometimes works and sometimes not? On my dev it works fine, but customers say they can not pay even though they have more than 1 successful payments in the database.

 

It the "ShoppingCartValidateCheckout" wrong to disable a payment on checkout? Or is it a Bug in WHMCS 7.5.2?

Link to comment
Share on other sites

2 hours ago, J-B said:

I am not a programmer. Can you tell me how to do it? 

not while i'm still emotionally on holiday from this place... i'm here in spirit, but not doing any coding (or installing betas) until next week! 🎄

that said, the way that you're trying to do it, is not really the way that I would do it - if for no other reason than by the time that hook is triggered, the client might well have already spent time entering their cc details... that would leave you with an unhappy, possibly angry customer who might not continue with the order! 😠

personally, I would do it by modifying the $gateways array and removing specific gateway(s) for clients who are not logged in, (e.g they are new clients) - i've quickly modified a similar hook that I posted previously...

<?php

/**
* Modify Payment Gateway Options Based On Loggedin Status for J-B
* @author brian!
*/
 
function cart_gateway_removal_based_on_loggedin_status_hook($vars)
{
	$client = Menu::context('client');
	if ($vars['templatefile']=='viewcart'){
		$gateways = $vars['gateways'];
		$removed = ['stripe'];
		if (!$client) {
			foreach ($gateways as $k => $item) {
				if (in_array($item['sysname'],$removed)) {
					unset($gateways[$k]);
				}
			} 
			return array("gateways" => $gateways);
		}
	}
}
add_hook("ClientAreaPageCart", 1, "cart_gateway_removal_based_on_loggedin_status_hook");

so for clients who are not logged in, this hook should remove the Stripe option from the gateway choices - meaning they will have to choose another gateway option (ensure that they have another option otherwise they can't checkout!)... if you want to remove multiple gateway options for clients who are not logged in, they just add additional gateways to the list...

$removed = ['stripe','paypal'];

for clients who are logged in, this hook will be irrelevant and they will be able to use all available gateways (if you're WHMCS is setup to allow that).

with regards to your $number_of_results query - if the client is logged in, then there is an existing Smarty variable you can use without querying the database...

$paidinvoices = $vars['clientsstats']['numpaidinvoices'];

... and it should give the same value as your query... that value could then be used in if statements, e.g if you wanted to check whether a logged in client had any paid invoices and then determining which gateways are shown to them based on that result.

... and with that, i'll disappear back to my non-coding holiday away from WHMCS!

giphy.gif

Link to comment
Share on other sites

55 minutes ago, J-B said:

Have any idea why? 

then i'll probably need to see the site and check which template and orderforms you are using... I assume you're using the WHMCS Stripe gateway module and not one from a third-party... if it's a third party gateway, it might be called something other than stripe internally.

on second thoughts, what it might be is that it doesn't like the $removed array declaration method... change it to the following to see if that helps...

$removed = array("stripe");
Link to comment
Share on other sites

20 minutes ago, brian! said:

 

on second thoughts, what it might be is that it doesn't like the $removed array declaration method... change it to the following to see if that helps...


$removed = array("stripe");

Yeah, this was the mistake!

You are my hero 💪

 

Link to comment
Share on other sites

  • 1 year later...

Hello @brian!

i have try to modified the hook for viewinvoice.php but it is not working:

 

<?php

/**
* Modify Payment Gateway Options Based On Loggedin Status for J-B
* @author brian!
*/

function viewinvoice_gateway_removal_based_on_loggedin_status_hook($vars)
{
        $client = Menu::context('client');
        $paidinvoices = $vars['clientsstats']['numpaidinvoices'];
        if ($vars['templatefile']=='viewinvoice'){
                $gateways = $vars['gateways'];
                $removed = array("stripe","stripe_sepa");
                if (!$client) {
                        foreach ($gateways as $k => $item) {
                                if (in_array($item['sysname'],$removed)) {
                                        unset($gateways[$k]);
                                }
                        }
                        return array("gateways" => $gateways);
                }

                if ($paidinvoices == 0) {
                        foreach ($gateways as $k => $item) {
                                if (in_array($item['sysname'],$removed)) {
                                        unset($gateways[$k]);
                                }
                        }
                        return array("gateways" => $gateways);
                }

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

 

I think the "if ($vars['templatefile']=='viewinvoice'){" is not right?!

 

Have you a idea?

Link to comment
Share on other sites

3 minutes ago, J-B said:

I think the "if ($vars['templatefile']=='viewinvoice'){" is not right?!

it's not that - though you could use ClientAreaPageViewInvoice and remove the need for that if statement, but that's not the cause of the problem.

also, no point checking whether they're not a client - if they're viewing an invoice they're either a client or an admin pretending to be a client... so that first IF statement is redundant.

the cart and invoice pages handle gateways differently - so modifying a cart hook to run on the invoices page is pointless and won't work.

with viewinvoice, the gateway dropdown is a long string of HTML rather than an array to loop through - so if you're intent on removing one or more gateways, then you're going to need to edit that HTML string (e.g string replace) and then potentially reset multiple other values too.

do the users have the choice of gateway when looking at the invoice or is that disabled and you want to determine which gateway is used if your conditions are met ??

Link to comment
Share on other sites

1 hour ago, brian! said:

do the users have the choice of gateway when looking at the invoice or is that disabled and you want to determine which gateway is used if your conditions are met ??

Yes, the users have the choice of the gateway. I want only to disable stripe for new customer who have never paid an invoice.  If there is a successfully paid invoice, everything should be displayed.

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